📄 fenxi.cpp
字号:
#include <stdio.h>#include <ctype.h>#define MAX 255int stack[MAX], sp = 0, count = 0, con = 0; int main(int argc, char *argv[]){ void push (int); int pop (void); int getop (void); int *identify_string(void); int yy_pushtab[13][4] = { {257,1,258,0}, {256,0}, {0}, {260,259,0}, {0}, {260,259,2,0}, {0}, {262,261,0}, {262,261,3,0}, {0}, {5,258,4,0}, {6,0}, {256,0} }; int yy_d[8][7] = { {-1, 0, -1, -1, 0,-1, 0}, {2, 1, -1, -1, 1, -1, 1}, {-1,4,-1,-1,3,4,3}, {-1,-1,-1,-1,7,-1,7}, {-1,6,5,-1,-1,6,-1}, {-1,-1,-1,-1,10,-1,11}, {-1,9,9,8,-1,9,-1}, {-1,12,-1,-1,12,-1,12} }; int what, *p, c, i; p=identify_string(); if(con==0) { push(263); printf("No. stack\t\twhat_to_do\n------------\n"); while (sp > 0) { if ((c=getop()) >= 0 && c <=6){ if (c != *p) { printf("error: grammar error 1\n", c); break; } else { printf("%2d ", ++count); for(i=0;i<sp;i++) printf("%d ",stack[i]); printf("\n"); pop(); p++; } } else { what = yy_d[(getop()-256)][*p]; if (what == -1) { printf("error: grammar error 2\n"); break; } else { printf("%2d ", ++count); for(i=0;i<sp;i++) printf("%d ",stack[i]); printf("\t\t%d", what); printf("\n"); pop(); i=0; while(yy_pushtab[what][i] != 0){ push(yy_pushtab[what][i++]); } } } } } return 0;}void push(int val){ if (sp < MAX) stack[sp++] = val; else printf("error:push stack full, can't push %d\n", val);}int pop(void){ if (sp > 0) return stack[--sp]; else { printf("error:pop stack empty\n"); return 0; }}int getop(void){ if (sp > 0) return stack[sp-1]; else { printf("error:getop stack empty\n"); return 0; }}int *identify_string(void){ char ch, buf[MAX], *pbuf; int in[MAX], *pin, k=0, kk,i; printf("input string: "); while((buf[k++]=getchar()) != '\n') ; buf[k] = '\0'; buf[--k] = '#'; pbuf = buf; k = 0; while(*pbuf != '\0') {
if(isdigit(*pbuf)) { while(isdigit(*++pbuf)) ; pbuf--; in[k++] = 6; } else if(*pbuf==')') in[k++] = 5; else if(*pbuf=='(') in[k++] = 4; else if(*pbuf=='*') in[k++] = 3; else if(*pbuf=='+') in[k++] = 2; else if(*pbuf==';') in[k++] = 1; else if(*pbuf=='#') in[k++] = 0; else if(*pbuf=='\n') ; else { printf("error: %c is unknown.\n", *pbuf); con = 1; } pbuf++; } for(kk=0;kk<k && con==0;kk++) printf("in[%d]=%d\n",kk,in[kk]); pin = in; return pin; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -