Versions Compared

Key

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

...

Java

Code Block
grammar SimpleCalc;

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

@header  {
	//package antlr3.tutorial.simple;
}

@lexer::header {
	//package antlr3.tutorial.simple;
}

@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	= '/' ;
}

@header  {
	//package antlr3.tutorial.simple;
}

@lexer::header {
	//package antlr3.tutorial.simple;
}

@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 : '||' ;

...