【编译原理】语法分析器

时间:2022-06-29 16:47:01


#define DeBUG
#include <iostream>
#include <cstring>
#include <cmath>
using namespace std ;

void yinzi(void);//因子
void biaodashi(void);//表达式
void yujuchuan(void);//语句串
void xiang(void);//项
void yuju(void);//语句
void lrparser(void);//分析函数
int scanner(void);//每次调用读取一个单词
int len;
int pre;
int sta;
int tlen;
int nowi;
int syn;
char token[1000];//临时存放单词
char PROGRAM[1000] = {0};//存放程序
bool isvar(char *s)
{
int l = strlen(s);
if (isdigit(s[0]))
return false;
for (int i = 0; i < l; i++)
{
if (isdigit(s[i]) || isalpha(s[i]))
continue;
else
return false;
}
return true;
}
bool IsNum(char *s)
{
int m = strlen(s);
for (int i = 0; i < m; i++)
{
if (!isdigit(s[i]))
return false;
}
return true;
}
bool iskong(char *s)
{
int m = strlen(s);
for (int i = 0; i < m; i++)
{
if (s[i] != ' ' || s[i] != '\n')
return false;
}
return true;

}
int getnum(char *s)
{
pre = sta;//状态切换
if (IsNum(s))
return 11;
else if (strcmp(s, "begin") == 0)return 1;
else if (strcmp(s, "if") == 0)return 2;
else if (strcmp(s, "then") == 0)return 3;
else if (strcmp(s, "while") == 0)return 4;
else if (strcmp(s, "do") == 0)return 5;
else if (strcmp(s, "end") == 0)return 6;
else if (isvar(s)) return 10;
else if (strcmp(s, "+") == 0)return 13;
else if (strcmp(s, "-") == 0)return 14;
else if (strcmp(s, "*") == 0)return 15;
else if (strcmp(s, "/") == 0)return 16;
else if (strcmp(s, ":") == 0)return 17;
else if (strcmp(s, ":=") == 0)return 18;
else if (strcmp(s, "<") == 0)return 20;
else if (strcmp(s, "<>") == 0)return 21;
else if (strcmp(s, "<=") == 0)return 22;
else if (strcmp(s, ">") == 0)return 23;
else if (strcmp(s, ">=") == 0)return 24;
else if (strcmp(s, "=") == 0)return 25;
else if (strcmp(s, ";") == 0)return 26;
else if (strcmp(s, "(") == 0)return 27;
else if (strcmp(s, ")") == 0)return 28;
else if (strcmp(s, "#") == 0)return 0;
else if (s[0] == '/' && s[1] == '*')return -2;
else return -1;
}
int getsta(char c)
{
if (isalpha(c))return 1;
else if (isdigit(c)) return 2;
else if (c == '(' || c == ')')return 33;
else return 3;
}
int scanner()
{
tlen = 0;
memset(token, 0, sizeof(token));

char ch;
for (int i = nowi; i < len; i++, nowi++)
{
ch = PROGRAM[i];
sta = getsta(ch);
if (pre == -1)
{
}
else if (pre == 1 && sta == 3)
{
return syn = getnum(token);
}
else if (pre == 2 && sta == 3)
{
return syn = getnum(token);
}
else if (pre == 3 && sta == 2)
{
if (!iskong(token))
return syn = getnum(token);
}
else if (pre == 3 && sta == 1)
{
if (!iskong(token))
return syn = getnum(token);
}
else if (pre == 33 || sta == 33)
{
if (!iskong(token))
return syn = getnum(token);
}
if (ch != ' ' && ch != '\n')
token[tlen++] = ch;
pre = sta;
}
return syn = 0;
}
void init()
{
int i = 0;
while (scanf("%c", &PROGRAM[i]),PROGRAM[i]!='#')
{
i++;
}
len = strlen(PROGRAM);
pre = -1;
sta = 0;
syn = -1;
tlen = 0;
nowi = 0;
memset(token, 0, sizeof(token));
}
void xiang()
{
yinzi();
while (syn == 15 || syn == 16)
{
scanner();
yinzi();
}
return ;
}
void biaodashi()
{
xiang();
while (syn == 13 || syn == 14)
{
scanner();
xiang();
}
return ;
}
void yinzi()
{
//scanner();
if (syn == 10 || syn == 11)
{
scanner();
}
else if (syn == 27)
{
scanner();
biaodashi();
if (syn == 28)
{
scanner();
}
else
{
printf("error!:miss )\n");
}
}
else
{
printf("error!:expression error!\n");
}
}

void yuju()
{
if (syn == 10)
{
scanner();
if (syn == 18)
{
scanner();
biaodashi();
}
else
{
printf("error!:miss :=\n");
}
}
else
{
printf("error!:error variable\n");
}
}
void yujuchuan()
{
yuju();
if(syn!=26)
printf("error!:miss ;");
while (syn == 26)
{
scanner();
if (syn != 6)
yuju();
}

return ;
}
void lrparser()
{
if (syn == 1)
{
scanner();
yujuchuan();
if (syn == 6)
{
scanner();
if (syn == 0)
printf("success!\n");
else
printf("error!:error position of '#'\n");
}
else
printf("error!:miss end or ;\n");
}
else
{
printf("error!: miss begin\n");
}
return ;
}
int main()
{
#ifdef DeBUGs
freopen("C:\\Users\\Sky\\Desktop\\1.in", "r", stdin);
#endif
init();
//printf("%s\n", PROGRAM);
scanner();
lrparser();
// do
// {
// scanner();
// printf("%d %s\n", syn, token);
// }
// while (syn != 0);

return 0;
}
/*
begin
x2:=9*(x-2);
x:=9;
x:=2*x+1/3;
end #
*/