1.1测试非数组变量引用

1.1.1 变量未定义

  • 测试样例
  1. program test(input,output);
    2. begin
    3. a:=a+1;//变量未定义
    4. end.
  • 预期结果

报变量未定义的错误

  • 测试结果
  1. [Undefined identifier!] a has not been defined.
    2. [Undefined identifier!] a has not been defined.
  • 分析

报错结果符合预期,语义分析程序继续运行

1.1.2 变量引用过程名错误

1.1.2.1 测试1

  • 测试样例
  1. program test(input,output);
    2. var a:integer;
    3. begin
    4. test:=a; //错误地引用了主程序名
    5. a:=test; //错误地引用了主程序名
    6. end.
  • 预期结果

报过程名test不能被引用和左右类型不匹配的错误

  • 测试结果
  1. [Invalid reference] Procedure name “test” can’t be referenced
    2. [Assign statement type mismatch!] Left “test” type is error while right “a” type is integer.
    3. [Invalid reference] Procedure name “test” can’t be referenced
    4. [Assign statement type mismatch!] Left “a” type is integer while right “test” type is error.
  • 分析

由于过程名不能被变量引用,因此过程的type为error。报错结果符合预期,语义分析程序继续运行

1.1.2.2 测试2

  • 测试样例
  1. program test(input,output);
    2. var a:integer;
    3. procedure pro;
    4. begin
    5. pro:=a; //在子过程的定义中错误地引用了该子过程名
    6. a:=pro; //在子过程的定义中错误地引用了该子过程名
    7. end;
    8. begin
    9. pro;
    10. end.
  • 预期结果

报过程名pro不能被引用和左右类型不匹配的错误

  • 测试结果
  1. [Invalid reference] Procedure name “pro” can’t be referenced
    2. [Assign statement type mismatch!] Left “pro” type is error while right “a” type is integer.
    3. [Invalid reference] Procedure name “pro” can’t be referenced
    4. [Assign statement type mismatch!] Left “a” type is integer while right “pro” type is error.
  • 分析

由于过程名不能被变量引用,因此过程的type为error。报错结果符合预期,语义分析程序继续运行

1.1.2.3 测试3

  • 测试样例
  1. program test(input,output);
    2. var a:integer;
    3. procedure pro;
    4. begin
    5. a:=1;
    6. end;
    7. begin
    8. a:=pro;//在子过程外错误地引用了子过程名
    9. writeln(a);
    10. end.
  • 预期结果

报过程名pro不能被引用和左右类型不匹配的错误

  • 测试结果
  1. [Invalid reference!] “pro” is a procedure, it can’t be referenced.
    2. [Assign statement type mismatch!] Left “a” type is integer while right “pro” type is error.
  • 分析

由于过程名不能被变量引用,因此过程的type为error。报错结果符合预期,语义分析程序继续运行

1.1.3 变量引用函数名,形式参数个数不为0

1.1.3.1 测试1

  • 测试用例
  1. program test(input,output);//斐波那契数列,递归调用,正确的程序
    2. var a:integer;
    3. function fib(i:integer):integer;
    4. begin
    5. if i=0 then
    6. fib:=1
    7. else
    8. begin
    9. if i=1 then
    10. fib:=1
    11. else
    12. fib:=fib(i-1)+fib(i-2);
    13. end;
    14. end;
    15. begin
    16. writeln(fib(5));
    17. end.
  • 预期结果

该测试样例没有语义错误,不报错

  • 测试结果

语义分析程序没有报错

  • 分析

报错结果符合预期,语义分析程序继续运行

1.1.3.2 测试2

  • 测试用例
  1. program test(input,output);//斐波那契数列,递归调用,错误的程序
    2. var a:integer;
    3. function fib(i:integer):integer;
    4. begin
    5. if i=0 then
    6. fib:=1
    7. else
    8. begin
    9. if i=1 then
    10. fib:=1
    11. else
    12. fib:=fib(i-1)+fib;//缺少实参
    13. end;
    14. end;
    15. begin
    16. writeln(fib(5));
    17. end.
  • 预期结果

报第12行函数调用缺少实参的错

  • 测试结果
  1. [Function parameter number mismatch!] Function “fib” should have 1 but not 0 parameters.
    2. please correct your semantic error
  • 分析

报错结果符合预期,语义分析程序继续运行

1.1.3.3 测试3

  • 测试用例
  1. program test(input,output); //在子函数外出现对函数名的左值引用
    2. var a:integer;
    3. function fib(i:integer):integer;
    4. begin
    5. if i=0 then
    6. fib:=1
    7. else
    8. begin
    9. if i=1 then
    10. fib:=1
    11. else
    12. fib:=fib(i-1)+fib(i-2);
    13. end;
    14. end;
    15. begin
    16. fib:=a; //在子函数外出现对函数名的左值引用
    17. end.
  • 预期结果

报子函数名不能作为左值引用和左右类型不匹配的错误

  • 测试结果
  1. [Invalid reference!] function name “fib” can’t be referenced as l-value.
    2. [Assign statement type mismatch!] Left “fib” type is error while right “a” type is integer.
  • 分析

报错结果符合预期,语义分析程序继续运行

1.1.3.4 测试4

  • 测试用例
  1. program test(input,output); //函数调用缺少实参(非递归)
    2. var a:integer;
    3. d:char;
    4. function fun(b:integer;c:boolean):char;
    5. begin
    6. if c then
    7. begin
    8. writeln(b);
    9. fun:=’y’
    10. end
    11. else
    12. begin
    13. fun:=’n’;
    14. end;
    15. end;
    16. begin
    17. d:=fun; //函数调用缺少实参(非递归)
    18. end.
  • 预期结果

报函数调用缺少实参的错误

  • 测试结果
  1. [Function parameter number mismatch!] Function “fun” should have 2 but not 0 parameters.
    2. please correct your semantic error
  • 分析

报错结果符合预期,语义分析程序继续运行

1.1.3.5 测试5

  • 测试用例
  1. program test(input,output); //正确的函数调用
    2. var a:integer;
    3. d:char;
    4. e:boolean;
    5. function fun(b:integer;c:boolean):char;
    6. begin
    7. if c then
    8. begin
    9. writeln(b);
    10. fun:=’y’
    11. end
    12. else
    13. begin
    14. fun:=’n’;
    15. end;
    16. end;
    17. begin
    18. read(a);
    19. e:=a>1;
    20. d:=fun(a,e);
    21. writeln(d);
    22. end.
  • 预期结果

该测试用例正确,不报错

  • 测试结果

语音分析程序没有报错

  • 分析

报错结果符合预期,语义分析程序继续运行

1.1.4 无法被引用的标识符被用作变量引用

1.1.4.1 测试1

  • 测试样例
  1. program test(input,output);//左值引用主程序参数
    2. begin
    3. input:=1;
    4. end.
  • 预期结果

报参数无法被引用和左右类型不匹配的错误

  • 测试结果
  1. [Invalid reference!] “input” is a parameter of program, it can’t be referenced.
    2. [Assign statement type mismatch!] Left “input” type is error while right “1” type is integer.
  • 分析

报错结果符合预期,语义分析程序继续运行

1.1.4.2 测试2

  • 测试样例
  1. program test(input,output);//右值引用主程序参数
    2. begin
    3. input:=output;
    4. end.
  • 预期结果

报参数无法被引用的错误

  • 测试结果
  1. [Invalid reference!] “input” is a parameter of program, it can’t be referenced.
    2. [Invalid reference!] “output” is a parameter of program, it can’t be referenced.
  • 分析

报错结果符合预期,语义分析程序继续运行

1.1.4.3 测试3

  • 测试样例
  1. program test(input,output);
    2. var a:array[1..6] of integer;
    3. b:integer;
    4. begin
    5. a:=1;//左值不带下标引用数组
    6. b:=a;//右值不带下标引用数组
    7. end.
  • 预期结果

报数组名无法被引用和左右类型不匹配的错误

  • 测试结果
  1. [Invalid reference!] “a” is a array, it can’t be referenced.
    2. [Assign statement type mismatch!] Left “a” type is error while right “1” type is integer.
    3. [Invalid reference!] “a” is a array, it can’t be referenced.
    4. [Assign statement type mismatch!] Left “b” type is integer while right “a” type is error.
  • 分析

报错结果符合预期,语义分析程序继续运行

1.2测试数组变量引用

