Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Download and install ANTLR 3 from the ANTLR 3 page of the ANTLR website

2.

...

Download and install AntlrWorks from the AntlrWorks page of the ANTLR website

...

Run ANTLR 3 on a simple grammar


To be written. Volunteers?

...

2.1 Create a simple grammar


To be written. Volunteers?

Java

Code Block
grammar SimpleCalc;

tokens {
	PLUS 	= '+' ;
	MINUS	= '-' ;
	MULT	= '*' ;
	DIV	= '/' ;
}

@members {
    public static void main(String[] args) throws Exception {
        SimpleCalcLexer lex = new SimpleCalcLexer(new ANTLRFileStream(args[0]));
       	CommonTokenStream tokens = new CommonTokenStream(lex);

        SimpleCalc parser = new SimpleCalc(tokens);

        try {
            parser.expr();
        } catch (RecognitionException e)  {
            e.printStackTrace();
        }
    }
}

/*------------------------------------------------------------------
 * PARSER RULES
 *------------------------------------------------------------------*/

expr	: term ( ( PLUS | MINUS )  term )* ;
	
term	: factor ( ( MULT | DIV ) factor )* ;
	
factor	: NUMBER ;


/*------------------------------------------------------------------
 * LEXER RULES
 *------------------------------------------------------------------*/

NUMBER	: (DIGIT)+ ;
	
WHITESPACE : ( '\t' | ' ' | '\r' | '\n'| '\u000C' )+ 	{ channel = 99; } ;
	
fragment DIGIT	: '0'..'9' ;

C#

Code Block
grammar SimpleCalc;

options {
    language=CSharp;
}

tokens {
	PLUS 	= '+' ;
	MINUS	= '-' ;
	MULT	= '*' ;
	DIV	= '/' ;
}

@members {
    public static void Main(string[] args) {
        SimpleCalcLexer lex = new SimpleCalcLexer(new ANTLRFileStream(args[0]));
       	CommonTokenStream tokens = new CommonTokenStream(lex);

        SimpleCalc parser = new SimpleCalc(tokens);

        try {
            parser.expr();
        } catch (RecognitionException e)  {
            Console.Error.WriteLine(e.StackTrace);
        }
    }
}

/*------------------------------------------------------------------
 * PARSER RULES
 *------------------------------------------------------------------*/

expr	: term ( ( PLUS | MINUS )  term )* ;
	
term	: factor ( ( MULT | DIV ) factor )* ;
	
factor	: NUMBER ;


/*------------------------------------------------------------------
 * LEXER RULES
 *------------------------------------------------------------------*/

NUMBER	: (DIGIT)+ ;
	
WHITESPACE : ( '\t' | ' ' | '\r' | '\n'| '\u000C' )+ 	{ channel = 99; } ;
	
fragment DIGIT	: '0'..'9' ;

Objective-C

Code Block
grammar Simple;

options 
{
    language=ObjC;
}

OR : '||' ;

C

Code Block
grammar Simple;

options 
{
    language=C;
}

OR : '||' ;

...

2.2 Run ANTLR 3 on the simple grammar


To be written. Volunteers?

...

2.3 Revisit the simple grammar and learn basic ANTLR 3 syntax


To be written. Volunteers?

...

Browse the list of questions frequently asked about ANTLR 3

Try the AntlrWorks IDE for ANTLR 3. AntlrWorks can be downloaded from the AntlrWorks page on the ANTLR website

Your five minutes are up!

...