想请教各位高手预测分析表怎么做?还有关于文法的机内存储形式的相关操作.
8 个解决方案
#1
程序名称: LL(1)文法分析程序
http://www.pudn.com/downloads56/sourcecode/chinese/detail199479.html
http://www.pudn.com/downloads56/sourcecode/chinese/detail199479.html
#2
LL1 文法分析的实现
http://www.yuanma.org/data/2006/0720/article_1174.htm
http://www.yuanma.org/data/2006/0720/article_1174.htm
#3
LL(1)文法及分析表的自动构造
http://scholar.ilib.cn/Abstract.aspx?A=shdegydxxb200102007
http://scholar.ilib.cn/Abstract.aspx?A=shdegydxxb200102007
#4
LL1算法
点击数:229 发布日期:2006-5-12 18:15:00
http://www.programfan.com/blog/article.asp?id=13961
#5
真心的谢谢两位!
不过这些网页怎么都打不开?
不过这些网页怎么都打不开?
#6
to 楼主:
http://www.programfan.com/blog/article.asp?id=13961
这个能打开~
http://www.programfan.com/blog/article.asp?id=13961
这个能打开~
#7
//文法是:E->TE' E'->+TE' T->FT' T'->+ET' F->(E)/i
#include <iostream.h>
#include <stdlib.h>
#include <string.h>
#include<stdio.h>
typedef char datatype;
#define maxsize 100
int save_i=0;
int save_j=0;
int l=0;
int w;
char a[10];
typedef struct
{
datatype data[maxsize];
int top;
}seqstack;
seqstack g;
void Input()//输入流
{
int i=-1;
char c;
c=getchar();
while(c!='#')
{
a[++i]=c;
c=getchar();
}
a[++i]='$';
save_i=i;
}
void SETNULL(seqstack *s)
{
s->top=-1;
}
int EMPTY(seqstack *s)
{
if(s->top>=0) return 0;
else return 1;
}
int PUSH(seqstack *s,datatype x)
{
if(s->top==maxsize-1){printf("overflow");}
else
{
s->top++;
s->data[s->top]=x;
l=s->top;
}
return l;
}
int POP(seqstack *s)
{
if(EMPTY(s))
{printf("underflow"); return NULL;}
else{
s->top--;
l=s->top;
//return (s->data[s->top+1]);
}
return l;
}
datatype TOP(seqstack *s)
{
if(EMPTY(s))
{printf("stack is empty ");
return NULL;} //空栈
else
return (s->data[s->top]);
}
int LL1()
{
char c;
if(g.data[g.top]==a[save_j])
{save_j++;
POP(&g);
return save_j;}
else if(a[save_j]=='i') ////i--id;t--T';e--E'
{
c=TOP(&g);
if(c=='E')
{
POP(&g);
PUSH(&g,'e');
PUSH(&g,'T');
}
if(c=='T')
{POP(&g);
PUSH(&g,'t');
PUSH(&g,'F');
}
if(c=='F')
{POP(&g);
PUSH(&g,'i');
}
}
else if(a[save_j]=='+')
{
c=TOP(&g);
if(c=='e')
{
POP(&g);
PUSH(&g,'e');
PUSH(&g,'T');
PUSH(&g,'+');
}
if(c=='t')
{
POP(&g);
}
}
else if(a[save_j]=='*')
{
c=TOP(&g);
if(c=='t')
{ POP(&g);
PUSH(&g,'t');
PUSH(&g,'F');
PUSH(&g,'*');
}
}
else if(a[save_j]=='(')
{
c=TOP(&g);
if(c=='E')
{POP(&g);
PUSH(&g,'e');
PUSH(&g,'T');
}
if(c=='T')
{ POP(&g);
PUSH(&g,'t');
PUSH(&g,'F');
/// PUSH(&g,'*');
}
if(c=='F')
{ POP(&g);
PUSH(&g,')');
PUSH(&g,'E');
PUSH(&g,'(');
}
}
else if(a[save_j]==')')
{
c=TOP(&g);
if(c=='e')
{
POP(&g);
}
if(c=='t')
{
POP(&g);
}
}
else if(a[save_j]=='$')
{
c=TOP(&g);
if(c=='e')
{
POP(&g);
}
if(c=='t')
{
POP(&g);
}
}
else cout<<"此语法错误"<<endl;
return 0;
}
void st()
{
//SETNULL(&g);
if(EMPTY(&g))
{PUSH(&g,'$');
PUSH(&g,'E');
}
}
void main()
{
SETNULL(&g);
Input();
st();
for(int j=0;j<=l;j++)
{
cout<<g.data[j];
}
cout<<" ";
for(int i=save_j;i<=save_i;i++)
{
cout<<a[i];
}
cout<<endl;
while(!(g.data[g.top]=='$'&&a[save_j]=='$')&&!(a[save_j]=='i'&&a[save_j+1]=='i')&&!(a[save_j]=='+'&&a[save_j+1]=='*')&&!(a[save_j+1]=='+'&&a[save_j]=='*')&&!(g.data[g.top]==')'&&a[save_j]=='$'))
{
//while(!(g.data[g.top]=='$'&&a[save_j]=='$'))
{
LL1();
for(int j=0;j<=l;j++)
{
cout<<g.data[j];
}
cout<<" ";
for(int i=save_j;i<=save_i;++i)
{
cout<<a[i];
}
cout<<endl;
}
}
//cout<<"此处错误不应该出现双运算符"<<endl;
}
#include <iostream.h>
#include <stdlib.h>
#include <string.h>
#include<stdio.h>
typedef char datatype;
#define maxsize 100
int save_i=0;
int save_j=0;
int l=0;
int w;
char a[10];
typedef struct
{
datatype data[maxsize];
int top;
}seqstack;
seqstack g;
void Input()//输入流
{
int i=-1;
char c;
c=getchar();
while(c!='#')
{
a[++i]=c;
c=getchar();
}
a[++i]='$';
save_i=i;
}
void SETNULL(seqstack *s)
{
s->top=-1;
}
int EMPTY(seqstack *s)
{
if(s->top>=0) return 0;
else return 1;
}
int PUSH(seqstack *s,datatype x)
{
if(s->top==maxsize-1){printf("overflow");}
else
{
s->top++;
s->data[s->top]=x;
l=s->top;
}
return l;
}
int POP(seqstack *s)
{
if(EMPTY(s))
{printf("underflow"); return NULL;}
else{
s->top--;
l=s->top;
//return (s->data[s->top+1]);
}
return l;
}
datatype TOP(seqstack *s)
{
if(EMPTY(s))
{printf("stack is empty ");
return NULL;} //空栈
else
return (s->data[s->top]);
}
int LL1()
{
char c;
if(g.data[g.top]==a[save_j])
{save_j++;
POP(&g);
return save_j;}
else if(a[save_j]=='i') ////i--id;t--T';e--E'
{
c=TOP(&g);
if(c=='E')
{
POP(&g);
PUSH(&g,'e');
PUSH(&g,'T');
}
if(c=='T')
{POP(&g);
PUSH(&g,'t');
PUSH(&g,'F');
}
if(c=='F')
{POP(&g);
PUSH(&g,'i');
}
}
else if(a[save_j]=='+')
{
c=TOP(&g);
if(c=='e')
{
POP(&g);
PUSH(&g,'e');
PUSH(&g,'T');
PUSH(&g,'+');
}
if(c=='t')
{
POP(&g);
}
}
else if(a[save_j]=='*')
{
c=TOP(&g);
if(c=='t')
{ POP(&g);
PUSH(&g,'t');
PUSH(&g,'F');
PUSH(&g,'*');
}
}
else if(a[save_j]=='(')
{
c=TOP(&g);
if(c=='E')
{POP(&g);
PUSH(&g,'e');
PUSH(&g,'T');
}
if(c=='T')
{ POP(&g);
PUSH(&g,'t');
PUSH(&g,'F');
/// PUSH(&g,'*');
}
if(c=='F')
{ POP(&g);
PUSH(&g,')');
PUSH(&g,'E');
PUSH(&g,'(');
}
}
else if(a[save_j]==')')
{
c=TOP(&g);
if(c=='e')
{
POP(&g);
}
if(c=='t')
{
POP(&g);
}
}
else if(a[save_j]=='$')
{
c=TOP(&g);
if(c=='e')
{
POP(&g);
}
if(c=='t')
{
POP(&g);
}
}
else cout<<"此语法错误"<<endl;
return 0;
}
void st()
{
//SETNULL(&g);
if(EMPTY(&g))
{PUSH(&g,'$');
PUSH(&g,'E');
}
}
void main()
{
SETNULL(&g);
Input();
st();
for(int j=0;j<=l;j++)
{
cout<<g.data[j];
}
cout<<" ";
for(int i=save_j;i<=save_i;i++)
{
cout<<a[i];
}
cout<<endl;
while(!(g.data[g.top]=='$'&&a[save_j]=='$')&&!(a[save_j]=='i'&&a[save_j+1]=='i')&&!(a[save_j]=='+'&&a[save_j+1]=='*')&&!(a[save_j+1]=='+'&&a[save_j]=='*')&&!(g.data[g.top]==')'&&a[save_j]=='$'))
{
//while(!(g.data[g.top]=='$'&&a[save_j]=='$'))
{
LL1();
for(int j=0;j<=l;j++)
{
cout<<g.data[j];
}
cout<<" ";
for(int i=save_j;i<=save_i;++i)
{
cout<<a[i];
}
cout<<endl;
}
}
//cout<<"此处错误不应该出现双运算符"<<endl;
}
#8
谢谢!真心的谢谢帮忙.
#1
程序名称: LL(1)文法分析程序
http://www.pudn.com/downloads56/sourcecode/chinese/detail199479.html
http://www.pudn.com/downloads56/sourcecode/chinese/detail199479.html
#2
LL1 文法分析的实现
http://www.yuanma.org/data/2006/0720/article_1174.htm
http://www.yuanma.org/data/2006/0720/article_1174.htm
#3
LL(1)文法及分析表的自动构造
http://scholar.ilib.cn/Abstract.aspx?A=shdegydxxb200102007
http://scholar.ilib.cn/Abstract.aspx?A=shdegydxxb200102007
#4
LL1算法
点击数:229 发布日期:2006-5-12 18:15:00
http://www.programfan.com/blog/article.asp?id=13961
#5
真心的谢谢两位!
不过这些网页怎么都打不开?
不过这些网页怎么都打不开?
#6
to 楼主:
http://www.programfan.com/blog/article.asp?id=13961
这个能打开~
http://www.programfan.com/blog/article.asp?id=13961
这个能打开~
#7
//文法是:E->TE' E'->+TE' T->FT' T'->+ET' F->(E)/i
#include <iostream.h>
#include <stdlib.h>
#include <string.h>
#include<stdio.h>
typedef char datatype;
#define maxsize 100
int save_i=0;
int save_j=0;
int l=0;
int w;
char a[10];
typedef struct
{
datatype data[maxsize];
int top;
}seqstack;
seqstack g;
void Input()//输入流
{
int i=-1;
char c;
c=getchar();
while(c!='#')
{
a[++i]=c;
c=getchar();
}
a[++i]='$';
save_i=i;
}
void SETNULL(seqstack *s)
{
s->top=-1;
}
int EMPTY(seqstack *s)
{
if(s->top>=0) return 0;
else return 1;
}
int PUSH(seqstack *s,datatype x)
{
if(s->top==maxsize-1){printf("overflow");}
else
{
s->top++;
s->data[s->top]=x;
l=s->top;
}
return l;
}
int POP(seqstack *s)
{
if(EMPTY(s))
{printf("underflow"); return NULL;}
else{
s->top--;
l=s->top;
//return (s->data[s->top+1]);
}
return l;
}
datatype TOP(seqstack *s)
{
if(EMPTY(s))
{printf("stack is empty ");
return NULL;} //空栈
else
return (s->data[s->top]);
}
int LL1()
{
char c;
if(g.data[g.top]==a[save_j])
{save_j++;
POP(&g);
return save_j;}
else if(a[save_j]=='i') ////i--id;t--T';e--E'
{
c=TOP(&g);
if(c=='E')
{
POP(&g);
PUSH(&g,'e');
PUSH(&g,'T');
}
if(c=='T')
{POP(&g);
PUSH(&g,'t');
PUSH(&g,'F');
}
if(c=='F')
{POP(&g);
PUSH(&g,'i');
}
}
else if(a[save_j]=='+')
{
c=TOP(&g);
if(c=='e')
{
POP(&g);
PUSH(&g,'e');
PUSH(&g,'T');
PUSH(&g,'+');
}
if(c=='t')
{
POP(&g);
}
}
else if(a[save_j]=='*')
{
c=TOP(&g);
if(c=='t')
{ POP(&g);
PUSH(&g,'t');
PUSH(&g,'F');
PUSH(&g,'*');
}
}
else if(a[save_j]=='(')
{
c=TOP(&g);
if(c=='E')
{POP(&g);
PUSH(&g,'e');
PUSH(&g,'T');
}
if(c=='T')
{ POP(&g);
PUSH(&g,'t');
PUSH(&g,'F');
/// PUSH(&g,'*');
}
if(c=='F')
{ POP(&g);
PUSH(&g,')');
PUSH(&g,'E');
PUSH(&g,'(');
}
}
else if(a[save_j]==')')
{
c=TOP(&g);
if(c=='e')
{
POP(&g);
}
if(c=='t')
{
POP(&g);
}
}
else if(a[save_j]=='$')
{
c=TOP(&g);
if(c=='e')
{
POP(&g);
}
if(c=='t')
{
POP(&g);
}
}
else cout<<"此语法错误"<<endl;
return 0;
}
void st()
{
//SETNULL(&g);
if(EMPTY(&g))
{PUSH(&g,'$');
PUSH(&g,'E');
}
}
void main()
{
SETNULL(&g);
Input();
st();
for(int j=0;j<=l;j++)
{
cout<<g.data[j];
}
cout<<" ";
for(int i=save_j;i<=save_i;i++)
{
cout<<a[i];
}
cout<<endl;
while(!(g.data[g.top]=='$'&&a[save_j]=='$')&&!(a[save_j]=='i'&&a[save_j+1]=='i')&&!(a[save_j]=='+'&&a[save_j+1]=='*')&&!(a[save_j+1]=='+'&&a[save_j]=='*')&&!(g.data[g.top]==')'&&a[save_j]=='$'))
{
//while(!(g.data[g.top]=='$'&&a[save_j]=='$'))
{
LL1();
for(int j=0;j<=l;j++)
{
cout<<g.data[j];
}
cout<<" ";
for(int i=save_j;i<=save_i;++i)
{
cout<<a[i];
}
cout<<endl;
}
}
//cout<<"此处错误不应该出现双运算符"<<endl;
}
#include <iostream.h>
#include <stdlib.h>
#include <string.h>
#include<stdio.h>
typedef char datatype;
#define maxsize 100
int save_i=0;
int save_j=0;
int l=0;
int w;
char a[10];
typedef struct
{
datatype data[maxsize];
int top;
}seqstack;
seqstack g;
void Input()//输入流
{
int i=-1;
char c;
c=getchar();
while(c!='#')
{
a[++i]=c;
c=getchar();
}
a[++i]='$';
save_i=i;
}
void SETNULL(seqstack *s)
{
s->top=-1;
}
int EMPTY(seqstack *s)
{
if(s->top>=0) return 0;
else return 1;
}
int PUSH(seqstack *s,datatype x)
{
if(s->top==maxsize-1){printf("overflow");}
else
{
s->top++;
s->data[s->top]=x;
l=s->top;
}
return l;
}
int POP(seqstack *s)
{
if(EMPTY(s))
{printf("underflow"); return NULL;}
else{
s->top--;
l=s->top;
//return (s->data[s->top+1]);
}
return l;
}
datatype TOP(seqstack *s)
{
if(EMPTY(s))
{printf("stack is empty ");
return NULL;} //空栈
else
return (s->data[s->top]);
}
int LL1()
{
char c;
if(g.data[g.top]==a[save_j])
{save_j++;
POP(&g);
return save_j;}
else if(a[save_j]=='i') ////i--id;t--T';e--E'
{
c=TOP(&g);
if(c=='E')
{
POP(&g);
PUSH(&g,'e');
PUSH(&g,'T');
}
if(c=='T')
{POP(&g);
PUSH(&g,'t');
PUSH(&g,'F');
}
if(c=='F')
{POP(&g);
PUSH(&g,'i');
}
}
else if(a[save_j]=='+')
{
c=TOP(&g);
if(c=='e')
{
POP(&g);
PUSH(&g,'e');
PUSH(&g,'T');
PUSH(&g,'+');
}
if(c=='t')
{
POP(&g);
}
}
else if(a[save_j]=='*')
{
c=TOP(&g);
if(c=='t')
{ POP(&g);
PUSH(&g,'t');
PUSH(&g,'F');
PUSH(&g,'*');
}
}
else if(a[save_j]=='(')
{
c=TOP(&g);
if(c=='E')
{POP(&g);
PUSH(&g,'e');
PUSH(&g,'T');
}
if(c=='T')
{ POP(&g);
PUSH(&g,'t');
PUSH(&g,'F');
/// PUSH(&g,'*');
}
if(c=='F')
{ POP(&g);
PUSH(&g,')');
PUSH(&g,'E');
PUSH(&g,'(');
}
}
else if(a[save_j]==')')
{
c=TOP(&g);
if(c=='e')
{
POP(&g);
}
if(c=='t')
{
POP(&g);
}
}
else if(a[save_j]=='$')
{
c=TOP(&g);
if(c=='e')
{
POP(&g);
}
if(c=='t')
{
POP(&g);
}
}
else cout<<"此语法错误"<<endl;
return 0;
}
void st()
{
//SETNULL(&g);
if(EMPTY(&g))
{PUSH(&g,'$');
PUSH(&g,'E');
}
}
void main()
{
SETNULL(&g);
Input();
st();
for(int j=0;j<=l;j++)
{
cout<<g.data[j];
}
cout<<" ";
for(int i=save_j;i<=save_i;i++)
{
cout<<a[i];
}
cout<<endl;
while(!(g.data[g.top]=='$'&&a[save_j]=='$')&&!(a[save_j]=='i'&&a[save_j+1]=='i')&&!(a[save_j]=='+'&&a[save_j+1]=='*')&&!(a[save_j+1]=='+'&&a[save_j]=='*')&&!(g.data[g.top]==')'&&a[save_j]=='$'))
{
//while(!(g.data[g.top]=='$'&&a[save_j]=='$'))
{
LL1();
for(int j=0;j<=l;j++)
{
cout<<g.data[j];
}
cout<<" ";
for(int i=save_j;i<=save_i;++i)
{
cout<<a[i];
}
cout<<endl;
}
}
//cout<<"此处错误不应该出现双运算符"<<endl;
}
#8
谢谢!真心的谢谢帮忙.