当前位置:首页 » 编程语言 » ll1文法的分析c语言
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

ll1文法的分析c语言

发布时间: 2022-03-01 12:26:23

A. c语言符合ll1文法吗

LCC编译器用的是LL1文法.

B. 编译原理LL1语法分析的例子谁有

这个句子要分成两大部分看:1.He said that....2.All things considered, he thought it was a bad idea.
2是1的宾语从句,但复杂的是在2这个宾语从句中包含了一个独立主格结构(All things considered),其实2的核心主语和谓语应该是he和thought,此时all things是独立于这个核心主语的,我们叫它独立主格,此时considered不是谓语动词,而是非谓语,在这里表示被动,所以all things considered不是一句完整的话(完整的应该是all things are considered)这里all things 是独立主格considered是它的非谓语,一起构成一个独立主格结构,和后面句子放在一起构成主句的宾语从句,是符合语法的。

C. 编译原理语法分析LL(1)程序

貌似我写过LL1文法的DO WHILE 的循环语句那个什么系统来着...
可惜要晚上回去在我电脑上菜能找到代码.....

D. 编译原理的LL(1)文法是什么意思

L表明自顶向下分析是从左向右扫描输入串,第2个L表明分析过程中将用最左到推倒,1表明只需向右看一个符号便可决定如何推倒即选择哪个产生式(规则)进行推导,类似也可以有LL(k)文法,也就是需要向前查看k个符号才能确定选用哪个产生式、、

E. 高分求LL(1)语法分析设计原理与实现技术

#include<stdio.h>
#include<iostream.h>
#include<string.h>
#define Vtn 8
#define Vnn 5
#define Pn 10
#define Pmaxlen 20
#define MaxStLength 50
#define MaxStackDepth 50

char Vn[Vnn]={'E','e','T','t','F'};

char Vt[Vtn]={'i','+','-','*','/','(',')','$'};

char Pstr[Pn][Pmaxlen]=
{
"E->Te",
"e->+Te",
"e->-Te",
"e->ε",
"T->Ft",
"t->*Ft",
"t->/Ft",
"t->ε",
"F->(E)",
"F->i"
};

int Prlen[Pn]={2,3,3,1,2,3,3,1,3,1};

int Pint[Pn][3]=
{
{102,101},
{1,102,101},
{2,102,101},
{-1},
{104,103},
{3,104,103},
{4,104,103},
{-1},
{5,100,6},
{0}
};

int analyseTable[Vnn][Vtn+1];

int analyseStack[MaxStackDepth+1];
int topAnalyse;

char st[MaxStLength];//要分析的符号串

/* ----------------------初始化----------------------------*/

void InitanalyseTable()
{
/*---预测分析表存放各个产生式的编号,-1表示找不到相匹配的产生式---*/
for(int i=0;i<Vnn;i++)
for(int j=0;j<Vtn;j++)
analyseTable[i][j]=-1;
analyseTable[0][0]=0;
analyseTable[0][5]=0;
analyseTable[1][1]=1;
analyseTable[1][2]=2;
analyseTable[1][6]=3;
analyseTable[1][7]=3;
analyseTable[2][0]=4;
analyseTable[2][5]=4;
analyseTable[3][1]=7;
analyseTable[3][2]=7;
analyseTable[3][3]=5;
analyseTable[3][4]=6;
analyseTable[3][6]=7;
analyseTable[3][7]=7;
analyseTable[4][0]=9;
analyseTable[4][5]=8;
}

void Init()
{
//分析栈的初始化
analyseStack[0]=7;//入栈
analyseStack[1]=100;//初始符入栈
topAnalyse=1;

//初始符号串
int i;
for(i = 0;i < MaxStLength;i++)
st[i] = '\0';
}

void Pop()
{
topAnalyse--;
}

/*--------------------产生式入栈操作----------------------*/

void Push(int r)
{
int i,len;
Pop();
len=Prlen[r];
//为空时
if((len==1)&&(Pint[r][0]==-1))
return;
//不为空时
topAnalyse+=len;
for(i=0;i<len;i++)
analyseStack[topAnalyse-i]=Pint[r][i]; //逆序入栈
}

