[antlr-interest] Simple expression grammar

向雅 fyaoxy at gmail.com
Fri May 16 02:04:59 PDT 2008


Hi,
If so many other strange constructions, I have an idea:
1, clean your grammar
2, make a min AST model
3, post-process on the AST model.

So, there has more scalabilities.You can use so many strategies to
post-process it. (I call it Object-Protocol layer).
There is a test-try example at http://on.dev.java.net
which ANT-ON sample use this protocol style processing.
And now, I continue this style to construtor an UI builder, seems not bad.

Regards,
Qinxian

2008/5/15 Maciej Bakalarz <shipmen at gmail.com>:

> Hi again !
>
> 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.
>
> Do you have any clues how to change it to accept only boolean expressions,
> -->nothing more<-- ....
>
> This is grammar:
>
> grammar GuardCondition;
>
> options {
>    output=AST;
>    ASTLabelType=CommonTree;
> backtrack=true;
> memoize=true;
> }
>
>   @rulecatch {
>       catch ( RecognitionException ex ) {
>           throw ex;
>       }
>   }
>
> prog:
>        logical_or_expression+
>        ;
>
> logical_or_expression:
>        logical_and_expression (OR_OP^ logical_and_expression)*
>        ;
>
> logical_and_expression
>        : rel_expression (AND_OP^ rel_expression)*
>        ;
>
> rel_expression
>        : expression (REL_OP^ expression)*
>        | LPAREN! prog RPAREN!
>        | NEG^ LPAREN! prog RPAREN!
>        ;
> expression
>        : ID | INT | NEG^ ID
>        ;
>
> NEG     :       '!';
> LPAREN  :       '(';
> RPAREN  :       ')';
> REL_OP          :        '==' | '<' | '>' | '<=' | '>='|'!=';
> OR_OP   :       '||';
> AND_OP  :        '&&';
> ID      :       ALFA ( ALFA | INT ) *;
> ALFA    :       ('a'..'z'|'A'..'Z') ;
> INT     :        '0'..'9'+;
> WS      :       (' '|'\t')+ {skip();};
>
>
> Thanks
> Maciej Bakalarz
>
>


-- 
致敬
向雅
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080516/16b54472/attachment.html 


More information about the antlr-interest mailing list