Import Java.awt.*;import java.util.scanner;/** * Created by s2002 on 2016/9/30. */public class COM {public static void main (string[] Args) {//limit string maximum final int max_len = 100; Keyword string[] Key_word = new string[]{"begin", "end", "if", "then", "when", "do"}; Inputting user input Scanner input = new Scanner (system.in); System.out.print ("please Input A string <end with ' # ';:"); String ustring = Input.nextline (); char[] analysedata = new char[max_len]; int index = 0, key = 0; List List = new List (); Do {String comparestr = null; Char temp = Ustring.charat (index); List = Extactcharacters (temp, analysedata, key_word, ustring, index, comparestr); If (list.getitemcount () = = 0) {index++; Continue }//specifies that the first element of the list is index and the second element is key index = Integer.parseint (list.getitem (0)); Key = Integer.parseint (lIst.getitem (1)); String words = List.getitem (2); System.out.println ("<" + key + "," + words + ">"); } While (key! = 0); } public static List extactcharacters (char temp, char[] analysedate, string[] keywords, String ustring, int index, String comparestr) {int keyID =-1, m = 0; List List = new List (); Determines whether the next read character is a space, skips if read to a space, extracts the next character to judge while (temp! = ") {//determines If the current character is a combination of letters or numbers and letters if (temp >= ' a ' && temp <= ' z ') {m = 0; When reading to a word that is not an uppercase or lowercase letter or a number, read complete while (temp >= ' a ' && temp <= ' z ' | | temp >= ' a ' && ; Temp <= ' Z ' | | Temp >= ' 0 ' && temp <= ' 9 ') {analysedate[m++] = temp; Comparestr + = temp + ""; temp = Ustring.charat (++index); }//with read-out characters to determine if the keyword is comParestr = comparestr.substring (4); for (int i = 0; i < 6; i++) {if (comparestr.equals (keywords[i])) {keyID = i + 1; List.add (index + ""); List.add (keyID + ""); List.add (comparestr); Return list; }}//if a non-keyword is used as an identifier KeyID = 10; List.add (index + ""); List.add (keyID + ""); List.add (comparestr); Return list; }//determine if the current character is a number else if (temp >= ' 0 ' && temp <= ' 9 ') {m = 0; String Temptokens = null; Determine if the following character is a number while (temp >= ' 0 ' && temp <= ' 9 ') {analysedate[m++] = Temp Temptokens + = temp; temp = Ustring.charat (++index); } Not a number returns the species code, ending the current method KeyID = 11; Temptokens = temptokens.substring (4); List.add (index + ""); List.add (keyID + ""); List.add (temptokens + ""); Return list; } m = 0; Determines whether the current character is a different relational operator String token = null; Switch (temp) {case ' < '://String token = null; analysedate[m++] = temp; Token + = temp; If (ustring.charat (++index) = = ' = ') {analysedate[m++] = temp; KeyID = 22; Token + = Ustring.charat (index++); } else if (ustring.charat (++index) = = ' > ') {analysedate[m++] = temp; KeyID = 21; Token + = Ustring.charat (index++); } Else {keyID = 23; } list.add (index + ""); List.add (keyID + ""); token = token.substring (4); List.add (token); Return list; Case ' > ': analysedate[m++] = temp; Token + = temp; If (ustring.charat (++index) = = ' = ') {keyID = 24; analysedate[m++] = temp; Token + = Ustring.charat (index++); } Else {keyID = 20; } list.add (index + ""); List.add (keyID + ""); token = token.substring (4); List.add (token); Return list; Case ': ': analysedate[m++] = temp; Token + = temp; If (ustring.charat (++index) = = ' = ') {keyID = 18; analysedate[m++] = temp; analysedate[m++] = Ustring.charat (index); Token + = Ustring.charat (index++); } Else {keyID = 17; } list.add (index + ""); List.add (keyID + ""); token = token.substring (4); List.add (token); Return list; Case ' * ': KeyID = 13; Break Case '/': KeyID = 14; Break Case ' + ': KeyID = 15; Break Case '-': KeyID = 16; Break Case ' = ': KeyID = 25; Break Case '; ': KeyID = 26; Break Case ' (': KeyID = 27; Break Case ') ': KeyID = 28; Break Case ' # ': KeyID = 0; Break Default:keyid =-1; Break } analysedate[m++] = temp; List.add (++index + ""); List.add (keyID + ""); List.add (temp + ""); Return list; } return list; }}
Program Function: can simulate the user input code decomposition into words, lexical analysis, and its characters and species code corresponding OUTPUT.
Species Code Table:
|
Word symbols |
Species yards |
Word symbols |
Species yards |
|
Begin |
1 |
: |
17 |
|
If |
2 |
:= |
18 |
|
Then |
3 |
< |
20 |
|
While |
4 |
<= |
21st |
|
Do |
5 |
<> |
22 |
|
End |
6 |
> |
23 |
|
L (L|D) * |
10 |
>= |
24 |
|
dd* |
11 |
= |
25 |
|
+ |
13 |
; |
26 |
|
- |
14 |
( |
27 |
|
* |
15 |
) |
28 |
|
/ |
16 |
# |
0 |
Lexical editor (Java)