State index

41 views
Skip to first unread message

zakaria jaiathe

unread,
Mar 21, 2014, 11:17:22 AM3/21/14
to antlr-di...@googlegroups.com
Maybe it's too easy, but I simply can't find how can I get the state index given a token name.

For the parser rules I use 

ATNState states[] = g.atn.ruleToStartState;
Rule r = g.getRule(startRule);
index
= r.index;
state
= states[index];

But I can't use this in the lexer grammar because the only existing rules are the parser rules.
Thanks a lot.

Terence Parr

unread,
Mar 21, 2014, 11:44:56 AM3/21/14
to antlr-di...@googlegroups.com
There is an array of
On Mar 21, 2014, at 8:17 AM, zakaria jaiathe <z.ja...@gmail.com> wrote:

Maybe it's too easy, but I simply can't find how can I get the state index given a token name.

For the parser rules I use 

ATNState states[] = g.atn.ruleToStartState;
Rule r = g.getRule(startRule);
index = r.index;
state = states[index];


 the index is a rule index not a state index. you will have to access the ATN state within the rule. I can remember what is available at the moment; take a look at the API or the source.

But I can't use this in the lexer grammar because the only existing rules are the parser rules.
Thanks a lot.

 You will have to look at the source to see what data structures are available in the LexerATNSimulator.  I didn’t anticipate making the states available.

 what are you trying to do?
Ter

Sam Harwell

unread,
Mar 21, 2014, 5:31:33 PM3/21/14
to antlr-di...@googlegroups.com

There is no direct relation between token types and lexer rule indexes (and thus no relation to states in the ATN). Lexer rules frequently contain the type, more, or skip commands, or even custom embedded actions to alter the type produced for a rule.

 

Depending on what you are trying to accomplish (you didn’t specify), you’ll need to do things some other way.

 

Sam

--
You received this message because you are subscribed to the Google Groups "antlr-discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to antlr-discussi...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

zakaria jaiathe

unread,
Mar 24, 2014, 3:46:34 AM3/24/14
to antlr-di...@googlegroups.com
Hi,

Thank you for your answers. What I want to do is to re-implement the Random Phrase class in Antlr4. At this stage, I want to generate a random value of a token given its name.

Regards
Zak
Reply all
Reply to author
Forward
0 new messages