Fourth job

 package com.disici;
  2 
  3 import java.io.File;
  4 import java.io.FileReader;
  5 
  6 public class LexicalAnalyze {
  7     private char ch;
  8     private String reservedWord[] = {"begin", "if", "then", "while", "do", "end"}; // 保留字
  9 
 10     // 判断是否是保留字
 11     boolean isReserveWord(String str) {
 12         for (int i = 0; i < reservedWord.length; i++) {
 13             if (reservedWord[i].equals(str))
 14                 return true;
 15         }
 16         return false;
 17     }
 18 
 19     // 判断是否是字母
 20     boolean isLetter(char letter) {
 21         if ((letter >= 'a' && letter <= 'z') || (letter >= 'A' && letter <= 'Z'))
 22             return true;
 23         else
 24             return false;
 25     }
 26 
 27     // 判断是否是数字
 28     boolean isDigit(char digit) {
 29         if (digit >= '0' && digit <= '9')
 30             return true;
 31         else
 32             return false;
 33     }
 34 
 35     public void analyze(char[] chars) {
 36         String array = "";
 37         for (int i = 0; i < chars.length; i++) {
 38             ch = chars[i];
 39             array = "";
 40 
 IF 41 is (CH == '' || CH == '\ T' || CH == '\ n-' || CH == '\ R & lt') { 
 42 is the else} IF (isLetter (CH)) { 
 43 is the while ((isLetter (CH) || isDigit (CH))) { 
 44 is Array + = CH; 
 45 CH = chars [I ++]; 
 46 is} 
 47 // back one character 
 48 i--; 
 49 IF (isReserveWord ( Array)) { 
 50 // System.out.println ( "here is shown"); 
 51 // reserved word 
 52 is // System.out.println (Array); 
 53 is IF (array.equals ( "the begin") ) { 
 54 is System.out.println ( "(the begin,. 1)");
 55                     } else if (array.equals("if")) {
 56                         System.out.println("(if, 2)");
 57                     } else if (array.equals("then")) {
 58                         System.out.println("(then, 3)");
 59                     } else if (array.equals("while")) {
 60                         System.out.println("(while, 4)");
 61                     } else if (array.equals("do")) {
 62                         System.out.println("(do, 5)");
 63                     } else if (array.equals("end")) {
 64                         System.out.println("(end, 6)");
 65                     }
 66                 } else {
 67                     if (array.equals("l(l|d)*"))
 68                         System.out.println("(l(l|d)*, 10)");
 69                     else if (array.equals("dd*"))
 70                         System.out.println("(dd*, 11)");
 71                 }
 72             } else if (isDigit(ch) || (ch == '.')) {
 73                 while (isDigit(ch) || (ch == '.' && isDigit(chars[++i]))) {
 74                     if (ch == '.')
 75                         i--;
 76                     array = array + ch;
 77                     ch = chars[++i];
 78                 }
 79                 // 属于无符号常数
 80                 System.out.println("(" + array + ", 11)");
 81             } else switch (ch) {
 82                 case '+':
 83                     System.out.println("(+, 13)");
 84                     break;
 85                 case '-':
 86                     System.out.println("(-, 14)");
 87                     break;
 88                 case '*':
 89                     System.out.println("(*, 15)");
 90                     break;
 91                 case '/':
 92                     System.out.println("(/, 16)");
 93                     break;
 94                 case '(':
 95                     System.out.println("((, 27)");
 96                     break;
 97                 case ')':
 98                     System.out.println("(), 28)");
 99                     break;
100                 case '#':
101                     System.out.println("(#, 0)");
102                     break;
103                 case '=':
104                     System.out.println("(=, 25)");
105                     break;
106                 case '>': {
107                     ch = chars[++i];
108                     if (ch == '=')
109                         System.out.println("(>=, 24)");
110                     else {
111                         System.out.println("(>, 23)");
112                         i--;
113                     }
114                 }
115                 break;
116                 case '<': {
117                     ch = chars[++i];
118                     if (ch == '=')
119                         System.out.println("(<=, 21)");
120                     else {
121                         System.out.println("(<, 20)");
122                         i--;
123                     }
124                 }
125                 break;
126                 case ':': {
127                     ch = chars[++i];
128                     if (ch == '=')
129                         System.out.println("(:=, 18)");
The else {130. 
131 is System.out.println ( "(:,. 17)"); 
132 i--; 
133} 
134} 
135 BREAK; 
136} 
137} 
138} 
139 
140 public static void main (String [] args) throws {Exception 
141 is input character stream //: txt file input in the form of the characters of the file to read the character array, using the way to iterate, after reading the classified output 
142 file file = new file ( " ./ txt /analyzeTest.txt "); 
143 = the FileReader new new Reader the FileReader (File); 
144 int length = (int) file.length (); 
145 char Buffer [] = new new char [length +. 1]; 
146 reader.Read (Buffer );
147         reader.close();
148         new LexicalAnalyze().analyze(buffer);
149     }
150 }
输出结果:
(begin,1)
(=,25)
(1,11)
(>=,24)
(if,2)
(>,23)
(2,11)
(then,3)
(:=,18)
(3,11)
(end,6)

Guess you like

Origin www.cnblogs.com/du162/p/11655216.html