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行徹底暈了
拿兩分
閃人~ ~ ~