📄 grammaranalysis.h
字号:
#include <iostream>
#include <string>
using namespace std;
string kind[100];
int counter = 1;
void s(string kind[100], int &bit);
void s1(string kind[100], int &bit);
void c(string kind[100], int &bit);
void c1(string kind[100], int &bit);
void e(string kind[100], int &bit);
void e1(string kind[100], int &bit);
void t(string kind[100], int &bit);
void t1(string kind[100], int &bit);
void f(string kind[100], int &bit);
bool IsDigit(char ch)
{
return (ch >= '0' && ch <= '9');
}
bool IsAlpha(char ch)
{
return (ch >= 'a' && ch <= 'z'|| ch >= 'A'&& ch <= 'Z');
}
void s(string kind[100],int &bit)
{
if(kind[bit] == "IDN")
{
bit++;
if(kind[bit] == "=")
bit++;
cout<<counter++<<".S → id = E"<<endl;
e(kind, bit) ;
}
if(kind[bit] == "IF")
{
bit++;
cout<<counter++<<".S → if C then S"<<endl;
c(kind, bit);
if(kind[bit] == "THEN")
{
bit++;
s(kind,bit);
}
}
if(kind[bit] == "WHILE")
{
bit++;
cout<<counter++<<".S → while C do S"<<endl;
c(kind, bit) ;
if(kind[bit] == "DO")
{
bit++;
s(kind, bit) ;
}
}
}
void c(string kind[100], int &bit)
{
cout<<counter++<<".C → EC`"<<endl;
e(kind, bit) ;
c1(kind, bit) ;
}
void c1(string kind[100], int &bit)
{
if(kind[bit] == ">")
{
bit++;
cout<<counter++<<".C` → > E"<<endl;
e(kind,bit) ;
}
if(kind[bit] == "<")
{
bit++;
cout<<counter++<<".C` → < E"<<endl;
e(kind, bit) ;
}
if(kind[bit] == "=")
{
bit++;
cout<<counter++<<".C` → = E"<<endl;
e(kind, bit) ;
}
}
void e(string kind[100],int &bit)
{
cout<<counter++<<".E → TE`"<<endl;
t(kind, bit) ;
e1(kind, bit) ;
}
void e1(string kind[100],int &bit)
{
if(kind[bit] == "+")
{
bit++;
cout<<counter++<<".E` → +TE`"<<endl;
t(kind, bit) ;
e1(kind, bit) ;
}
else if(kind[bit]=="-")
{
bit++;
cout<<counter++<<".E` → -TE`"<<endl;
t(kind, bit) ;
e1(kind, bit) ;
}
else
{
cout<<counter++<<".E` → ε"<<endl;
}
}
void t(string kind[100], int &bit)
{
cout<<counter++<<".T → FT`"<<endl;
f(kind, bit) ;
t1(kind, bit) ;
}
void t1(string kind[100], int &bit)
{
if(kind[bit] == "*")
{
bit++;
cout<<counter++<<".T` → * FT`"<<endl;
f(kind, bit) ;
t1(kind, bit) ;
}
else if(kind[bit] == "/")
{
bit++;
cout<<counter++<<".T` → / FT`"<<endl;
f(kind,bit) ;
t1(kind,bit) ;
}
else
{
cout<<counter++<<".T` →ε"<<endl;
}
}
void f(string kind[100], int &bit)
{
if(kind[bit] == "(")
{
bit++;
cout<<counter++<<".F → ( E )"<<endl;
e(kind, bit) ;
if(kind[bit] == ")")
bit++;
}
if(kind[bit] == "IDN")
{
bit++;
cout<<counter++<<".F → id"<<endl;
}
if(kind[bit] == "INT8")
{
bit++;
cout<<counter++<<".F → int8"<<endl;
}
if(kind[bit] == "INT16")
{
bit++;
cout<<counter++<<".F → int16"<<endl;
}
if(kind[bit] == "INT10")
{
bit++;
cout<<counter++<<".F → int10"<<endl;
}
}
int scan()
{
string word;
int bit = 0;
char a[200];
char b = ' ';
int m = 0;
cout<<"现在您可以输入您要测试的语句(以!结束):";
while(b != '!')
{
b = getchar();
a[m] = b;
m++;
}
word = a;
int y = 0;
char arr[200];
char ch;
int x = 0;
int i = 0;
int j = 0;
while(1) //while 1
{
ch = word[x]; //从输入文件中读入一个字符
if( ch == ' ' || ch == '\t') //过滤掉空格和tab if 1
x++;
else if(IsDigit(ch)) //读入的是数字 else if 1
{
if(ch == '0' && word[x+1] != ' ' && word[x+1] != '!') //if 1 1
{
arr[j] = ch;
j++;
x++;
ch = word[x];
if(ch == 'x') //if 1 1 1 16进制
{
arr[j] = ch;
j++;
x++;
ch = word[x];
while(IsDigit(ch) || (IsAlpha(ch) && ((ch >= 'a' && ch <= 'f') || (ch >= 'A'&& ch <= 'F') )))
{
arr[j] = ch;
j++;
x++;
ch=word[x];
}//
if(ch != ' ' && ch != '!' && ch != '+' && ch != '-' && ch != '*' && ch != '/' && ch != '(' && ch != ')' && ch != '.' && ch != '>' && ch != '<' && ch != '=' && ch != ',') // if 1 1 1 1
{
cout<<"16进制数输入错误"<<endl;
system("pause");
return 0;
}// if 1 1 1 1
else // else 1 1 1 1
{
cout<<"Token is : ";
while(y != j)
{
cout<<arr[y];
y++;
}
y=0;
cout<<endl;
kind[bit] = "INT16";
cout<<"Class:"<<kind[bit]<<endl;
bit++;
cout<<endl;
j=0;
}//else 1 1 1 1
} // if 1 1 1
else if(IsDigit(ch)) //else if 1 1 1
{
while(IsDigit(ch) && ch >= '0' && ch <= '7')
{
arr[j] = ch;
j++;
x++;
ch=word[x];
}
if(ch != ' ' && ch != '!' && ch != '+' && ch != '-' && ch != '*' && ch != '/' && ch != '(' && ch != ')' && ch != '.' && ch != '>' && ch != '<' && ch != '=' && ch != ',')
{
cout<<"8进制数输入错误";
system("pause");
return 0;
j=0;
}//if
else
{
cout<<"Token is :";
while(y != j)
{
cout<<arr[y];
y++;
}
y = 0;
cout<<endl;
kind[bit] = "INT8";
cout<<"Class:"<<kind[bit]<<endl;
bit++;
cout<<endl;
j=0;
}//else
} //else if 1 1 1
} // if 1 1
else if(ch == '0' && word[x+1] == '!') //else if 1 1
{
cout<<"Token is :0"<<endl;
kind[bit] = "INT10";
cout<<"Class:"<<kind[bit]<<endl;
bit++;
cout<<"Attribute is :"<<0<<endl;
cout<<"End"<<endl;
cout<<endl;
system("pause");
return 0;
}// else if 1 1
else // else 1 1
{
while(IsDigit(ch))
{
arr[j] = ch;
j++;
x++;
ch = word[x];
}
if(ch != ' ' && ch != '!' && ch != '+' && ch != '-' && ch != '*' && ch != '/' && ch != '(' && ch != ')' && ch != '.' && ch != '>' && ch != '<' && ch != '=' && ch != ',')
{
cout<<"10进制数输入错误";
system("pause");
return 0;
j = 0;
}
cout<<"Token is :";
while(y != j)
{
cout<<arr[y];
y++;
}
y=0;
cout<<endl;
kind[bit] = "INT10";
cout<<"Class:"<<kind[bit]<<endl;
bit++;
char* temp1 =(char*)malloc(j+1);
memcpy(temp1,arr,j);
temp1[j] ='\0';
string tempshit=temp1;
cout<<"Attribute is :"<<tempshit<<endl;
cout<<endl;
j = 0;
}// else 1 1
} //else if 1
else if(IsAlpha(ch))//是字母开头的 //else if 2
{
while(IsAlpha(ch) || IsDigit(ch))
{
arr[i] =ch;
i++;
x++;
ch=word[x];
}
/*思想和方法与数字的很相似*/
char* temp = (char*)malloc(i+1) ;
memcpy(temp,arr,i);
temp[i] ='\0';
string tempshit=temp;
if(tempshit == "while") //查表 if 2 1
{
cout<<"Token is :";
while(y != i)
{
cout<<arr[y];
y++;
}
y=0;
cout<<endl;
kind[bit]="WHILE";
cout<<"class:"<<kind[bit]<<endl;
bit++;
cout<<"Attribute is :"<<"_"<<endl;
cout<<endl;
} //if 2 1
else if(tempshit == "if")//查表
{
cout<<"Token is :";
while(y != i)
{
cout<<arr[y];
y++;
}
y=0;
cout<<endl;
kind[bit]="IF";
cout<<"class:"<<kind[bit]<<endl;
bit++;
cout<<"Attribute is :"<<"_"<<endl;
cout<<endl;
}
else if(tempshit == "then")//查表
{
cout<<"Token is :";
while(y != i)
{
cout<<arr[y];
y++;
}
y=0;
cout<<endl;
kind[bit]="THEN";
cout<<"Class:"<<kind[bit]<<endl;
bit++;
cout<<"Attribute is :"<<"_"<<endl;
cout<<endl;
}
else if(tempshit == "else")//查表
{
cout<<"Token is :";
while(y != i)
{
cout<<arr[y];
y++;
}
y=0;
cout<<endl;
kind[bit] = "ELSE";
cout<<"Class:"<<kind[bit]<<endl;
bit++;
cout<<"Attribute is :"<<"_"<<endl;
cout<<endl;
}
else if(tempshit == "do")//查表 else if 2 1
{
cout<<"Token is :";
while(y != i)
{
cout<<arr[y];
y++;
}
y=0;
cout<<endl;
kind[bit] = "DO";
cout<<"Class:"<<kind[bit]<<endl;
bit++;
cout<<"Attribute is :"<<"_"<<endl;
cout<<endl;
}
else //else 2 1
{
cout<<"Token :";
while(y != i)
{
cout<<arr[y];
y++;
}
y=0;
cout<<endl;
kind[bit] = "IDN";
cout<<"Class:"<<kind[bit]<<endl;
bit++;
cout<<"Attribute is :"<<"DATA"<<endl;
cout<<endl;
}//else 2 1
i = 0;
}//else if 2
else if(ch == '-') //else if 3
{
kind[bit] = "-";
cout<<"Class:"<<kind[bit]<<endl;
bit++;
cout<<"Class is :"<<"-"<<endl;
cout<<"Attribute is :"<<"_ "<<endl;
x++;
ch = word[x];
cout<<endl;
}
else if(ch == ':')
{
kind[bit] = ":";
cout<<"Class:"<<kind[bit]<<endl;
bit++;
cout<<"Class is :"<<":"<<endl;
cout<<"Attribute is :"<<"_ "<<endl;
x++;
ch = word[x];
cout<<endl;
}
else if(ch == '+')
{
kind[bit] = "+";
cout<<"Class:"<<kind[bit]<<endl;
bit++;
cout<<"Class is :"<<"+"<<endl;
cout<<"Attribute is :"<<"_ "<<endl;
x++;
ch = word[x];
cout<<endl;
}
else if(ch == '*')
{
kind[bit] = "*";
cout<<"Class:"<<kind[bit]<<endl;
bit++;
cout<<"Class is :"<<"*"<<endl;
cout<<"Attribute is :"<<"_ "<<endl;
x++;
ch = word[x];
cout<<endl;
}
else if(ch == ';')
{
kind[bit] = ";";
cout<<"Class:"<<kind[bit]<<endl;
bit++;
cout<<"Class is :"<<";"<<endl;
cout<<"Attribute is :"<<"_ "<<endl;
x++;
ch = word[x];
cout<<endl;
}
else if(ch == '/')
{
kind[bit] = "/";
cout<<"Class:"<<kind[bit]<<endl;
bit++;
cout<<"Class is :"<<"/"<<endl;
cout<<"Attribute is :"<<"_ "<<endl;
x++;
ch = word[x];
cout<<endl;}
else if(ch == '(')
{
kind[bit] = "(";
cout<<"Class:"<<kind[bit]<<endl;
bit++;
cout<<"Class is :"<<"("<<endl;
cout<<"Attribute is :"<<"_ "<<endl;
x++;
ch = word[x];
cout<<endl;}
else if(ch == ')')
{
kind[bit] = ")";
cout<<"Class:"<<kind[bit]<<endl;
bit++;
cout<<"Class is :"<<")"<<endl;
cout<<"Attribute is :"<<"_ "<<endl;
x++;
ch = word[x];
cout<<endl;
}
else if(ch == '.')
{
kind[bit] = ".";
cout<<"Class:"<<kind[bit]<<endl;
bit++;
cout<<"Class is :"<<"."<<endl;
cout<<"Attribute is :"<<"_ "<<endl;
x++;
ch = word[x];
cout<<endl;
}
else if(ch == ',')
{
kind[bit] = ",";
cout<<"Class:"<<kind[bit]<<endl;
bit++;
cout<<"Class is :"<<","<<endl;
cout<<"Attribute is :"<<"_ "<<endl;
x++;
ch=word[x];
cout<<endl;
}
else if(ch == '>')
{
kind[bit] = ">";
cout<<"Class:"<<kind[bit]<<endl;
bit++;
cout<<"Class is :"<<">"<<endl;
cout<<"Attribute is :"<<"_ "<<endl;
x++;
ch=word[x];
cout<<endl;
}
else if(ch == '<')
{
kind[bit] = "<";
cout<<"Class:"<<kind[bit]<<endl;
bit++;
cout<<"Class is :"<<"<"<<endl;
cout<<"Attribute is :"<<"_ "<<endl;
x++;
ch=word[x];
cout<<endl;
}
else if(ch == '=')
{
kind[bit] = "=";
cout<<"Class:"<<kind[bit]<<endl;
bit++;
cout<<"Class is :"<<"="<<endl;
cout<<"Attribute is :"<<"_ "<<endl;
x++;
ch=word[x];
cout<<endl;
}
else
{
if(ch == '!')
{
cout<<"End"<<endl;
bit = 0;
s(kind, bit);
break;
//system("pause");
//return 0;
}
else
{
cout<<"Token is :"<<ch<<endl;
cout<<"字符错误"<<endl;
//system("pause");
//return 0;
break;
}
}
}//while 1
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -