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
 : 'program' IDENT '='
   ( constant | variable )*
   'begin'
   statement*
   'end' IDENT '.'
   ;

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

type
: 'Integer'
;

statement
: assignment
;

assignment
: IDENT ':=' expression ';'
;

term
: IDENT
| '(' expression ')'
| INTEGER
;

negation
: 'not'* term
;

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

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

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

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

expression
: 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.

Online video lectures of a Compiler course

10 January, 2011 § 2 Comments

As mentioned earlier, in the Spring I will be taking a course on Compilers. I like to watch lectures from other universities to broaden my views on the topics so I did a little searching through the internet and compiled this list of places to visit for video lectures of Compiler courses.

MIT’s Computer Language and Engineering course from 2005 has some lectures available online. Unfortunately they are only in RealMedia format, however I recommend RealPlayer Enterprise version which is ad-free and nag-free.

UMass has screencasts of their Compiler Techniques course from Fall 2010 online. These are available in Flash video and playable through your standard web browser as long as you have the Flash plugin installed or are running Google Chrome. This looks to be great but &hellip

Unfortunately, the lectures for that UMass course seem to be kind of messed up (many of them are missing part or all of the audio). Too bad.

spacemanaki via StackOverflow

And last but not least, I found this very topic as a question on StackOverflow asking if there were any lectures online of Compiler courses.

Where Am I?

You are currently browsing entries tagged with compilers at JAWS.

Follow

Get every new post delivered to your Inbox.

Join 1,004 other followers