[antlr-interest] accessing lexer sub tokens from a rule

hakan eryargi hakan.eryargi at gmail.com
Mon Jul 20 13:27:23 PDT 2009


after some re-thought, i've realized that ability should be a parser
rule rather than a lexer rule. i've also realized why antlr couldnt
parse my expression: all SCOPE tokens also match ID tokens. i guess i
was trying to make antlr do some task which indeed must be handled in
application logic. so i removed SCOPE token, put ID in place of it,
and after lots of struggling with syntax, all seems ok now.  it works
beatifully with rest of the application. below is relevant modified
parts.

thanks to all who helped :)
hakan

reqExpression
	:reqPrimary ( /* empty */ -> reqPrimary
                 	| (AND reqPrimary)+ -> ^(AND<ReqNode.And> reqPrimary+)
                 	| (COMMA reqPrimary)+ -> ^(COMMA<ReqNode.And> reqPrimary+)
                 	
                 	| (OR  reqPrimary)+ -> ^(OR<ReqNode.Or>  reqPrimary+)
                 	)
       	;

reqPrimary	:	
	ability -> ability
	| NOT ability -> ^(NOT<ReqNode.Not> ability)
	| LPAREN reqExpression RPAREN -> reqExpression
	| NOT LPAREN reqExpression RPAREN -> ^(NOT<ReqNode.Not> reqExpression)
	;
	
	
ability	:
	s=ID COLON p=PERSISTENT? name=ID (COLON (intValue=INTEGER |
floatValue=FLOAT))?
	-> ^(ABILITY<ReqNode.A>[$p, $s, $name, $intValue, $floatValue] ID ID
INTEGER? FLOAT?)
	;

On Mon, Jul 20, 2009 at 4:04 AM, hakan eryargi<hakan.eryargi at gmail.com> wrote:
> hello,
>
> is it possible to access sub tokens of lexer from a rule ? (sorry for
> stupid naming) at the bottom is part of my grammar. i want to re-write
> primary rule something like:
>
> primary :
>        a=ABILITY -> ABILITY<ReqNode.A>[$a.scope, $a.id, ($a.INTEGER | $a.FLOAT)? ]
>        | LPAREN! expression RPAREN!
>        ;
>
> of course i can parse ability in ReqNode.A constructor for my needs
> but ability is already parsed by lexer so using it seems wiser. but
> how ?
>
> i tried to make ability a rule but generated code couldnt parse my
> input complaining about no viable alternative. i'm not sure why, and
> also i'm not sure if making ability a rule makes sense. ie: it sounds
> like a lexical rule rather than parsing rule
>
> thanks,
> hakan
>
>
> primary :
>        ABILITY -> ABILITY <ReqNode.A>
>        | LPAREN! expression RPAREN!
>        ;
>
>
> ABILITY :
>        SCOPE WS? COLON WS? ID WS? (COLON WS? (INTEGER | FLOAT))?;
>
> ID      :
>        ('a'..'z' | 'A'..'A') ('a'..'z' | 'A'..'A' | DIGIT | '_' )*
>        ;
>
> fragment SCOPE
>        : /* only query scopes*/
>        'b' /* building */
>        | 'l' /* cell */
>        | 't' /* slot */
>        | 'c' /* city */
>        | 'u' /* user */
>        | 'a' /* any */
>        ;
>


More information about the antlr-interest mailing list