[antlr-interest] simple parser lookahead problem

Robert Colquhoun rjc at trump.net.au
Thu May 13 06:27:32 PDT 2004


Hello,

FWIW i implemented the same grammar anthony is trying a few years ago.

At 01:40 AM 13/05/2004, Monty Zukowski wrote:
>On May 12, 2004, at 7:46 AM, Anthony Youngman wrote:
> > Unfortunately, ":" has three different meanings, as
> > exemplified in this simple line of code ...
> >
> > XXX: PRINT A : B :
> >
> > Where the first colon says "this is a label" (I haven't even touched
> > this yet!),
>Yeah, see my parser filter article--I was trying to solve that for your
>grammar!

This technique did not work for me. Using parser filters has an implicit 
dependency not mentioned in the article: the input must be lexable.

For the language the position you can place labels, statements and comments 
is context dependent, the same text could be either 3 depending on its 
position.  The comments almost by definition were not lexable(unless there 
is a way to do a 'catch all' lexer rule) therefore you couldn't feed this 
info through to the parser filter to figure out the context to determine 
whether something was a statement/label/comment.

There is also a C style #ifdef preprocessor in the language which failed 
similarly over non-lexable input.

>printst
>         : #(PRINT
>                 (
>                 expr COLON) { st.write("PRINT");}
>                 | expr { st.write("PRTLN");}
>                 | /*empty*/ { st.write("CRLF");}
>                 )
>         )
>         ;

Empty alts are kindof scary...i did the above by:

         printst: #(PRINT (e:expr)? (c:colon)?)
                 {
                         if (#e != null && #c != null) {
                                 st.write("PRINT");
                         } else if (#e != null) {
                                 st.write("PRTLN");
                         } else if (#c != null) {
                                 //do nothing...
                         } else {
                                 st.write("CRLF");
                         }
                 }

For the above to work you need a fixed root token for all expr's ie EXPR.

  - Robert 



 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
     http://groups.yahoo.com/group/antlr-interest/

<*> To unsubscribe from this group, send an email to:
     antlr-interest-unsubscribe at yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
     http://docs.yahoo.com/info/terms/
 



More information about the antlr-interest mailing list