1.1 测试表达式相关检查
1.1.1 测试1
- 测试用例:关系运算符类型检查
- program test(input,output);
2. const m = 100;
3. var a,b,c:integer;
4. d,e,f:real;
5. g,h,i:char;
6. j,k,l:boolean;
7.
8. function fun:real;
9. begin
10. fun:=1;
11. end;
12.
13. begin
14. j:=a>=b; //正确
15. j:=d>e; //正确
16. j:=g<=h; //正确
17. j:=j18. j:=a<>d; //正确,隐式类型转换
19. j:=a=m; //正确
20. j:=a=fun; //正确,隐式类型转换
21. j:=a=g; //错误,a和g类型不匹配
22. k:=a>=j; //错误,a和j类型不匹配
23. k:=d>g; //错误,d和g类型不匹配
24. k:=d<=j; //错误,d和j类型不匹配
25. k:=g26. end.
- 预期结果
报real、integer、char和boolean类型不匹配,integer、real与char类型不匹配
- 测试结果
- [Operands expression type mismatch!]
Left “a” type is integer while right “g” type is char.
2. [Assign statement type mismatch!]Left “j” type is boolean while right “a = g” type is error.
3. [Operands expression type mismatch!]Left “a” type is integer while right “j” type is boolean.
4. [Assign statement type mismatch!]Left “k” type is boolean while right “a >= j” type is error.
5. [Operands expression type mismatch!]Left “d” type is real while right “g” type is char.
6. [Assign statement type mismatch!]Left “k” type is boolean while right “d > g” type is error.
7. [Operands expression type mismatch!]Left “d” type is real while right “j” type is boolean.
8. [Assign statement type mismatch!]Left “k” type is boolean while right “d <= j” type is error.
9. [Operands expression type mismatch!]Left “g” type is char while right “j” type is boolean.
10. [Assign statement type mismatch!]Left “k” type is boolean while right “g < j” type is error.
- 分析
1.1.2 测试2
- 测试用例:not、and、or运算符
- program test(input,output);
2. var a,b,c:integer;
3. d,e,f:real;
4. g,h,i:char;
5. j,k,l:boolean;
6.
7. function fun1:integer;
8. begin
9. fun1:=1;
10. end;
11.
12. function fun2:boolean;
13. begin
14. fun2:=2>1;
15. end;
16.
17. begin
18. j:= not a; //a是integer而不是boolean
19. j:= not d; //d是real而不是boolean
20. j:= not g; //g是char而不是boolean
21. j:= not j; //正确
22. j:= not fun1; //fun1是integer而不是boolean
23. j:= not fun2; //正确
24. j:= j and k; //正确
25. j:= a and l; //a是integer而不是boolean
26. j:= g and h; //g是char而不是boolean,h是char而不是boolean
27. j:= j or k; //正确
28. j:= a or l; //a是integer而不是boolean
29. j:= g or h; //g是char而不是boolean,h是char而不是boolean
30. j:= ((a+b)>c) or k; //正确
31. end.
- 预期结果
报integer、char、real类型与bool不匹配的错误
- 测试结果
- [Operand expression type error!]
Expression “a” type should be boolean but not integer.
2. [Assign statement type mismatch!]Left “j” type is boolean while right “ not a” type is error.
3. [Operand expression type error!]Expression “d” type should be boolean but not real.
4. [Assign statement type mismatch!]Left “j” type is boolean while right “ not d” type is error.
5. [Operand expression type error!]Expression “g” type should be boolean but not char.
6. [Assign statement type mismatch!]Left “j” type is boolean while right “ not g” type is error.
7. [Operand expression type error!]Expression “fun1” type should be boolean but not integer.
8. [Assign statement type mismatch!]Left “j” type is boolean while right “ not fun1” type is error.
9. [Operand expression type error!]Expression “a” type should be boolean but not integer.
10. [Assign statement type mismatch!]Left “j” type is boolean while right “a and l” type is error.
11. [Operand expression type error!]Expression “g” type should be boolean but not char.
12. [Operand expression type error!]Expression “h” type should be boolean but not char.
13. [Assign statement type mismatch!]Left “j” type is boolean while right “g and h” type is error.
14. [Operand expression type error!]Expression “a” type should be boolean but not integer.
15. [Assign statement type mismatch!]Left “j” type is boolean while right “a or l” type is error.
16. [Operand expression type error!]Expression “g” type should be boolean but not char.
17. [Operand expression type error!]Expression “h” type should be boolean but not char.
18. [Assign statement type mismatch!]Left “j” type is boolean while right “g or h” type is error.
- program test(input,output);
2. var a,b,c:integer;
3. d,e,f:real;
4. g,h,i:char;
5. j,k,l:boolean;
6. begin
7. a:=b+c; //正确
8. d:=d+e; //正确
9. a:=b+f; //错误,左值为integer,右值为real
10. d:=e+c; //正确
11.
12. a:=b/c; //正确
13. d:=d/e; //正确
14. a:=b/f; //错误,左值为integer,右值为real
15. d:=e/c; //正确
16.
17. a:=g-h; //错误,g和h均为char
18. d:=j-k; //错误,j和k均为boolean
19. a:=b-g; //错误,g为char
20. d:=c-j; //错误,j为boolean
21. a:=d-g; //错误,a为int,d为real,g为char
22. d:=d-l; //错误,l为boolean
23. a:=g-k; //错误,a为integer,g为char,k为boolean
24.
25. a:=gh; //错误,g和h均为char
26. d:=jk; //错误,j和k均为boolean
27. a:=bg; //错误,g为char
28. d:=cj; //错误,j为boolean
29. a:=dg; //错误,a为int,d为real,g为char
30. d:=dl; //错误,l为boolean
31. a:=gk; //错误,a为integer,g为char,k为boolean
32.
33. a:=-b; //正确
34. d:=-b; //正确
35. a:=-d; //错误,a为integer,d为real
36. d:=-d; //正确
37. a:=-g; //错误,a为integer,g为char
38. d:=-g; //错误,d为real,g为char
39. a:=-j; //错误,a为integer,j为boolean
40. d:=-j; //错误,d为real,j为boolean
41. *end.
- 预期结果
报左边integer与右边real类型不匹配、integer与boolean类型不匹配等左右表达式类型不匹配的错误
- 测试结果
- [Assign statement type mismatch!]
Left “a” type is integer while right “b + f” type is real.
2. [Assign statement type mismatch!]Left “a” type is integer while right “b / f” type is real.
3. [Operand expression type error!]Expression “g” type should be integer or real but not char.
4. [Operand expression type error!]Expression “h” type should be integer or real but not char.
5. [Assign statement type mismatch!]Left “a” type is integer while right “g - h” type is error.
6. [Operand expression type error!]Expression “j” type should be integer or real but not boolean.
7. [Operand expression type error!]Expression “k” type should be integer or real but not boolean.
8. [Assign statement type mismatch!]Left “d” type is real while right “j - k” type is error.
9. [Operand expression type error!]Expression “g” type should be integer or real but not char.
10. [Assign statement type mismatch!]Left “a” type is integer while right “b - g” type is error.
11. [Operand expression type error!]Expression “j” type should be integer or real but not boolean.
12. [Assign statement type mismatch!]Left “d” type is real while right “c - j” type is error.
13. [Operand expression type error!]Expression “g” type should be integer or real but not char.
14. [Assign statement type mismatch!]Left “a” type is integer while right “d - g” type is error.
15. [Operand expression type error!]Expression “l” type should be integer or real but not boolean.
16. [Assign statement type mismatch!]Left “d” type is real while right “d - l” type is error.
17. [Operand expression type error!]Expression “g” type should be integer or real but not char.
18. [Operand expression type error!]Expression “k” type should be integer or real but not boolean.
19. [Assign statement type mismatch!]Left “a” type is integer while right “g - k” type is error.
20. [Operand expression type error!]Expression “g” type should be integer or real but not char.
21. [Operand expression type error!]Expression “h” type should be integer or real but not char.
22. [Assign statement type mismatch!]Left “a” type is integer while right “g h” type is error.
23. [Operand expression type error!]Expression “j” type should be integer or real but not boolean. k” type is error.
24. [Operand expression type error!]Expression “k” type should be integer or real but not boolean.
25. [Assign statement type mismatch!]Left “d” type is real while right “j
26. [Operand expression type error!]Expression “g” type should be integer or real but not char.
27. [Assign statement type mismatch!]Left “a” type is integer while right “b g” type is error.
28. [Operand expression type error!]Expression “j” type should be integer or real but not boolean. j” type is error.
29. [Assign statement type mismatch!]Left “d” type is real while right “c
30. [Operand expression type error!]Expression “g” type should be integer or real but not char.
31. [Assign statement type mismatch!]Left “a” type is integer while right “d g” type is error.
32. [Operand expression type error!]Expression “l” type should be integer or real but not boolean. l” type is error.
33. [Assign statement type mismatch!]Left “d” type is real while right “d
34. [Operand expression type error!]Expression “g” type should be integer or real but not char.
35. [Operand expression type error!]Expression “k” type should be integer or real but not boolean.
36. [Assign statement type mismatch!]Left “a” type is integer while right “g k” type is error.
37. [Assign statement type mismatch!]Left “a” type is integer while right “ - d” type is real. error.
38. [Operand expression type error!]Expression “g” type should be integer or real but not char.
39. [Assign statement type mismatch!]Left “a” type is integer while right “ - g” type is error.
40. [Operand expression type error!]Expression “g” type should be integer or real but not char.
41. [Assign statement type mismatch!]Left “d” type is real while right “ - g” type is error.
42. [Operand expression type error!]Expression “j” type should be integer or real but not boolean.
43. [Assign statement type mismatch!]Left “a” type is integer while right “ - j” type is error.
44. [Operand expression type error!]Expression “j” type should be integer or real but not boolean.
45. [Assign statement type mismatch!]Left “d” type is real while right “ - j” type *is
- 分析
+-*/支持integer到real的隐式转换,不支持real到integer的隐式转换等其他类型之间的隐式转换
1.1.4 测试4
- 测试用例:div、mod
- program test(input,output);
2. var a,b,c:integer;
3. d,e,f:real;
4. g,h,i:char;
5. j,k,l:boolean;
6. begin
7. a:= a div b; //正确
8. a:= a div d; //d为real
9. a:= a div g; //g为char
10. a:= a div j; //j为boolean
11. a:= d div g; //d为real,g为char
12. a:= d div j; //d为real,j为boolean
13. a:= g div j; //g为char,j为boolean
14.
15. a:= a mod b; //正确
16. a:= a mod d; //d为real
17. a:= a mod g; //g为char
18. a:= a mod j; //j为boolean
19. a:= d mod g; //d为real,g为char
20. a:= d mod j; //d为real,j为boolean
21. a:= g mod j; //g为char,j为boolean
22. end.
- 预期结果
报告运算数类型不为integer而是real、char、boolean等类型的错误
- 测试结果
- [Operand expression type error!]
Expression “d” type should be integer but not real.
2. [Assign statement type mismatch!]Left “a” type is integer while right “a div d” type is error.
3. [Operand expression type error!]Expression “g” type should be integer but not char.
4. [Assign statement type mismatch!]Left “a” type is integer while right “a div g” type is error.
5. [Operand expression type error!]Expression “j” type should be integer but not boolean.
6. [Assign statement type mismatch!]Left “a” type is integer while right “a div j” type is error.
7. [Operand expression type error!]Expression “d” type should be integer but not real.
8. [Operand expression type error!]Expression “g” type should be integer but not char.
9. [Assign statement type mismatch!]Left “a” type is integer while right “d div g” type is error.
10. [Operand expression type error!]Expression “d” type should be integer but not real.
11. [Operand expression type error!]Expression “j” type should be integer but not boolean.
12. [Assign statement type mismatch!]Left “a” type is integer while right “d div j” type is error.
13. [Operand expression type error!]Expression “g” type should be integer but not char.
14. [Operand expression type error!]Expression “j” type should be integer but not boolean.
15. [Assign statement type mismatch!]Left “a” type is integer while right “g div j” type is error.
16. [Operand expression type error!]Expression “d” type should be integer but not real.
17. [Assign statement type mismatch!]Left “a” type is integer while right “a mod d” type is error.
18. [Operand expression type error!]Expression “g” type should be integer but not char.
19. [Assign statement type mismatch!]Left “a” type is integer while right “a mod g” type is error.
20. [Operand expression type error!]Expression “j” type should be integer but not boolean.
21. [Assign statement type mismatch!]Left “a” type is integer while right “a mod j” type is error.
22. [Operand expression type error!]Expression “d” type should be integer but not real.
23. [Operand expression type error!]Expression “g” type should be integer but not char.
24. [Assign statement type mismatch!]Left “a” type is integer while right “d mod g” type is error.
25. [Operand expression type error!]Expression “d” type should be integer but not real.
26. [Operand expression type error!]Expression “j” type should be integer but not boolean.
27. [Assign statement type mismatch!]Left “a” type is integer while right “d mod j” type is error.
28. [Operand expression type error!]Expression “g” type should be integer but not char.
29. [Operand expression type error!]Expression “j” type should be integer but not boolean.
30. [Assign statement type mismatch!]Left “a” type is integer while right “g mod j” type is error.
- 分析
div、mod要求两个表达式类型均为integer,其他类型不支持
1.1.5 测试5
- 测试用例:/、div、mod的除0错误,常数表达式的计算
- program test(input,output);
2. var a,b,c:integer;
3. d,e,f:real;
4. begin
5. a:=a / (2+3-5); //除0错误
6. a:=a div ( 6 mod 3); //除0错误
7. a:=a mod ( 1 div 3); //除0错误
8. end.
- 预期结果
报除0错误
- 测试结果
- [Divide zero error!]
The value of expression “(2 + 3 - 5)” is 0, which is the second operand of operation “/“.
2. [Divide zero error!]The value of expression “(6 mod 3)” is 0, which is the second operand of operation “div“.
3. [Divide zero error!]The value of expression “(1 div 3)” is 0, which is the second operand of operation “mod“.
- 分析
1.1.6 测试6
- 测试用例
- program test(input,ouput);
2. var a,b,c:integer;
3. d,e,f:real;
4. g,h,i:char;
5. j,k,l:boolean;
6. m:array[1..5] of integer;
7.
8. procedure pro;
9. begin
10.
11. end;
12.
13. function fun:integer;
14. begin
15.
16. end;
17.
18. begin
19. a:=a+test; //test是主程序名
20. a:=a-input; //input是主程序参数
21. a:=a+pro; //pro是子过程名
22. a:=a-fun; //正确
23. a:=a+m; //m是数组名
24. end.
- 预期结果
报主程序名、主程序参数、过程名、数组不能被引用,以及语句左右类型不匹配的错误
- 测试结果
- [Invalid reference]
Procedure name “test” can’t be referenced
2. [Assign statement type mismatch!]Left “a” type is integer while right “a + test” type is error.
3. [Invalid reference!]“input” is a parameter of program, it can’t be referenced.
4. [Assign statement type mismatch!]Left “a” type is integer while right “a - input” type is error.
5. [Invalid reference!]“pro” is a procedure, it can’t be referenced.
6. [Assign statement type mismatch!]Left “a” type is integer while right “a + pro” type is error.
7. [Invalid reference!]“m” is a array, it can’t be referenced.
8. [Assign statement type mismatch!]Left “a” type is integer while right “a + m” type is error.
- 分析
1.2 测试库程序、主程序名、主程序参数相关检查
库程序有四个,为read,write,writeln,exit,均为过程
主程序名不能和库程序同名
主程序参数不能和库程序、主程序名同名
其它所有标识符不能和这三者同名
1.2.1 测试1
- 测试用例
- program read(input,output); //主程序名与库程序同名
2. begin
3.
4. end.
- 预期结果
报read已经被定义为库函数的错误
- 测试结果
- [Duplicate defined error!]
Name of program “read” has been defined as a lib program.
- 分析
1.2.2 测试2
- 测试用例
- program test(write,output); //主程序参数与库程序同名
2. begin
3.
4. end.
- 预期结果
报主程序参数write已被定义为库函数的错误
- 测试结果
- [Dulicate defined error!]
parameter of program “write“ has been defined as a lib program.
- 分析
1.2.3 测试3
- 测试用例
- program test(test,output); //主程序参数与主程序名同名
2. begin
3.
4. end.
- 预期结果
报主程序参数test与主程序名相同的错误
- 测试结果
- [Duplicate defined error!]
parameter of program “test” is the same as name of program.
- 分析
1.2.4 测试4
- 测试用例
- program test(input,output);
2. const output=5; //常量与主程序参数同名
3. var read:integer; //变量与库程序同名
4. test:char; //变量与主程序名同名
5. input:real; //变量与主程序参数同名
6. write:array[1..5] of integer; //数组名与库程序名同名
7.
8. function writeln:integer; //函数名与库程序同名
9. begin
10.
11. end;
12.
13. procedure exit; //过程名与库程序同名
14. begin
15.
16. end;
17.
18. function test:integer; //函数名与主程序名同名
19. begin
20.
21. end;
22.
23. procedure test; //过程名与主程序名同名
24. begin
25.
26. end;
27.
28. function input:integer; //函数名与主程序参数同名
29. begin
30.
31. end;
32.
33. procedure output; //过程名与主程序参数同名
34. begin
35.
36. end;
37. procedure pro(var test:integer;write:real;input:char);//参数与主程序名、库程序名、主程序参数名同名
38. const writeln=5; //常量与库程序同名
39. var output:integer; //变量与主程序参数同名
40. begin
41.
42. end;
43.
44. begin
45.
46. end.
- 预期结果
报常量、变量、参数等与库函数、主程序名、主程序参数相等的错误
- 测试结果
- [Duplicate defined error!]
“output” has been defined as a program parameter at Line 1.
2. [Duplicate defined error!]“read” has been defined as a lib program.
3. [Duplicate defined error!]“test” has been defined as the name of program at Line 1.
4. [Duplicate defined error!]“input” has been defined as a program parameter at Line 1.
5. [Duplicate defined error!]“write“ has been defined as a lib program.
6. [Duplicate defined error!]“writeln“ has already been defined as a lib program.
7. [Duplicate defined error!]“exit” has already been defined as a lib program.
8. [Duplicate defined error!]“test” has already been defined as a (sub)program name at line 1.
9. [Duplicate defined error!]“test” has already been defined as a (sub)program name at line 1.
10. [Duplicate defined error!]“input” has already been defined as a parameter of program at line 1.
11. [Duplicate defined error!]“output” has already been defined as a parameter of program at line 1.
12. [Duplicate defined error!]“test” has been defined as the name of program at Line 1.
13. [Duplicate defined error!]“write“ has been defined as a lib program.
14. [Duplicate defined error!]“input” has been defined as a program parameter at Line 1.
15. [Duplicate defined error!]“writeln“ has been defined as a lib program.
16. [Duplicate defined error!]“output” has been defined as a program parameter at Line 1.
- 分析
变量、常量、参数、数组等不能与库函数、主程序和主程序参数重名。测试结果符合预期,语义分析程序继续运行。
1.3 综合测试
- 测试用例
- program read(write,output);//主程序名、主程序参数和库程序同名
2. const m=10;
3. exit=m;//exit是库函数
4. var a,x,y:integer;
5. b:real;
6. c:char;
7. d:boolean;
8. e:array[1..5] of integer;
9. function fun1:integer;//报函数没有返回语句的警告
10. begin
11. v:=a;//v未定义
12. end;
13.
14. function fun2:integer;
15. begin
16. fun2:=1;
17. fun2:=e[6];//数组下标越界
18. fun2;//函数不能作为一条单独的语句
19. a:=fun2[1];//错把函数名当做数组
20. end;
21.
22. procedure pro1(a:integer;b:real);
23. var c:real;
24. d:integer;
25. begin
26. c:=a+b;//integer可以隐式转换为real
27. d:=a+b;//real不能隐式转换为integer
28. end;
29.
30. procedure pro2(var a:real;b:integer);
31. begin
32. exit(a+b);//过程没有返回值
33. end;
34.
35. begin
36. a:=1;
37. m:=a;//常量赋值语句右值不为常量
38. b:=2;
39. c:=3;//赋值语句左右类型不匹配
40. d:=a>b;//d为false
41. if a then b:=b+1;//if条件表达式不为boolean
42. repeat a:=a+c until not d;//a:=a+c语句左右类型不匹配
43. for b:=10 to 1 do e(a,a);//循环变量不可以是real;错把数组名当做函数
44. while a<10 **do** a:=a+1;
45. x:=pro1(x,y);//pro1为过程,没有返回值
46. x:=1;
47. y:=2;
48. pro1(x,y);//传值参数支持integer到real的隐式转换
49. pro2(x,y);//pro2的第一个参数为传引用,integer无法隐式转换为real
50. pro1(x+y);//pro1有两个参数
51. pro1(x+y,x+y);//传值参数支持复合表达式
52. pro2(x+y);//pro2有两个参数
53. pro2(x+y,x+y);//pro2第一个参数为引用参数,只能是变量或者数组元素,不能是复杂表达式
54. end.
- 预期结果
- 第1行报主程序名、主程序参数与库程序名重名的错误;
2. 第3行报exit是库程序,不能被定义为常量的错误;
3. 第9行报fun1子函数没有返回值的警告;
4. 第11行报变量v未定义错误;
5. 第17行报数组下标越界错误;
6. 第18行报fun2为函数而不是过程,不能作为一条单独语句的错误;
7. 第19行报fun2为函数而非数组的错误;
8. 第27行报d为integer而a+b为real,类型不匹配的错误;
9. 第32行报pro2为过程,没有返回值的错误;
10. 第37行报常量m不能作为左值被引用的错误;
11. 第39行报赋值语句左右类型不匹配的错误;
12. 第41行报if条件语句类型不为boolean的错误;
13. 第42行报c的类型不为integer且a:=a+c语句左右类型不匹配的错误;
14. 第43行报for语句循环变量类型错误;
15. 第45行报pro1为过程而非函数,没有返回值的错误;
16. 第49行报x为integer而第一个形参为real,类型不匹配的错误(传引用传输不支持integer到real的隐式转换);
17. 第50行报pro1应有两个参数而实参只有一个的错误;
18. 第52行报pro2应有两个参数而实参只有一个的错误;
19. 第53行报pro2的第一个参数只能是变量、数组元素而不能是复杂表达式的错误(传引用参数不支持复杂表达式)
- 测试结果
- **Here are the semantic warnings**
2. [Return value statement missing!]Incomplete return value statement of function “fun1”.
3. **Please pay attention to these semantic warnings**
4.
5. **Here are the semantic errors**
6. [Duplicate defined error!]Name of program “read” has been defined as a lib program.
7. [Dulicate defined error!]parameter of program “write” has been defined as a lib program.
8. [Duplicate defined error!]“exit” has been defined as a lib program.
9. [Undefined identifier!]v has not been defined.
10. [Assign statement type mismatch!]Left “v” type is error while right “a” type is integer.
11. [Array range out of bound!]The value of expression “6” is 6, but the range of array “e” 0th index is 1 to 5.
12. [Symbol kinds mismatch!]“fun2” defined at line 14 is a function but not a procedure.
13. [Symbol kinds mismatch!]“fun2” defined at line 14 is a (sub)program name but not a array.
14. [Assign statement type mismatch!]Left “a” type is integer while right “fun2” type is error.
15. [Assign statement type mismatch!]Left “d” type is integer while right “a + b” type is real.
16. [Return value redundancy!]Number of return value of procedure must be 0, that is, exit must have no actual parameters.
17. [Constant as l-value error!]Costant “m” can’t be referenced as l-value.
18. [Assign statement type mismatch!]Left “c” type is char while right “3” type is integer.
19. [Expression type error!]Expression “a” used for condition of if statement should be boolean but not integer.
20. [Operand expression type error!]Expression “c” type should be integer or real but not char.
21. [Assign statement type mismatch!]Left “a” type is integer while right “a + c” type is error.
22. [Usage type error!]“b” used for cyclic variable of for statement should be integer but not real.
23. [Symbol kinds mismatch!]“pro1” defined at line 22 is a procedure but not a function.
24. [Assign statement type mismatch!]Left “x” type is integer while right “pro1(x, y)” type is error.
25. [Expression type error!]Expression “x” used for 1th actual parameter of procedure call of “pro2” should be real but not integer.
26. [Procedure parameter number mismatch!]Procedure “pro1” should have 2 but not 1 parameters.
27. [Procedure parameter number mismatch!]Procedure “pro2” should have 2 but not 1 parameters.
28. [Referenced actual parameter error!]The 1th actual parameter expression should be a normal variable、value parameter、referenced parameter or array element.
- 分析
测试结果符合预期,语义分析程序继续运行。