├── .DS_Store
├── README.md
├── interpreter.iml
├── lhy.iml
├── src
├── AddressByte.java
├── Groove.java
├── Jvm.java
├── Lexer.java
├── Main.java
├── Parser.java
├── Token.java
├── TokenType.java
└── Value.java
└── tmp
├── block1.i
├── expr1.i
├── expr2.i
├── expr3.i
├── expr4.i
├── identity.i
├── identity2.i
├── jump1.i
├── jump2.i
├── jump3.i
├── prog0.i
├── prog1.i
├── prog2.i
├── prog3.i
├── prog4.i
└── test.i
/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JavaGraduationProject/CompilationPrincipleDesign/59b169fd92326c11025f5b0bd661286ced4d34d9/.DS_Store
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # 全目录
2 |
3 | [更多系统、论文,供君选择 ~~>](https://www.yuque.com/wisebit/blog)
4 |
5 | # 7.CompilationPrincipleDesign
6 |
7 |
8 |
群: 983063232(入群获取sql文件)
9 | QQ: 206157502(加好友获取sql文件)
10 |
11 | 7.编译解释器
12 |
13 |
14 |
15 |
16 |
17 | # 简介
18 |
19 | > 本代码来源于网络,仅供学习参考使用, 请入群(983063232)后联系群主索要sql文件!
20 | >
21 | > 提供1.远程部署/2.修改代码/3.设计文档指导/4.框架代码讲解等服务
22 | >
23 | >
24 | 一、需求分析
25 | 以编译原理书中最后一项作业,根据前端生成的中间代码来编写后端解释器,其中间代码标示了每行的行号,同时还包含了赋值语句、跳转语句、二位运算表达式的四则运算,条件语句跳转,列表类型的值获取等等,对于这个简单的中间代码执行程序,可以很简单的根据每行定义一个槽列表,方便跳转语句。主要的值类型可分为整型,布尔类型,我使用Object类型的列表存入,用instance判断其类型,从而转换,语法分析器中读入词法列表,根据每个槽生成槽内的字节码列表,槽对应行号和字节码列表。
26 | 二、可行性分析
27 |
28 | 上文中我使用槽代表每行应该执行的语句,后面我定义了一部分字节码,整个流程是以编译成字节码后执行的,对于字节码和语句树的方式,前者更加方便操作(对于列表的前进后退等等),更能完好的控制流程跳转。
29 | 最后使用虚拟机的方式遍历执行字节码,虚拟机参考Python的执行方式,虚拟机内有两个值列表,一个存入当前作用域内执行的值,一个HashMap存入变量,对应执行列表中的某个位置。
30 |
31 |
32 | 三、总体设计
33 | 1. 首先主程序读入文件,转换成字符串。
34 | 2. 传入词法分析器,生成特定的词法列表。
35 | 3. 传入语法分析器,生成槽列表(字节码列表)。
36 | 4. 槽列表传入虚拟机,遍历执行。
37 | 5. 得到执行完成的两个列表,运行内列表和变量哈希列表,输出。
38 |
39 |
40 |
41 |
42 | # 环境
43 |
44 | - IntelliJ IDEA 2009.3
45 |
46 | - Mysql 5.7.26
47 |
48 | - Tomcat 7.0.73
49 |
50 | - JDK 1.8
51 |
52 |
53 | ## 缩略图
54 |
55 | 
56 |
57 | 
58 |
59 | 
60 |
61 | 
62 |
63 | 
64 |
65 |
--------------------------------------------------------------------------------
/interpreter.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/lhy.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/src/AddressByte.java:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JavaGraduationProject/CompilationPrincipleDesign/59b169fd92326c11025f5b0bd661286ced4d34d9/src/AddressByte.java
--------------------------------------------------------------------------------
/src/Groove.java:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JavaGraduationProject/CompilationPrincipleDesign/59b169fd92326c11025f5b0bd661286ced4d34d9/src/Groove.java
--------------------------------------------------------------------------------
/src/Jvm.java:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JavaGraduationProject/CompilationPrincipleDesign/59b169fd92326c11025f5b0bd661286ced4d34d9/src/Jvm.java
--------------------------------------------------------------------------------
/src/Lexer.java:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JavaGraduationProject/CompilationPrincipleDesign/59b169fd92326c11025f5b0bd661286ced4d34d9/src/Lexer.java
--------------------------------------------------------------------------------
/src/Main.java:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JavaGraduationProject/CompilationPrincipleDesign/59b169fd92326c11025f5b0bd661286ced4d34d9/src/Main.java
--------------------------------------------------------------------------------
/src/Parser.java:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JavaGraduationProject/CompilationPrincipleDesign/59b169fd92326c11025f5b0bd661286ced4d34d9/src/Parser.java
--------------------------------------------------------------------------------
/src/Token.java:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JavaGraduationProject/CompilationPrincipleDesign/59b169fd92326c11025f5b0bd661286ced4d34d9/src/Token.java
--------------------------------------------------------------------------------
/src/TokenType.java:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JavaGraduationProject/CompilationPrincipleDesign/59b169fd92326c11025f5b0bd661286ced4d34d9/src/TokenType.java
--------------------------------------------------------------------------------
/src/Value.java:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/JavaGraduationProject/CompilationPrincipleDesign/59b169fd92326c11025f5b0bd661286ced4d34d9/src/Value.java
--------------------------------------------------------------------------------
/tmp/block1.i:
--------------------------------------------------------------------------------
1 | L1: a = 0
2 | L3: b = 0
3 | L4: b = 1
4 | L6: a = 2
5 | L7: b = 3
6 | L8: a = a + 1
7 | L9: b = b + 1
8 | L5: a = a + 1
9 | L10: b = b + 1
10 | L2:
11 |
--------------------------------------------------------------------------------
/tmp/expr1.i:
--------------------------------------------------------------------------------
1 | L1: i = 0
2 | L3: i = 365
3 | L4: x = 0.0
4 | L5: x = 3.1415896
5 | L6: b = true
6 | L7: b = false
7 | L8: i = x
8 | L9: x = i
9 | L2:
10 |
--------------------------------------------------------------------------------
/tmp/expr2.i:
--------------------------------------------------------------------------------
1 | b = 4
2 | c = 8
3 |
4 | L1: a = b + c
5 | L3: a = b - c
6 | L4: a = b * c
7 | L5: a = b / c
8 | L6: a = minus b
9 | L7: t1 = a - b
10 | d = t1 - c
11 | L8: t2 = a * b
12 | d = t2 * c
13 | L9: t3 = b * c
14 | d = a + t3
15 | L10: t4 = a * b
16 | d = t4 + c
17 | L11: t5 = a - b
18 | d = t5 - c
19 | L12: t6 = b - c
20 | d = a - t6
21 | L13: t7 = a + b
22 | d = t7 * c
23 | L14: t8 = b + c
24 | d = a * t8
25 | L15: t9 = b * b
26 | t10 = 4.0 * a
27 | t11 = t10 * c
28 | term = t9 - t11
29 | L2:
30 |
--------------------------------------------------------------------------------
/tmp/expr3.i:
--------------------------------------------------------------------------------
1 | x = 5
2 | y = 3
3 |
4 | L1: if x < y goto L4
5 | t1 = true
6 | goto L5
7 | L4: t1 = false
8 | L5: r = t1
9 | L3: iffalse x == y goto L7
10 | t2 = true
11 | goto L8
12 | L7: t2 = false
13 |
14 | L2:
15 |
--------------------------------------------------------------------------------
/tmp/expr4.i:
--------------------------------------------------------------------------------
1 | L1: t1 = i * 12
2 | t2 = j * 4
3 | t3 = t1 + t2
4 | t4 = a [ t3 ]
5 | x = c + t4
6 | L3: t5 = i * 12
7 | t6 = j * 4
8 | t7 = t5 + t6
9 | a [ t7 ] = 0
10 | L4: t8 = i * 100
11 | t9 = j * 10
12 | t10 = t8 + t9
13 | t11 = k * 1
14 | t12 = t10 + t11
15 | b [ t12 ] = true
16 | L5: t13 = i * 100
17 | t14 = j * 10
18 | t15 = t13 + t14
19 | t16 = k * 1
20 | t17 = t15 + t16
21 | d = b [ t17 ]
22 | L2:
23 |
--------------------------------------------------------------------------------
/tmp/identity.i:
--------------------------------------------------------------------------------
1 | L1: i = 0
2 | L3: iffalse i < 10 goto L4
3 | L5: j = 0
4 | L6: iffalse j < 10 goto L7
5 | L8: t1 = i * 80
6 | t2 = j * 8
7 | t3 = t1 + t2
8 | a [ t3 ] = 0.0
9 | L9: j = j + 1
10 | goto L6
11 | L7: i = i + 1
12 | goto L3
13 | L4: i = 0
14 | L10: iffalse i < 10 goto L2
15 | L11: t4 = i * 80
16 | t5 = i * 8
17 | t6 = t4 + t5
18 | a [ t6 ] = 1.0
19 | L12: i = i + 1
20 | goto L10
21 | L2:
22 |
--------------------------------------------------------------------------------
/tmp/identity2.i:
--------------------------------------------------------------------------------
1 | L1: i = 0
2 | L3:L5: j = 0
3 | L6:L8: t1 = i * 80
4 | t2 = j * 8
5 | t3 = t1 + t2
6 | a [ t3 ] = 0.0
7 | L9: iffalse j >= 10 goto L6
8 | L10: goto L7
9 | goto L6
10 | L7: iffalse i >= 10 goto L3
11 | L11: goto L4
12 | goto L3
13 | L4: i = 0
14 | L12:L13: t4 = i * 80
15 | t5 = i * 8
16 | t6 = t4 + t5
17 | a [ t6 ] = 1.0
18 | L14: iffalse i >= 10 goto L12
19 | L15: goto L2
20 | goto L12
21 | L2:
22 |
--------------------------------------------------------------------------------
/tmp/jump1.i:
--------------------------------------------------------------------------------
1 | L1:L4: a = 0
2 | L3: goto L5
3 | L6: x = 0
4 | L5: iffalse a < b goto L7
5 | L8: a = b
6 | L7: iffalse x <= y goto L9
7 | L10: x = y
8 | L9: iffalse a == b goto L11
9 | L12: a = b
10 | L11: iffalse x != y goto L13
11 | L14: x = y
12 | L13: iffalse a >= b goto L15
13 | L16: b = a
14 | L15: iffalse x > y goto L17
15 | L18: y = x
16 | L17: iffalse a == b goto L19
17 | L20:L19: if x < 100 goto L23
18 | iffalse x > 200 goto L21
19 | L23:L22: x = 0
20 | L21: iffalse a < 100 goto L24
21 | iffalse a > 200 goto L24
22 | L25: b = 0
23 | L24: if x < 100 goto L28
24 | iffalse x > 200 goto L26
25 | iffalse x != y goto L26
26 | L28:L27: x = 0
27 | L26: if a < 100 goto L31
28 | iffalse a > 200 goto L32
29 | if a != 150 goto L31
30 | L32: iffalse a != 0 goto L29
31 | L31:L30: a = 0
32 | L29: iffalse x > 200 goto L36
33 | if x != b goto L35
34 | L36: iffalse x < 100 goto L33
35 | L35:L34: x = 0
36 | L33: if a < 100 goto L38
37 | iffalse a > 200 goto L2
38 | iffalse a != b goto L2
39 | L38:L37: a = 0
40 | L2:
41 |
--------------------------------------------------------------------------------
/tmp/jump2.i:
--------------------------------------------------------------------------------
1 | L1: r = true
2 | L3: r = false
3 | L4: iffalse a < b goto L6
4 | t1 = true
5 | goto L7
6 | L6: t1 = false
7 | L7: r = t1
8 | L5: iffalse x <= y goto L9
9 | t2 = true
10 | goto L10
11 | L9: t2 = false
12 | L10: r = t2
13 | L8: iffalse a == b goto L12
14 | t3 = true
15 | goto L13
16 | L12: t3 = false
17 | L13: r = t3
18 | L11: iffalse x != y goto L15
19 | t4 = true
20 | goto L16
21 | L15: t4 = false
22 | L16: r = t4
23 | L14: iffalse a >= b goto L18
24 | t5 = true
25 | goto L19
26 | L18: t5 = false
27 | L19: r = t5
28 | L17: iffalse x > y goto L21
29 | t6 = true
30 | goto L22
31 | L21: t6 = false
32 | L22: r = t6
33 | L20: if x < 100 goto L26
34 | iffalse x > 200 goto L24
35 | L26: t7 = true
36 | goto L25
37 | L24: t7 = false
38 | L25: r = t7
39 | L23: iffalse a < 100 goto L28
40 | iffalse a > 200 goto L28
41 | t8 = true
42 | goto L29
43 | L28: t8 = false
44 | L29: r = t8
45 | L27: if x < 100 goto L33
46 | iffalse x > 200 goto L31
47 | iffalse x != y goto L31
48 | L33: t9 = true
49 | goto L32
50 | L31: t9 = false
51 | L32: r = t9
52 | L30: if a < 100 goto L37
53 | iffalse a > 200 goto L38
54 | if a != 150 goto L37
55 | L38: iffalse a != 0 goto L35
56 | L37: t10 = true
57 | goto L36
58 | L35: t10 = false
59 | L36: r = t10
60 | L34: iffalse x > 200 goto L43
61 | if x != b goto L42
62 | L43: iffalse x < 100 goto L40
63 | L42: t11 = true
64 | goto L41
65 | L40: t11 = false
66 | L41: r = t11
67 | L39: if a < 100 goto L46
68 | iffalse a > 200 goto L44
69 | iffalse a != b goto L44
70 | L46: t12 = true
71 | goto L45
72 | L44: t12 = false
73 | L45: r = t12
74 | L2:
75 |
--------------------------------------------------------------------------------
/tmp/jump3.i:
--------------------------------------------------------------------------------
1 | L1: r = b
2 | L3: t1 = i * 1
3 | r = a [ t1 ]
4 | L4: t2 = i * 1
5 | a [ t2 ] = b
6 | L5: t3 = i * 1
7 | a [ t3 ] = true
8 | L6: t4 = i * 1
9 | a [ t4 ] = false
10 | L7: iffalse b goto L8
11 | L9: x = y
12 | L8: t5 = i * 1
13 | t6 = a [ t5 ]
14 | iffalse t6 goto L2
15 | L10: x = y
16 | L2:
17 |
--------------------------------------------------------------------------------
/tmp/prog0.i:
--------------------------------------------------------------------------------
1 | L1:L3: i = i + 1
2 | L5: t1 = i * 8
3 | t2 = a [ t1 ]
4 | if t2 < v goto L3
5 | L4: j = j - 1
6 | L7: t3 = j * 8
7 | t4 = a [ t3 ]
8 | if t4 > v goto L4
9 | L6: iffalse i >= j goto L8
10 | L9: goto L2
11 | L8: t5 = i * 8
12 | x = a [ t5 ]
13 | L10: t6 = i * 8
14 | t7 = j * 8
15 | t8 = a [ t7 ]
16 | a [ t6 ] = t8
17 | L11: t9 = j * 8
18 | a [ t9 ] = x
19 | goto L1
20 | L2:
21 |
--------------------------------------------------------------------------------
/tmp/prog1.i:
--------------------------------------------------------------------------------
1 | L1: r = a
2 | L3: dd = d
3 | L4: iffalse dd <= r goto L5
4 | L6: dd = 2 * dd
5 | goto L4
6 | L5: iffalse dd != r goto L2
7 | L7: dd = dd / 2
8 | L8: iffalse dd <= r goto L5
9 | L9: r = r - dd
10 | goto L5
11 | L2:
12 |
--------------------------------------------------------------------------------
/tmp/prog2.i:
--------------------------------------------------------------------------------
1 | L1: prod = 0
2 | L3: i = 1
3 | L4: t1 = i * 8
4 | t2 = a [ t1 ]
5 | t3 = i * 8
6 | t4 = b [ t3 ]
7 | t5 = t2 * t4
8 | prod = prod + t5
9 | L6: i = i + 1
10 | L5: if i <= 20 goto L4
11 | L2:
12 |
--------------------------------------------------------------------------------
/tmp/prog3.i:
--------------------------------------------------------------------------------
1 | L1: i = 0
2 | L3: iffalse i < 10 goto L4
3 | L5: j = 0
4 | L6: iffalse j < 10 goto L7
5 | L8: t1 = i * 80
6 | t2 = j * 8
7 | t3 = t1 + t2
8 | a [ t3 ] = 0
9 | L9: j = j + 1
10 | goto L6
11 | L7: i = i + 1
12 | goto L3
13 | L4: i = 0
14 | L10: iffalse i < 10 goto L2
15 | L11: t4 = i * 80
16 | t5 = i * 8
17 | t6 = t4 + t5
18 | a [ t6 ] = 1
19 | L12: i = i + 1
20 | goto L10
21 | L2:
22 |
--------------------------------------------------------------------------------
/tmp/prog4.i:
--------------------------------------------------------------------------------
1 | L1:L3: if peek == BLANK goto L7
2 | iffalse peek == TAB goto L6
3 | L7:L5: goto L4
4 | L6: iffalse peek == NEWLINE goto L9
5 | L8: line = line + 1
6 | goto L4
7 | L9: goto L2
8 | L4: peek = readch
9 | goto L1
10 | L2:
11 |
--------------------------------------------------------------------------------
/tmp/test.i:
--------------------------------------------------------------------------------
1 | L1: i = 0
2 | L3: i = 365
3 | L4: x = 0.0
4 | L5: x = 3.1415896
5 | L6: b = true
6 | L7: b = false
7 | L8: i = x
8 | L9: x = i
9 | L2:
10 |
--------------------------------------------------------------------------------