[antlr-interest] Return types on lexer rules do not seem to work.

Kay Roepke kroepke at classdump.org
Wed Jul 18 13:23:55 PDT 2007


On Jul 18, 2007, at 1:11 PM, Johannes Luber wrote:

> Randall R Schulz wrote:
>> On Wednesday 18 July 2007 10:31, Johannes Luber wrote:
>>> ...
>>>
>>> Only tree and parser rules may include return values. The book  
>>> states
>>> this on page 86. ...
>>>
>>> But it should be possible to use scopes for this purpose instead.
>>
>> How is that? Lexical analysis is all done before parsing begins.
>
> I didn't use scopes to transfer information between parser and lexer
> rules, so I don't know of this would be possible or not. But within  
> the
> lexer the appropriate code was generated - although untested yet.  
> Every
> time I seem to get closer to finish my side project, a new problem  
> turns
> up... At least I need only to rework the data storage side and only  
> the
> string templates before I can turn my attention back to the C#  
> grammar.

Scopes do work in lexers. Apart from probably not being very useful,  
since
lexer rules normally don't recurse that much, it's ok.

See this grammar:

lexer grammar TestScope;

scope Scope {
	int foo;
}

TOKEN
scope Scope;
	:	('A' {$Scope::foo = 0;} |'B' {$Scope::foo = 1;}) SUBFOO 'B'
	;
	
fragment	
SUBFOO
	:	({$Scope::foo > 0}? 'SUB') 'FOO'
	;
	

Looking at the generated code, everthings cool. This won't enable you  
to pass stuff between
the parser and the lexer, though.
Don't ask me how this would behave in a combined grammar (I don't  
wanna know it anyway...;))

What's the reason for doing this? Is there a total requirement to  
pass stuff between lexical rules?

cheers,
-k
-- 
Kay Röpke
http://classdump.org/






More information about the antlr-interest mailing list