#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <stack>
using namespace std
struct Node1
{
char vn
char vt
char s[10]
}MAP[20]//存储分析预测表每个位置对应的终结符,非终结符,产生式
int k
//用R代表E',W代表T',e代表空
char G[10][10]={"E->TR","R->+TR","R->e","T->FW","W->*FW","W->e","F->(E)","F->i"}//存储文法中的产吵唯生式
char VN[6]={'E','R','T','W','F'}//存储非终结符
char VT[6]={'i','+','*','(',')','#'}//存储终结符
char SELECT[10][10]={"(,i","+","),#","雹碰败(,i","*","+,),#","(","i"}//存储文法中每个产生式对应的SELECT集
char Right[10][8]={"->TR","->+TR","->e","->FW","->*FW","->e","->(E)","->i"}
stack <char>stak,stak1,stak2
bool compare(char *a,char *b)
{
int i,la=strlen(a),j,lb=strlen(b)
for(i=0i<lai++)
for(j=0j<lbj++)
{
if(a[i]==b[j])
return 1
}
return 0
}
char *Find(char vn,char vt)
{
int i
for(i=0i<ki++)
{
if(MAP[i].vn==vn &&MAP[i].vt==vt)
return MAP[i].s
}
return "error"
}
char * Analyse(char * word)
{
char p,action[10],output[10]
int i=1,j,l=strlen(word),k=0,l_act,m
while(!stak.empty())
stak.pop()
stak.push('#')
stak.push('E')
printf("________________________________________________________________________________\n")
printf("\n对符号串%s的分析过程\n",word)
printf("步骤栈顶元素剩余输入串推到所用产生式或匹配\n")
p=stak.top()
while(p!='#')
{
printf("%7d "源颤,i++)
p=stak.top()
stak.pop()
printf("%6c ",p)
for(j=k,m=0j<lj++)
output[m++]=word[j]
output[m]='\0'
printf("%10s",output)
if(p==word[k])
{
if(p=='#')
{
printf("接受\n")
return "SUCCESS"
}
printf(" “%c”匹配\n",p)
k++
}
else
{
strcpy(action,Find(p,word[k]))
if(strcmp(action,"error")==0)
{
printf("没有可用的产生式\n")
return "ERROR"
}
printf("%c%s\n",p,action)
int l_act=strlen(action)
if(action[l_act-1]=='e')
continue
for(j=l_act-1j>1j--)
stak.push(action[j])
}
}
if(strcmp(output,"#")!=0)
return "ERROR"
}
int main ()
{
freopen("in.txt","r",stdin)
//freopen("out.txt","w",stdout)
char source[100]
int i,j,flag,l,m
printf("\n*****为了方便编写程序,用R代表E',W代表T',e代表空*****\n\n")
printf("该文法的产生式如下:\n")
for(i=0i<8i++)
printf(" %s\n",G[i])
printf("________________________________________________________________________________\n")
printf("\n该文法的SELECT集如下:\n")
for(i=0i<8i++)
{
printf(" SELECT(%s) = { %s }\n",G[i],SELECT[i])
}
printf("________________________________________________________________________________\n")
//判断是否是LL(1)文法
flag=1
for(i=0i<8i++)
{
for(j=i+1j<8j++)
{
if(G[i][0]==G[j][0])
{
if(compare(SELECT[i],SELECT[j]))
{
flag=0break
}
}
}
if(j!=8)
break
}
if(flag)
printf("\n有相同左部产生式的SELECT集合的交集为空,所以文法是LL(1)文法。\n")
else
printf("\n有相同左部产生式的SELECT集合的交集不为空,所以文法不是LL(1)文法。\n")
printf("________________________________________________________________________________\n")
//预测分析表
for(i=0,k=0i<8i++)
{
l=strlen(SELECT[i])
for(j=0j<lj+=2)
{
MAP[k].vn=G[i][0]
MAP[k].vt=SELECT[i][j]
strcpy(MAP[k].s,Right[i])
k++
}
}
printf("\n表达式文法的预测分析表如下:\n\n")
printf(" ")
for(i=0i<6i++)
printf("%10c",VT[i])
printf("\n")
for(i=0i<5i++)
{
printf("---------------------------------------------------------------\n")
printf("%10c",VN[i])
for(j=0j<6j++)
{
for(m=0m<km++)
{
if(VN[i]==MAP[m].vn &&VT[j]==MAP[m].vt)
{
printf("%10s",MAP[m].s)
break
}
}
if(m==k)
printf(" ")
}
printf("\n")
}
/*预测分析程序
Analyse函数*/
//输入源文件串
while(cin>>source)
{
printf("\n分析结果:%s\n\n",Analyse(source))
}
while(1)
return 0
}
#include <iostream>using namespace std
#define m 45
#define n 100
#define t 10
int main()
{
FILE *fp
char filename[20],c[n]
printf("Type the file name which you want to open:")
scanf("%s",&filename)
fp=fopen(filename,"r+")/*以r-只读方式打开指定文件*/
if((fp=fopen(filename,"r"))==NULL) /*文件不存在输出错误*/
{cout<<"文件不存在!"<<endlexit(-1)}
cout<<"文件中内容如下:"<<endl
for(int j=0!feof(fp)j++){
c[j]=fgetc(fp)//从流中读取字符
}
char keyword[m][t]={"include","int","string","cout","cin","auto","break","case","char","class","const",
"continue","default","delete","do","double","else","enum","extern","float","for","friend","if","inline",
"int","long","new","operator","private","protected","public","register","return","short","sizeof","static",
"struct","switch","template","this","typedef","union","virtual","void","while"}//关键字数组
char a[t],*p=c,*q=a,*s=a
bool w=0,r=0
int i=0
for(i=0i<10i++)a[i]=NULL//初始化临时数组
while (*p !=NULL){
q=s=a
if((*p>='a'&&*p<='z')||(*p>='A'&&*p<='Z')||*p=='_'){//识别标识符
*q=*pp++q++
while ((*p>='a'&&*p<='z')||(*p>='A'&&*p<='Z')||(*p>='0'&&*p<='9')||*p=='_'){
*q=*pp++q++
}
for(i=0i<mi++)if(strcmp(keyword[i],a)==0){r=1break}
if(r==1){cout<<"关键字为:"r=0}
else cout<<"标识符为:"
while(s!=q){
cout<<*s
s++
}
cout<<endl
for(i=0i<ti++)a[i]=NULL
}
else if(*p=='\''){//识辩盯缺别字符常量
p++
while(*p!='\''){
*q=*p
p++
q++
}
cout<<"字符常量为:"
while(s!=q){
cout<<*s
s++
}
cout<<endl
for(i=0i<ti++)a[i]=NULL
p++
}
else if(*p=='\"'){//识别字符串常量
p++
while(*p!='\"'){
*q=*p
p++
q++
}
cout<<"字符串常量为:"
while(s!=q){
cout<携辩<*s
s++
}
cout<<endl
for(i=0i<ti++)a[i]=NULL
p++
}
else if(*p=='+'||*p=='-'||*p=='*'||*p=='/'||*p=='='||*p=='%'||*p=='/'){//识别运算符
cout<<"运算符为:"<<*p
cout<<endl
p++
}
else if(*p==''||*p==','){//识别分解符
cout<<"分界符为:"<<*p
cout<<则升endl
p++
}
else if(*p>='0'&&*p<='9'){
s=q=a
*q=*pp++q++
while(*p>='0'&&*p<='9'||*p=='.'){
*q=*pp++q++
}
while(s!=q){
if(*s=='.'){w=1break}//识别实型常量
s++
}
s=a
if(w ==1){
cout<<"实型常量为:"
while(s!=q){
cout<<*s
s++
}
for(i=0i<ti++)a[i]=NULL
}
else {
cout<<"整型常量为:"
while(s!=q){//识别整型常量
cout<<*s
s++
}
for(i=0i<ti++)a[i]=NULL
}
cout<<endl
}
else p++
}
return 0
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)