[antlr-interest] Help for a newbie please!

Raphael Reitzig r_reitzi at cs.uni-kl.de
Wed Jun 25 09:27:51 PDT 2008


Hi Peter!

You do not have any recursion in your grammar. The first expression
matches, but then, there is no rule available for the second line.

Try adding something like that as starting rule:

querylist : query (('\r'|'\n'|'\r\n')* query)* EOF;

Good luck

Raphael

On Wed, 25 Jun 2008 17:28:33 +0200, "Peter Seban" <pseban12 at gmail.com>
wrote:
> 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



More information about the antlr-interest mailing list