Java Language Lexical analyzer

Source: Internet
Author: User

First, the purpose of the experiment

Through the design and debugging a specific lexical analysis program, deepen the understanding of the principle of CI analysis. In the process of scanning the programming language source program, it is decomposed into lexical analysis methods of all kinds of words.

Compiling a read word process, from the input of the source program, the identification of the individual meaning of the word, that is, the basic reserved words, identifiers, constants, operators, separators five categories. and sequentially output the internal code of each word and the value of the word symbol itself. (You can display "error" When you encounter an error, and then skip the error section to continue showing).

Ii. preparatory work for the experiment

1. Function and output format of lexical analyzer

The function of the lexical analyzer is to input the source program and output the word symbol. The lexical parser's word symbol is often expressed as the following two-dollar (Word species code, the attribute value of the word symbol). In this experiment, a kind of symbol is used in the way of a kind of code.

2. The species of the word symbol in the Java programming language.

Identify the Java keyword:

Boolean,byte,char,double,false,float,int,long,new,null,short,true,void,instanceof,break,case,catch,continue, Default,do,else,for,if,return,switch,try,while,finally,throw,this,super,abstract,final,namtive,private, Protected,public, Static,synchronized,transient,volatile,class,extends,implements,interface,package,import, throws; The word identification code is 1;

Identifiers: Identifiers must start with a letter, underscore, dollar sign, and a word identification number of 2;

Constants: Constant is the number of unsigned shaping; Word species code is 3;

Operators: + 、-、 *,/, =, >, <, >=, <=, = =,! =, + + 、--、%, &&, | | |,! The word identification code is 4;

Delimiter:,,;, {,}, (,); Word species code is 5

4. Unit Modules

Business code:

