新添测试文件
This commit is contained in:
parent
d4cdeaacce
commit
41bf0e0ab7
7
main.py
7
main.py
@ -4,6 +4,7 @@
|
||||
from lexical.lexical import Lexical
|
||||
from syntax.syntax import Syntax
|
||||
from syntax.syntax import LL1Generator
|
||||
import sys
|
||||
|
||||
# print(PredictingAnalysisTable().compile())
|
||||
|
||||
@ -13,8 +14,12 @@ from syntax.syntax import LL1Generator
|
||||
|
||||
# 新建词法分析器
|
||||
lexical = Lexical()
|
||||
if len(sys.argv) < 2:
|
||||
print("请提供源代码文件名作为参数。")
|
||||
sys.exit(1)
|
||||
source_filename = sys.argv[1]
|
||||
# 载入源代码
|
||||
lexical.load_source(open('test9.c', encoding='utf-8', errors='ignore').read())
|
||||
lexical.load_source(open(source_filename, encoding='utf-8', errors='ignore').read())
|
||||
# 执行词法分析
|
||||
lexical_success = lexical.execute()
|
||||
# 打印结果
|
||||
|
108
semantic/rule.py
108
semantic/rule.py
@ -4,91 +4,6 @@ from semantic.code import get_temp_block_name, get_temp_var_name
|
||||
|
||||
|
||||
|
||||
"""
|
||||
添加语义规则的文法
|
||||
0. program{code} ->{.init} define-list
|
||||
1. define-list{code} -> define define-list
|
||||
{code} | empty
|
||||
2. define{code} -> type ID define-type{type id}
|
||||
3. define-type{code .enter} ->{.judge} var-define-follow{type id}
|
||||
{code} |{.judge} fun-define-follow{type fun}
|
||||
4. var-define-follow{type} -> ;
|
||||
{type length} | [ NUM ] ;
|
||||
5. type ->{type} int
|
||||
|{type} void
|
||||
6. fun-define-follow{code} -> ( params{type fun} ) code-block{fun}
|
||||
7. params{.create} -> param-list{fun}
|
||||
{.create} | empty
|
||||
8. param-list -> param{fun} param-follow{fun}
|
||||
9. param-follow -> , param{fun} param-follow{fun}
|
||||
| empty
|
||||
10. param -> type ID array-subscript
|
||||
11. array-subscript{type} -> [ ]
|
||||
{type} | empty
|
||||
12. code-block{code} -> { local-define-list{fun} code-list{fun} }
|
||||
13. local-define-list -> local-var-define{fun} local-define-list{fun}
|
||||
| empty
|
||||
14. local-var-define -> type ID var-define-follow
|
||||
15. code-list{code} -> code{fun} code-list{fun}
|
||||
| empty
|
||||
16. code{code} -> normal-statement{fun}
|
||||
| selection-statement{fun}
|
||||
| iteration-statement{fun}
|
||||
| return-statement{fun}
|
||||
17. normal-statement -> ;
|
||||
| ID normal-statement-follow{fun id}
|
||||
18. normal-statement-follow{code} -> var-follow{fun} = expression{fun} ;
|
||||
{code} | call-follow{fun id} ;
|
||||
19. call-follow{code} -> ( call-params{fun id} )
|
||||
20. call-params{code} -> call-param-list{fun}
|
||||
| empty
|
||||
21. call-param-list{num code} -> expression{fun} call-param-follow{fun}
|
||||
22. call-param-follow{num code names} -> , expression{fun} call-param-follow{fun}
|
||||
| empty
|
||||
23. selection-statement{code} -> if ( expression{fun} ) { code-list{fun} } selection-follow{fun}
|
||||
24. selection-follow{code} -> else { code-list{fun} }
|
||||
{code} | empty
|
||||
25. iteration-statement -> while ( expression{fun} ) iteration-follow{fun}
|
||||
26. iteration-follow{code} -> { code-list{fun} }
|
||||
{code} | code{fun}
|
||||
27. return-statement{code} -> return return-follow{fun}
|
||||
28. return-follow -> ;
|
||||
| expression{fun} ;
|
||||
29. var-follow{code name type} -> [ expression{fun} ]
|
||||
{type} | empty
|
||||
30. expression{code name bool} -> additive-expr{fun} expression-follow{fun}
|
||||
31. expression-follow{bool code op name} -> rel-op additive-expr{fun}
|
||||
{bool} | empty
|
||||
32. rel-op{op} -> <=
|
||||
| <
|
||||
| >
|
||||
| >=
|
||||
| ==
|
||||
| !=
|
||||
33. additive-expr{name code} -> term{fun} additive-expr-follow{fun}
|
||||
34. additive-expr-follow{add op code name} -> add-op term{fun} additive-expr-follow{fun}
|
||||
{add} | empty
|
||||
35. add-op{op} -> +
|
||||
| -
|
||||
36. term{name code} -> factor{fun} term-follow{fun}
|
||||
37. term-follow{mul op code name} -> mul-op factor{fun} term-follow{fun}
|
||||
{mul} | empty
|
||||
38. mul-op{op} -> *
|
||||
| /
|
||||
| %
|
||||
39. factor{name code} -> ( expression{fun} )
|
||||
| ID id-factor-follow{id fun}
|
||||
| NUM
|
||||
40. id-factor-follow -> var-follow{fun}
|
||||
| ( args{fun} )
|
||||
41. args{code num} -> arg-list{fun}
|
||||
| empty
|
||||
42. arg-list{code num} -> expression{fun} arg-list-follow{fun}
|
||||
43. arg-list-follow{code num names} -> , expression{fun} arg-list-follow{fun}
|
||||
| empty
|
||||
"""
|
||||
|
||||
|
||||
# 定义一个符号表池,每次调用函数的时候使用深拷贝从这里取局部变量表
|
||||
symbol_table_pool = SymbolTablePool()
|
||||
|
||||
@ -389,6 +304,8 @@ class SemanticRuleFactory:
|
||||
return IterationStatement48E(node)
|
||||
|
||||
# 49
|
||||
if rule_key == 'ReturnStatement49C1':
|
||||
return ReturnStatement49C1(node)
|
||||
if rule_key == 'ReturnStatement49E':
|
||||
return ReturnStatement49E(node)
|
||||
|
||||
@ -397,6 +314,8 @@ class SemanticRuleFactory:
|
||||
return ReturnFollow50E(node)
|
||||
|
||||
# 51
|
||||
if rule_key == 'ReturnFollow51C0':
|
||||
return ReturnFollow51C0(node)
|
||||
if rule_key == 'ReturnFollow51E':
|
||||
return ReturnFollow51E(node)
|
||||
|
||||
@ -1481,6 +1400,13 @@ class IterationStatement48E(SemanticRule):
|
||||
node.code.append(f"(goto, _, _, {judge_block})")
|
||||
node.code.append(f"(block, _, _, {next_block})")
|
||||
|
||||
class ReturnStatement49C1(SemanticRule):
|
||||
def execute(self):
|
||||
return self.__rule(self.node)
|
||||
|
||||
def __rule(self, node):
|
||||
node.fun = node.parent.fun
|
||||
|
||||
class ReturnStatement49E(SemanticRule):
|
||||
def execute(self):
|
||||
self.__rule(self.node)
|
||||
@ -1496,6 +1422,13 @@ class ReturnFollow50E(SemanticRule):
|
||||
def __rule(self, node):
|
||||
node.code.append("(return, _, _, _)")
|
||||
|
||||
class ReturnFollow51C0(SemanticRule):
|
||||
def execute(self):
|
||||
self.__rule(self.node)
|
||||
|
||||
def __rule(self, node):
|
||||
node.fun = node.parent.fun
|
||||
|
||||
|
||||
class ReturnFollow51E(SemanticRule):
|
||||
def execute(self):
|
||||
@ -1862,6 +1795,7 @@ class IdFactorFollow74E(SemanticRule):
|
||||
self.__rule(self.node)
|
||||
|
||||
def __rule(self, node):
|
||||
|
||||
if symbol_table_pool.query(node.fun).exist(node.id):
|
||||
if node.children[0].type == "var":
|
||||
node.name = node.id
|
||||
@ -1949,9 +1883,9 @@ class ArgList78E(SemanticRule):
|
||||
node.code.append(c)
|
||||
for c in node.children[1].code:
|
||||
node.code.append(c)
|
||||
node.code.append("(param, _, _, )" + node.children[0].name)
|
||||
node.code.append(f"(param, _, _, {node.children[0].name})")
|
||||
for name in node.children[1].names:
|
||||
node.code.append("(param, _, _, )" + name)
|
||||
node.code.append(f"(param, _, _, {name})")
|
||||
|
||||
class ArgListFollow79C1(SemanticRule):
|
||||
def execute(self):
|
||||
|
@ -552,7 +552,7 @@ productions = [
|
||||
),
|
||||
# 49
|
||||
Production('return-statement', ['return', 'return-follow'],
|
||||
None, [None, None], "ReturnStatement49E",
|
||||
None, [None, 'ReturnStatement49C1'], "ReturnStatement49E",
|
||||
),
|
||||
# 50
|
||||
Production('return-follow', ['semicolon'],
|
||||
@ -560,7 +560,7 @@ productions = [
|
||||
),
|
||||
# 51
|
||||
Production('return-follow', ['exp', 'semicolon'],
|
||||
None, [None, None], 'ReturnFollow51E',
|
||||
None, ['ReturnFollow51C0', None], 'ReturnFollow51E',
|
||||
),
|
||||
# 52
|
||||
Production('exp', ['additive-expr', 'exp-follow'],
|
||||
@ -648,7 +648,7 @@ productions = [
|
||||
None, [None, 'ArgListFollow79C1', 'ArgListFollow79C2'], 'ArgListFollow79E'),
|
||||
# 80
|
||||
Production('arg-list-follow', [],
|
||||
None, [], 'ArgListFollowE'),
|
||||
None, [], 'ArgListFollow80E'),
|
||||
|
||||
# 81
|
||||
Production('arraylist-with-num', [],
|
||||
|
18
test.c
18
test.c
@ -1,18 +0,0 @@
|
||||
/* A program to perform Euclid s Algorithm to compute gcd. */
|
||||
int gcd(int u, int v) {
|
||||
if (v == 0) {
|
||||
return u;
|
||||
} else {
|
||||
return gcd(v, u-u/v*v);
|
||||
}
|
||||
/* u-u/v*v* == u mod v */
|
||||
}
|
||||
|
||||
void main() {
|
||||
int x;
|
||||
int y;
|
||||
x = input();
|
||||
y = input();
|
||||
output(gcd(x, y));
|
||||
return;
|
||||
}
|
@ -1,17 +1,18 @@
|
||||
/*test1:斐波那契数列(控制流 + 函数调用)*/
|
||||
int fib(int n) {
|
||||
if (n < 1){
|
||||
fn fib(n:i32) -> i32 {
|
||||
if n < 1 {
|
||||
return n;
|
||||
} else {
|
||||
return fib(n - 1) + fib(n - 2);
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
int y;
|
||||
int x;
|
||||
fn main() {
|
||||
i32 x;
|
||||
i32 y;
|
||||
x = 5 * 2 + 2;
|
||||
a = 2 == 2;
|
||||
x = fib(6);
|
||||
return x;
|
||||
}
|
||||
|
14
test2.c
14
test2.c
@ -1,14 +0,0 @@
|
||||
/* test2:阶乘计算(while 循环 + 局部变量)*/
|
||||
int fact(int n) {
|
||||
int result;
|
||||
result = 1;
|
||||
while (n > 0) {
|
||||
result = result * n;
|
||||
n = n - 1;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
int main() {
|
||||
return fact(5);
|
||||
}
|
16
test2.hy
Normal file
16
test2.hy
Normal file
@ -0,0 +1,16 @@
|
||||
// test2:阶乘计算(while 循环 + 局部变量)
|
||||
|
||||
fn fact(n: i32) {
|
||||
i32 result;
|
||||
result = 1;
|
||||
while n > 0 {
|
||||
result = result * n;
|
||||
n = n - 1;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
fn main() {
|
||||
return fact(5);
|
||||
}
|
21
test3.c
21
test3.c
@ -1,21 +0,0 @@
|
||||
/* test3:多函数协作(函数调用 + 全局变量)*/
|
||||
int a;
|
||||
int b;
|
||||
|
||||
int sum() {
|
||||
return a + b;
|
||||
}
|
||||
|
||||
int product() {
|
||||
return a * b;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int s;
|
||||
int p;
|
||||
a = 3;
|
||||
b = 4;
|
||||
s = sum();
|
||||
p = product();
|
||||
return s + p;
|
||||
}
|
22
test3.hy
Normal file
22
test3.hy
Normal file
@ -0,0 +1,22 @@
|
||||
// test3:多函数协作(函数调用 + 全局变量)
|
||||
i32 a;
|
||||
i32 b;
|
||||
|
||||
fn sum() {
|
||||
return a + b;
|
||||
}
|
||||
|
||||
fn product() {
|
||||
return a * b;
|
||||
}
|
||||
|
||||
fn main() {
|
||||
i32 s;
|
||||
i32 p;
|
||||
a = 3;
|
||||
b = 4;
|
||||
// s = sum(a);
|
||||
s = sum();
|
||||
p = product();
|
||||
return s + p;
|
||||
}
|
19
test4.c
19
test4.c
@ -1,19 +0,0 @@
|
||||
/* test4:嵌套 if 判断(复杂控制流)*/
|
||||
int max(int x, int y) {
|
||||
if (x > y) {
|
||||
return x;
|
||||
}
|
||||
else {
|
||||
return y;
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
int a;
|
||||
int b;
|
||||
int c;
|
||||
a = 10;
|
||||
b = 20;
|
||||
c = max(a, b);
|
||||
return c;
|
||||
}
|
20
test4.hy
Normal file
20
test4.hy
Normal file
@ -0,0 +1,20 @@
|
||||
// test4:嵌套 if 判断(复杂控制流)
|
||||
|
||||
fn max(x: i32, y: i32) -> i32 {
|
||||
if x > y {
|
||||
return x;
|
||||
} else {
|
||||
return y;
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
i32 a;
|
||||
i32 b;
|
||||
i32 c;
|
||||
|
||||
a = 10;
|
||||
b = 20;
|
||||
c = max(a, b);
|
||||
return c;
|
||||
}
|
29
test5.c
29
test5.c
@ -1,29 +0,0 @@
|
||||
/* test5:冒泡排序(数组访问模拟)*/
|
||||
int swap(int i, int j) {
|
||||
int temp;
|
||||
temp = i;
|
||||
i = j;
|
||||
j = temp;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int a;
|
||||
int b;
|
||||
int c;
|
||||
int d;
|
||||
int e;
|
||||
a = 5;
|
||||
b = 3;
|
||||
c = 8;
|
||||
d = 1;
|
||||
e = 9;
|
||||
|
||||
/* 冒泡排序模拟 */
|
||||
if (a > b) { swap(a, b); }
|
||||
if (b > c) { swap(b, c); }
|
||||
if (c > d) { swap(c, d); }
|
||||
if (d > e) { swap(d, e); }
|
||||
|
||||
return e;
|
||||
}
|
20
test5.hy
Normal file
20
test5.hy
Normal file
@ -0,0 +1,20 @@
|
||||
// while + if
|
||||
fn is_prime(n:i32) -> i32 {
|
||||
i32 i;
|
||||
if n <= 1 {
|
||||
return 0;
|
||||
}
|
||||
|
||||
i = 2;
|
||||
while i * i <= n {
|
||||
if n % i == 0 {
|
||||
return 0;
|
||||
}
|
||||
i = i + 1;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
fn main() {
|
||||
return is_prime(17);
|
||||
}
|
13
test6.c
13
test6.c
@ -1,13 +0,0 @@
|
||||
/* test6:逻辑表达式优先级测试 */
|
||||
int main() {
|
||||
int a;
|
||||
int b;
|
||||
int c;
|
||||
a = 1;
|
||||
b = 2;
|
||||
c = 3;
|
||||
|
||||
int result;
|
||||
result = a == 1 && b < c || c != 4;
|
||||
return result;
|
||||
}
|
@ -1,18 +1,18 @@
|
||||
/* test8:带错误的语法测试(用于验证报错机制)*/
|
||||
int main() {
|
||||
int x;
|
||||
/* test6:带错误的语法测试(用于验证报错机制)*/
|
||||
fn main() {
|
||||
i32 x;
|
||||
x = ; /* 错误:缺少右值 */
|
||||
return x;
|
||||
}
|
||||
|
||||
int main1() {
|
||||
fn main1() {
|
||||
int x;
|
||||
x == 10; /* 错误:应为赋值 = */
|
||||
return x;
|
||||
}
|
||||
|
||||
int main2() {
|
||||
int x;
|
||||
fn main2() {
|
||||
i32 x;
|
||||
if (x) /* 错误:if 条件必须是整型表达式 */
|
||||
return 1;
|
||||
return 0;
|
19
test7.c
19
test7.c
@ -1,19 +0,0 @@
|
||||
/* test7:嵌套循环(while + if)*/
|
||||
int is_prime(int n) {
|
||||
int i;
|
||||
if (n <= 1) {
|
||||
return 0;
|
||||
}
|
||||
i = 2;
|
||||
while (i * i <= n) {
|
||||
if (n % i == 0) {
|
||||
return 0;
|
||||
}
|
||||
i = i + 1;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int main() {
|
||||
return is_prime(17);
|
||||
}
|
Loading…
Reference in New Issue
Block a user