#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<stack>
using namespace std;
/*const int STACK_INIT_SIZE = 30; //存储空间初始分配量
const int STACKINCREMENT = 10; //存储空间分配增量
const int VTNUM = 10;
const int VNNUM = 10;
const int prNUM = 15;
const int pLength = 9;
const int MAXNUM = 25;
int pnum;
int vnum;
char VT[VTNUM]; // 终结符
char VN[VNNUM]; // 非终结符
char pr[prNUM][pLength]; // 产生式
bool VEmpty[VNNUM];
char VFirst[VNNUM][VTNUM + 1];
char VFollow[VNNUM][VTNUM + 1];
char VSelect[MAXNUM][VTNUM + 3];
typedef char SElemType, ElemType;
typedef struct {
SElemType * base;
SElemType * top;
int stacksize;
}SqStack;
bool InitStack (SqStack &S);
SElemType GetTop (SqStack S);
bool Push (SqStack &S, SElemType e);
bool Pop (SqStack &S);
bool InitStack (SqStack &S) // 构建一个空栈 S
{
S.base = (SElemType *)malloc(STACK_INIT_SIZE * sizeof(ElemType));
if(!S.base) return false; //exit (OVERFLOW); // 存储分配失败
S.top = S.base;
S.stacksize = STACK_INIT_SIZE;
return true;
}
SElemType GetTop (SqStack S)
{ // 若不为空,用e返回栈顶元素
if(S.top == S.base) // if(S.top = S.base) 错错错
{
return false;
}
SElemType e = *(S.top - 1);
return e;
}
bool Push (SqStack &S, SElemType e) // 入栈
{ //插入元素 e 为新的栈顶元素
if(S.top - S.base >= S.stacksize)
{
S.base = (ElemType * )realloc(S.base,
(S.stacksize + STACKINCREMENT) * sizeof(ElemType));
if(!S.base) return false;//exit(OVERFLOW);
S.top = S.base + S.stacksize;
S.stacksize += STACKINCREMENT;
}
*S.top ++ = e;
return true;
}
bool Pop (SqStack &S) // 出栈
{ // 删除 S 栈顶元素
if(S.top == S.base) return false;
--S.top;
return true;
}
int StackEmpty(SqStack S)
{
if(S.top == 0) return 1;
else return 0;
}
void PrintStack(SqStack S)
{
SElemType * p;
p = S.top - 1;
while (1 > 0)
{
cout<<*p;
if (p == S.base)
{
return;
}
p--; ///
}
}
void RePrintStack(SqStack S)
{
SElemType *p;
p = S.base;
while (p != S.top)
{
cout<<*p;
p++;
}
}
void First(char ch) // 求非终结符ch 的First集
{
int a, b, code;
char c;
code = IsVN(ch);
for (int i = 1; i <= pnum; i++)
{
if (pr[i][0] != ch )
{
continue;
}
int L = strlen( pr[i] );
for (int k = 3; k < L; k++)
{
c = pr[i][k];
a = IsVT( pr[i][k] );
b = IsVN( pr[i][k] );
if ( b >= 0 ) // A->Bc A->BCD
{
Addch(VFirst[code], VFirst[b] );
if ( !VEmpty[b] ) // 如果不能推出空
{
break;
}
}
else // A->c
{
Addchar( VFirst[code], c );
break;
}
}
}
}
void Follow(char vn) // A->aBc
{
int a, b, c;
c = IsVN(vn);
for (int i = 1; i <= pnum; i++)
{
a = IsVN(pr[i][0] );
int L = strlen( pr[i] );
for (int j = 3; j < L; j++)
{
if ( pr[i][j] == vn )
{
for (int k = j + 1; k < L; k++)
{
// a = IsVT(pr[i][k] );
b = IsVN(pr[i][k] );
if (b < 0)
{
Addchar(VFollow[c], pr[i][k] );
break;
}
else
{
Addch(VFollow[c], VFirst[b] );
if (!VEmpty[b] )
{
break;
}
}
}
if (k == L)
{
Addch(VFollow[c], VFollow[a] );
}
}
}
}
}
void Select(int c) // 求第c条产生式的Select集
{
int a, b = IsVN(pr[c][0]);
int L = strlen(pr[c] );
for (int i = 3; i < L; i++)
{
a = IsVN(pr[c][i] );
if (a < 0)
{
Addchar(VSelect[c], pr[c][i] );
// i--; //
break;
}
else
{
Addch(VSelect[c], VFirst[a] );
if (!VEmpty[a])
{
break;
}
}
}
if (i == L) // 用来判断 for 循环时有无中断,如果for()顺利执行,则有 i == L
{
Addch(VSelect[c], VFollow[b] );
}
}
void GetSelect()
{
for (int i = 1; i <= pnum; i++)
{
Select(i);
}
}
void GetFirst()
{
int a = 0, num = 0;
while (1 > 0)
{
for (int i = 0; i < vnum; i++)
{
First(VN[i] );
}
num = 0;
for (int c = 0; c < VNNUM; c++)
{
for (int d = 0; d < VNNUM + 1; d++)
{
if (VFirst[c][d] == '\0' )
{
continue;
}
num++;
}
}
if (a == num)
{
return;
}
a = num;
}
}
void GetFollow()
{
int a = 0, num = 1;
while (1 > 0)
{
for (int i = 0; i < vnum; i++)
{
Follow(VN[i] );
}
num = 0; /
for (int c = 0; c < VNNUM; c++)
{
for (int d = 0; d < VNNUM + 1; d++)
{
if (VFirst[c][d] == '\0' )
{
continue;
}
num++;
}
}
if (a == num)
{
return;
}
a = num;
}
}
void Inite()
{
cout<<"输入产生式的个数:";
cin>>pnum;
cin.get();
cout<<"请输入各产生式:\n";
cout<<"\n说明:若有空产生式A->ε,则ε不必打出来;直接输入A->\n\n";
int i = 1;
while ( i <= pnum )
{
cout<<"\n第"<<i<<"个产生式:";
int k = 0;
char a = cin.get();
while (a != '\n')
{
if (a != ' ')
{
pr[i][k++] = a;
}
a = cin.get();
}
pr[i][k++] = '\0';
GetVN( pr[i][0] );
i++;
}
// 提取出终结符
for (int j = 1; j <= pnum; j++)
{
GetVT( &pr[j][0] );
VSelect[j][0] = pr[j][0];
}
for (j = 0; j < VNNUM; j++)
{
if (VN[j] == '\0' )
{
break;
}
}
vnum = j;
VFollow[0][0] = '#';
}*/
stack<char>stac;
char H[200]="SXPDT"; /*非终结符集*/
char L[200]="z()+*#"; /*运算符集*/
char LL1[30][30][50] = {{"->XP","->XP" ,"null","null" ,"null","null" },/*将产生式存入数组*/
{"->TD","->TD" ,"null","null" ,"null", "null"},
{"null","null" ,"->$" ,"->+XP","null","->$" },
{"null","null" ,"->$" ,"->$" ,"->*TD","->$" },
{"->z" ,"->(S)","null","null" ,"null","null" } };
int findH(char a) //找到对应非终结符
{
for(int i = 0; i < 5; i++)
{
if(a==H[i])
{
return i;
}
}
return -1;
}
int findL(char b) //找到对应产生式
{
for(int i = 0; i < 6; i++)
{
if(b==L[i])
{
return i;
}
}
return -1;
}
int error(int i,int step, int len, char p[],char str[])
{
printf("%d\t%s\t",step,p);
for(int q = i; q<len;q++)
{
cout<<str[q];
}
printf("\t出错\n");
return len;
}
void analyze(char str[],int len)
{
int step = 1; //记录Step
int i = 0;
char p[200] = "#S"; //输出stack
int pindex = 2;
printf("步骤 分析栈 剩余输入串 推导所用产生式或匹配\n");
while(i<len)
{
int x,y;
char ch = stac.top(); /*取栈顶元素*/
if(ch>='A'&&ch<='Z') /*判定非终结符*/
{
stac.pop(); /*删除栈顶元素*/
x = findH(ch);
y = findL(str[i]);
if(x!=-1&&y!=-1) /*判断是否在数组里找到对应产生式*/
{
int len2 = strlen(LL1[x][y]);
if(strcmp(LL1[x][y],"null")==0) /*判断所找到的产生式是否为空*/
{
i = error(i,step,len,p,str); /*出错处理*/
continue;
}
printf("%d\t%s\t",step,p);
if(p[pindex-1] != '#') /*判断剩余输入串是否为空*/
{
p[pindex] = '\0';
pindex--;
}
if(LL1[x][y][2]!='$') /*进行下一步推导*/
{
for(int q = len2-1; q>1; q--)
{
p[pindex++] = LL1[x][y][q];
stac.push(LL1[x][y][q]);
}
}
else
{
p[pindex] = '\0';
编译原理实验二 LL(1)语法分析器.zip
版权申诉
5星 · 超过95%的资源 53 浏览量
2021-04-27
15:33:40
上传
评论 19
收藏 24.81MB ZIP 举报
Pxtod
- 粉丝: 20
- 资源: 35
最新资源
- Unity适用于Windows与移动平台双端的轮盘交互式提示菜单
- UE5/UE4超详细教程接入科大讯飞语音唤醒SDK并初始持久监听(10102错误码解决)
- 靠搬运古装剧解说视频,日收益200+,作品原创度90%以上,超详细教程-教程网盘链接提取码下载 .txt
- 2024最强数学建模之美赛攻略.md
- STM32单片机FPGA毕设电路原理论文报告一种机器人寻线控制系统
- STM32单片机FPGA毕设电路原理论文报告一种光电编码器位置检测系统研究与应用
- linux常用命令大全
- ACM题库,分类整理.md
- STM32单片机FPGA毕设电路原理论文报告一种高速折刀运动控制系统的设计与实现
- ZYWIFI0939C循迹、红外避障、遥控综合程序.plg
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
- 1
- 2
- 3
- 4
- 5
- 6
前往页