[antlr-interest] Dynamic scope for lexer rule

Junkman j at junkwallah.org
Tue May 25 10:07:10 PDT 2010


Thanks for the reply, Jim.  I understand the rationale for your suggestion.

Might this info be worth adding to the wiki?  It may be obvious to
seasoned hands, but difference in constraints between lexer and parser
rules would be helpful especially since both rule types share the same
basic syntax in Antlr.

Junkman

Jim Idle wrote:
> Scopes are not supported for lexer rules, you need to implement your own things to do this, but try to leave any kind of context out of the lexer if you can. You want to push such things as high up the tool chain as you can. IT isn't always possible though.
> 
> Jim
> 
>> -----Original Message-----
>> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
>> bounces at antlr.org] On Behalf Of Junkman
>> Sent: Monday, May 24, 2010 3:22 PM
>> To: antlr-interest at antlr.org
>> Subject: Re: [antlr-interest] Dynamic scope for lexer rule
>>
>> Greetings,
>>
>> Let me raise the question again.  Sorry that this is becoming something
>> of pattern for me.
>>
>> Adding a dynamically scoped attribute to a lexer rule seems to generate
>> the error message (shown at the bottom as part of my previous post on
>> this subject) when generating recognizers.
>>
>> The grammar is as follows:
>> ---------------------
>>
>> grammar Junkscript;
>>
>>
>>
>> NEWLINE
>> @init { $channel=HIDDEN; }
>> 	:	'\n'
>> 	;
>>
>>
>> COMMENT
>> /*
>> scope {
>> 	String dynamic;
>> 	}
>> @init {
>> 	$COMMENT::dynamic = null;
>> 	}
>> 	*/
>> 	:	'#' ( options {greedy=false;} : (~ NEWLINE)* ) ;
>>
>>
>> stmt	:
>> 	( . )+
>> 	;
>>
>>
>>
>> ------------------------
>>
>> The simple grammar works fine, but with the "scope" section (along with
>> init action) under COMMENT uncommented, Antlr generates the error.
>>
>> Are dynamically scoped attributes allowed for lexer rules?   If so,
>> what
>> is the error in the grammar above?
>>
>> Thanks for any assistance.
>>
>> Junkman
>>
>> Junkman wrote:
>>> Greetings,
>>>
>>> I've added an attribute with dynamic scoping to a lexer rule, and
>> when
>>> generating code, I'm encountering an "internal error".  Listed below
>> is
>>> partial call stack reported:
>>>
>>> error(10): internal error: Junkscript.g :
>> java.lang.NullPointerException
>> 	org.antlr.grammar.v2.DefineGrammarItemsWalker.ruleScopeSpec(Defin
>> eGrammarItemsWalker.java:1050)
>>> 	at
>>>
>> org.antlr.grammar.v2.DefineGrammarItemsWalker.rule(DefineGrammarItemsWa
>> lker.java:891)
>>> 	at
>>>
>> org.antlr.grammar.v2.DefineGrammarItemsWalker.rules(DefineGrammarItemsW
>> alker.java:576)
>>> 	at
>>>
>> org.antlr.grammar.v2.DefineGrammarItemsWalker.grammarSpec(DefineGrammar
>> ItemsWalker.java:361)
>>> 	at
>>>
>> org.antlr.grammar.v2.DefineGrammarItemsWalker.grammar(DefineGrammarItem
>> sWalker.java:193)
>>> 	at org.antlr.tool.Grammar.defineGrammarSymbols(Grammar.java:702)
>>> 	at
>>>
>> org.antlr.tool.CompositeGrammar.defineGrammarSymbols(CompositeGrammar.j
>> ava:351)
>>> ...
>>>
>>>
>>> Is dynamic scoping allowed for lexer rule attributes?
>>>
>>> Thanks for any info.
>>>
>>> J
>>>
>>>
>>>
>>>
>>>
>>
>> List: http://www.antlr.org/mailman/listinfo/antlr-interest
>> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-
>> email-address
> 
> 
> 
> 
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
> 



More information about the antlr-interest mailing list