ANTLR

Table of Contents

1 ANTLR简介

ANTLR, or ANother Tool for Language Recognition, is a parser generator that uses LL(*) parsing.

1.1 Where is it used?

Here is a non-comprehensive list of software built using ANTLR:

  • Groovy
  • Jython
  • Hibernate
  • OpenJDK Compiler Grammar project experimental version of the javac compiler based upon a grammar written in ANTLR.
  • Apex, Salesforce.com's programming language.
  • The expression evaluator in Numbers, Apple's spreadsheet.
  • Twitter's search query language.
  • Weblogic server
  • IntelliJ IDEA and Clion.

Reference: http://en.wikipedia.org/wiki/ANTLR

1.2 安装ANTLR

安装ANTLR,测试环境:Ubuntu 14.04

$ sudo apt-get install antlr3

说明:生成的parser无法单独运行,它需要antlr runtime的支持。

2 ANTLR简单实例

下面实例来自:The Definitive ANTLR Reference,2007, 1.4节

第一步:准备gammar文件

$ cat T.g
grammar T;
/** Match things like "call foo;" */
r : 'call' ID ';' {System.out.println("invoke "+$ID.text);} ;
ID: 'a'..'z' + ;
WS: (' ' |'\n' |'\r' )+ {$channel=HIDDEN;} ; // ignore whitespace

第二步:生成词法和语法分析器

$ antlr3 T.g

运行成功后会生成三个文件:TLexer.java, TParser.java, T.tokens

第三步:准备测试类,入口函数main

// file Test.java
import org.antlr.runtime.*;

public class Test {
  public static void main(String[] args) throws Exception {
    // create a CharStream that reads from standard input
    ANTLRInputStream input = new ANTLRInputStream(System.in);
    // create a lexer that feeds off of input CharStream
    TLexer lexer = new TLexer(input);
    // create a buffer of tokens pulled from the lexer
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    // create a parser that feeds off the tokens buffer
    TParser parser = new TParser(tokens);
    // begin parsing at rule r
    parser.r();
  }
}

第四步:编译java文件

$ CLASSPATH=/usr/share/java/stringtemplate.jar:/usr/share/java/antlr3.jar:/usr/share/java/antlr3-runtime.jar  javac *.java

第五步:运行程序

$ CLASSPATH=/usr/share/java/stringtemplate.jar:/usr/share/java/antlr3.jar:/usr/share/java/antlr3-runtime.jar:.  java Test

启动后输入:
call abc;<EOF>
会输出:
invoke abc


Author: cig01

Created: <2014-05-17 Sat 00:00>

Last updated: <2015-11-10 Tue 21:36>

Creator: Emacs 25.1.1 (Org mode 9.0.7)