作者:ANNEROSE  环境:Jdk1.5.0 + JLex 1.2.6 日期:2007-01-07

Jlex工具基于Lex词法分析生成器,它接受类似Lex文件格式的文件,生成Java源代码格式的词法分析器。

(本项目用命令行编译,不使用Eclipse) 
 
1. 开发环境jdk1.5.0配置
  
  第一步:下载j2sdk:到sun官方站(
http://java.sun.com/j2se/1.5.0/download.jsp)下载j2sdk,注意下载版本为Windows Offline Installation的SDK,同时最好下载J2SE 1.5.0 Documentation;
  
  第二步:安装和配置你的j2sdk执行j2sdk的安装程序,然后按默认设置进行安装即可。
  
  第三步:安装j2sdk以后,需要配置一下环境变量,在我的电脑->属性->高级->环境变量->系统变量中添加以下环境变量(假定你的j2sdk安装在C:\Program Files\Java\jdk1.5.0)

  
  JAVA_HOME=C:\Program Files\Java\jdk1.5.0
  classpath=.;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar;(.;一定不能少,因为它代表当前路径)
  path=%JAVA_HOME%\bin
  
  接着可以写一个简单的java程序来测试jdk1.5.0是否已安装成功:
  
     public class HelloJava {
        public static void main(String _args[]) {
            System.out.println("Hello, Java!");
        }
    }
  
  将上面的这段程序保存为文件名为HelloJava.java的文件。
  
  然后打开命令提示符窗口,cd到你的HelloJava.java所在目录,然后键入下面的命令
  
  javac HelloJava.java
  java HelloJava
(注意HelloJava的大小写!!!)
  
  此时如果看到打印出来Hello, Java! 的话说明安装成功了,如果没有打印出这句话,你需要仔细检查一下你的配置情况。


2. 下载JLEX 1.2.6 就是那个200K的Main.java文件

http://www.cs.princeton.edu/~appel/modern/java/JLex/

3. 以下步骤参照JLex的README

(1) Choose some directory that is on your CLASSPATH, where  you install Java utilities such as JLex.  I will refer to this directory as "J", for example.
于是建立文件夹 "E:\J\JLex", 把"E:\J" 写入环境变量CLASSPATH

(2) Make a directory "J/JLex" and put the sourcefile Main.java in J/JLex.
把下载的文件Main.java 拷入 J/JLex.

(3) Compile Main.java as you would any Java source file: javac Main.java
This should produce a number of Java class files, including Main.class, in the "J/JLex" directory, where "J" is in your CLASSPATH.

进入E:\J\JLex为当前目录(不作也可以,因为之前设了环境变量)。运行 javac Main.java 可以得到很多class文件,包括Main.class
命令行有2行注意
Note: Main.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

反正class文件已经生成。不管它。

(4) To run JLex with a JLex specification file, the usage is:
 java JLex.Main <filename>

运行java JLex.Main sample.lex,得到词法分析器代码sample.lex.java。lex文件怎么写,本文不作论述。可以从JLex下一个sample.lex试一下。


(5) The resulting lexical analyzer source file should be compiled with the Java compiler:
 javac <filename>
where <filename> is the name of the lexical analyzer

运行 javac sample.lex.java 不能通过,有7 errors。

sample.lex.java:11: as of release 1.4, 'assert' is a keyword, and may not be used as an identifier
(try -source 1.3 or lower to use 'assert' as an identifier)
  public static void assert
...

再运行 javac  -source 1.3 sample.lex.java,有7 warnings。生成Sample.class, Utility.class, Yylex.class, Yytoken.class

sample.lex.java:11: warning: as of release 1.4, 'assert' is a keyword, and may not be used as an identifier
(try -source 1.4 or higher to use 'assert' as a keyword)
  public static void assert
...

上面两则警告和出错信息自相矛盾,真是岂有此理。不过既然编译通过,就不管了。我们继续:)

 注: javac -source <release>          Provide source compatibility with specified release

(6) Run the generated lexer with:
  java Sample
which expects input on stdin.  The lexer parses tokens that resemble those for a typical programming language; whitespace is generally ignored.  Java
buffers input from stdin a line at a time, so you won't see any output until you type enter.

运行该词法分析器 java Sample(注意大小写!!!)。输入一行就显示分析结果。

输入{ /* comment */ a := b & c; }
显示

Token #7: { (line 0)
Token #43: a (line 0)
Token #22: := (line 0)
Token #43: b (line 0)
Token #20: & (line 0)
Token #43: c (line 0)
Token #2: ; (line 0)
Token #8: } (line 0)
Exception in thread "main" java.lang.Error: Lexical Error: Unmatched Input.
        at Yylex.yylex(sample.lex.java:374)
        at Sample.main(sample.lex.java:6)

 以上是我学习使用JLEX的一点心得,欢迎各位高手指正。

 

posted on 2007-01-07 22:10  astate  阅读(5653)  评论(1编辑  收藏  举报