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.

Complete newbie tips for ANTLR

19 January, 2011 § 1 Comment

I’m getting my feet wet with ANTLR as part of my compilers course that I’m taking this semester. ANTLR is what one may call a “compiler-compiler”: A tool that is used to create compilers. I’ve learned a couple things now about ANTLR and want to share them with others to help make your first ANTLR project a success.

First, I would highly recommend the tutorials recorded by Scott Stanchfield. Scott starts off the tutorials by showing what versions of Eclipse to download and what plugins you’ll want to use with Eclipse to make ANTLR development easier. He then moves on to explaining the ANTLR-generated code and building expression trees. I’ve watched three of the nine tutorials up to this point, and am planning on watching the rest of them shortly.

This is the code that I had after watching the first three tutorials:

grammar Sample;
options {  language = Java;}
@header {  package a.b.c;}
@lexer::header {  package a.b.c;}
 : 'program' IDENT '='
   ( constant | variable )*
   'end' IDENT '.'

: 'constant' IDENT ':' type ':=' expression ';'
: 'var' IDENT (',' IDENT)* ':' type ';'

: 'Integer'

: assignment

: IDENT ':=' expression ';'

| '(' expression ')'

: 'not'* term

: ('+' | '-')* negation

: unary (('*' | '/' | 'mod') unary)*

: mult (('+' | '-') mult)*

: add (('=' | '/=' | '<' | '<=' | '>=' | '>') add)*

: relation (('and' | 'or') relation)*

INTEGER : '0'..'9'+ ;
IDENT : ('a'..'z' | 'A'..'Z')('a'..'z' | 'A'..'Z' | '0'..'9')* ;
WS : (' ' | '\t' | '\n' | '\r' | '\f')+ {$channel = HIDDEN;} ;

I wanted to add code to the rules that I had written. At this point, I had rules for the parser and the lexer. After watching Tutorial 2, I learned that the code between the curly brackets was placed within the generated Java file. This means that you can place other code between the curly brackets, in addition to the “$channel = HIDDEN” that is shown above.

That is just a simple start of what I’ve got so far with the tutorials. Good luck learning ANTLR, it is a lot of fun to see it all in action.

Where Am I?

You are currently browsing entries tagged with antlr at JAWS.