[antlr-interest] How do you include a constant string in an AST?

George S. Cowan cowang at comcast.net
Mon May 28 07:22:05 PDT 2012


I'm confused over what the phrase "... what my generator expects" might
mean. 

I'll take a swing at this anyway, assuming that you don't know how to turn
the string "ItemID" into a token on the fly by dropping into Java in the
middle of a rule using braces {}. This all assumes that you're using ANTLR
3.4 to generate a java parser. Choose some token name you already have
defined or define a new one in the tokens {} option, for example IDENTIFIER,
then try this:

linkItemID: linkPredicate '(' PROPERTIES ',' string ')' 
  -> ^(linkPredicate PROPERTIES ^(Eql {new CommonTree(new
CommonToken(IDENTIFIER,"ItemID"))} string))
I don't know if that can be simplified to 
linkItemID: linkPredicate '(' PROPERTIES ',' string ')' 
  -> ^(linkPredicate PROPERTIES ^(Eql {new CommonToken(IDENTIFIER,"ItemID")}
string))

I didn't run this through ANTLR, so after you debug it be sure to let us
know what actually works. CommonTree() and CommonToken() are just
constructors in the CommonTree.java and CommonToken.java files in the
org.antlr.runtime package, so you can look there to understand exactly what
they do, and you can look in the Java parser that ANTLR creates to see
exactly how ANTLR defines your linkItemID rule; I recommend a search for the
exact phrase that you include inside the braces to get you to the right part
of the rule quickly.

And if I'm not even in the ballpark, try stating your question a different
way or giving us more detail.

George


Chris McConnell said
I have a rule like this:

linkItemID: linkPredicate '(' PROPERTIES ',' string ')' -> ^(linkPredicate
PROPERTIES ^(Eql 'ItemID' string))

What I want is to rewrite the syntax into a specific form that involves a
constant that is not matched on the left side.  So far I have not been able
to figure out how to do this.  I know I could make it a token constant like
Eql, but I want this form since it is what my generator expectes.  I have
the ANTLR book and I've read through it several times... :)



Thanks!




More information about the antlr-interest mailing list