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

Rajesh Menon prm225 at gmail.com
Sun Dec 31 15:04:59 PST 2006


Agnisys wrote:
> Andy thanks, that was helpful. I have a follow up question (very elementary again):
> 
> I need to parse text like the following:
> 
> LHS1=ABC LHS2=EFG=123 LHS3=345 
> 
> The value of LHS2 should be "EFG=123". The problem is with the "=" sign in the value. I can
> understand why Antrl complains, but I'm not sure how to fix it. How to tell Antlr that the value
> can contain "=" sign.
> 
> The lexer has the following:
> 
> IDENT :
>  ( 'a'..'z'|'A'..'Z'|'_'|'0'..'9'|'.'|'-'|':'|'/'|','|'^'|'+'|'=' )+ 
> 
> EQUAL   : '=' ;
> 
> other code
> --------------------
> 
> Any help or pointers would be appreciated.
> Thanks,
> Anupam.
> 
> 
> 
> 

Hi.

For inputs:

LHS1=ABC
LHS2=EFG=123
LHS3=345
LHS4=3.14
LHS5=CDE=ABC=XYZ=10

, something like the following should do:

//
grammar test;
options {
	k=1
}
tokens {
	EQUALS='=';
}
expressions
	:	expression*
	;
expression
	:	lhs EQUALS rhs('\r'?'\n')!
	;
lhs
	:	IDENTIFIER
	;
rhs
	:	lhs(EQUALS rhs)*
	|	NUMBER
	;
IDENTIFIER
	:	('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'0'..'9'|'_')*
	;
NUMBER
	:	('0'..'9')+('.'('0'..'9')+)?
	;	
//

HTH.


More information about the antlr-interest mailing list