[Compilation Principle] lexical analyzer jlex User Guide (jdk1.5.0 + jlex 1.2.6)

Source: Internet
Author: User
Tags lexer

Author: annerose environment: jdk1.5.0 + jlex 1.2.6 Date:

Jlex is based on the lex lexical analysis generator. It accepts files similar to the lex file format and generates a lexical analyzer in the Java source code format.

(This project is compiled using command line instead of eclipse)
 
1. Development Environment jdk1.5.0 Configuration
  
Step 1: Download j2sdk: To the sun official site (http://java.sun.com/j2se/1.5.0/download.jsp) download j2sdk, pay attention to download the version of the Windows offline installation SDK, at the same time it is best to download j2se 1.5.0 documentation;
  
Step 2: install and configure your j2sdk to execute the j2sdk installation program, and then install it according to the default settings.
  
Step 3: After j2sdk is installed, You need to configure the environment variables, add the following environment variables to my computer> Properties> advanced> environment variables> system variables (assuming your j2sdk is installed in 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; (.; must not be small, because it represents the current path)
Path = % java_home % \ bin
  
Then, you can write a simple Java program to test whether jdk1.5.0 has been installed successfully:
  
Public class hellojava {
Public static void main (string _ ARGs []) {
System. Out. println ("Hello, Java! ");
}
}
  
Save the above program as a file named hellojava. java.
  
Then open the Command Prompt window, CD to the directory where your hellojava. Java is located, and then type the following command
  
Javac hellojava. Java
Java hellojava (note the case sensitivity of hellojava !!!)
  
Now, if you see the hello, Java! The installation is successful. If this statement is not printed, You need to carefully check your configuration.

2. Download jlex 1.2.6, which is the K main. Java file.

Http://www.cs.princeton.edu /~ Appel/modern/Java/jlex/

3. For the following steps, refer to the 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.
Create the folder "E: \ J \ jlex" and write "E: \ J" to the environment variable classpath.

(2) Make a directory "J/jlex" and put the sourcefile main. Java in J/jlex.
Import the downloaded file main. Java into J/jlex.

(3) Compile main. Java as you wowould any Java source file: javac main. Java
This shoshould produce a number of Java class files, including main. class, in the "J/jlex" directory, where "J" is in your classpath.

Enter E: \ J \ jlex as the current directory (this is acceptable if no environment variable is set ). Run javac main. Java to obtain many class files, including main. Class.
The command line has two lines of attention.
Note: Main. Java uses unchecked or unsafe operations.
Note: recompile with-xlint: unchecked for details.

The class file has been generated. Ignore it.

(4) To Run jlex with a jlex specification file, the usage is:
Java jlex. Main <FILENAME>

Run Java jlex. Main sample. Lex to obtain the lexical analyzer code sample. Lex. java. This article does not discuss how to write Lex files. You can try the next sample. Lex from jlex.

(5) The resulting lexical analyzer source file shocould be compiled with the Java compiler:
Javac <FILENAME>
Where <FILENAME> is the name of the lexical analyzer

The javac sample. Lex. Java cannot be run. There are 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
...

Run javac-source 1.3 sample. Lex. Java, with 7 warnings. Generate 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
...

The two warnings and error messages are different. However, since the compilation is successful, no matter. Let's continue :)

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

(6) run the generated lexer:
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.

Run the lexical analyzer Java sample (Case Sensitive !!!). Enter a line to display the analysis results.

Enter {/* Comment */A: = B & C ;}
Display

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)

I have learned how to use jlex. Thank you for your attention.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.