[antlr-interest] Gramar without semicolon
joanpujol at gmail.com
Wed Jul 28 15:20:41 PDT 2004
Thanks a lot. Is exactly what I need.
On Wed, 28 Jul 2004 14:43:43 -0400, jbb at acm.org <jbb at acm.org> wrote:
> You wrote:
> >I make a simple case with only two instructions an if
> >and an assignament:
> >instructions: (instruction)+;
> >instruction: inst_if | inst_assig;
> >inst_if: IF expr THEN (NL)* instructions (ELSE (NL)*
> >instructions)? ENDIF ((NL)+ |SEMICOLON instruction);
> >inst_assig: IDENT ASSIG expr ((NL)+ |SEMICOLON
> >This I think that works quite well and recognizes
> >thinks like
> >IF expr THEN inst1;inst2
> >ELSE inst1
> >END IF
> >But it can't recognize
> >IF expr THEN inst1 ELSE inst2 END IF
> >And I don't know how to manage this, I think that I've
> >to use semantic predicates but I am a little confused.
> The (NL)+ phrase near the end of both the inst_if and the inst_assig
> rules mean that a NL is always required to terminate an instruction
> But you want the NL to be optional sometimes (e.g. at the end of the
> then phrase and at the end of the else phrase).
> Here is a grammar fragment that makes the NL optional - and maybe
> making it optional in too many places in the rest of your grammar...
> program :
> instruction_block EOF
> instruction_block :
> single_line ( (NL)+ ( instruction_block )? )?
> single_line :
> instruction ( SEMICOLON instruction )*
> instruction :
> inst_if | inst_assig
> inst_if :
> IF expr THEN (NL)* instruction_block
> ( ELSE (NL)* instruction_block )?
> inst_assig :
> IDENT ASSIG expr
> Hope this helps....
> John B. Brodie (jbb at acm.org)
Joan Jesús Pujol Espinar
Yahoo! Groups Links
<*> To visit your group on the web, go to:
<*> To unsubscribe from this group, send an email to:
antlr-interest-unsubscribe at yahoogroups.com
<*> Your use of Yahoo! Groups is subject to:
More information about the antlr-interest