[antlr-interest] Simple expression grammar

Maciej Bakalarz shipmen at gmail.com
Fri May 16 01:40:37 PDT 2008


>>>> OK I have written my grammar for boolean expressions mentioned earlier 
>>>> (like ( (a>=3 || b<=5)&& c>=4 )  ).
>>>>
>>>> But my grammar accept also expressions like "abc def ght" and many
>> other 
>>>> strange constructions
>>> I think it is only accepting that because you have used:
>>>
>>> logical_or_expression+
>>>
>>> Remove the + and it will only accept a single logical expression. this 
>>> code accepts  abc def ght as 3 separate logical_or_expressions.
>> I have tried it but ... when I remove '+' it will not parse correctly 
>> expression like "x>=4 || b<=5 && d<=5" . It will parse only 'x'

> How about 
> logical_or_expression EOF

Ok thanks for this clue. I have changed two things:

Added EOF in prog:

	"prog: 	logical_or_expression EOF!"

And changed definition of WS so it is using channel now instead of skip():

	WS  	:   	(' '|'\t')+ { $channel = HIDDEN;};

Best regards
Maciej Bakalarz


More information about the antlr-interest mailing list