[antlr-interest] I have a doubt

Martin Probst mail at martin-probst.com
Wed Mar 2 02:25:51 PST 2005


Hi,

> Is possible to use parser rules inside lexer rules?

No that's not possible. The general idea of ANTLR parsing is that the
Parser asks the Lexer for the (type of the) next token. Depending on
that token it descends to the according rule.

So to match what you have you would possibly write in the Parser:

constantExpression:
	'#' (WS)* "Const" Identifier '=' expression LineTerminator;

And have to make sure that the Lexer is able to identify the
corresponding Tokens (i.e. provide rules for Identifier, LineTerminator,
"Const" etc.).

> For example I have this rule in my parser:
> 
> ConstantExpression
> 	:
> 		Expression
> 	;
> 
> And then I use this rule in lexer as follows:
> 
> ConditionalConstantDeclaration
> 	:
>    		'#' (WS)* "Const" Identifier '=' ConstantExpression LineTerminator
> 
> 	;




More information about the antlr-interest mailing list