[antlr-interest] help with ANTLRWorks and grammar error....

John B. Brodie jbb at acm.org
Sat May 31 07:42:18 PDT 2008


On Sat, 31 May 2008 23:13:19 +0930, Lloyd Dupont asked (summarized):
>I decided to add a new, original, expression type in my expression
>interpreter.
>I named it the IFS expression and it goes like that
>'ifs' LPAREN (condExpr COMMA valExpr COMMA)+ defExpr RPAREN
>
>where condExpr, valExpr, defExpr are just expression, but I give them a
>nice name to make them more meaningful.
>condExp is a condition expression
>valExpr is the value expression to return if the condition is true
>defExpr is default value expression to return if all condition where false

since condExpr, valExpr, and defExpr are all just an alias for the same
rule (e.g. expression), lets rewrite your rule above as:

ifs : 'ifs' LPAREN (e COMMA e COMMA)+ e RPAREN ;

where e is just expression. now i think it is easier to see the ambiguity.

consider this fragment of input:

ifs ( e1 , e2 , e3

where e1, e2, and e3 are valid instances of expression; now does the e3
above correspond to a second condExpr or to the final defExpr from your
original rule?

suggest you rewrite your rule as follows (untested):

ifs : 'ifs' LPAREN e ( COMMA e )+ RPAREN ;

and then split up the list of e's as you desire during your semantic
analysis phase.

or perhaps these rules (fairly ugly) (untested):

ifs : 'ifs' LPAREN e COMMA e tail RPAREN ;
tail : COMMA e ( COMMA e tail )? ;

Hope this helps (oh, and i do not know the answer to your AntlrWorks green
vs red lines in the Syntax Diagram question, sorry)

   -jbb


More information about the antlr-interest mailing list