JavaCC -- Linux安装与使用

  • 简介
  • 安装
  • 使用

简介

JavaCC官网地址https://javacc.java.net/。
JavaCC是一个词法分析器、语法分析器的生成工具,从功能上类似于语法分析器YACC和词法分析器Lex。JavaCC是SUN公司开发的一个编译器自动生成工具,是Java Compiler Compiler的简称,功能强大且与平台无关。使用JavaCC的基本步骤是编写词法分析的语法文件,生成语法分析器代码,整合语法规则和语法分析代码。JavaCC是一个Java语言分析器,就是按照规则自动生成语言分析程序的源代码。程序语言的语法通常都是使用BNF或EBNF描述。

安装

  1. 安装ant
    需要ant-1.5.3及以上版本来构建JavaCC。
    下载地址http://ant.apache.org/bindownload.cgi。
    #解压
    tar -xzvf apache-ant-1.9.7-bin.tar.gz
    #检查安装是否成功
    在apache-ant-1.9.7/bin目录下执行命令:
    ./ant
    提示如下信息,表示ant安装成功:
    Buildfile: build.xml does not exist!
    Build failed
  2. 安装JavaCC
    (1)在JavaCC官网下载javacc-6.0src.zip代码包。
    (2)在javacc根目录javacc-6.0下执行命令:
    apache-ant-1.9.7/bin/ant
    在此指定ant的绝对路径,也可以将ant所在路径加入PATH环境变量,然后直接指向ant。
    成功安装JavaCC的提示信息:BUILD SUCCESSFUL。
    同时在javacc-6.0/bin/lib目录下的生成javacc.jar包。
    该文件内容大致如下:

        " zip.vim version v22                                                                                                                
        " Browsing zipfile /home/zfl/test/javacc-6.0src/javacc-6.0/bin/lib/javacc.jar
        " Select a file with cursor and press ENTER
    
        META-INF/
        META-INF/MANIFEST.MF
        org/
        org/javacc/
        org/javacc/jjdoc/
        org/javacc/jjdoc/test/
        org/javacc/jjtree/
        org/javacc/parser/
        org/javacc/parser/test/
        org/javacc/utils/
        templates/
        templates/cpp/
        javacc.class
        jjdoc.class
        jjtree.class
        org/javacc/JavaCCTestCase.class
        org/javacc/Version.class
        org/javacc/jjdoc/BNFGenerator.class
    

    使用

    JavaCC代码包中目录examples是JavaCC自带的例子,在此以SimpleExamples为例:Simple1.jj是用来匹配花括号的(This is a simple JavaCC grammar that recognizes a set of left braces
    followed by the same number of right braces and finally followed by
    zero or more line terminators and finally an end of file.)。

    (1)运行javacc(我在PATH添加了javacc.jar的路径,直接运行javacc时,提示找不到命令。不知道啥原因,最后用下面的方式运行了javacc。)
    /javacc-6.0/bin/lib目录下:
    [zfl@search-engine---dev4 lib]$ java -cp javacc.jar javacc Simple1.jj 
    Java Compiler Compiler Version 6.0_1 (Parser Generator)
    (type "javacc" with no arguments for help)
    Reading from file Simple1.jj . . .
    File "TokenMgrError.java" does not exist.  Will create one.
    File "ParseException.java" does not exist.  Will create one.
    File "Token.java" does not exist.  Will create one.
    File "SimpleCharStream.java" does not exist.  Will create one.
    Parser generated successfully.
    [zfl@search-engine---dev4 lib]$ ll
    total 440
    -rw-rw-r-- 1 zfl zfl   6147 May 18 03:18 ParseException.java
    -rw-rw-r-- 1 zfl zfl   7203 May 18 03:18 Simple1.java
    -rw-rw-r-- 1 zfl zfl   2512 May 17 22:29 Simple1.jj
    -rw-rw-r-- 1 zfl zfl    429 May 18 03:18 Simple1Constants.java
    -rw-rw-r-- 1 zfl zfl   5238 May 18 03:18 Simple1TokenManager.java
    -rw-rw-r-- 1 zfl zfl  12351 May 18 03:18 SimpleCharStream.java
    -rw-rw-r-- 1 zfl zfl   4055 May 18 03:18 Token.java
    -rw-rw-r-- 1 zfl zfl   4399 May 18 03:18 TokenMgrError.java
    -rwxrwxrwx 1 zfl zfl 386322 May 17 21:13 javacc.jar
    
    (2)编译JavaCC生成的Java文件,结果如下生成.class文件
    [zfl@search-engine---dev4 lib]$ javac *.java
    [zfl@search-engine---dev4 lib]$ ll
    total 476
    -rw-rw-r-- 1 zfl zfl   3038 May 18 03:24 ParseException.class
    -rw-rw-r-- 1 zfl zfl   6147 May 18 03:18 ParseException.java
    -rw-rw-r-- 1 zfl zfl   5134 May 18 03:24 Simple1.class
    -rw-rw-r-- 1 zfl zfl   7203 May 18 03:18 Simple1.java
    -rw-rw-r-- 1 zfl zfl   2512 May 17 22:29 Simple1.jj
    -rw-rw-r-- 1 zfl zfl    423 May 18 03:24 Simple1Constants.class
    -rw-rw-r-- 1 zfl zfl    429 May 18 03:18 Simple1Constants.java
    -rw-rw-r-- 1 zfl zfl   3877 May 18 03:24 Simple1TokenManager.class
    -rw-rw-r-- 1 zfl zfl   5238 May 18 03:18 Simple1TokenManager.java
    -rw-rw-r-- 1 zfl zfl   6609 May 18 03:24 SimpleCharStream.class
    -rw-rw-r-- 1 zfl zfl  12351 May 18 03:18 SimpleCharStream.java
    -rw-rw-r-- 1 zfl zfl    985 May 18 03:24 Token.class
    -rw-rw-r-- 1 zfl zfl   4055 May 18 03:18 Token.java
    -rw-rw-r-- 1 zfl zfl   2307 May 18 03:24 TokenMgrError.class
    -rw-rw-r-- 1 zfl zfl   4399 May 18 03:18 TokenMgrError.java
    -rwxrwxrwx 1 zfl zfl 386322 May 17 21:13 javacc.jar
    
    (3)执行并测试,前两个为不合法的用例,会提示错误;后两个为合法的用例,无错误提示。
    [zfl@search-engine---dev4 lib]$ java Simple1
    {{}
    Exception in thread "main" ParseException: Encountered " "\n" "\n "" at line 1, column 4.
    Was expecting:
        "}" ...
    
    at Simple1.generateParseException(Simple1.java:239)
    at Simple1.jj_consume_token(Simple1.java:177)
    at Simple1.MatchedBraces(Simple1.java:57)
    at Simple1.Input(Simple1.java:14)
    at Simple1.main(Simple1.java:9)
    [zfl@search-engine---dev4 lib]$ java Simple1
    {}{}
    Exception in thread "main" ParseException: Encountered " "{" "{ "" at line 1, column 3.
    Was expecting one of:
     
    "\n" ...
    "\r" ...
    
    at Simple1.generateParseException(Simple1.java:239)
    at Simple1.jj_consume_token(Simple1.java:177)
    at Simple1.Input(Simple1.java:42)
    at Simple1.main(Simple1.java:9)
    [zfl@search-engine---dev4 lib]$ java Simple1
    {{{{{}}}}}
    [zfl@search-engine---dev4 lib]$ java Simple1
    {}
    

你可能感兴趣的:(Java)