[antlr-interest] ANTLR Two Simple Questions :-)

Sameh W. Zaky sameh.wz at gmail.com
Mon May 17 07:33:23 PDT 2010


Dear All,

I am a Masters student, I use Java, and I am trying to make my parser with
the help of ANTLR. I need this for my Masters project.
I am using ANTLRWorks 1.3.1
I had 2 questions concerning it:

1) How can I have a dynamic token name (or dynamic lexer)?
*Example: (the symbol SERVICE, will have different token values, depending
on certain list (array) whose contents change over time)*
*
*
So, in some scenario, SERVICE will, let's say, have the following form:
*SERVICE* : ('networking' | 'internet' | 'intranet') ;

But in other scenario, I want service to address a different set of token
values, let's say:
*SERVICE* : ('hardware' | 'software') ;

=========================================================
I tried to solve it in 2 ways:
A) each time the contents of the array change, I generate a new grammar
file, *but I faced the problem:* that I did not find the method that takes
as input the grammar file (.g) and generates the tokens file, the
lexer.java, and the parser.java (i.e. the exact task that is done by the
button 'Generate Code' in ANTLRWorks 1.3.1)
B) I tried to play in the generated Lexer.java, but I fear that this might
destroy my plans in point 2 (if what I will ask in this point is doable)

2) Given a certain grammar, how can I (or: is it possible to) get the list
of all possible next tokens? In other words, is there a method (or a trick
to make such a method) that takes as input a grammar, and a non-complete
(but so far correct) sentence of this grammar and gives as output the set of
all possible next tokens that can be inserted to keep the sentence true?
*Example: (the good thing is that all tokens to be used are pre-defined, so
we do NOT have a rule such as ID  : ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..
'Z'|'0'..'9'|'_')*;)*

*start* : *ANIMAL* ('or' *ANIMAL*)* 'and' *SERVICE EOF* ;

*ANIMAL* : ('dog' | 'cat' | 'horse') ;
*SERVICE* : ('hardware' | 'software') ;

==========================================================
So, imagine this scenario:
A) the user inserts "dog" and now asks for the autocomplete, so I want to
give the user the list of possible next tokens that he can write, and the
list will be "or, and"
B) Now let's say the user chose "or", so the sentence now is "dog or" and
then asks for the autocomplete so I display the list "dog, cat, horse"
C) .. and so forth :-)


Any Idea?
Many Thanks for your time :-)
-- 
Sameh W. Zaky


More information about the antlr-interest mailing list