Java Authoring Lexical Analyzer

Source: Internet
Author: User

The lexical analyzer is to scan a program to determine whether it is a keyword, identifier, constant, delimiter, operator. Generally divided into one character and the classic V;

Here I use the classic V, this lexical parser is written in Java;

/*
Reserved words | Keyword: 1
operator | operators: 2
Delimiter: 3
Identifier: 4
Constant: 5
No recognition: 6
*/

The main code is:

/*** This program is by reading the characters of the file into a character array, and then traversing the array, the characters are read * sorted and output *@author * */ Public classWordanalyze {PrivateString keyword[] = {"Break", "include", "Begin", "End", "if", "Else", "while", "switch"}; Private Charch; //determine if it is a keyword    BooleanIsKey (String str) { for(inti = 0;i < keyword.length;i++)        {            if(Keyword[i].equals (str))return true; }        return false; }    //determine if it is a letter    BooleanIsletter (CharLetter ) {        if(Letter >= ' a ' && letter <= ' z ') | | (Letter >= ' A ' && letter <= ' Z '))            return true; Else            return false; }    //determine if it is a number    BooleanIsDigit (CharDigit) {        if(Digit >= ' 0 ' && digit <= ' 9 ')            return true; Else            return false; }    //Lexical Analysis    voidAnalyzeChar[] chars) {String arr= "";  for(inti = 0;i< chars.length;i++) {ch=Chars[i]; Arr= ""; if(ch = = "| | ch = = ' \ t ' | | ch = = ' \ n ' | | ch = = ' \ r '){}            Else if(Isletter (ch)) { while(Isletter (ch) | |isdigit (CH)) {arr+=ch; CH= chars[++i]; }                //fallback one characteri--; if(IsKey (arr)) {//Key WordsSystem.out.println (arr+ "\t4" + "\ t keyword"); }                Else{                    //identifiersSystem.out.println (arr+ "\t4" + "\ T identifier"); }            }            Else if(IsDigit (ch) | | (ch = = '. ')))            {                 while(IsDigit (ch) | | (ch = = '. ') &&isdigit (chars[++i] )) {if(ch = = '. ') i--; Arr= arr +ch; CH= chars[++i]; }                //belongs to the unsigned constantSystem.out.println (arr+ "\t5" + "\ T constant"); }            Else Switch(CH) {//operator                 Case' + ': System.out.println (ch+ "\t2" + "\ t operator"); Break;  Case'-': System.out.println (ch+ "\t2" + "\ t operator"); Break;  Case' * ': System.out.println (ch+ "\t2" + "\ t operator"); Break;  Case'/': System.out.println (ch+ "\t2" + "\ t operator"); Break; // Delimiter                 Case' (': System.out.println (ch+ "\t3" + "\ t delimiter"); Break;  Case') ': System.out.println (ch+ "\t3" + "\ t delimiter"); Break;  Case' [': System.out.println (ch+ "\t3" + "\ t delimiter"); Break;  Case'] ': System.out.println (ch+ "\t3" + "\ T"); Break;  Case'; ': System.out.println (ch+ "\t3" + "\ t delimiter"); Break;  Case' {': System.out.println (ch+ "\t3" + "\ t delimiter"); Break;  Case'} ': System.out.println (ch+ "\t3" + "\ t delimiter"); Break; //operator                 Case=: {ch= chars[++i]; if(ch = = ' = ') System.out.println ("= =" + "\t2" + "\ t operator")); Else{System.out.println ("=" + "\t2" + "\ t operator"); I--; }                         } Break;  Case‘:‘: {ch= chars[++i]; if(ch = = ' = ') System.out.println (": =" + "\t2" + "\ t operator"); Else{System.out.println (":" + "\t2" + "\ t operator"); I--; }                         } Break;  Case' > ': {ch= chars[++i]; if(ch = = ' = ') System.out.println (">=" + "\t2" + "\ t operator"); Else{System.out.println (">" + "\t2" + "\ t operator"); I--; }                         } Break;  Case' < ': {ch= chars[++i]; if(ch = = ' = ') System.out.println ("<=" + "\t2" + "\ t operator"); Else{System.out.println ("<" + "\t2" + "\ t operator"); I--; }                         } Break; //No recognition                default: System.out.println (ch+ "\t6" + "\ T no identifier"); }        }    } Public Static voidMain (string[] args)throwsException {File file=NewFile ("E:\\data.txt");//defines a file object that is used to initialize the FileReaderFileReader reader =NewFileReader (file);//defines a FileReader object that is used to initialize the BufferedReader    intLength = (int) file.length (); //when defining a character array, you need to define one more, because the lexical analyzer will encounter a character that is read ahead, and if it is the last//The character is read, and if the next character is read, an out-of-bounds exception occurs    CharBuf[] =New Char[Length+1];    Reader.read (BUF);    Reader.close (); Newwordanalyze (). analyze (BUF); }}

Operation Result:

Java Authoring 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.