首页 > 代码库 > 简易词法分析器
简易词法分析器
private static void search(char[] ch) { String s = "" ; int de = 0; char a; boolean isImport = false;//是否为关键字 boolean isNum =false;//是否为数字 boolean isId = false;//是否为字符串 boolean isBi= false;//是否为单个操作符 boolean isBiM = false;//是狗为组合操作符 String[] proter = { "int", "main", "char", "if", "else", "for", "while" }; int i = 0; while (i < ch.length) { String pr = ""; while (ch[i] != ‘ ‘ ){ if((ch[i] <= ‘z‘) && (ch[i]>=‘a‘) || (ch[i] <=‘Z‘) && (ch[i] >=‘A‘)) //System.out.print(ch[i]); isId = true; if((ch[i]<=‘9‘) && (ch[i]>=‘0‘)) //System.out.print(ch[i]); isNum = true; switch (ch[i]) { case ‘=‘: if(ch[i+1] == ‘=‘){ de=39; s="=="; isBiM = true; i++; //System.out.print(s); } else de = 21; isBi = true; break; case ‘>‘: if(ch[i+1] == ‘=‘){ de=37; s=">="; isBiM = true; i++; //System.out.print(s); } else de=35; isBi = true; break; case ‘]‘: de=29; isBi=true; break; case ‘{‘: de=30; isBi=true; break; case ‘}‘: de=31; isBi=true; break; case ‘:‘: de=33; isBi=true; break; case ‘;‘: de=34; isBi=true; break; case ‘.‘: de=32; isBi=true; break; case ‘<‘: if(ch[i+1] == ‘=‘){ de=38; s="<="; isBiM = true; i++; //System.out.print(s); } else de=36; isBi=true; break; case ‘*‘: de=24; isBi = true; break; case ‘+‘: de=22; isBi=true; break; case ‘-‘: de=23; isBi = true; break; case ‘/‘: de=25; isBi = true; break; case ‘(‘: de=26; isBi = true; break; case ‘)‘: de=27; isBi = true; break; case ‘#‘: de=0; isBi =true; break; } if((ch[i] == ‘!‘) && (ch[i+1] == ‘=‘)){ de=40; s="!="; isBiM = true; i++; } if(isBi){ break; } pr += ch[i++]; } // System.out.print(pr); ////判断关键字 for (int j = 0; j < proter.length; j++) { if (pr.equals(proter[j])){ isImport = true; System.out.println("(" + (j + 1) + "," + proter[j] + ")"); } } //字母或字母数字串 if(isId && !isImport) System.out.println("(" +10+ "," + pr+ ")"); //数字 if(!isId && isNum ) System.out.println("(" +20+ "," + pr+ ")"); //单个专用符号 if(isBi && !isBiM) System.out.println("("+de+","+ch[i]+")"); //组合符号 if(isBiM) System.out.println("("+de+","+s+")"); //else System.out.println("("+-1+","+"ERRO"+")"); i++; isId = false; isNum = false; isImport = false; isBi = false; isBiM=false; // System.out.print("("+); } }
简易词法分析器
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。