How do you write an interactive interpreter?

View: New views
2 Messages — Rating Filter:   Alert me  

How do you write an interactive interpreter?

by Pete Poulos :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I read the following email
(http://lists.sablecc.org/pipermail/sablecc-discussion/msg00213.html)
and I am wondering how you handle this in the more general case of a
language interpreter (for example see the web based ruby interpreter
here http://tryruby.hobix.com/).  In this case, expressions are
evaluated when they are complete (and a newline is encountered), not
when a new line is encountered.  For example:

4 + 5
=> 9

4 +
5
=> 9

def hello_world
    puts "hello world"
end
=> nil

My first approach to solving this was to use the "Parser.filter()"
method, mentioned in the thesis, to identify when a "statement" node
was parsed and then tell my lexer to give me an EOF token as the next
token in the stream.  However, my Parser class doesn't have the
documented filter method so I'm not sure what how to handle this.  Can
you explain why this documented filter method is missing?

Is there a way to tell the parser to treat a production as a complete
"language" and stop reading the token stream without encountering an
EOF token or without throwing an exception?  If there is nothing for
this now, can I expect to see something like this in SableCC 4?  Is
there some easy way to tell within the lexer that a complete statement
has occurred so that I can use it's filter method (which is present)
to inject an EOF token?  If you have nothing planned, maybe add a
special grammer token STOP which tells the parser to behave as though
an EOF was encountered. then I could write something like the
following

program: statement? STOP;

statement
   : production1
   | production2
   | production3
   ;

I also think this mechanism would be useful for reading data files
which are demarcated by records where you could parse one record into
memory, process it and then discard it before moving on to the next
record.  This way you don't have to have every record in memory at the
same time.

Thanks,
Pete Poulos
pete.poulos@...

_______________________________________________
SableCC-Discussion mailing list
SableCC-Discussion@...
http://lists.sablecc.org/listinfo/sablecc-discussion

Re: How do you write an interactive interpreter?

by Etienne M. Gagnon :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Pete,

Pete Poulos wrote:
... However, my Parser class doesn't have the
documented filter method so I'm not sure what how to handle this.  Can
you explain why this documented filter method is missing?
  

SableCC 2 generated parsers have the filter method. SableCC 3, dues to its internal handling of lists and transformations, cannot offer a coherent filter method.

Is there a way to tell the parser to treat a production as a complete
"language" and stop reading the token stream without encountering an
EOF token or without throwing an exception?  If there is nothing for
this now, can I expect to see something like this in SableCC 4?

This is actually one of the main features of SableCC 4: you can explicitly define start parsing productions with expected ending (with or without EOF).

Example:
$start_no_end my_start; // no EOF expected after my_start (imposes LR(0) final state)

my_start = x y x;

If the fact of not expecting an EOF causes conflicts, SableCC will report them. So, there is a significant difference in the behavior of a grammar defined with $start and one defined $start_no_end.

If you want an idea of the SableCC 4 grammar, look at http://sablecc.org/browser/grammars/sablecc4.sablecc3.

I also think this mechanism would be useful for reading data files
which are demarcated by records where you could parse one record into
memory, process it and then discard it before moving on to the next
record.  This way you don't have to have every record in memory at the
same time.
  
Memory management is another issue. But, changing the memory behavior will simply be a matter of defining a new target using the new flexible back-end.

Etienne

-- 
Etienne M. Gagnon, Ph.D.
SableCC:                                            http://sablecc.org
SableVM:                                            http://sablevm.org


_______________________________________________
SableCC-Discussion mailing list
SableCC-Discussion@...
http://lists.sablecc.org/listinfo/sablecc-discussion

signature.asc (265 bytes) Download Attachment
LightInTheBox - Buy quality products at wholesale price