1.2.1 符号表中记录的不是数组

  • 测试样例
  1. program test(input,output);
    2. var a:integer;
    3. b:array[0..5] of integer;
    4. function fun:integer;
    5. begin
    6. fun:=1;
    7. a:=fun[1]; //错把函数名当数组
    8. fun[1]:=a; //错把函数名当数组
    9. end;
    10. procedure pro;
    11. begin
    12. a:=pro[1]; //错把过程名当数组
    13. pro[1]:=a; //错把过程名当数组
    14. writeln(‘y’);
    15. end;
    16. procedure pro2(var d:integer;e:char);
    17. begin
    18. a:=d[1]; //错把引用参数当数组
    19. a:=e[1]; //错把传值参数当数组
    20. a:=pro2[1]; //错把过程名当数组
    21. d[1]:=a; //错把引用参数当数组
    22. e[1]:=a; //错把传值参数当数组
    23. pro2[1]:=a; //错把过程名当数组
    24. end;
    25. begin
    26. a:=test[1]; //错把主程序名当数组
    27. a:=input[1]; //错把主程序参数当数组
    28. a:=fun[1]; //错把函数名当数组
    29. a:=pro[1]; //错把过程名当数组
    30. a:=b[1]; //正确的数组引用
    31. test[1]:=a; //错把主程序名当数组
    32. output[1]:=a; //错把主程序参数当数组
    33. fun[1]:=a; //错把函数名当数组
    34. pro[1]:=a; //错把过程名当数组
    35. b[1]:=a; //正确的数组引用
    36. end.
  • 预期结果

报把函数、过程、非数组的函数参数当成数组的错误,以及左右类型不匹配的错误

  • 测试结果
  1. [Symbol kinds mismatch!] “fun” defined at line 4 is a (sub)program name but not a array.
    2. [Assign statement type mismatch!] Left “a” type is integer while right “fun” type is error.
    3. [Symbol kinds mismatch!] “fun” defined at line 4 is a (sub)program name but not a array.
    4. [Assign statement type mismatch!] Left “fun” type is error while right “a” type is integer.
    5. [Symbol kinds mismatch!] “pro” defined at line 10 is a (sub)program name but not a array.
    6. [Assign statement type mismatch!] Left “a” type is integer while right “pro” type is error.
    7. [Symbol kinds mismatch!] “pro” defined at line 10 is a (sub)program name but not a array.
    8. [Assign statement type mismatch!] Left “pro” type is error while right “a” type is integer.
    9. [Symbol kinds mismatch!] “d” defined at line 16 is a var parameter but not a array.
    10. [Assign statement type mismatch!] Left “a” type is integer while right “d” type is error.
    11. [Symbol kinds mismatch!] “e” defined at line 16 is a value parameter but not a array.
    12. [Assign statement type mismatch!] Left “a” type is integer while right “e” type is error.
    13. [Symbol kinds mismatch!] “pro2” defined at line 16 is a (sub)program name but not a array.
    14. [Assign statement type mismatch!] Left “a” type is integer while right “pro2” type is error.
    15. [Symbol kinds mismatch!] “d” defined at line 16 is a var parameter but not a array.
    16. [Assign statement type mismatch!] Left “d” type is error while right “a” type is integer.
    17. [Symbol kinds mismatch!] “e” defined at line 16 is a value parameter but not a array.
    18. [Assign statement type mismatch!] Left “e” type is error while right “a” type is integer.
    19. [Symbol kinds mismatch!] “pro2” defined at line 16 is a (sub)program name but not a array.
    20. [Assign statement type mismatch!] Left “pro2” type is error while right “a” type is integer.
    21. [Symbol kinds mismatch!] “test” defined at line 1 is a (sub)program name but not a array.
    22. [Assign statement type mismatch!] Left “a” type is integer while right “test” type is error.
    23. [Symbol kinds mismatch!] “input” defined at line 1 is a parameter of program but not a array.
    24. [Assign statement type mismatch!] Left “a” type is integer while right “input” type is error.
    25. [Symbol kinds mismatch!] “fun” defined at line 4 is a function but not a array.
    26. [Assign statement type mismatch!] Left “a” type is integer while right “fun” type is error.
    27. [Symbol kinds mismatch!] “pro” defined at line 10 is a procedure but not a array.
    28. [Assign statement type mismatch!] Left “a” type is integer while right “pro” type is error.
    29. [Symbol kinds mismatch!] “test” defined at line 1 is a (sub)program name but not a array.
    30. [Assign statement type mismatch!] Left “test” type is error while right “a” type is integer.
    31. [Symbol kinds mismatch!] “output” defined at line 1 is a parameter of program but not a array.
    32. [Assign statement type mismatch!] Left “output” type is error while right “a” type is integer.
    33. [Symbol kinds mismatch!] “fun” defined at line 4 is a function but not a array.
    34. [Assign statement type mismatch!] Left “fun” type is error while right “a” type is integer.
    35. [Symbol kinds mismatch!] “pro” defined at line 10 is a procedure but not a array.
    36. [Assign statement type mismatch!] Left “pro” type is error while right “a” type is integer.
  • 分析

报错结果符合预期,语义分析程序继续运行

1.2.2 数组下标维数不对

  • 测试样例
  1. program test(input,output);
    2. var a: array[0..5,6..10,11..15] of integer;
    3. b: integer;
    4. begin
    5. a[0]:=b;
    6. b:=a[0, 6];
    7. a[0, 6, 11]:=b;
    8. b:=a[0, 6, 11, 16];
    9. end.
  • 预期结果

报数组下标维数不对的错误

  • 测试结果
  1. [Array index number mismatch!] Array “a” should have 3 but not 1 indices.
    2. [Array index number mismatch!] Array “a” should have 3 but not 2 indices.
    3. [Array index number mismatch!] Array “a” should have 3 but not 4 indices.
  • 分析

测试结果符合预期,语义分析程序继续运行

1.2.3 数组下标类型检查和越界检查

  • 测试用例
  1. program test(input,output);
    2. const e=10;
    3. f=20;
    4. var a: array[0..5,6..10,11..15] of integer;
    5. b,c: integer;
    6. d: char;
    7. begin
    8. a[d,b>c,b+c]:=b;//表达式类型不为integer
    9. b:=a[e-f, 6-3, 20];//下标越界 常量表达式也可以计算出结果
    10. a[e+f, ef, e/f]:=b;
    11. b:=a[e mod f, e div f, e+f
    e];
    12. end.
  • 预期结果

报数组下标类型非integer、数组下标越界的错误

  • 测试结果
  1. [Expression type error!] Expression “d” used for 1th index of array “a” should be integer but not char.
    2. [Expression type error!] Expression “b > c” used for 2th index of array “a” should be integer but not boolean.
    3. [Array range out of bound!] The value of expression “e - f” is -10, but the range of array “a” 0th index is 0 to 5.
    4. [Array range out of bound!] The value of expression “6 - 3” is 3, but the range of array “a” 1th index is 6 to 10.
    5. [Array range out of bound!] The value of expression “20” is 20, but the range of array “a” 2th index is 11 to 15.
    6. [Array range out of bound!] The value of expression “e + f” is 30, but the range of array “a” 0th index is 0 to 5.
    7. [Array range out of bound!] The value of expression “e f” is 200, but the range of array “a” 1th index is 6 to 10.
    8. [Array range out of bound!] The value of expression “e / f” is 0, but the range of array “a” 2th index is 11 to 15.
    9. [Array range out of bound!] The value of expression “e mod f” is 10, but the range of array “a” 0th index is 0 to 5.
    10. [Array range out of bound!] The value of expression “e div f” is 0, but the range of array “a” 1th index is 6 to 10.
    11. [Array range out of bound!] The value of expression “e + f
    e” is 210, but the range of array “a” 2th index is 11 to 15.
  • 分析

测试结果符合预期,语义分析程序继续运行

1.3测试repeat、while、if语句的条件表达式的类型检查

1.3.1 测试1

  • 测试用例
  1. program test(input,output);//正确的程序
    2. var a,b:integer;
    3. c:boolean;
    4. begin
    5. a:=1;
    6. repeat
    7. begin
    8. writeln(a);
    9. a:=a+1
    10. end
    11. until a=10;
    12. while a<=20 do
    13. begin
    14. writeln(a);
    15. a:=a+1
    16. end;
    17. if a=20 then
    18. begin
    19. a:=a+1;
    20. writeln(a)
    21. end
    22. else
    23. begin
    24. a:=a+10;
    25. writeln(a)
    26. end;
    27. end.
  • 预测结果

这是一个正确的测试样例,不报错

  • 测试结果

语义分析程序没有报错

  • 分析

测试结果符合预期,语义分析程序继续运行

1.3.2 测试2

  • 测试用例
  1. program test(input,output);//正确的程序
    2. var a,b:integer;
    3. c:boolean;
    4. d:real;
    5. e:char;
    6. begin
    7. a:=1;
    8. repeat
    9. begin
    10. writeln(a);
    11. a:=a+1
    12. end
    13. until a+b; //条件表达式类型错误
    14. while e do //条件表达式类型错误
    15. begin
    16. writeln(a);
    17. a:=a+1
    18. end;
    19. if c=d then //条件表达式类型错误
    20. begin
    21. a:=a+1;
    22. writeln(a)
    23. end
    24. else
    25. begin
    26. a:=a+10;
    27. writeln(a)
    28. end;
    29. end.
  • 预测结果

报until、if、while条件表达式类型错误

  • 测试结果
  1. [Expression type error!] Expression “a + b” used for condition of repeat-until statement should be boolean but not integer.
    2. [Expression type error!] Expression “e” used for condition of while statement should be boolean but not char.
    3. [Operands expression type mismatch!] Left “c” type is boolean while right “d” type is real.
    4. [Expression type error!] Expression “c = d” used for condition of if statement should be boolean but not error.
  • 分析

测试结果符合预期,语义分析程序继续运行

1.4测试for语句相关类型检查

1.4.1 测试1

  • 测试用例
  1. program test(input,output);
    2. var a,b:integer;
    3. c:char;
    4. begin
    5. for a:=6 to 1+3 do
    6. writeln(b);
    7. for a:=c to a+b do //start表达式不是integer类型
    8. writeln(a);
    9. for b:=b to a>b do //end表达式不是integer类型
    10. writeln(a+b)
    11. end.
  • 预期结果

报start表达式不是integer而是char、bool类型的错误

  • 测试结果
  1. [Expression type error!] Expression “c” used for start value of for statement should be integer but not char.
    2. [Expression type error!] Expression “a > b” used for end value of for statement should be integer but not boolean.
  • 分析

测试结果符合预期,语义分析程序继续运行

1.4.2 测试2

  • 测试用例
  1. program test(input,output);
    2. var a,b:integer;
    3. c:char;
    4.
    5. function fun:integer;
    6. begin
    7. for fun:=1 to 3 do //错把函数名当循环变量
    8. writlen(‘y’)
    9. end;
    10.
    11. procedure pro(var d:integer;e:char);
    12. begin
    13. for pro:=1 to 3 do //错把过程名当循环变量
    14. writeln(ab);
    15. for d:=1 to 3 do //正确
    16. writeln(a/b);
    17. for e:=1 to 3 do //错把char变量当循环变量
    18. writlen(a mod b);
    19. for fun:=1 to 3 do //错把函数名当循环变量
    20. writeln(a div b)
    21. end;
    22.
    23. begin
    24. for test:= 1 to 3 do //错把程序名当循环变量
    25. writeln(a);
    26. for input:=1 to 3 do //错把主程序参数当循环变量
    27. writeln(b);
    28. for c:=1 to 3 do //错把char变量当循环变量
    29. writeln(c);
    30. for fun:=1 to 3 do //错把函数名当循环变量
    31. writeln(a+b);
    32. for pro:=1 to 3 do //错把过程名当循环变量
    33. writlen(a-b);
    34. for d:=1 to 3 do //循环变量未定义
    35. writeln(a
    b)
    36. end.
  • 预期结果

报把函数、过程、主程序、函数参数、字符当做循环变量以及循环变量未定义的错误

  • 测试结果
  1. [Symbol kinds mismatch!] “fun” defined at line 5 is a (sub)program name but not a value parameter, var parameter or normal variant.
    2. [Symbol kinds mismatch!] “pro” defined at line 11 is a (sub)program name but not a value parameter, var parameter or normal variant.
    3. [Usage type error!] “e” used for cyclic variable of for statement should be integer but not char.
    4. [Symbol kinds mismatch!] “fun” defined at line 5 is a function but not a value parameter, var parameter or normal variant.
    5. [Symbol kinds mismatch!] “test” defined at line 1 is a (sub)program name but not a value parameter, var parameter or normal variant.
    6. [Symbol kinds mismatch!] “input” defined at line 1 is a parameter of program but not a value parameter, var parameter or normal variant.
    7. [Usage type error!] “c” used for cyclic variable of for statement should be integer but not char.
    8. [Symbol kinds mismatch!] “fun” defined at line 5 is a function but not a value parameter, var parameter or normal variant.
    9. [Symbol kinds mismatch!] “pro” defined at line 11 is a procedure but not a value parameter, var parameter or normal variant.
    10. [Undefined identifier!] d has not been defined.
  • 分析

测试结果符合预期,语义分析程序继续运行