void ShowStack()
{
int i;
for(i =0;i<=topAnalyse;i++)
{
if(analyseStack[i]>=100)
printf("%c",Vn[analyseStack[i]-100]);
else
printf("%c", Vt[analyseStack[i]]);

}
}

/*----------------------返回表中的位置,-1表示未找到----------*/

int Index(char c)
{
int i=0;
while((i<Vtn)&&(c!= Vt[i]))
i++;
if((i<Vtn)&&(c==Vt[i])) return i;
else return -1;
}
void Identify()
{
int current,step,r;
printf("\n%s:\n\n", st);
step = 0;
current = 0;
printf("%d\t",step);
ShowStack();
printf("\t\t%s\t\t- -\n", st+current);

while(1)
{
if(analyseStack[topAnalyse]<100)
{
if(Vt[analyseStack[topAnalyse]]==st[current])
{
Pop();
current++;
step++;
printf("%d\t",step);
ShowStack();
printf("\t\t%s\t\t出栈、后移\n", st+current);
}
else
{
printf("字符 %c 与字符 %c 不匹配!", Vt[analyseStack[topAnalyse]], st[current]);
printf("此串不符合此文法!\n");
return;
}
}
else
{
int n,m;
n=analyseStack[topAnalyse] - 100;
m=Index(st[current]);
if(m==-1)
{
printf(" %c 字符输入错误!" ,st[current]);
printf("此串不符合此文法!\n");
return;
}
r = analyseTable[n][m];
if( r!=-1)
{
Push(r);
step++;
printf("%d\t", step);
ShowStack();
printf("\t\t%s\t\t%s\n", st+current,Pstr[r]);
}
else
{
printf("无可用产生式,此串不是此文法的句子!\n");
return;
}
}
if(topAnalyse==0&&st[current]=='$') break;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
void main()
{
InitanalyseTable();
while(1)
{
Init();
printf("请输入符号串(以$结束):");
int i=0;
char c;
c=getchar();
while(i<MaxStLength)
{
if(c=='$')
{
st[i]='$';
break;
}
else if(c!=' '&&c!='\n')
st[i++]=c;
c=getchar();
if(c=='\n')
{
printf("请以$结束:");
c=getchar();
}
}
if(i<MaxStLength)
Identify();
else printf("输入的符号串超过规定长度");
}
}

F. 求用LL1文法的汇编器程序,最好用C或C++

/////////////////////////////////////////////////////////////
// LL(1)文法预测分析程序 ////
/////////////////////////////////////////////////////////////

/////////////////LL(1)文法///////////////////////////////////
// E->TG ////
// G->+TG|$ ////
// T->FH ////
// H->*FH|$ ////
// F->(E)|$ ////
/////////////////////////////////////////////////////////////

////////////////LL(1)预测分析表//////////////////////////////
// | i | + | * | ( | ) | # |///
//————————————————————————————///
// E | TG | | TG | | | |///
//————————————————————————————///
// G | | +TG | | | $ | $ |///
//————————————————————————————///
// T | FH | | | FH | | |///
//————————————————————————————///
// H | | $ | *FH | | $ | $ |///
//————————————————————————————///
// F | i | | | (E) | | |///
//————————————————————————————///
/////////////////////////////////////////////////////////////

#ifndef PREANALYSIS_H
#define PREANALYSIS_H

struct CHARSTACK{
char ch;
CHARSTACK *nextNode;
};
struct HEADSTACK
{
CHARSTACK *stackHead;
};
int Stack_Count(HEADSTACK *head)
{
int count=0;
if(head->stackHead==NULL)
return 0;
CHARSTACK *currentNode=head->stackHead;
while(currentNode!=NULL)
{
currentNode=currentNode->nextNode;
count++;
}
return count;
}

HEADSTACK *Stack_Init()
{
HEADSTACK *headNode=(HEADSTACK *)malloc(sizeof(HEADSTACK));
headNode->stackHead=NULL;
return headNode;
}

void Stack_Push(HEADSTACK *head,char ch)
{
CHARSTACK *currentNode,*tail;
tail=(CHARSTACK *)malloc(sizeof(CHARSTACK));
tail->ch=ch;
tail->nextNode=NULL;
if(head->stackHead==NULL)
{
head->stackHead=tail;
return;
}
currentNode=head->stackHead;
while(currentNode->nextNode!=NULL)
{
currentNode=currentNode->nextNode;
}
currentNode->nextNode=tail;
currentNode=tail;
currentNode->nextNode=NULL;
}

CHARSTACK Stack_Pop(HEADSTACK *head)
{
CHARSTACK *currentNode,*tail,popNode;
if(head->stackHead==NULL)
{
printf("Error:The Stack does not has node\n");
return popNode;
}
currentNode=tail=head->stackHead;
if(tail->nextNode==NULL)
{
//只有一个节点时
popNode.ch=tail->ch;
popNode.nextNode=NULL;
head->stackHead=NULL;
return popNode;
}
while (tail->nextNode!=NULL)
{
tail=tail->nextNode;
if(tail->nextNode==NULL)
{
currentNode->nextNode=NULL;
popNode.ch=tail->ch;
popNode.nextNode=tail->nextNode;
return popNode;
}
currentNode=currentNode->nextNode;
}
}
#define MAXSYMBOL 30
//终结符
char NT[]={'E','G','T','H','F'};
//非终结符
char TE[]={'i','+','*','(','(',')'};
//LL(1)预测分析表
char *analysisTable[MAXSYMBOL][3]={
{"E","i","TG"},{"E","+",""},{"E","*",""},{"E","(","TG"},{"E",")",""},{"E","#",""},
{"G","i",""},{"G","+","+TG"},{"G","*",""},{"G","(",""},{"G",")","$"},{"G","#","$"},
{"T","i","FH"},{"T","+",""},{"T","*",""},{"T","(","FH"},{"T",")",""},{"T","#",""},
{"H","i",""},{"H","+","$"},{"H","*","*FH"},{"H","(",""},{"H",")","$"},{"H","#","$"},
{"F","i","i"},{"F","+",""},{"F","*",""},{"F","(","(E)"},{"F",")",""},{"F","#",""},
};
//判断是否为终结符
bool IsNT(char ch)
{
for(int i=0;i<sizeof(NT)/sizeof(char);i++)
{
if(ch==NT[i])
return true;
}
return false;
}
//判断是否为非终结符
bool IsTE(char ch)
{
for(int i=0;i<sizeof(TE)/sizeof(char);i++)
{
if(ch==TE[i])
return true;
}
return false;
}
char* GetMatrixValue(char NT,char TE)
{
char nt[2],te[2];
nt[0]=NT;
nt[1]='\0';
te[0]=TE;
te[1]='\0';
for(int i=0;i<MAXSYMBOL;i++)
{
if((strcmp(nt,analysisTable[i][0])==0)&&(strcmp(te,analysisTable[i][1])==0)&&(analysisTable[i][2]!=""))
{
return analysisTable[i][2];
}
}
return "";
}
//判断是否为句子
bool IsCorrectSentence(char str[])
{
int pos=0;
bool Flag=true;
char X;
char *conclude;
HEADSTACK *head;
head=Stack_Init();
Stack_Push(head,'#');
Stack_Push(head,'E');
while(Flag)
{
X=Stack_Pop(head).ch;
if(IsTE(X))
{
if(X==str[pos])
{
pos++;
}
else
{
return false;
}
}
else if(X=='#')
{
if(X==str[pos])
Flag=false;
else
return false;
}
else if(strcmp((conclude=GetMatrixValue(X,str[pos])),"")!=0)
{
if(strcmp(conclude,"$")==0)
{
continue;
}
for(int j=strlen(conclude)-1;j>-1;j--)
{
Stack_Push(head,conclude[j]);
}
}
else
{
return false;
}

}
return true;
}
#endif
原来学编译原理的时候写的,不过是文法是固定死的,现在也忘了:)

G. LL(1)语法分析程序设计

的撒大厦大厦

H. 该文法是ll1文法吗 是否能改造成ll1文法 并画出使用预测分析表对符号串int

先求出每个非终结符的首符集和跟随集,在构造预测分析表时对于每一条规则,只有终结符属于这条规则的选择集时就把这条规则填入此终结符与相应非终结符交界处
选择集是针对于规则而言,与首符集、跟随集有关

I. 已知LL(1)文法和First集和Follow集,如何用c++将LL(1)预测分析表输出急!

到第5行彻底晕了
拿两分
闪人~ ~ ~