实现一个一遍扫描的编译前端,将简化高级语言的部分语法成分(含赋值语句、分支语句、循环语句等)翻译成四元式(或三地址代码),还要求有合理的语法出错报错和错误恢复功能。
测试样例
begin
while a<b do
if c<5
begin
while x>y
begin
z:=x+1;
end
a:=2; b:=4;
c:=c-1;
area:=3.14*a*a;
s:=2*3.1416*r*(h+r);
end
end
end
#
测试结果
词法分析程序
从文件中读取程序
(1, begin)
(4, while)
(10, a)
(20, <)
(10, b)
(5, do)
(2, if)
(10, c)
(20, <)
(11, 5)
(1, begin)
(4, while)
(10, x)
(23, >)
(10, y)
(1, begin)
(10, z)
(18, :=)
(10, x)
(13, +)
(11, 1)
(26, ; )
(6, end)
(10, a)
(18, :=)
(11, 2)
(26, ; )
(10, b)
(18, :=)
(11, 4)
(26, ; )
(10, c)
(18, :=)
(10, c)
(14, -)
(11, 1)
(26, ; )
(10, area)
(18, :=)
(11, 3.14)
(15, *)
(10, a)
(15, *)
(10, a)
(26, ; )
(10, s)
(18, :=)
(11, 2)
(15, *)
(11, 3.1416)
(15, *)
(10, r)
(15, *)
(27, ()
(10, h)
(13, +)
(10, r)
(28, ))
(26, ; )
(6, end)
(6, end)
(6, end)
(0, #)
=词法分析完成=
语法分析中…,请稍候
error!--------缺少开头符号begin,程序第4行出现语法错误
error!--------不是符号then,程序第5行出现语法错误
error!--------不是符号do,程序第7行出现语法错误
=词法分析完成=
语法分析完成,共检查出3个语法错误
=中间代码生成结果=
0:if a < b goto 2
1:goto 22
2:if c < 5 goto 4
3:goto 21
4:if x > y goto 6
5:goto 9
6:t1 = x + 1
7:z = t1
8:goto 4
9:a = 2
10:b = 4
11:t2 = c - 1
12:c = t2
13:t3 = 3.14 * a
14:t4 = t3 * a
15:area = t4
16:t5 = 2 * 3.1416
17:t6 = t5 * r
18:t7 = h + r
19:t8 = t6 * t7
20:s = t8
21:goto 0
语义分析
public class Element {
String times;
String data1;
String op;
String data2;
String type;
public void setInteger(Integer integer) {
this.integer = integer;
}
Integer integer;
Element(String times,String data1,String op,String data2) {
//=
this.times = times;
this.data1 = data1;
this.op = op;
this.data2 = data2;
}
Element(String times,String data1,String op,String data2,Integer integer) {
//if goto
this.times = times;
this.data1 = data1;
this.op = op;
this.data2 = data2;
this.integer=integer;
}
Element(Integer integer) {
//if goto
this.integer=integer;
}
Element() {
//if goto
}
}
public class SemanticAnalysis {
static int j=0,t=1;
static List<Element> elements = new ArrayList<Element>();
static void memset(String times,String data1,String op,String data2) {
Element e = new Element(times,data1,op,data2);
elements.add(e);
}
public static String expression() {
// 表达式
String times,data1,op,data2;
data1 = term();
while(analyzer.llist.get(j).equals("+") || analyzer.llist.get(j).equals("-")) {
// 当前单词为+、-
if(analyzer.llist.get(j).equals("+")) // +
op = "+";
else // -
op = "-";
j++;
data2 = term();
times = "t" + (t++);
memset(times,data1,op,data2);
data1 = times;
}
return data1;
}
private static String term() {
// 项
String times,data1,op,data2;
data1 = factor();
while(analyzer.llist.get(j).equals("*") || analyzer.llist.get(j).equals("/") ) {
// 当前单词为*、/
if(analyzer.llist.get(j).equals("*")) // *
op = "*";
else // /
op = "/";
j++;
data2 = factor();
times = "t" + (t++);
memset(times,data1,op,data2);
data1 = times;
}
return data1;
}
private static String factor() {
// 因子
String data = "";
if(analyzer.map2.get(analyzer.llist.get(j)) == 10) {
// ID
data = analyzer.llist.get(j);
j++;
} else if(analyzer.map2.get(analyzer.llist.get(j)) == 11) {
// NUM
data = analyzer.llist.get(j);
j++;
}
else if(analyzer.llist.get(j).equals("(")) {
// 左括号
j++;
data = expression();
if(analyzer.llist.get(j).equals(")"))
j++;
} else {
System.out.println("Error,表达式错误");
}
return data;
}
}
语法分析
public class GrammarAnalysis {
static char[] s = new char[100];
static int sing;
static int i; //用来记录数组s中的下标;
static int error = 0