[antlr-interest] expression parser example Expr.g dont work

Mats Ekberg mats.ekberg at arcticgroup.se
Wed Jan 7 07:43:29 PST 2009


I removed the NEWLINE requirement entierly and evaluates "expr" in
ANTLRWorks, same result. Sorry.
The input "(1 + 2) * 3 - 4" (with a newline at the end) just recognizes
"( 1 + 2" and the nothing more.

grammar Expr;

expr
    :   multExpr ( '+' multExpr | '-' multExpr )*
    ;

multExpr 
    :   atom ('*' atom )*
    ; 

atom 
    :   INT 
    |   '(' expr ')'
    ;

INT :   '0'..'9'+ ;
NEWLINE:'\r'? '\n' ;
WS  :   (' '|'\t')+ {skip();} ;

ons 2009-01-07 klockan 09:55 -0500 skrev Chris Snyder:
> stat:   expr NEWLINE 
> 
>  
> 
> looks like is should just be
> 
>  
> 
> stat:   expr 
> 
>  
> 
> The current form hits the ‘(‘ and then looks for expr which requires a
> NEWLINE after ‘2’ which you don’t have.
> 
>  
> 
> I didn’t verify this though.
> 
>  
> 
> C
> 
> 
> 
> 
>                                    
> ______________________________________________________________________
> 
> From:antlr-interest-bounces at antlr.org
> [mailto:antlr-interest-bounces at antlr.org] On Behalf Of Mats Ekberg
> Sent: Wednesday, January 07, 2009 1:22 AM
> To: antlr-interest at antlr.org
> Subject: [antlr-interest] expression parser example Expr.g dont work
> 
> 
> 
>  
> 
> I have stripped the classical expr.g example and when I run it in
> ANTLRWorks it dont work.
> The input "(1 + 2) * 3 - 4" (with a newline at the end) just
> recognizes "( 1 + 2" and the nothing more.
> How come?
> 
> grammar Expr;
> 
> prog:   stat+ ;
> 
> stat:   expr NEWLINE 
>     |   NEWLINE
>     ;
> 
> expr
>     :   multExpr ( '+' multExpr | '-' multExpr )*
>     ;
> 
> multExpr 
>     :   atom ('*' atom )*
>     ; 
> 
> atom 
>     :   INT 
>     |   '(' expr ')'
>     ;
> 
> INT :   '0'..'9'+ ;
> NEWLINE:'\r'? '\n' ;
> WS  :   (' '|'\t')+ {skip();} ; 
> 
> 
> 
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20090107/71e74463/attachment.html 


More information about the antlr-interest mailing list