1.1测试programstruct
正确产生式为:programstruct —-> program_head ‘;’ program_body ‘.’
1.1.1 整体程序结束后有多余内容
programstruct —-> program_head ‘;’ program_body ‘.’
测试用例
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(3,8);
end.
b:=c[1,6];
预期结果
在建立完整的语法分析树之后,读取到多余的信息,报告信息冗余,并定位到行号以及具体的符号位。
- 测试结果
- Here is the syntax error information
2. syntax error, redundant content at the end!, location: 10.5
- 分析
实际结果与预期一致。程序分析完毕后栈内状态为0,当读取到多余的记号序列时,语法分析程序提示内容冗余。
1.1.2 主程序头尾部缺少分号
programstruct —-> program_head error program_body ‘.’
- 测试用例
```pascal
program test(input,output)
begin
end.
- 预期结果
遇到错误应报告“缺少分号”的错误,并报告行号和具体位置。
- 测试结果
1. Here **is** the syntax error information <br />2. syntax error, unexpected _BEGIN, expecting ';', location: 2.1 <br />3. syntax error, missing a semicolon here, location: 1.27
- 分析
实际结果与预期结果一致,当遇到错误记号时,语法分析程序能正确记录错误信息,并继续分析后续记号串直到读到结束符,在最后报告整体错误信息。
<a name="ZopSD"></a>
### 1.1.3 程序结尾缺少点号
programstruct ---> program_head ';' program_body error
- 测试用例
```pascal
program test(input,output);
begin
end
- 预期结果
遇到错误应报告“缺少点号”的错误,并报告行号和具体位置。
- 测试结果
- Here is the syntax error information
2. syntax error, unexpected $end, expecting ‘.’, location: 4.3
3. syntax error, missing a dot here, location: 4.4
- 分析
实际结果与预期结果一致,当遇到错误记号时,语法分析程序能正确记录错误信息,并继续分析后续记号串直到读到结束符,在最后报告整体错误信息。
1.1.4 主程序头书写错误
programstruct —-> error ‘;’ program_body ‘.’
- 测试用例
```pascal
;
begin
end.
- 预期结果
语法分析程序识别主程序头失败,应能报告确实主程序头的错误信息,并准确定位。
- 测试结果
1. Here **is** the syntax error information <br />2. syntax error, unexpected ';', expecting PROGRAM, location: 1.1 <br />3. syntax error, fatal error **in** **program** head, maybe missing keyword "**program**", location: 1.1-1.1
- 分析
实际结果与预期结果一致。语法分析程序开始状态为0,此状态下读到’;’时,会移进error符号,从而触发相关报错动作,并继续分析后续记号串。
<a name="W8V4Y"></a>
### 1.1.5 主程序体书写错误
programstruct ---> program_head ';' error '.'
- 测试用例
```pascal
program test(input,output);
.
- 预期结果
语法分析程序识别主程序体失败,应在不影响其他记号串分析的情况下,报告主程序体的错误信息,并报告位置。
- 测试结果
- Here is the syntax error information
2. syntax error, unexpected ‘.’, location: 3.1
3. fatal error in program body, location: 3.1
- 分析
实际结果与预期结果一致。当语法分析程序状态为11时,读取到’.’时,会移进error记号,从而触发相关报错动作,并继续分析后续记号串。
1.1.6 主程序头前有非法字符
programstruct —-> error program_head ‘;’ program_body ‘.’
- 测试用例
```pascal
1 program test(input,output);
begin
end.
- 预期结果
语法分析程序识别到主程序头前的非法字符,应在不影响其他记号串分析的情况下,报告主程序体的错误信息,并报告位置。
- 测试结果
1. Here **is** the syntax error information <br />2. syntax error, unexpected UINUM, expecting PROGRAM, location: 1.1 <br />3. syntax error, invalid symbol before **program** head, location: 1.1-1.2
- 分析
实际结果与预期结果一致。当语法分析程序在开始状态0时,读取到非法记号例如UINUM时,会移进error记号,从而触发相关报错动作,并继续分析后续记号串。
<a name="02LDR"></a>
### 1.1.7 主程序头前有非法记号,且后部缺失分号
programstruct ---> error program_head error program_body '.'
- 测试用例
```pascal
1 program test(input,output)
begin
end.
- 预期结果
语法分析程序识别到主程序头前的非法字符,后部缺失分号,应在不影响其他记号串分析的情况下,报告主程序体的所有错误信息,并报告位置。
- 测试结果
- Here is the syntax error information
2. syntax error, unexpected UINUM, expecting PROGRAM, location: 1.1
3. syntax error, unexpected _BEGIN, expecting ‘;’, location: 2.1
4. syntax error, invalid token before program head, maybe missing keyword “program“, location: 1.1-1.2
5. syntax error, missing a semicolon here, location: 1.29
- 分析
实际结果与预期结果一致。当语法分析程序在开始状态0时,读取到非法记号例如UINUM时,会移进error记号,从而触发相关报错动作,并继续分析后续记号串。随后在状态6未遇到’;’就读到”begin”时,也是同样的处理。
1.1.8 主程序头前有非法记号,且程序尾部缺失分号
programstruct —-> error program_head ‘;’ program_body error
- 测试用例 ```pascal 1 program test(input,output); begin
end
- 预期结果
语法分析程序识别到主程序头前的非法字符,程序尾部缺失点号,应在不影响其他记号串分析的情况下,报告主程序体的所有错误信息,并报告位置。
- 测试结果
1. Here **is** the syntax error information <br />2. syntax error, unexpected UINUM, expecting PROGRAM, location: 1.1 <br />3. syntax error, unexpected $end, expecting '.', location: 4.3 <br />4. syntax error, invalid token before **program** head, maybe missing keyword "**program**", location: 1.1-1.2 <br />5. syntax error, missing a dot here, location: 4.4
- 分析
实际结果与预期结果一致。当语法分析程序在开始状态0时,读取到非法记号例如UINUM时,会移进error记号,从而触发相关报错动作,并继续分析后续记号串。随后在状态31未遇到’.’就读到文本结束符时,也是同样的处理。
<a name="1jUqq"></a>
### 1.1.9 主程序头前包含非法记号,主程序体书写错误
programstruct ---> error program_head ';' error '.'
- 测试用例
```pascal
1 program test(input,output);
.
- 预期结果
语法分析程序识别到主程序头前的非法字符,主程序体书写错误,应在不影响其他记号串分析的情况下,报告主程序的所有错误信息,并报告位置。
- 测试结果
- Here is the syntax error information
2. syntax error, unexpected UINUM, expecting PROGRAM, location: 1.1
3. syntax error, unexpected ‘.’, location: 3.1
4. syntax error, invalid token before program head, maybe missing keyword “program“, location: 1.1-1.2
5. syntax error, fatal error in program body, location: 1.30-3.0
- 分析
实际结果与预期结果一致。当语法分析程序在开始状态0时,读取到非法记号例如UINUM时,会移进error记号,从而触发相关报错动作,并继续分析后续记号串。随后在状态16未遇到program_body就读到点号时,也是同样的处理。
1.2测试program_head
正确产生式为:program_head —-> PROGRAM IDENTIFIER ‘(‘ idlist ‘)’
1.2.1 缺少左括号
program_head —-> PROGRAM IDENTIFIER error idlist ‘)’
- 测试用例
```pascal
program test input,ouput);
begin
end.
- 预期结果
语法分析程序识别到主程序头缺失左括号,应在不影响其他记号串分析的情况下,报告主程序的错误信息,并报告位置。
- 测试结果
1. Here **is** the syntax error information <br />2. syntax error, unexpected IDENTIFIER, expecting '(', location: 1.14 <br />3. syntax error, missing a left bracket here, location: 1.13
- 分析
实际结果与预期结果一致。当语法分析程序在开始状态8时,未读取到’(‘就读取到标识符记号时,会移进error记号,从而触发相关报错动作,并继续分析后续记号串。
<a name="bQ0ge"></a>
### 1.2.2 缺少右括号
program_head ---> PROGRAM IDENTIFIER '(' idlist error
- 测试用例
```pascal
program test (input,output;
begin
end.
- 预期结果
语法分析程序识别到主程序头缺失右括号,应在不影响其他记号串分析的情况下,报告主程序的错误信息,并报告位置。
- 测试结果
- Here is the syntax error information
2. syntax error, unexpected ‘;’, expecting ‘)’ or ‘,’, location: 1.27
3. syntax error, missing a right bracket here, location: 1.27
- 分析
实际结果与预期结果一致。当语法分析程序在开始状态36时,未读取到’)‘就读取到分号时,会移进error记号,从而触发相关报错动作,并继续分析后续记号串。
1.2.3 缺少主程序名
program_head —-> PROGRAM error ‘(‘ idlist ‘)’
- 测试用例
```pascal
program (input,output);
begin
end.
- 预期结果
语法分析程序识别到主程序头缺失名字,应在不影响其他记号串分析的情况下,报告主程序的错误信息,并报告位置。
- 测试结果
1. Here **is** the syntax error information <br />2. syntax error, unexpected '(', expecting IDENTIFIER, location: 1.9 <br />3. syntax error, missing **program** name here, location: 1.8
- 分析
实际结果与预期结果一致。当语法分析程序在开始状态2时,未读取到’)‘就读取到分号时,会移进error记号,从而触发相关报错动作,并继续分析后续记号串。
<a name="EZED0"></a>
### 1.2.4 主程序头不完整
program_head ---> PROGRAM error
- 测试用例
```pascal
program
begin
end.
- 预期结果
语法分析程序识别到主程序头缺失两个或两个以上的必要记号,应在不影响其他记号串分析的情况下,报告主程序的错误信息,并报告位置。
- 测试结果
- Here is the syntax error information
2. syntax error, unexpected _BEGIN, expecting IDENTIFIER, location: 2.1
3. syntax error, program head imcomplete, location: 1.1-1.7
- 分析
实际结果与预期结果一致。当语法分析程序在开始状态2时,未读取到主程序头所需后续记号就读取到主程序体信息时,会移进error记号,从而触发相关报错动作,并继续分析后续记号串。
1.2.5 主程序头参数列表缺失
1:program_head —-> PROGRAM IDENTIFIER ‘(‘ error ‘)’
2:program_head —-> PROGRAM IDENTIFIER error
3:program_head —-> PROGRAM IDENTIFIER ‘(‘ error
- 测试用例1
```pascal
program test (1,2);
begin
end.
- 预期结果
语法分析程序识别到主程序头参数列表缺失,应在不影响其他记号串分析的情况下,报告主程序的错误信息,并报告位置。
- 测试结果
1. Here **is** the syntax error information <br />2. syntax error, unexpected UINUM, expecting IDENTIFIER, location: 1.15 <br />3. syntax error, **program** identifier list missing **or** imcomplete, location: 1.1-1.12 <br />4. syntax error, missing a semicolon here, location: 1.16 <br />
- 分析
实际结果与预期结果一致。当语法分析程序在开始状态4时,读取到参数列表错误时,会移进error记号,从而触发相关报错动作,并继续分析后续记号串。
- 测试用例2
```pascal
program test
begin
end.
- 预期结果
语法分析程序识别到主程序头参数列表缺失,应在不影响其他记号串分析的情况下,报告主程序的错误信息,并报告位置。
- 测试结果
- Here is the syntax error information
2. syntax error, unexpected _BEGIN, expecting ‘(‘, location: 2.1
3. syntax error, program identifier list missing or imcomplete, location: 1.1-1.12
- 分析
实际结果与预期结果一致。当语法分析程序在开始状态2时,读取到参数列表错误时,会移进error记号,从而触发相关报错动作,并继续分析后续记号串。
- 测试用例3
```pascal
program test(
begin
end.
- 预期结果
语法分析程序识别到主程序头参数列表缺失,应在不影响其他记号串分析的情况下,报告主程序的错误信息,并报告位置。
- 测试结果
1. Here **is** the syntax error information <br />2. syntax error, unexpected _BEGIN, expecting IDENTIFIER, location: 2.1 <br />3. syntax error, **program** identifier list missing **or** imcomplete, location: 1.1-1.12
- 分析
实际结果与预期结果一致。当语法分析程序在开始状态19时,读取到参数列表错误时,会移进error记号,从而触发相关报错动作,并继续分析后续记号串。
<a name="KhMxP"></a>
## 1.3测试const_declarations
正确的表达式:const_declarations ---> CONST const_declaration ';' | empty
<a name="5vLrg"></a>
### 1.3.1 常量定义出现错误
const_declarations ---> CONST error
- 测试用例
```pascal
program test(input,output);
const 1
begin
end.
- 预期结果
语法分析程序识别到常量定义出现错误,应在不影响其他记号串分析的情况下,报告主程序的错误信息,并报告位置。
- 测试结果
- Here is the syntax error information
2. syntax error, unexpected UINUM, expecting IDENTIFIER, location: 2.7
- 分析
实际结果与预期结果一致。当语法分析程序在状态12,读取到非法字符(例如测试用例中的UINUM)时会进行丢弃并移进error,从而触发相关报错动作,并继续分析后续记号串。
1.3.2 常量定义出现错误的特殊情况
const_declarations —-> CONST error ‘;’
- 测试用例
```pascal
program test(input,output);
const 1;
begin
end.
- 预期结果
语法分析程序识别到常量定义出现错误,应在不影响其他记号串分析的情况下,报告主程序的错误信息,并报告位置。
- 测试结果
1. Here **is** the syntax error information <br />2. syntax error, unexpected UINUM, expecting IDENTIFIER, location: 2.7 <br />3. syntax error, fatal error **in** **const** declarations, location: 2.7-2.7
- 分析
实际结果与预期结果一致。当语法分析程序在状态12,读取到非法字符(例如测试用例中的UINUM)时会进行丢弃并移进error,从而触发相关报错动作,并继续分析后续记号串。
<a name="F1L9j"></a>
### 1.3.3 缺少分号
const_declarations ---> CONST const_declaration error
- 测试用例
```pascal
program test(input,output);
const a=3;b=5
begin
end.
- 预期结果
语法分析程序识别到常量定义缺少分号,应在不影响其他记号串分析的情况下,报告主程序的错误信息,并报告位置。
- 测试结果
- Here is the syntax error information
2. syntax error, unexpected _BEGIN, expecting ‘;’, location: 3.1
3. syntax error, missing a semicolon here, location: 2.7-2.13
- 分析
实际结果与预期结果一致。当语法分析程序在状态25时,未读到分号便读到了“beigin”时,会移进error记号,从而触发相关报错动作,并继续分析后续记号串。
1.4测试const_declaration
正确的产生式为:const_declaration —-> const_declaration ‘;’ IDENTIFIER ‘=’ const_value
| IDENTIFIER ‘=’ const_value
1.4.1 常数初始化右值缺失
1: const_declaration —-> const_declaration ‘;’ IDENTIFIER ‘=’ error
2: const_declaration —-> IDENTIFIER ‘=’ error
- 测试用例1 ```pascal program test(input,output); const a=1;b=; begin
end.
- 预期结果
语法分析程序识别到常数初始化右值缺失,应在不影响其他记号串分析的情况下,报告主程序的错误信息,并报告位置。
- 测试结果
1. Here **is** the syntax error information <br />2. syntax error, unexpected ';', location: 2.13 <br />3. syntax error, constant definition missing initial r-value, location: 2.12-2.12
- 分析
实际结果与预期结果一致。当语法分析程序在状态91时,读取到常数初始化右值缺失时,会移进error记号,从而触发相关报错动作,并继续分析后续记号串。
- 测试用例2
```pascal
program test(input,output);
const a=;
begin
end.
- 预期结果
语法分析程序识别到常数初始化右值缺失,应在不影响其他记号串分析的情况下,报告主程序的错误信息,并报告位置。
- 测试结果
- Here is the syntax error information
2. syntax error, unexpected ‘;’, location: 2.9
3. syntax error, constant definition missing initial r-value, location: 2.9-2.9
- 分析
实际结果与预期结果一致。当语法分析程序在状态43时,读取到常数初始化右值缺失时,会移进error记号,从而触发相关报错动作,并继续分析后续记号串。
1.4.2 常量定义缺少分号
const_declaration —-> const_declaration error IDENTIFIER ‘=’ const_value
- 测试用例 ```pascal program test(input,output); const a=1 b=a; begin
end.
- 预期结果
语法分析程序识别到常量定义缺少分号,应在不影响其他记号串分析的情况下,报告主程序的错误信息,并报告位置。
- 测试结果
1. Here **is** the syntax error information <br />2. syntax error, unexpected IDENTIFIER, expecting ';', location: 2.11 <br />3. syntax error, missing a semicolon here, location: 2.7-2.10
- 分析
实际结果与预期结果一致。当语法分析程序在状态25时,读取到常量定义缺少分号时,会移进error记号,从而触发相关报错动作,并继续分析后续记号串。
<a name="OBkfP"></a>
### 1.4.3 常量定义缺少等号
1:const_declaration ---> const_declaration ';' IDENTIFIER error const_value<br />2:const_declaration ---> IDENTIFIER error const_value
- 测试用例1
```pascal
program test(input,output);
const a=1;b 2;
begin
end.
- 预期结果
语法分析程序识别到常量定义缺少等号,应在不影响其他记号串分析的情况下,报告主程序的错误信息,并报告位置。
- 测试结
- Here is the syntax error information
2. syntax error, unexpected UINUM, expecting ‘=’, location: 2.13
3. syntax error, missing a equal sign here, location: 2.11-2.11
- 分析
实际结果与预期结果一致。当语法分析程序在状态70时,读取到常量定义缺少等号时,会移进error记号,从而触发相关报错动作,并继续分析后续记号串。
- 测试用例2 ```pascal program test(input,output); const a 1; begin
end.
- 预期结果
语法分析程序识别到常量定义缺少等号,应在不影响其他记号串分析的情况下,报告主程序的错误信息,并报告位置。
- 测试结果
1. Here **is** the syntax error information <br />2. syntax error, unexpected UINUM, expecting '=', location: 2.9 <br />3. syntax error, missing a equal sign here, location: 2.9-2.9
- 分析
实际结果与预期结果一致。当语法分析程序在状态24时,读取到常量定义缺少等号时,会移进error记号,从而触发相关报错动作,并继续分析后续记号串。
<a name="F2JCe"></a>
## 1.5测试var_declarations
var_declarations ---> VAR var_declaration ';' <br />| empty
<a name="IGZbN"></a>
### 1.5.1 变量定义出现错误
- 测试用例1
```pascal
program test(input,output);
var ;
begin
end.
- 预期结果
语法分析程序识别到变量定义出现错误,应在不影响其他记号串分析的情况下,报告主程序的错误信息,并报告位置。
- 测试结果
- Here is the syntax error information
2. syntax error, unexpected ‘;’, expecting IDENTIFIER, location: 2.5
3. syntax error, fatal error in variant declarations, location: 2.1-2.3
- 分析
实际结果与预期结果一致。当语法分析程序在状态27时,读取到变量定义出现错误时,会移进error记号,从而触发相关报错动作,并继续分析后续记号串。
- 测试用例2 ```pascal program test(input,output); var x; begin
end.
- 预期结果
语法分析程序识别到变量定义出现错误,应在不影响其他记号串分析的情况下,报告主程序的错误信息,并报告位置。
- 测试结果
1. Here **is** the syntax error information <br />2. syntax error, unexpected ';', expecting ',' **or** ':', location: 2.6
- 分析
实际结果与预期结果一致。当语法分析程序在状态47时,读取到变量定义出现错误时,会移进error记号,从而触发相关报错动作,并继续分析后续记号串。
<a name="7CXI1"></a>
### 1.5.2 变量定义尾部缺少分号
var_declarations ---> VAR var_declaration error
- 测试用例
```pascal
program test(input,output);
var x:integer; y:real
begin
end.
- 预期结果
语法分析程序识别到变量定义尾部缺少分号,应在不影响其他记号串分析的情况下,报告主程序的错误信息,并报告位置。
- 测试结果
- Here is the syntax error information
2. syntax error, unexpected _BEGIN, expecting ‘;’, location: 3.1
3. syntax error, missing a semicolon here, location: 2.22
- 分析
实际结果与预期结果一致。当语法分析程序在状态48时,读取到变量定义尾部缺少分号时,会移进error记号,从而触发相关报错动作,并继续分析后续记号串。
1.6测试var_declaration
var_declaration —-> var_declaration ‘;’ idlist ‘:’ type
| idlist ‘:’ type
1.6.1 多个变量定义之间缺少分号分隔
var_declaration —-> var_declaration error idlist ‘:’ type
- 测试用例 ```pascal program test(input,output); var x:integer y:real; begin
end.
- 预期结果
语法分析程序识别到多个变量定义之间缺少分号分隔,应在不影响其他记号串分析的情况下,报告主程序的错误信息,并报告位置。
- 测试结果
1. Here **is** the syntax error information <br />2. syntax error, unexpected IDENTIFIER, expecting ';', location: 2.15 <br />3. syntax error, missing a semicolon here, location: 2.14
- 分析
实际结果与预期结果一致。当语法分析程序在状态48时,读取到多个变量定义之间缺少分号分隔时,会移进error记号,从而触发相关报错动作,并继续分析后续记号串。
<a name="8CVji"></a>
### 1.6.2 变量定义缺少冒号
1:var_declaration ---> var_declaration ';' idlist error type<br />2:var_declaration ---> idlist error type
- 测试用例1
```pascal
program test(input,output);
var x:integer;y real;
begin
end.
- 预期结果
语法分析程序识别到多个变量定义缺少冒号,应在不影响其他记号串分析的情况下,报告主程序的错误信息,并报告位置。
- 测试结果
- Here is the syntax error information
2. syntax error, unexpected TYPE, expecting ‘,’ or ‘:’, location: 2.17
3. syntax error, missing a colon here, location: 2.16
- 分析
实际结果与预期结果一致。当语法分析程序在状态98时,读取到变量定义缺少冒号时,会移进error记号,从而触发相关报错动作,并继续分析后续记号串。
- 测试用例2 ```pascal program test(input,output); var b real; procedure pro; begin end; begin
end.
- 预期结果
语法分析程序识别到多个变量定义缺少冒号,应在不影响其他记号串分析的情况下,报告主程序的错误信息,并报告位置。
- 测试结果
1. Here **is** the syntax error information <br />2. syntax error, unexpected TYPE, expecting ',' **or** ':', location: 2.7 <br />3. syntax error, missing a colon here, location: 2.6
- 分析
实际结果与预期结果一致。当语法分析程序在状态47时,读取到变量定义缺少冒号时,会移进error记号,从而触发相关报错动作,并继续分析后续记号串。
<a name="zNwyQ"></a>
### 1.6.3 变量定义类型识别失败
var_declaration ---> var_declaration ';' idlist ':' error
- 测试用例1
```pascal
program test(input,output);
var x:integer;y:;
begin
end.
- 预期结果
遇到错误时报告“缺少变量定义类型”,并报告错误所在的具体位置,行号和列号。
- 测试结果
- Here is the syntax error information
2. syntax error, unexpected ‘;’, expecting ARRAY or TYPE, location: 2.17
3. syntax error, missing a type here, location: 2.17
- 分析
测试结果与预期相同。可以看到错误被成功识别并报告了正确位置,第一行为yacc自带的yyerror函数结果,第二行为自行使用函数重载进行错误处理的结果。