編譯原理 語法分析

2023-01-17 20:51:05 字數 2608 閱讀 4795

#include

#include

#include

#include

using namespace std;

#define maxn 20000

int syn,p,sum,kk,m,n,row,error;

double dsum,pos;

char index[800],len;//記錄指數形式的浮點數

char r[6][10]=;

char token[maxn],s[maxn];

char ch;

bool is_letter(char c)

bool is_digtial(char c)

bool is_dot(char c)

void identifier()//標示符的判斷

token[m]='\0';

ch=s[--p];

syn=10;

for(n=0;n<6;n++)

if(strcmp(token,r[n])==0)

}void digit(bool positive)//數字的判斷

if(ch=='.')

if(ch=='e')

if(!(ch>='0' && ch<='9'))

else}}

if(syn==-1 || (ch>='a' && ch<='z') || ch=='.')

else

}else

else}}

void to_double()

printf(")\n");

}bool check_behind_digit()

return false;

}bool check_pre_prog()

i--;

}return true;

}void pot()

else if(is_digtial(ch))

else

else syn=20;

token[len]=0;

}else if(ch=='>')

else syn=23;

token[len]=0;

}else if(chlen=0,token[len++]=ch;

if(s[p+1syn=25;

token[len++]=s[p+1];

p++;

}else syn=18;

token[len]=0;

}else if(chlen=0,token[len++]=ch;

if(s[p+1syn=22;

token[len++]=s[p+1];

p++;

}else syn=-1;

}else if(chif(check_behind_digit() && check_pre_prog())

else

}else if(chif(check_behind_digit() && check_pre_prog())

else

}else if(ch=='*') syn=15,strcpy(token,"*");

else if(ch=='/') syn=16,strcpy(token,"/");

else if(ch==';') syn=26,strcpy(token,";");

else if(ch=='(') syn=27,strcpy(token,"(");

else if(ch==')') syn=28,strcpy(token,")");

else if(ch=='#') syn=0,strcpy(tokencout

else

}void lrparser()//詞法分析主要函式

}else

if(kk!=1)

if(syn==0) return;

}void lrparser()

cout<<"語義分析:";

p=0;

scaner();

lrparserint parse()//詞法分析

}while(syn!=0);

printf("詞法分析完成!共有%d errors\n",error);

return error;

}void input()

while(chint main()

{input();

parse();

lrparser();

return 0function

a=9;

x=2*4;

b=a+x;

a=0.13413e-1234;

c=+0.24e+13;

endfunc

#x=a+b*c;

endfunc

#function

x=a+b*c;

#function

a=(a+b)/c;

x=(x+y)*(a+b)+c+d*(a+c)+(a+f)+(d-g);

endfunc

#function

a=(a+b)/c;

x=(x+y)*(a+b)+c+d*(a+c)+(a+f)+(d-g);

b=(a*c+4)*9;

endfunc

編譯原理分知識點習題自下而上語法分析

1 已知文法g s s saa a a abb b b csd e 請證實aacabcbaadbed是文法的乙個句型,並寫出該句型的所有短語 素短語以及控制代碼。解 本題考查 句型 短語 控制代碼 素短語 等概念。因為存在從文法開始符號s到符號串aacabcbaadbed的推導過程 如圖6.1中的語...

語法分析器

部分核心 如下 first follow 計算first集,u xx.void first int u else else j if j p i rlength 當產生式右部都能推出空時 addfirst u,1 加入first集 void addfirst int u,int nch 當數值小於1...

語法分析上機實習題

一 對於如下的文法,試編寫除錯乙個語法分析程式 程式 program 識別符號 分程式 分程式 變數說明 begin 語句表 end.變數說明 var 變數說明表 變數說明表 變數表 型別 變數表 型別 變數說明表 型別 integer real 變數表 變數 變數 變數表 語句表 語句 語句 語句...