Import Java.io.bufferedreader;import java.io.file;import java.io.filenotfoundexception;import java.io.FileReader;  Import Java.util.vector;public class javaanaylsis{private static string[] keyword ={"boolean", "Byte", "char", "double", "false", "float", "int", "Long", "new", "null", "short", "true", "void", "instanceof", "Break", "case", "catch", "continue"  , ' Default ', ' do ', ' else ', ' for ', ' if ', ' return ', ' switch ', ' Try ', ' while ', ' finally ', ' throw ', ' this ', ' super ', ' abstract ', "Final", "Namtive", "private", "protected", "public", "Static", "synchronized", "transient", "volatile", "class", " Extends "," implements "," Interface "," Package "," import "," throws "};static string string;static string wordstring; Static Vector vc;public javaanaylsis (String str) throws Exception{file InFile = new File (str), if (Infile.exists ()) {System . OUT.PRINTLN ("File open successfully!!! "); Try{filereader reader = new FileReader (inFile); BufferedReader br = new BufferedReader (reader); VC = new Vector (); while (string = Br.readline ())! =NULL) {//preprocessing replaces a row of strings with a single space or a contiguous tab with a space string = String.Trim (). Replace ("+", ""). ReplaceAll ("\\t+", "" ");//System . out.println (string); judgement ();} Br.close ();} catch (FileNotFoundException e) {//TODO auto-generated catch Blocke.printstacktrace ();}} ELSE{SYSTEM.OUT.PRINTLN ("File open Failed");}} /* * Determine if the read-in character is a letter */public static Boolean isletter (char c) {if ((C >= ' a ' && c <= ' z ') | | (C > + ' A ' && C <= ' Z ')) {return true;} Elsereturn false;}  /* * Determine if the read-in character is a numeric */public static Boolean isdigit (char c) {if (c >= ' 0 ' && C <= ' 9 ') {return true;} Elsereturn false;} /* * Determine if the keyword */public static boolean iskey (String ss) {int flag = 0;for (int i = 0; i < keyword.length; i++) {if (ss.equ ALS (Keyword[i])) {flag = 1;break;}} if (flag = = 1) {return true;} return false;} /* * Determine if the operator is */public static boolean isspilt (char ch) {if (ch = = ' + ' | | ch = = '-' | | ch = = ' | ' | | ch = = ' = ' | | ch = = ') ' &amp ;‘) {return true;} Elsereturn false;} /* * Determine the character entered and output the word symbol */public static void JudgemenT () throws Exception{char ch = 0;int m = 0;  String str = null;for (int i = 0; i < string.length (); i++) {switch (m) {Case 0:ch = String.charat (i);//Determine if the operator is using advance search if  (ch = = ' + ' | | ch = = '-' | | ch = = ' * ' | | ch = = ' = ' | | ch = = ' > ' | | ch = = ' < ' | | ch = = '! ' | | ch = = ' | ') {str = ""; str + = ch;if (ch = = ' + ' | | ch = = '-' | | ch = = ' > ' | | ch = = ' < ' | | CH = = '! ' | | ch = = ' | ' | | ch = = ' & ') {ch = string.charat (i + 1);//Determine whether the next character is an operator if (isspilt (CH)) {str + = Ch;m = 4;} else{ch = String.charat (i -1); The fallback operation is not an operator M = 4;}} Determines if the interface is the other if (ch = = ', ' | | | ch = = '; ' | | ch = = ' {' | | ch = = '} ' | | ch = = ' (' | | ch = = ') ' {m = 5;} Determine if the number else if (isdigit (ch = string.charat (i))) {str = ""; str + = Ch;m = 3;} Determines if the letter else if (isletter (ch = string.charat (i))) {str = ""; str + = Ch;m = 2;} Determine if an underscore or dollar sign is an else if (ch = = ' _ ' | | ch = = ' $ ') {str = ""; str + = Ch;m = 2;} Else{}break;case 4:i--; System.out.println (("(4" + "" "+ str +" ")") Wordstring = ("(4" + "" "+ str +")")"); Vc.add (wordstring); m = 0;break;case 5:i--; System.out.println (("(5" + "" "+ ch +" ")"), wordstring = ("(5" + "" "+ ch +" ")"); Vc.add (wordstring); m = 0;brea K;case 2:if (isletter (ch = string.charat (i))) {str + = ch;} else{if (IsKey (str)) {System.out.println ("(1" + "" "+ str +" Wordstring = ("(1" + "" "+ str +" ")"); Vc.add (wordstring);} Else{system.out.println (("(2 +" "" + str + "") ") Wordstring = (" (2 "+" "" + str + "") "); Vc.add (wordstring);} i--;m = 0;} Break;case 3:if (isdigit (ch = string.charat (i))) {str + = ch;} else{system.out.println ("(3" + "" + str + "") "); WordS Tring = "(3" + "" "+ str +" ")"; Vc.add (wordstring); i--;m = 0;} Break;}}} public static Vector Getvector () {return VC;}}

//Interface code:

Import Java.awt.borderlayout;import java.awt.dimension;import java.awt.toolkit;import java.awt.event.ActionEvent; Import Java.awt.event.actionlistener;import Java.io.bufferedreader;import Java.io.bufferedwriter;import Java.io.file;import Java.io.filereader;import Java.io.filewriter;import Java.io.ioexception;import Java.util.iterator;import Java.util.vector;import Javax.swing.jbutton;import Javax.swing.jfilechooser;import Javax.swing.jframe;import Javax.swing.jmenu;import Javax.swing.jmenubar;import Javax.swing.JMenuItem;import Javax.swing.jpanel;import Javax.swing.jscrollpane;import Javax.swing.jtextarea;import javax.swing.WindowConstants ;p ublic class Javaframe extends Jframe{private jtextarea textArea, textarea2;private JButton anaylisbbutton;static String filename;static Vector vcvector;public javaframe () {super (); Settitle ("Java lexical Analyzer"); Setdefaultcloseoperation ( Windowconstants.exit_on_close); Set the Close button to handle the event Toolkit tool = Toolkit.getdefaulttoolkit (); Create a toolbar dimension screensize = Tool. Getscreensize (); Get screen Size setsize (800, 500); Set the form size setlocation ((Screensize.width-getwidth ())/2, (Screensize.height-getheight ())/2); JPanel Scolljpanel = (JPanel) getcontentpane (); Get the content panel setresizable (false); The Set Maximize button cannot be used with jmenubar MenuBar = CreateMenu (); Set menu item This.setjmenubar (menuBar);//Textarea=new TextArea (); JScrollPane JScrollPane = new JScrollPane (TextArea = new JTextArea ("Source code:" + "\ n", 1000, 35)); JScrollPane jScrollPane2 = new JScrollPane (textArea2 = new JTextArea ("After lexical analysis:" + "\ n", +)); Scolljpanel.add (JSCROLLP Ane2, Borderlayout.east); Scolljpanel.add (JScrollPane, borderlayout.west); JPanel Buttonjpanel = new JPanel () Anaylisbbutton = new JButton ("Start lexical analysis"); Anaylisbbutton.addactionlistener (New Start () ); Buttonjpanel.add (Anaylisbbutton); Scolljpanel.add (Buttonjpanel, "South"); setvisible (true);} Private JMenuBar CreateMenu () {//TODO auto-generated method Stubjmenubar MenuBar = new JMenuBar ();//Initialize menu bar jmenu menu = NE W jmenu ("file"); JMenu menu2 = new JMenu ("window"); menu2.SetEnabled (FALSE); JMenu menu3 = new JMenu ("help"); menu3.setenabled (false); JMenu menu4 = new JMenu ("format"); menu4.setenabled (false); JMenuItem meun1 = new JMenuItem ("open"); JMenuItem meun2 = new JMenuItem ("Save"); JMenuItem meun3 = new JMenuItem ("Save As"); JMenuItem meun4 = new JMenuItem ("Exit"); Meun4.addactionlistener (new ActionListener () {@Overridepublic void actionperformed (ActionEvent arg0) {//TODO auto-generated method StubJavaFrame.this.dispose ();}}); Meun1.addactionlistener (new ActionListener () {@Overridepublic void actionperformed (ActionEvent arg0) {//TODO Auto-generated method stub//Initialization File selection box Textarea.settext ("Source code:" + "\ n"); JFileChooser fdialog = new JFileChooser ("d:/");//Set the title of the File selection box Fdialog.setdialogtitle ("Please select File");//Popup box int returnval = Fdialog.showopendialog (null);//If you chose the file if (jfilechooser.approve_option = = ReturnVal) {//print out the path of the file, you can modify the bit to write the path value to TextField System.out.println (Fdialog.getselectedfile ()); filename = Fdialog.getselectedfile (). toString (); File InFile = new file (filename); tRy{filereader reader = new FileReader (inFile); BufferedReader br = new BufferedReader (reader); String strings;while ((strings = Br.readline ()) = null) {Textarea.append (strings + "\ n");} Br.close ();} catch (Exception e) {//Todo:handle Exception}}}); Meun2.addactionlistener (new ActionListener () {@Overridepublic void  actionperformed (ActionEvent arg0) {//TODO auto-generated method Stubjfilechooser JF = new JFileChooser ("d:/"); int value = Jf.showsavedialog (NULL), if (value = = jfilechooser.approve_option) {//Determines whether the window points to open or save file GetPath = Jf.getselectedfile ( ); Get path System.out.println (getpath), try{filewriter fwriter = new FileWriter (GetPath); BufferedWriter out = new BufferedWriter (fwriter), Iterator Iterator = Vcvector.iterator (); while (Iterator.hasnext ()) {// Textarea2.append (Iterator.next (). ToString () + "\ n"), Out.write (Iterator.next (). ToString ()); Out.newline (); Out.close ();} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}} else{//no choice, that is, click the window Cancel}}); Menu.add (MEUN1); menU.add (meun2); Menu.add (MEUN3); Menu.addseparator (); Menu.add (MEUN4); Menubar.add (menu); Menubar.add (MENU2); Menubar.add (MENU4); Menubar.add (MENU3); return menuBar;} Private class start implements actionlistener{@Overridepublic void actionperformed (ActionEvent arg0) {//TODO Auto-generated method Stubtextarea2.settext ("Lexical Analysis:" + "\ n"); try{new javaanaylsis (filename); vcvector = new Vector (); Vcvector = Javaanaylsis.getvector (); Iterator Iterator = Vcvector.iterator (); while (Iterator.hasnext ()) { Textarea2.append (Iterator.next (). toString () + "\ n");}} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();}}} public static void Main (string[] args) {new Javaframe ();}}


Experimental summary: I spent a day in the process of programming a total of time, and on paper design took about half an hour, spent half a day to go on the machine input and commissioning, in the course of the experiment spent nearly half a day to think about the problem, but also encountered the process of programming problems, mainly in logic error problems, At first I thought my logic is not wrong, I thought it was other problems, finally calm down to clarify their own logic, or found that there is a more serious logic problems, through the online access to relevant information to solve the problem. In the end, the experiment of this Java lexical Analyzer is basically completed. For my own program I think still can deal with the basic lexical analysis, can make up this program I feel a little sense of accomplishment, this experiment let me further familiar with the Java language, improve my ability of programming thinking, increase my interests and hobbies, consolidate my knowledge. And good ...

  

Java Language Lexical analyzer

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.