[antlr-interest] Help for a newbie please!

Peter Seban pseban12 at gmail.com
Wed Jun 25 08:28:33 PDT 2008


Dear All,

I'm a newbie in antlr.
I have tried this example grammar by input text:

apples= 100 * peaches;             // this is a simple expression
*apples= 100 * peaches;           //  this is a comment line

, but it doesn't work.

The grammar:
--------------------


grammar expr_test;
options{
   output=AST;
   ASTLabelType=CommonTree;
}
query  :
          ex1=expr1  { System.out.println("[EXPRESSION_1] \n"+$ex1.text); }
        | ex2=expr2  { System.out.println("[EXPRESSION_2] \n"+$ex2.text); }
       ;
expr1
       :
       terms '=' terms multiplication terms ';'
       ;

expr2
       :
       asterisk ~('\r'|'\n')* ';'
       ;
terms  :
       WCHAR*
       ;
asterisk
       :
       {input.LT(-1).getText().toLowerCase().equals("\n")}?
       {input.LT(0).getText().toLowerCase().equals(";")}?
       {input.LT(1).getText().toLowerCase().equals("*")}? '*'
       ;

multiplication
       :
       {input.LT(1).getText().toLowerCase().equals("*")}? '*'
       ;

WS     : (' '|'\t'|'\r'|'\n')+ {skip();};
WCHAR  : ~('*'|'='|'('| ')'|'"'|' '|'\t'|'\n'|'\r'|'#'| ';')*;

/*
Test it :
---------
apples= 100 * peaches;
*apples= 100 * peaches;
*/

The generated output is:
-----------------------------------

[EXPRESSION_1]
apples=100*peaches;

Why do'nt generated:
------------------------------
[EXPRESSION_2]
*apples=100*peaches;

too?

Thank you all for your kind tips!
Peter
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080625/38deda6b/attachment.html 


More information about the antlr-interest mailing list