0917 Lexical Analysis Program (Java edition)

Source: Internet
Author: User

1. Program Description:

C and Java Two versions of all did once, slightly compared to think that although the language is different, but the central idea is the same. The idea of this program (Java version) is to first put the source code program into a large string,

Then parse the string, according to the different lexical splitting into a small string into a string array, and then identify some keywords, and corresponding to the corresponding species code, using the Loop body list. And for the C language

Version, the author's idea is: because C language does not have a string type, so the source code into a character array, and then according to the lexical interception of words or symbols, stored in a struct array, because of the need for a

Each character operation, to use more circulation body, so the speed is relatively slow, I believe there is a better idea, I also want to ponder, so I do not publish C language version.

2. Identify problems and deficiencies:

At first, the program is very small, it may be too simple, but the actual operation to find that there will be many problems, including some very low-level errors. There is the program itself has certain limitations, can identify the key words

Limited, the format of the input is limited, there are some other details of the problem, if you want to do better, you still need to change slowly. Playing code is like planting flowers, sometimes it's really urgent, and it takes patience to ensure efficiency.

3. Operation Result:

4. Source code:

Package lexical analysis;
Import Java.util.Scanner;
public class Fenxi {public static void main (string[] args) {//main function

Scanner scanner=new Scanner (system.in);
int k=0,i=0;
String[] Word=new string[20];
Word[0]= "";
System.out.println ("Please enter a program (End With ' # '):");
String Sent=scanner.nextline ();
SYSTEM.OUT.PRINTLN (Sent);
Judgetype (k, I, Word, sent);//Judgment Analysis composition type
Justone (Word);//The special case of only one English letter is unified as "L (l|d) *"
OutPut (word);
}

private static void Justone (string[] word) {//To determine if there is a special case of a single letter
int i = 0,k = 0;
while (word[i]!= null) {
if (word[i]!= "") {
if (Character.isletter (Word[i].charat (0)) &&word[i].length () ==1) {
Word[i]= "L (l|d) *";
k++;
if (k>1)
Word[i]= "NULL";//Do not repeat
}
}
i++;
}
}

private static void output (string[] word) {//Corresponds to species code, and output list
int i = 0;
SYSTEM.OUT.PRINTLN ("Word symbol \t\t\t" + "Species Code");
while (Word[i]!=null) {
Switch (Word[i]) {
Case "Begin":
System.out.println (word[i]+ "\t\t\t" + "1");
Break
Case "if":
System.out.println (word[i]+ "\t\t\t" + "2");
Break
Case ' Then ':
System.out.println (word[i]+ "\t\t\t" + "3");
Break
Case ' while ':
System.out.println (word[i]+ "\t\t\t" + "4");
Break
Case ' do ':
System.out.println (word[i]+ "\t\t\t" + "5");
Break
Case "End":
System.out.println (word[i]+ "\t\t\t" + "6");
Break
Case "L (l|d) *":
System.out.println (word[i]+ "\t\t\t" + "7");
Break
Case "dd*":
System.out.println (word[i]+ "\t\t\t" + "8");
Break
Case "+":
System.out.println (word[i]+ "\t\t\t" + "9");
Break
Case "-":
System.out.println (word[i]+ "\t\t\t" + "10");
Break
Case "*":
System.out.println (word[i]+ "\t\t\t" + "11");
Break
Case "/":
System.out.println (word[i]+ "\t\t\t" + "12");
Break
Case ":":
System.out.println (word[i]+ "\t\t\t" + "13");
Break
Case ": =":
System.out.println (word[i]+ "\t\t\t" + "14");
Break
Case "<":
System.out.println (word[i]+ "\t\t\t" + "15");
Break
Case "<=":
System.out.println (word[i]+ "\t\t\t" + "16");
Case "<>":
System.out.println (word[i]+ "\t\t\t" + "17");
Break
Case ">":
System.out.println (word[i]+ "\t\t\t" + "18");
Break
Case ">=":
System.out.println (word[i]+ "\t\t\t" + "19");
Break
Case "=":
System.out.println (word[i]+ "\t\t\t" + "20");
Break
Case ";":
System.out.println (word[i]+ "\t\t\t" + "21");
Break
Case "(":
System.out.println (word[i]+ "\t\t\t" + "22");
Break
Case ")":
System.out.println (word[i]+ "\t\t\t" + "23");
Break
Case "#":
System.out.println (word[i]+ "\t\t\t" + "24");
Break
Case ' for ':
System.out.println (word[i]+ "\t\t\t" + "25");
Break
}
i++;
}
}

private static void Judgetype (int k, int i, string[] word, String sent) {
while (Sent.charat (k)! = ' # ') {
Word[i]= "";
while (Character.isletter (Sent.charat (k))) {//is a letter
Word[i]=word[i]+sent.charat (k);
k++;

}
i++;
Word[i]= "";

//is a continuous operator
while ((Sent.charat (k) = = ' < ' | | Sent.charat (k) = = ' > ' | | Sent.charat (k) = = ' = ' | | Sent.charat (k) = = ': ') && (Sent.charat (k+1) = = ' < ' | | Sent.charat (k+1) = = ' > ' | | Sent.charat (k+1) = = ' = ' | | Sent.charat (k+1) = = ': ')} {
word[i]= "" +sent.charat (k) +sent.charat (k+1);
i++;
K=k+2;
word[i]= "";
}

//is a single symbol or operator
while (Sent.charat (k) = = ' (' | | Sent.charat (k) = = ') ' | | Sent.charat (k) = = '; ' | | Sent.charat (k) = = ': ' | | Sent.charat (k) = = ' + ' | | Sent.charat (k) = = '-' | | Sent.charat (k) = = ' * ' | | Sent.charat (k) = = '/' | | Sent.charat (k) = = ' < ' | | Sent.charat (k) = = ' > ' | | Sent.charat (k) = = ' = ') {
word[i]= "" +sent.charat (k);
k++;
i++;
word[i]= "";
}


while (Sent.charat (k) = = ") {//is a space
//system.out.println (" space ");
Word[i]= "NULL";
k++;
i++;
word[i]= "";
}

while (Character.isdigit (Sent.charat (k))) {//is a number
word[i]= "dd*";
k++;
i++;
word[i]= "";
}

}

word[i]= "" + ' # ';
System.out.println ("Jump out of the Loop" +word[i]+i);
}
}

0917 Lexical Analysis Program (Java edition)

Related Article

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.