• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ANTLR: multiple TreeParsers how?

 
Ranch Hand
Posts: 171
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am using the ANTLR parser generator for my project. I need to define multiple TreeParser implementations to operate on my AST. The documentation says you can do this, but it doesn't say how. I tried all the obvious things but couldn't make it work.

1) if you put the treeparsers in the same grammar file, it complains and says, you are not allowed to define more than one TreeParser per grammar file. (No idea why this restriction exists!)

2) If you put the second TreeParser in its own grammar file, then it generates its own TokenTypes interface and generates a new numbering for all the tokens, that is not compatible with the numbering used in my AST. When you parse the AST using this second TreeParser, it misinterprets all of the tokens.

I need the second TreeParser to share the same input token list from the ones defined in the first grammar file. How to do this??

I have looked through the ANTLR manual and I am surprised that it doesn't explain how to do this, since the ability to code more than one class is a pretty standard feature of most Java tools(!)..

Thanks,
Geoffrey
 
Geoffrey Falk
Ranch Hand
Posts: 171
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found the solution. You need to put

options {
importVocab=MyParser;
}

in the second TreeParser definition. This assumes "MyParser" is the name of the AST parser vocabulary. It will get its token list from the file MyParserTokenTypes.txt in the generated source directory.

This also means you need to generate the main parser first, and then the 2nd one after.

Geoffrey
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic