1.1测试procedure_call
正确表达式:procedure_call —-> IDENTIFIER | IDENTIFIER ‘(‘ expression_list ‘)’
1.1.1 缺少右括号
procedure_call —-> IDENTIFIER ‘(‘ expression_list error
- 测试用例 ```pascal program test(input,output); var b:real; c:array[1..5,6..10] of integer; procedure pro; var a:integer; begin pro(a; end; begin
end.
- 预期结果
遇到错误时报告“缺少右括号”,并报告错误所在的具体位置,行号和列号。
- 测试结果
1. Here **is** the syntax error information <br />2. syntax error, unexpected ';', expecting ')' **or** ',', location: 7.10 <br />3. syntax error, missing a right bracket here, location: 7.10 <br />测试结果与预期一致,可以看到错误被成功识别并报告了正确位置,第一行为yacc自带的yyerror函数结果,第二行为自行使用函数重载进行错误处理的结果。
<a name="9gX1a"></a>
## 1.2测试expression_list
正确表达式:expression_list ---> expression_list ',' expression | expression
<a name="ojWmj"></a>
### 1.2.1 缺少逗号
expression_list ---> expression_list error expression
- 测试用例:
```pascal
program test(input,output);
var b:real;
c:array[1..5,6..10] of integer;
procedure pro;
var a:integer;
begin
c[a+b a*b]:=1;
end;
begin
end.
- 预期结果
遇到错误时报告:“缺少逗号”,并报告错误所在的具体位置,行号和列号。
- 测试结果:
- Here is the syntax error information
2. syntax error, unexpected IDENTIFIER, expecting ‘,’ or ‘]’, location: 7.11
3. syntax error, missing a comma here, location: 7.10
测试结果与预期一致,可以看到错误被成功识别并报告了正确位置,第一行为yacc自带的yyerror函数结果,第二行为自行使用函数重载进行错误处理的结果。1.3测试simple _expression
正确表达式:
simple_expression —-> simple_expression ADDOP term| simple_expression ‘-‘ term| term1.3.1 缺少操作数
simple_expression —-> simple_expression ADDOP error term %prec ADD
| simple_expression ‘-‘ error term %prec ADD
- 测试用例 ```pascal program test(input,output); var a,b:integer; procedure pro; var c:integer; begin a:=1; b:=2; c:=a-+b end; begin
end.
- 预期结果
遇到错误时报告“缺少操作数”,并报告错误所在的具体位置,行号和列号。
- 测试结果
1. Here **is** the syntax error information <br />2. syntax error, unexpected ADDOP, location: 8.14 <br />3. syntax error, missing operand, location: 8.14 <br />测试结果与预期一致,可以看到错误被成功识别并报告了正确位置,第一行为yacc自带的yyerror函数结果,第二行为自行使用函数重载进行错误处理的结果。
<a name="UzFfq"></a>
## 1.4 测试 term
正确表达式:term ---> term MULOP factor| factor
<a name="kvSv5"></a>
### 1.4.1 缺少操作数
term ---> term MULOP error factor %prec MUL
- 测试用例
```pascal
program test(input,output);
var a,b:integer;
procedure pro;
var c:integer;
begin
a:=1;
b:=2;
c:=a*+b
end;
begin
end.
- 预期结果
遇到错误时报告“缺少操作数”,并报告错误所在的具体位置,行号和列号。
- 测试结果
- Here is the syntax error information
2. syntax error, unexpected ADDOP, location: 8.14
3. syntax error, missing operand, location: 8.14
测试结果与预期一致,可以看到错误被成功识别并报告了正确位置,第一行为yacc自带的yyerror函数结果,第二行为自行使用函数重载进行错误处理的结果。1.5测试Factor
正确表达式:factor —-> UINUM | UFNUM | variable | IDENTIFIER ‘(‘ expression_list ‘)’ | ‘(‘ expression ‘)’ | NOT factor | ‘-‘ factor | CHAR1.5.1 缺少右括号
factor —-> IDENTIFIER ‘(‘ expression_list error
测试用例:
program test(input,output); var b:real; c:array[1..5,6..10] of integer; function fun(d,e:integer):integer; begin fun:=d+e; end; begin c[3,8]:=fun(b; end.
预期结果:
遇到错误时报告“缺少右括号”,并报告错误的具体位置,行号和列号。
- 测试结果:
- Here is the syntax error information
2. syntax error, unexpected ‘;’, expecting ‘)’ or ‘,’, location: 9.18
3. syntax error, missing a right bracket here, location: 9.18
测试结果与预期一致,可以看到错误被成功识别并报告了正确位置,第一行为yacc自带的yyerror函数结果,第二行为自行使用函数重载进行错误处理的结果。1.5.2 函数调用表达式列表缺失
factor —-> IDENTIFIER ‘(‘ error
测试用例:
program test(input,output); var b:real; c:array[1..5,6..10] of integer; function fun(d,e:integer):integer; begin fun:=d+e; end; begin c[3,8]:=fun(; end.
预期结果:
遇到错误时报告“缺失函数调用表达式列表”,并报告错误所在的具体位置,行号和列号。
- 测试结果:
- Here is the syntax error information
2. syntax error, unexpected ‘;’, location: 9.17
3. syntax error, missing actual parameter list of function call, location: 9.17
测试结果与预期一致,可以看到错误被成功识别并报告了正确位置,第一行为yacc自带的yyerror函数结果,第二行为自行使用函数重载进行错误处理的结果。1.6最终黑盒测试
此时我们使用复杂的PASCAL源程序,对语法分析程序进行黑盒测试。
注释掉main.cpp里的DEBUG信息显示:
1. //yydebug=1;
- 测试用例
快速排序:
program quicksort(input,output);
var
n,i:integer;
a:array[0..100000] of integer;
b:char;
procedure kp(l,r:integer);
var
i,j,mid:integer;
begin
b:='a';
if l>=r then exit;
i:=l;j:=r;mid:=a[(l+r) div 2];
repeat
begin
while a[i]<mid do inc(i);
while a[j]>mid do dec(j);
if i<=j then
begin
a[0]:=a[i];a[i]:=a[j];a[j]:=a[0];
inc(i);dec(j);
end
end
until i>j;
kp(l,j);
kp(i,r)
end;
begin
readln(n);
for i:=1 to n do
read(a[i]);
kp(1,n);
for i:=1 to n do
write(a[i]);
end.
- 预期结果
语法分析程序顺利分析到文本尾部,无任何错误信息,且输出文本与输入文本内容一致。
- 测试结果
- ——-begin parsing /Users/mac/yacc_and_lex_repository/lex_and_yacc/preProcessed.pas
2. ——-end parsing
- 分析
实际结果与预期结果一致。