[antlr-interest] advice for grammar modification

Daniel Zuberbuehler dzubi at users.sourceforge.net
Thu Aug 11 07:44:30 PDT 2005


Hello

I have to adapt an existing grammar but I'm new to the topic of grammars, so I 
could use some advice.

In my grammar, there are several rules with actions. The rules allow empty 
matches, and as it is, the actions get executed even if it was an empty 
match. I have to modify the rules, so that the actions get only executed if 
the match is not empty.
I think I have modified the rules correctly, but would be grateful if anybody 
could check if my modifications make sense:

< original version
> modified version

---------------------------------------
< rulename : ( rule )?
<              { actions; }
<          ;

> rulename : ( rule 
>              { actions; } )?
>          ;
---------------------------------------
< rulename : ( rule | /*empty*/ )
<              { actions; }
<          ;

> rulename : ( rule { actions; } |
>              /*empty*/ )
>          ;
---------------------------------------
< rulename : ( rules... )*
<              { actions; }
<          ;

> rulename : ( ( rules... )+
>              { actions; } )?
>          ;
---------------------------------------
< rulename : ( rule1 )? ( rule2 )?
<              { actions; }
<          ;

> rulename : { boolean doit = false; }
>              ( rule1 { doit = true; } )?
>              ( rule2 { doit = true; } )?
>              { if(doit){ actions; }}
---------------------------------------    
< rulename : ( rule1 )* ( rule2 )*
<              { actions; }
<          ;

> rulename : { boolean doit = false; }
>              ( rule1 { doit = true; } )*
>              ( rule2 { doit = true; } )*
>              { if(doit){ actions; }}
---------------------------------------           


Thanks a lot for your help
Daniel Zuberbühler


More information about the antlr-interest mailing list