[antlr-interest] Newbie questions: How to traverse the AST ...

Rajesh Menon prm225 at gmail.com
Mon Jan 1 08:15:12 PST 2007


Agnisys wrote:
> Hi Rajesh,
>  I'm using 2.7.7 so I converted your code but when running Antlr on it I got the following. Once
> again the  problem is that '=' can be in the RHS.
> 
> ANTLR Parser Generator   Version 2.7.7 (20060930)   1989-2005
> menon.g:12: warning:nondeterminism upon
> menon.g:12:     k==1:"="
> menon.g:12:     between alt 1 and exit branch of block
> 
>   Here is my conversion:
> 
> class FlexParser  extends Parser;
> expressions
> 	:	(expression)*
> 	;
> expression
> 	:	lhs EQUALS rhs NEWLINE!
> 	;
> lhs
> 	:	IDENTIFIER
> 	;
> rhs
> 	:	lhs(EQUALS rhs)*
> 	|	NUMBER
> 	;
> class FlexLexer extends Lexer;
> options {
> 	k=2;
> }
> tokens {
> 	EQUALS="=";
> }
> NEWLINE :('\r''\n')=> '\r''\n' //DOS
>      | '\n'                  //UNIX
>      { newline(); }
>      ;
> IDENTIFIER
> 	:	('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*
> 	;
> NUMBER
> 	:	('0'..'9')+('.'('0'..'9')+)?
> 	;	
> 
> Thanks for your help,
> Anupam.
> 
> 

Right, there'll be a non-determinism in the production for `rhs`.
For the sample inputs you had provided,
	rhs:lhs(EQUALS rhs)? | NUMBER ;
should work.

lhs(EQUALS rhs)* was just trying to handle the last case of the input I 
had tried, namely LHS5=CDE=ABC=XYZ=10.


More information about the antlr-interest mailing list