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

Jack Pan Jack.Pan at Lewtan.com
Wed Jan 7 10:40:01 PST 2009


How does AntlrWorks pick the default starting rule? It is the first rule in alphabetical order, neither is the first rule in grammar file.

 

 

From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-bounces at antlr.org] On Behalf Of Jim Idle
Sent: Wednesday, January 07, 2009 11:31 AM
Cc: antlr-interest at antlr.org
Subject: Re: [antlr-interest] expression parser example Expr.g dont work

 

Mats Ekberg wrote: 

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.


Place an EOF in the mix:

stat : expr? EOF ;

And fix your NEWLINE:

NEWLINE: '\r'? '\n' { $channel = HIDDEN; } ;


Then when you run ANTLR Works, make sure that you are choosing the stat rule as the start rule (and use the debugger, not the interpretter).


Jim




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
	    

 


  _____  



 
 
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/9dab5ab6/attachment.html 


More information about the antlr-interest mailing list