📄 syntax.h
字号:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int Errors = 0, Level = -1;
int type, Line=0;//种别,行号
int Begin = 0;
char p[20];
ifstream fin("words.txt");
ofstream fout("errors.txt");
void Input();//从词法分析的结果中读入字符
void ErrorOut(int line, char *p, int code);//出错处理函数
int ConstAna(int ProStep);//常量说明部分,保留字CONST
int Var(int ProStep);//Var变量定义
int Expression(int);//处理表达式
int Factor();//因子处理函数
int Item();//项
int Condition();//条件处理函数
int Sentence() ;//句子处理函数
void Syntax() ; //程序块
void Input()
{
fin >> Line >> type >> p;
return;
}
struct Identity
{
char id[20];
int type;//0 - const, 1 - var, 2 - Procedure
int level;
int value;
Identity *next;
} *Head, *End;
void ErrorOut(int line, char *p, int code)
{
Errors++;
char ErrorCode[80];
switch (code)
{
case 0 :strcpy(ErrorCode," ");break;
case 1 :strcpy(ErrorCode," 常数说明中的\"=\"写成\":=\"。 ");break;
case 2 :strcpy(ErrorCode," 常数说明中的\"=\"后应是数字 ");break;
case 3 :strcpy(ErrorCode," 常数说明中的标示符后应是\"=\" ");break;
case 4 :strcpy(ErrorCode," const, var, procedure 后应为标示符。 ");break;
case 5 :strcpy(ErrorCode," 漏掉了','或';' ");break;
case 7 :strcpy(ErrorCode," 应是语句开始符。 ");break;
case 9 :strcpy(ErrorCode," 程序结尾丢了句号'.' ");break;
case 11:strcpy(ErrorCode," 不可识别的标示符。");break;
case 12:strcpy(ErrorCode," 赋值语句中,赋值号左部标示符属性应是变量 ");break;
case 13:strcpy(ErrorCode," 赋值语句左部标识符后应是赋值号\":=\" ");break;
case 14:strcpy(ErrorCode," call后应为标识符 ");break;
case 15:strcpy(ErrorCode," call后标识符属性应为过程 ");break;
case 16:strcpy(ErrorCode," 条件语句中丢了\"then\" ");break;
case 17:strcpy(ErrorCode," 丢了'END'或';' ");break;
case 18:strcpy(ErrorCode," while型循环语句中丢了\"do\" ");break;
case 19:strcpy(ErrorCode," 语句后的符号不正确 ");break;
case 20:strcpy(ErrorCode," 应为关系运算符 ");break;
case 21:strcpy(ErrorCode," 表达式内标识符属性能是过程 ");break;
case 22:strcpy(ErrorCode," 表达式中漏掉右括号 ");break;
case 23:strcpy(ErrorCode," 因子后的非法符号 ");break;
case 24:strcpy(ErrorCode," 表达式的开始符不能是此符号 ");break;
case 31:strcpy(ErrorCode," 数越界 ");break;
case 32:strcpy(ErrorCode," read语句括号中的标识符不是变量 ");break;
case 33:strcpy(ErrorCode," 重复定义 ");break;
case 34:strcpy(ErrorCode," BEGIN与END不匹配 ");break;
case 35:strcpy(ErrorCode," 常数说明部分已结束。 ");break;
case 36:strcpy(ErrorCode," 变量说明部分已结束。 ");break;
}
if(!code)
{
Errors--;
return;
}
cout << "Line (" << line << ") : " << p << ErrorCode << endl;
fout << "Line (" << line << ") : " << p << ErrorCode << endl;
}
int ConstAna(int ProStep)//常量说明部分,保留字CONST
{
int step = 0;
int flag = 0;
while(true)
{
Input();
step = (step + 1) % 5;
if(!step)
{
step++;
flag = 0;
}
if(step == 1)
{
if(type != 3) ErrorOut(Line, p, 4);//const, var, procedure 后应为标示符
else if(ProStep == 0)
{
Identity *pId = new Identity;
strcpy(pId->id,p);
pId->type = 0;
pId->next = NULL;
pId->level = Level;
if(Head == NULL)
{
End = pId;
Head = pId;
}
else
{
End = Head;
if(strcmp(End->id,p) == 0)
{
ErrorOut(Line, p, 33);//重复定义
flag = 1;
}
while(End->next)
{
End = End->next;
if(strcmp(End->id,p) == 0)
{
ErrorOut(Line, p, 33);//重复定义
flag = 1;
break;
}
}
if(strcmp(End->id,pId->id) != 0) End->next = pId;
}
}
}
if(step == 2)
{
if(type == 5)
ErrorOut(Line, p, 1);//常数说明中的\"=\"写成\":=\"。
else
if(type != 4)
ErrorOut(Line, p,3);// 常数说明中的标示符后应是\"=\"
else
if(strcmp(p,"=") != 0)
{
ErrorOut(Line, p, 3); // 常数说明中的标示符后应是\"=\"
step--;
}
}
if(step == 3)
{
if(type != 2)
ErrorOut(Line, p, 2);//常数说明中的\"=\"后应是数字
else
{
int num = 0, i = 0;
while(p[i])
{
num = 10 * num + p[i] - '0';
i++;
}
if(!flag)
End->value = num;
}
}
if(step == 4) if(p[0] != ',' && p[0] != ';')
ErrorOut(Line, p, 5);//漏掉了','或';'
if(strcmp(p,";") == 0) return 0;
}
}
int Var(int ProStep)//Var变量定义
{
int step = 0;
while(true)
{
Input();
step = (step + 1) % 3;
if(!step) step++;
if(step == 1)
{
if(type != 3)
ErrorOut(Line, p, 4);//const, var, procedure 后应为标示符。
else if(ProStep == 1)
{
Identity *pId = new Identity;
strcpy(pId->id,p);
pId->type = 1;
pId->next = NULL;
pId->level = Level;
if(Head == NULL) Head = pId;
else
{
End = Head;
if(strcmp(End->id,p) == 0) ErrorOut(Line, p, 33);
while(End->next)
{
End = End->next;
if(strcmp(End->id,p) == 0)//重复定义
{
ErrorOut(Line, p, 33);
break;
}
}
if(strcmp(End->id,pId->id) != 0) End->next = pId;
}
}
}
if(step == 2)
if(p[0] != ',' && p[0] != ';')
ErrorOut(Line, p, 5);//漏掉了','或';
if(strcmp(p,";") == 0) return 0;
}
}
int Factor()
{
if(type == 2) return 0;
if(type == 3)
{
End = Head;
while(End)
{
if(strcmp(p,End->id) == 0) return 0;
End = End->next;
}
return 11;
}
if(p[0] == '(')
{
Input();
Expression(0);
}
else return 0;
Input();
if(p[0] != ')') return 22;
return 0;
}
int Item()
{
int a = Factor();
ErrorOut(Line, p, a);
if(p[0] == '*' || p[0] == '/')
Input();
else return 0;
a = Factor();
ErrorOut(Line, p, a);
return 0;
}
int Expression(int notFirst)//表达式
{
if(p[0] == '+' || p[0] == '-') Input();
else if(notFirst) return 0;
Item();
Input();
if(p[0] == '>' || p[0] == '<' || p[0] == '=' || p[0] == '#') return 0;
Expression(1);
return 0;
}
int Condition() //条件
{
Input();
if(strcmp(p,"ODD") == 0) Input();
Expression(1);
if(p[0] == '>' || p[0] == '<' || p[0] == '=' || p[0] == '#') Input();
else return 20;
Expression(1);
return 0;
}
int Sentence()
{
if(!p) return 0;
if(strcmp(p,"BEGIN") == 0)
{
Begin++;
Input();
while(p && strcmp(p,"END") != 0)
{
Sentence();
if(p[0] != ';') Input();
if(p[0] == ';') Input();
else ErrorOut(Line, p, 5);//漏掉了','或';'
}
Begin--;
return Begin;
}
if(type == 3)
{
End = Head;
while(End)
{
if(strcmp(End->id,p) == 0)
{
if(End->type != 1) ErrorOut(Line, p, 12);//赋值语句中,赋值号左部标示符属性应是变量
break;
}
End = End->next;
}
if(!End || strcmp(End->id,p) != 0)
ErrorOut(Line, p, 11);
Input();
if(type != 5)
ErrorOut(Line, p, 13);
Input();
int a = Expression(0);
if(a)
ErrorOut(Line, p, a);
return 0;
}
if(strcmp(p,"CALL") == 0)
{
Input();
if(type != 3)
ErrorOut(Line, p, 14);
return 0;
}
if(strcmp(p,"IF") == 0)
{
int a = Condition();
ErrorOut(Line, p, a);
Input();
if(strcmp(p,"THEN") != 0)
ErrorOut(Line, p, 16);
else Input();
a = Sentence();
ErrorOut(Line, p, a);
return 0;
}
if(strcmp(p,"WRITE") == 0)
{
Input();
if(p[0] =='(')
Input();
else
ErrorOut(Line, p, 7);
while(true)
{
Expression(0);
if(p[0] != ',') break;
else Input();
}
if(p[0] != ')')
ErrorOut(Line ,p, 22);
return 0;
}
if(strcmp(p,"READ") == 0)
{
Input();
if(p[0] != '(')
ErrorOut(Line, p, 7);
else
Input();
while(type == 3)
{
Input();
if(p[0] != ',') break;
else
Input();
}
if(p[0] != ')' )
ErrorOut(Line, p, 22);
return 0;
}
if(strcmp(p,"WHILE") == 0)
{
Input();
int a = Condition();
ErrorOut(Line, p, a);
if(strcmp(p,"DO") != 0) Input();
if(strcmp(p,"DO") != 0) ErrorOut(Line, p, 18);
else Input();
a = Sentence();
ErrorOut(Line, p, a);
return 0;
}
return 0;
}
void Syntax() //程序块
{
cout<<"语法分析如下..."<<endl;
int Step = 0;
Level ++;
while(fin >> type >> Line >> p)
{
if(type == 0)
{
ErrorOut(Line, p, 11);
continue;
}
if(type == 1)
{
if(strcmp(p,"CONST") == 0)
{
if(Step >= 1) ErrorOut(Line, p, 35);
ConstAna(Step);
if(Step < 1) Step = 1;
}
if(strcmp(p,"VAR") == 0)
{
if(Step >= 2) ErrorOut(Line, p, 36);
Var(Step);
if(Step < 2) Step = 2;
}
if(strcmp(p,"PROCEDURE") == 0)
{
Step = 3;
Identity *pId = new Identity;
Input();
strcpy(pId->id,p);
pId->level = 0;
pId->type = 2;
pId->next =NULL;
End = Head;
while(End->next)
{
if(strcmp(pId->id,End->id) == 0)
{
ErrorOut(Line, p, 33);
break;
}
End = End->next;
}
if(strcmp(pId->id,End->id) != 0) End->next = pId;
Syntax();
}
if(strcmp(p,"BEGIN") == 0) Step = 4;
}//End Type = 1
if(type == 2) ErrorOut(Line, p, 7);
if(Step == 4)
{
int a = Sentence();
if(a) ErrorOut(Line, p, 34);
//break;
}
}//End While
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -