ANTLR tip: Member variables for Lexer and Parser

23 January, 2011 § Leave a comment

As I mentioned earlier, I’m in the earliest of early stages when it comes to learning ANTLR and all that it offers. As I’ve been working through an assignment for my Compilers course, I learned another thing that I wanted to share.

Much of the documentation on the web states that an ANTLR grammar file can only add member variables for the parser. I am here to say that this is not true.

Using the @member syntax will only add member variables to the parser, however there is another way.

In your ANTLR grammar, simply change the syntax to: @parser::members {} and @lexer::members {}. This will allow you to declare member variables for the parser and lexer, and reference those member variables in the rules defined within your grammar.

Do you have an ANTLR tip that you can teach me about? Please leave one for me in the comments.

Tagged: , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

What’s this?

You are currently reading ANTLR tip: Member variables for Lexer and Parser at JAWS.

meta