编译原理上机实验程序.docVIP

  • 25
  • 0
  • 约3.49千字
  • 约 6页
  • 2018-01-02 发布于河南
  • 举报
编译原理上机实验程序

程序源代码: #includestdio.h #includestdlib.h #includestring.h #includectype.h #define LEN sizeof(struct Node) struct Node { char data; struct Node *next; }; struct Node *head,*p; char ch; char *key[7]={main,int,char,if,else,for,while}; char token[20]; void getch() { ch=p-data; p=p-next; } void output(struct Node *head) { if(!head) { printf(Something wrong with head node!); exit(1); } p=head-next; while(p-next!=NULL) { printf(%c,p-data); p=p-next; } printf(\n); } void getbc() { while(ch==) getch(); } void concat() { unsigned int i; i=strlen(token); token[i]=ch; token[i+1]=\0; } int letter(char ch) { return isalpha((int)ch); } int digit(char ch) { return isdigit((int)ch); } int reserve() { int k; for(k=0;k7;k++) { if(strcmp(key[k],token)==0) return(k+1); } return 10; } void retract() { struct Node *Q; Q=head-next; while(Q-next!=p) Q=Q-next; p=Q; } void back(int a,char *b) { printf((%d,%s)\n,a,b); } void scaner() { int c; token[0]=NULL; getch(); getbc(); if(letter(ch)) { while(letter(ch)||digit(ch)) { concat(); getch(); } retract(); c=reserve(); if(c!=10) back(c,token); else back(10,token); } else if(digit(ch)) { while(digit(ch)) { concat(); getch(); } retract(); printf((20,%d)\n,atoi(token)); } else switch(ch) { case+: back(22,+); break; case-: back(23,-); break; case*: back(24,*); break; case/: back(25,/); break; case(: back(26,(); break; case): back(27,)); break; case{: back(28,{); break; case}: back(29,}); break; case,: back(30,,); break; case;: back(31,:); break; case:: back(32,:); break; case=: getch(); if(ch===) back(37,==); else { retract(); back(21,=); } break; case: getch(); if(ch===) back(35,=); el

文档评论(0)

1亿VIP精品文档

相关文档