词法分析程序的设计与实现
Posted ccla
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了词法分析程序的设计与实现相关的知识,希望对你有一定的参考价值。
词法分析程序(Lexical Analyzer)
要求:
- 从左至右扫描构成源程序的字符流
- 识别出有词法意义的单词(Lexemes)
- 返回单词记录(单词类别,单词本身)
- 滤掉空格
- 跳过注释
- 发现词法错误
程序结构:
输入:文本输入格式,String结构保存
处理:
–遍历:嵌套循环
–词法规则:·
输出:单词流
–二元组
单词类别:
1.标识符(12)
2.无符号数(13)
3.保留字(一词一码)
4.运算符(一词一码)
5.界符(一词一码)
实验文本Test.txt:
public class Test { public static void main(String[] args) { int i = 11; int j = 22; int k = 1 ^ 3; System.out.println("i+j=" + (i + j)); if (i >= 10) { System.out.println("词法分析器"); } // 词法分析器 } }
词法分析程序源代码:
import java.io.BufferedReader; import java.io.FileReader; import java.util.ArrayList; import java.util.HashMap; import java.util.Map; public class work { public static void main(String[] args) throws Exception { //读取文本 FileReader fr = new FileReader("src//Test.java"); BufferedReader bf = new BufferedReader(fr); String s1 = ""; String s2 = ""; while ((s1 = bf.readLine()) != null) { if (!s1.startsWith("//") && !s1.endsWith("//")) { s2 += s1; }//消除注释 } bf.close(); //运算符集合 ArrayList<String> list = new ArrayList(); list.add("+"); list.add("-"); list.add("*"); list.add("/"); list.add("%"); list.add(">"); list.add("<"); list.add("="); list.add("^"); //种别码集合 Map<Integer,String > map=new HashMap<>(); map.put(1,"public"); map.put(2,"class"); map.put(3,"static"); map.put(4,"void"); map.put(5,"main"); map.put(6,"String"); map.put(7,"args"); map.put(8,"int"); map.put(9,"System"); map.put(10,"out"); map.put(11,"println"); map.put(14,"+"); map.put(15,"-"); map.put(16,"*"); map.put(17,"/"); map.put(18,"%"); map.put(19,">"); map.put(20,"<"); map.put(21,"="); map.put(22,"=="); map.put(23,">="); map.put(24,"<="); map.put(25,"^"); map.put(26,"("); map.put(27,")"); map.put(28,"{"); map.put(29,"}"); map.put(30,";"); System.out.println(s2); //读取每个字符 char s3[] = s2.toCharArray(); for (int i = 0; i < s3.length; i++) { //判断关键字与标识符 int news3 = (int) s3[i]; String word = ""; boolean flag1 = false; while (((news3 >= 65) && (news3 <= 90)) || ((news3 >= 97) && (news3 <= 122))) { word += s3[i]; i++; news3 = (int) s3[i]; flag1 = true; } //判断数字 news3 = (int) s3[i]; String number = ""; boolean flag2 = false; while (news3 >= 48 && news3 <= 57) { number += s3[i]; i++; news3 = (int) s3[i]; flag2 = true; } //判断运算符 String operator = ""; boolean flag3 = false; if (list.contains("" + s3[i])) { operator += s3[i]; i++; flag3 = true; if (list.contains("" + s3[i])) { operator += s3[i]; i++; } } //判断字符串常量 news3 = (int) s3[i]; boolean flag4 = false; String zfc = ""; if (news3 == 34) { flag4 = true; zfc += s3[i]; i++; news3 = (int) s3[i]; while (news3 != 34) { zfc += s3[i]; i++; news3 = (int) s3[i]; } zfc += """; } //二元组输出 if (flag1 == true) { for (int j = 1; j <=11 ; j++) { if (word.equalsIgnoreCase(map.get(j))){ System.out.println("("+j+","+word+")"); break; }else if (j==11){ System.out.println("(12,"+word+")"); } } } if (flag2 == true) { System.out.println("(13," + number + ")"); } if (flag3 == true) { for (int j = 14; j <=25 ; j++) { if (operator.equalsIgnoreCase(map.get(j))){ System.out.println("("+j+","+operator+")"); } } } if (flag4 == true) { System.out.println("(13," + zfc + ")"); } //跳过空格 if (news3 == 32) { continue; } //输出界符 if (flag4 == false) { for (int j = 26; j <=30 ; j++) { if ((""+s3[i]).equalsIgnoreCase(map.get(j))){ System.out.println("("+j+","+map.get(j)+")"); } } } } } }
输出结果:
(1,public)
(2,class)
(12,Test)
(28,{)
(1,public)
(3,static)
(4,void)
(5,main)
(26,()
(6,String)
(7,args)
(27,))
(28,{)
(8,int)
(12,i)
(21,=)
(13,11)
(30,;)
(8,int)
(12,j)
(21,=)
(13,22)
(30,;)
(8,int)
(12,k)
(21,=)
(13,1)
(25,^)
(13,3)
(30,;)
(9,System)
(10,out)
(11,println)
(26,()
(13,"i+j=")
(14,+)
(26,()
(12,i)
(14,+)
(12,j)
(27,))
(27,))
(30,;)
(12,if)
(26,()
(12,i)
(23,>=)
(13,10)
(27,))
(28,{)
(9,System)
(10,out)
(11,println)
(26,()
(13,"词法分析器")
(27,))
(30,;)
(29,})
(29,})
(29,})
结论:本程序能很好的进行词法分析,由于发现词法错误过程太复杂,本程序并没有完成此功能。
以上是关于词法分析程序的设计与实现的主要内容,如果未能解决你的问题,请参考以下文章