1. JAVA环境
2. Antler 工具包:
antlr-3.5.1-complete-no-st3.jar
路径加入classpath
3. cmd命令行:
java org.antlr.Tool XXXXX.g
(此命令可生成.java或.cs文件等,因.g文件中内容而异)
1).g (转成.cs) 实例:

grammar SimpleCalc; options { language=CSharp2; } tokens { PLUS = '+' ; MINUS = '-' ; MULT = '*' ; DIV = '/' ; } @members { static void Main(string[] args) { string source = Console.ReadLine(); SimpleCalcLexer lex = new SimpleCalcLexer(new ANTLRStringStream(source)); CommonTokenStream tokens = new CommonTokenStream(lex); SimpleCalcParser parser = new SimpleCalcParser(tokens); try { Console.WriteLine(parser.expr()); } catch (RecognitionException e) { Console.Error.WriteLine(e.StackTrace); } Console.ReadLine(); } } //PARSER RULES expr returns [int value] : e=term { $value=$e.value; } ( PLUS e=term { $value += $e.value; } | MINUS e=term { $value -= $e.value; } )*; term returns [int value] : e=factor { $value = $e.value; } ( MULT e=factor { $value *= $e.value; } | DIV e=factor { $value /= $e.value; } )* ; factor returns [int value] : NUMBER { $value = int.Parse($NUMBER.text); }; //LEXER RULES NUMBER : (DIGIT)+ ; WHITESPACE : ( ' ' | ' ' | ' ' | ' '| 'u000C' )+ { $channel = HIDDEN; } ; fragment DIGIT : '0'..'9' ;
2) .g (转成.java)实例:

grammar Expr; @header { import java.util.HashMap; } @members { /** Map variable name to Integer object holding value */ HashMap memory = new HashMap(); } prog: stat+ ; stat: expr NEWLINE {System.out.println($expr.value);} | ID '=' expr NEWLINE {memory.put($ID.text, new Integer($expr.value));} | NEWLINE ; expr returns [int value] : e=multExpr {$value = $e.value;} ( '+' e=multExpr {$value += $e.value;} | '-' e=multExpr {$value -= $e.value;} )* ; multExpr returns [int value] : e=atom {$value = $e.value;} ('*' e=atom {$value *= $e.value;})* ; atom returns [int value] : INT {$value = Integer.parseInt($INT.text);} | ID { Integer v = (Integer)memory.get($ID.text); if ( v!=null ) $value = v.intValue(); else System.err.println("#ff0000 variable "+$ID.text); } | '(' expr ')' {$value = $expr.value;} ; ID : ('a'..'z'|'A'..'Z')+ ; INT : '0'..'9'+ ; NEWLINE:' '? ' ' ; WS : (' '|' ')+ {skip();} ;
4. 生成的.cs需要的依赖包:Antlr3.Runtime.dll
参考:
http://www.cnblogs.com/RicCC/archive/2008/03/01/antlr-getting-started-examples.html