?? 語法分析.c
字號:
#include "lex.yy.c"
void begindo();
int fcx();//分程序
int blsm();//變量說明
int blsmb();//變量說明表
int yjb();//語句表
int initstal();//下個單詞
int lx();//類型
int yj();//語句
int fzyj();//賦值語句
int tjyj(); //條件語句
int xhyj();//循環語句
int fhyj();//復合語句
int ssbds(); //算術表達式
int blb(); ///變量表
int xiang(); //項
int gxbds(); //關系表達式
int ys(); //因式
void finderror();
void showerror(char *);
char * error[50];
///構造一個堆棧
typedef struct {
int * base;
int * top;
int size;
}stack;
stack next; ///定義一個輸入序列堆棧
int printstack(stack s){
int e;
while(s.top!=s.base)
{ e=* s.base++;
printf("%d ",e);}
return 1;
}
int pop(stack s){
int e;
if(next.top==next.base)
return 0;
e=*next.base++;
printf("這里讀取了一個單詞 %d \n",e);
return e;
}
///堆棧定義結束
void main(int args,char * * argv)
{
int c=0;
if(args>=2)
{
if((yyin=fopen(argv[1],"r"))==NULL)
{
printf("can't open file %s",argv[1]);
exit(0);
}
}
next.base=(int *)malloc(1000*sizeof(int));
next.top=next.base;
initstal();
printstack(next);
printf("\n");
begindo();
return;
}
void begindo(){ //主程序開始
if(pop(next)==PROGRAM) //1
if(pop(next)==NAME) //2
if(pop(next)==SEMI) //3
if(fcx())
printf("successful................\n");
else
showerror("分程序錯誤");
else
showerror("開始時少一個");
else
showerror("少程序名");
else
showerror("沒有以PROGRAM開頭");
}
int fcx(){ ///分程序開始
if(blsm())
{
printf("分析到變量說明正確\n");
if(pop(next)==1)
{
printf("分析到BEGIN正確\n");
if(yjb())
{
printf("分析到語句表正確\n");
if(pop(next)==END)
return 1;
showerror("缺少end標志");
}
else
showerror("語句表出錯");
next.base--;
}
}
else
showerror("變量說明出錯");
return 0;
}
int blsm(){ //變量說明
if(pop(next)==VAR)
{
if(blsmb())
{
if(pop(next)==SEMI)
return 1;
next.base--;
showerror("缺少定義結束標志");
}
else
showerror("變量說明表出錯");
next.base--;
}
else
showerror("缺少VAR標志");
return 0;
}
int blsmb(){ //變量說明表
if(blb())
{
if(pop(next)==COLON)
{
printf("分析到COLON正確\n");
if(lx())
{
printf("分析到類型正確\n");
if(pop(next)==SEMI)
{
printf("分析到SEMI正確\n");
if(blsmb())
return 1;
next.base--;
return 1;
}
return 1;
}
next.base--;
}
}
else
showerror("變量表出錯");
return 0;
}
int blb(){ ///變量表
if(pop(next)==NAME)
{
if(pop(next)==COMMA)
if(blb())
return 1;
else
return 0;
else
{
next.base--;
return 1;
}
}
else
showerror("缺少變量名");
next.base--;
return 0;
}
int lx(){ //類型
int c;
if((c=pop(next))==INTEGER)
return 1;
if(c==REAL)
return 1;
next.base--;
return 0;
}
int yjb(){ //語句表
if(yj())
{
printf("分析到語句222222判斷正確\n");
if(pop(next)==SEMI)
{
if(yjb())
return 1;
else
return 0;
}
else
{
next.base--;
return 1;
}
return 1;
}
else
printf("分析到語句出錯\n");
return 0;
}
int yj(){ //語句
if(fzyj())
{
printf("分析到語句判斷正確\n");
return 1;
}
printf("分析到語句ok....判斷正確\n");
if(tjyj())
return 1;
if(xhyj())
return 1;
if(fhyj())
return 1;
return 0;
}
int fzyj(){ //賦值語句
if(pop(next)==NAME)
{
printf("分析到賦值語句正確\n");
if(pop(next)==GIVE)
{
printf("分析到:=正確\n");
if(ssbds())
{
printf("分析到算術表達式正確\n");
return 1;
}
next.base--;
}
}
next.base--;
return 0;
}
int tjyj(){ //條件語句
if(pop(next)==IF)
if(gxbds())
{
if(pop(next)==THEN)
if(yj())
{
if(pop(next)==ELSE)
if(yj())
return 1;
next.base--;
}
next.base--;
}
next.base--;
return 0;
}
int xhyj(){///循環語句
if(pop(next)==WHILE)
if(gxbds())
{
if(pop(next)==DO)
if(yj())
return 1;
next.base--;
}
next.base--;
return 0;
}
int fhyj(){//復合語句
if(pop(next)==1)
if(yjb())
{
if(pop(next)==END)
return 1;
next.base--;
}
next.base--;
return 0;
}
int ssbds(){ //算術表達式
if(xiang())
return 1;
if(ssbds())
{
int k=pop(next);
if(k==PLUS||k==MINUS)
if(xiang())
return 1;
next.base--;
}
return 0;
}
int xiang(){ //項
if(ys())
return 1;
if(xiang())
{
int k=pop(next);
if((k==STAR)||(k==DIVOP))
if(ys())
return 1;
next.base--;
}
return 0;
}
int ys(){ //因式
if(pop(next)==NAME)
return 1;
next.base--;
if(pop(next)==ICON)
return 1;
next.base--;
if(ssbds())
return 1;
return 0;
}
int gxbds(){ //關系表達式
if(ssbds())
{
int k=pop(next);
if(k>=15&&k<=20)
if(ssbds())
return 1;
next.base--;
}
return 0;
}
int initstal(){
int c;
while(c=yylex()){
if(c<200)
{
*next.top++=c;
printf("%d ",c);
}
}
printf("\n");
return 1;
}
void showerror(char * error){
next.base--;
printf(" %s %d \n",error,pop(next));
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -