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

Chris McConnell chrimc at microsoft.com
Tue May 29 15:49:16 PDT 2012


I'm actually generating C++, but I have a syntax to AST grammar which I can easily debug in AntlrWorks and a tree processing grammar which connects to the C++ generator.  This allows me to easily debug the grammar using AntlrWorks.  If I have to drop into the target grammar, I will end up with multiple generator methods and just introduce specific AST nodes for this case.  I was hoping for some way of doing it without having to drop into the target language.  From the documentation it seemed like I could do this:



tokens {ItemID ;}

linkItemID: linkPredicate '(' PROPERTY ',' string ')' -> ^(linkPredicate PROPERTY ^(Eq ^(PropCollection ItemID[')', "ItemID"]) string));



This is the closest I've gotten, but I still get this error:

[15:48:41] D:\tau\ESearch\latest\QuerySyntax\output\QuerySyntaxParser.java:2352: cannot find symbol

[15:48:41] symbol  : method create(int,char,java.lang.String)

[15:48:41] location: class org.antlr.runtime.debug.DebugTreeAdaptor

[15:48:41]                 adaptor.addChild(root_3, (Object)adaptor.create(ItemID, ')', "ItemID"));

[15:48:41]                                                         ^

[15:48:41] 1 error





-----Original Message-----

From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-bounces at antlr.org] On Behalf Of George S. Cowan

Sent: Monday, May 28, 2012 7:22 AM

To: antlr-interest at antlr.org

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



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!







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