📄 新建 文本文档.txt
字号:
#include <stdio.h>
char *action[10][3]={"S3#","S4#",NULL, /*ACTION表*/
NULL,NULL,"acc",
"S6#","S7#",NULL,
"S3#","S4#",NULL,
"r3#","r3#",NULL,
NULL,NULL,"r1#",
"S6#","S7#",NULL,
NULL,NULL,"r3#",
"r2#","r2#",NULL,
NULL,NULL,"r2#"};
int goto1[10][2]={1,2, /*QOTO表*/
0,0,
0,5,
0,8,
0,0,
0,0,
0,9,
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <malloc.h>
#include "math.h"
#include "iostream.h"
char str[100];
char ch;
int nn=0;
int f=0;
char count='0';
struct cifa//词法结构体
{
int type;//类型
char word[10];//字符串内容
cifa *next;
};
cifa *cifa_head,*cifa_end,*cifa_p;//cifa队列
struct yufa//语法结构体
{
yufa *pre;
char word[10];//字符串内容
yufa *next;
};
yufa *yufa_head,*yufa_end,*yufa_q,*yufa_vt;//yufa队列
struct yuyi//语法结构体
{
char op;
char op1[10];
char op2[10];
char result[10];
yuyi *next;
};
yuyi *yuyi_head,*yuyi_end,*yuyi_q,*yuyi_vt;//yuyi队列
char E_name[10],T_name[10],F_name[10],temp_name[10];
//*****************************************词法分析部分***************************************
cifa *cifa_add(cifa *p) //在分析结果列表尾添加一个新接点
{
cifa_end -> next = p;
cifa_end = cifa_end -> next;
return cifa_head;
}
void cifa_disp(cifa *cifa_head) //输出词法分析结果
{
cifa *p;
p = cifa_head -> next ;
cout<<" "<<'('<<'\t'<<"编码"<<'\t'<<','<<'\t'<<"符号"<<'\t'<<')'<<endl;
while ( p != NULL)
{
cout<<" "<<'('<<'\t'<<p->type<<'\t'<<','<<'\t'<<p->word<<'\t'<<')'<<endl;
p = p ->next;
}
}
void GetChar() //取字符
{
ch = str[nn];
nn++;
}
void notock() //去掉空格
{
if ( ch == ' ' )
while ( ch == ' ' )
GetChar();
}
void alph(void) //标识符
{
int i=0;
char temp[10];
int type = 1;
temp[i] = ch;
i++;
GetChar();
while ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch == '_') || (ch >= '0' && ch <= '9'))
{
temp[i] = ch;
i++;
GetChar();
}
temp[i] = '\0';
if (ch == ' ') notock();
else if (ch != '^' && ch != '+' && ch != '-' && ch != '=' && ch != '&' && ch != '*' && ch != '/' && ch != '('&& ch != '|'&& ch != ')'&& ch != ';')
{
cout<<temp<<"接错误后缀,出错"<<endl;
return ;
}
//
cifa *p;
p = new cifa;
p -> next = NULL;
p -> type = type;
strcpy(p->word,temp);
cifa_add(p);
}
void number(void) //数字
{
int type=2;
int i=0;
char temp[10];
while('0'<= ch && ch <= '9')
{
temp[i] = ch;
i++;
GetChar();
}
temp[i]='\0';
if (ch == ' ') notock();
else if (ch != '^' && ch != '+' && ch != '-' && ch != '=' && ch != '&' && ch != '*' && ch != '/' && ch != '('&& ch != '|'&& ch != ')')
{
cout<<temp<<"接错误后缀,出错"<<endl;
return ;
}
if (ch == ' ') notock();
cifa *p;
p = new cifa;
p -> next = NULL;
p -> type = type;
strcpy(p->word,temp);
cifa_add(p);
}
void test(void) //符号
{
char temp[3];
int i=0;
int type;
switch (ch)
{
case ';' : // ';'
{
temp[i++] = ch;
GetChar();
if (ch ==' ' ) temp[i++] =' ';
temp[i] = '\0';
type = 4;
break;
}
case '=' :
{
temp[i++] = ch;
GetChar();
if (ch ==' ' )
temp[i++] =' '; // '='
temp[i] = '\0';
type = 3;
break;
}
case '+' :
{
temp[i++] = ch;
GetChar();
if (ch ==' ' ) temp[i++] =' '; // '+'
temp[i] = '\0';
type = 3;
break;
}
case '-' :
{
temp[i++] = ch;
GetChar();
if (ch ==' ' ) temp[i++] =' '; // '-'
temp[i] = '\0';
type = 3;
break;
}
case '*' : // '*'
{
temp[i++] = ch;
GetChar();
if (ch ==' ' )
temp[i++] =' ';
temp[i] = '\0';
type = 3;
break;
}
case '/' : // '/'
{
temp[i++] = ch;
GetChar();
if (ch ==' ' )
temp[i++] =' ';
temp[i] = '\0';
type = 3;
break;
}
case '(' :
{
temp[i++] = ch; // '('
GetChar();
if (ch ==' ' )
temp[i++] =' ';
temp[i] = '\0';
type = 4;
break;
}
case ')' :
{
temp[i++] = ch; // ')'
GetChar();
if (ch ==' ' )
temp[i++] =' ';
temp[i] = '\0';
type = 4;
break;
}
case ' ': return ;
case '^': return ;
default :
{ cout<<ch;
cout<<"无法识别,出错!"<<endl;
GetChar();
if (ch == ' ') notock();
return ;
}
}
if (ch == ' ') notock(); // 空格跳过
cifa *p;
p = new cifa;
p -> next = NULL;
p -> type = type;
strcpy(p->word,temp);
cifa_add(p);
}
void cifa_main() //词法分析主函数
{
cifa_head = new cifa;
cifa_head -> type = -1;
cifa_head -> next = NULL;
cifa_end = cifa_head;
cout<<" 单词种类定义如下 : "<<endl;
cout<<"****************************************"<<endl;
cout<<" 种类编码 "<<endl;
cout<<" 标识符 ----------1 "<<endl;
cout<<" 常数 ----------2 "<<endl;
cout<<" 算术运算符 ----------3 "<<endl;
cout<<" 界限符 ----------4 "<<endl;
GetChar();
notock();
cout<<"****************************************"<<endl<<" 词法分析结果如下:"<<endl;
while ( nn < 100 && ch != '^')
{
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch == '_')) alph(); //字母串
else if (ch >= '0' && ch <= '9') number(); //数字串
else test();//其他符号
}
cifa_disp(cifa_head);
}
//*****************************************语法分析部分***************************************
int E();
int G();
int S();
int T();
int F();
void advance()//取词法分析产生列表中的结点作语法分析
{
cifa_p = cifa_p -> next;
}
void yufa_zfc_disp(cifa *p)
{
while(p!=NULL)
{
cout<<p->word ;
p = p->next;
}
cout<<endl;
}
void yufa_zhan_disp(yufa *p)
{
while(p!=NULL)
{
cout<<p->word ;
p = p->next;
}
cout<<endl;
}
int E()
{
int t,g;
cout<<f++<<'\t'<<"E -> TG"<<endl;
t = T();
if (t == 0) return (0);
g = G();
if (g == 0) return (0);
else return (1);
}
int G()
{
int t,g;
if (strcmp(cifa_p->word,"+") == 0)
{
cout<<f++<<'\t'<<"G -> +TG"<<endl;
advance();
t=T();
if (t == 0) return(0);
g=G();
if ( g== 0) return (0);
return (1);
}
else if (strcmp(cifa_p->word,"-") == 0)
{
cout<<f++<<'\t'<<"G -> -TG"<<endl;
advance();
t=T();
if (t == 0) return(0);
g=G();
if (g == 0) return (0);
return(1);
}
else if (strcmp(cifa_p->word,")") == 0 || strcmp(cifa_p->word,"#") == 0)
{
cout<<f++<<'\t'<<"G -> ε"<<endl;
return(1);
}
}
int T()
{
int t,g;
cout<<f++<<'\t'<<"T -> FS"<<endl;
t = F();
if (t== 0) return 0;
g = S();
if (g == 0) return 0;
return(1);
}
int S()
{
int t,g;
if (strcmp(cifa_p->word,"*") == 0)
{
cout<<f++<<'\t'<<"S -> *FS"<<endl;
advance();
t = F();
if (t== 0) return 0;
g = S();
if (g == 0) return 0;
return(1);
}
else if (strcmp(cifa_p->word,"/") == 0)
{
cout<<f++<<'\t'<<"S -> /FS"<<endl;
advance();
t = F();
if (t== 0) return 0;
g = S();
if (g == 0) return 0;
return(1);
}
else if (strcmp(cifa_p->word,"+") == 0 ||(strcmp(cifa_p->word,"-") == 0)||(strcmp(cifa_p->word,"#") == 0)||(strcmp(cifa_p->word,")") == 0))
{
cout<<f++<<'\t'<<"S -> ε"<<endl;
return(1);
}
}
int F()
{
int m;
if ((strcmp(cifa_p->word,"(") == 0 ) )
{
cout<<f++<<'\t'<<"F -> (E)"<<endl;
advance();
m =E();
if (m==0) return (0);
if ((strcmp(cifa_p->word,")") == 0 ) )
{
advance();
return (1);
}
else
{
cout<<"ERROR"<<endl;
return (0);
}
}
else if ( cifa_p->type == 1 || cifa_p->type == 2)
{
cout<<f++<<'\t'<<"F -> 标识符|无符号整数"<<endl;
advance();
return (1);
}
else return 0;
}
int yufa_main()
{
int n;
cifa *p = new cifa;
strcpy(p -> word ,"#");
p -> type =-1;
p -> next = NULL;
cifa_add(p);
//
cifa_p = cifa_head;
yufa_zfc_disp(cifa_head->next);
yufa_head = new yufa;
yufa_head -> pre = NULL;
yufa_head -> next = NULL;
strcpy(yufa_head -> word ,"#");
yufa_end = yufa_head;
//
advance();
n = E();
//
if (n == 0)
{
cout<<endl<<"输入串不是该文法的一个句子!"<<endl;
return (0);
}
else if (n == 1)
{
cout<<endl<<"输入串是该文法的一个句子! "<<endl;
return (1);
}
}
//********************************语义分析**************************************************
yuyi *yuyi_add(yuyi *p)
{
yuyi_end->next = p ;
yuyi_end = p;
return yuyi_head;
}
int E1();
int T1();
int F1();
void yuyi_sys_disp()
{
yuyi *p;
p = yuyi_head->next;
while(p!=NULL)
{
cout<<" "<<'('<<p->op<<','<<p->op1<<','<<p->op2<<','<<p->result<<')'<<endl;
p = p->next;
}
cout<<endl;
}
int E1()
{
yuyi *p = new yuyi;
T1();
//
strcpy(p->op1,T_name);
if (strcmp(cifa_p->word,"+") == 0)
{
advance();
E1();
p->next =NULL;
p->op = '+';
strcpy(p->op2,E_name);
//
E_name[0] = 't';
E_name[1] = ++count;
E_name[2] = '\0';
strcpy(p->result,E_name);
yuyi_add(p);
return (1);
}
else if (strcmp(cifa_p->word,"-") == 0)
{
advance();
E1();
//
p->next =NULL;
p->op = '-';
strcpy(p->op2,E_name);
//
E_name[0] = 't';
E_name[1] = ++count;
E_name[2] = '\0';
strcpy(p->result,E_name);
yuyi_add(p);
return(1);
}
else//
{
//
strcpy(E_name,T_name);
return(1);
}
//
}
int T1()
{
yuyi *p = new yuyi;
//
F1();
//
strcpy(p->op1,F_name);
if (strcmp(cifa_p->word,"*") == 0)
{
advance();
T1();
p->next =NULL;
p->op = '*';
strcpy(p->op2,T_name);
//
T_name[0] = 't';
T_name[1] = ++count;
T_name[2] = '\0';
strcpy(p->result,T_name);
yuyi_add(p);
return(1);
}
else if (strcmp(cifa_p->word,"/") == 0)
{
advance();
T1();
//
p->next =NULL;
p->op = '/';
strcpy(p->op2,T_name);
//
T_name[0] = 't';
T_name[1] = ++count;
T_name[2] = '\0';
strcpy(p->result,T_name);
yuyi_add(p);
return(1);
}
else //
{
//
strcpy(T_name,F_name);
return(1);
}
//
}
int F1()
{
//
if ((strcmp(cifa_p->word,"(") == 0 ) )
{
advance();
//
strcpy(F_name,cifa_p->word);
strcpy(E_name,F_name);
E1();
if ((strcmp(cifa_p->word,")") == 0 ) )
{
advance();
strcpy(F_name,E_name);
return (1);
}
else
{
cout<<"ERROR"<<endl;
return (0);
}
}
else if ( cifa_p->type == 1 || cifa_p->type == 2)
{
strcpy(F_name,cifa_p->word);
advance();
return (1);
}
else return 0;
}
void yuyi_main()
{
cifa_p = cifa_head;
yuyi_head = new yuyi;
yuyi_head -> next = NULL;
yuyi_end = yuyi_head;
yufa_zfc_disp(cifa_head->next);
advance();
E1();
yuyi_sys_disp();
}
//******************************************************************************************
void main()
{
int len;
cout<<"***************************************************"<<endl;
cout<<" 算术表达式的语法分析及语义分析程序设计(递归下降法)"<<endl;
cout<<"***************************************************"<<endl;
cout<<" 计科0301 05 林捷 "<<endl<<endl;
cout<<"算术表达式的文法描述:"<<endl;
cout<<" E -> TG "<<endl;
cout<<" T -> FS "<<endl;
cout<<" G -> +TG|-TG|ε"<<endl;
cout<<" S -> *FS|/FS|ε"<<endl;
cout<<" F -> (E)|标识符|无符号整数"<<endl<<endl;
cout<<"请输入算术表达式:"<<endl<<endl;
gets(str);
len = strlen(str);
str[len] = '^';
cout<<endl<<"---------------进行词法分析----------------"<<endl<<endl;
cifa_main();
cout<<endl<<"---------------进行语法分析----------------"<<endl<<endl;
yufa_main();
cout<<endl<<"---------------进行语义分析----------------"<<endl<<endl;
yuyi_main();
// cout<<"按任意数字或字母键,回车退出!"<<endl;
// cin>>len;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -