首页 > 代码库 > 编译原理小作业词法分析程序

编译原理小作业词法分析程序

词法分析程序所参照的编码如70页表3.3

(《编译技术》张莉等著.--北京:高等教育出版社,2016.9.ISBN: 978-7-04-046317-0)

用Java语言实现如下:

  1 import java.io.IOException;  2 import java.util.Arrays;  3 import java.util.HashMap;  4 import java.util.Map;  5   6 class Code{  7     public static final int BEGINSY = 1;  8     public static final int ENDSY = 2;  9     public static final int IFSY = 3; 10     public static final int THENSY = 4; 11     public static final int ELSE = 5; 12     public static final int IDSY = 20; 13     public static final int INTSY = 21; 14     public static final int PLUSY = 22; 15     public static final int MINUSSY = 23; 16     public static final int STARSY = 24; 17     public static final int DIVISY = 25; 18     public static final int LPARSY = 26; 19     public static final int RPARSY = 27; 20     public static final int COMMASY = 28; 21     public static final int SEMISY = 29; 22     public static final int COLONSY = 30; 23     public static final int ASSIGNSY = 31; 24     public static final int EQUSY = 32; 25     public static Map<String, Integer> map = new HashMap<String, Integer>(); 26     // map to find the id of reserved. 27     private static final String[] reserved = {"BEGIN", "END", "IF", "THEN", "ELSE"}; 28     public static void addReserv(){ 29         map.put("BEGIN", BEGINSY); 30         map.put("END", ENDSY); 31         map.put("IF", IFSY); 32         map.put("THEN", THENSY); 33         map.put("ELSE", ELSE); 34     } 35     public static boolean isReserv(String s){ 36         return Arrays.asList(reserved).contains(s); 37     } 38 } 39  40  41 public class Word { 42     private static String token = ""; 43     private static char lastC = 0; 44      45     public static void main(String[] args) { 46         char c=‘ ‘;//c is a char 47         Code.addReserv(); 48         do { 49             if(lastC==0){ 50                 do{//erase the blank character. 51                     try { 52                         c = (char)System.in.read(); 53                     } catch (IOException e) {e.printStackTrace();} 54                 }while(c==‘ ‘||c==‘\t‘); 55             }else{ 56                 c = lastC;//withdraw 57             } 58             int tmp = getsym(c); 59             if(tmp!=-1&&tmp!=0) 60                 System.out.println(tmp); 61         } while (c!=-1); 62     } 63      64     private static int getsym(char c){ 65         lastC = 0;//give lastC a constant value to test whether it is altered. 66         if(Character.isLetter(c)){// if c is a letter then link them together. 67             token = ""; 68             do{ 69                 token += c; 70                 try { 71                     c = (char)System.in.read(); 72                 } catch (IOException e) {e.printStackTrace();} 73             }while(Character.isDigit(c)||Character.isLetter(c)); 74             lastC = c;//use lastC to withdraw; 75             System.out.print(token+"\t"); 76             return Code.isReserv(token)? Code.map.get(token).intValue() : Code.IDSY; 77             //judge if token is reserved or Identifier. 78         }else if(Character.isDigit(c)){// if c is digit then link them together 79             token = ""; 80             do{ 81                 token += c; 82                 try { 83                     c = (char)System.in.read(); 84                 } catch (IOException e) {e.printStackTrace();} 85             }while(Character.isDigit(c)); 86             lastC = c;//use lastC to withdraw; 87             System.out.print(token+"\t"); 88             return Code.INTSY; 89         }else if(c==‘:‘){ 90             try { 91                 c = (char)System.in.read(); 92             } catch (IOException e) {e.printStackTrace();} 93             if(c==‘=‘){ 94                 System.out.print(":=\t"); 95                 return Code.ASSIGNSY; 96             }else{ 97                 System.out.print(":\t"); 98                 lastC = c;//use lastC to withdraw; 99                 return Code.COLONSY;100             }101         }else if(c==‘+‘){102             System.out.print("+\t");103             return Code.PLUSY;104         }else if(c==‘-‘){105             System.out.print("-\t");106             return Code.MINUSSY;107         }else if(c==‘*‘){108             System.out.print("*\t");109             return Code.STARSY;110         }else if(c==‘(‘){111             System.out.print("(\t");112             return Code.LPARSY;113         }else if(c==‘)‘){114             System.out.print(")\t");115             return Code.RPARSY;116         }else if(c==‘,‘){117             System.out.print(",\t");118             return Code.COMMASY;119         }else if(c==‘;‘){120             System.out.print(";\t");121             return Code.SEMISY;122         }else if(c==‘=‘){123             System.out.print("=\t");124             return Code.EQUSY;125         }else if(c==‘/‘){126             try {127                 c = (char)System.in.read();128             } catch (IOException e) {e.printStackTrace();}129             if(c!=‘*‘){130                 System.out.print("/\t");131                 lastC = c;//use lastC to withdraw;132                 return Code.DIVISY;133             }else{//handle with the comments.134                 do{135                     do{136                         try {137                             c = (char)System.in.read();138                         } catch (IOException e) {e.printStackTrace();}139                     }while(c!=‘*‘);140                     do{141                         try {142                             c = (char)System.in.read();143                         } catch (IOException e) {e.printStackTrace();}144                         if(c==‘/‘)    return 0;145                     }while(c==‘*‘);146                 }while(c!=‘*‘);147             }148         }149         return -1;//-1 means wrong;150     }151     152     153 }

示例:

技术分享

技术分享

编译原理小作业词法分析程序