1 Star 0 Fork 70

zren/compiler-homework4

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
lrparse.c 3.09 KB
一键复制 编辑 原始数据 按行查看 历史
Zhilei Ren 提交于 2016-05-18 17:19 . typo fixed
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
char lookahead;
enum TERMINAL {
ID,
ADD,
MUL,
LBR,
RBR,
END,
ERR
};
enum NON_TERMINAL {
E,
T,
F
};
enum ACTION_TYPE {
SHIFT,
REDUCE,
ERROR,
ACC
};
#define NUM_TERMINAL 6
#define NUM_NONTERMINAL 3
#define NUM_STATES 12
struct action{
enum ACTION_TYPE type;
int id;
} actionTable[NUM_STATES][NUM_TERMINAL];
int gotoTable[NUM_STATES][NUM_NONTERMINAL];
int stackPtr;
int stack[1024];
void push(int state) {
stack[stackPtr] = state;
stackPtr ++;
}
void pop() {
stackPtr --;
}
int top() {
return stack[stackPtr - 1];
}
void init() {
int i, j;
//stack
stackPtr = 0;
push(0);
//action and goto tables
for(i = 0; i < NUM_STATES; i ++) {
for(j = 0; j < NUM_TERMINAL; j ++) {
actionTable[i][j].type = ERROR;
}
for(j = 0; j < NUM_NONTERMINAL; j ++) {
gotoTable[i][j] = -1;
}
}
//fill in the goto table
gotoTable[0][E] = 1;
gotoTable[0][T] = 2;
gotoTable[0][F] = 3;
//... your turn
//fill in the action table
actionTable[2][ADD].type = REDUCE;
actionTable[2][ADD].id = 2;
actionTable[2][MUL].type = SHIFT;
actionTable[2][MUL].id = 7;
actionTable[2][RBR].type = REDUCE;
actionTable[2][RBR].id = 2;
actionTable[2][END].type = REDUCE;
actionTable[2][END].id = 2;
//... your turn
}
enum TERMINAL lexical(FILE *fp) {
lookahead = getc(fp);
while(lookahead == ' ' || lookahead == '\t' || lookahead == '\n') {
lookahead = getc(fp);
}
if(isdigit(lookahead)) {
while(isdigit(lookahead)) {
lookahead = getc(fp);
}
ungetc(lookahead, fp);
return ID;
} else if (lookahead == '+') {
return ADD;
} else if (lookahead == '*') {
return MUL;
} else if(lookahead == EOF) {
return END;
}else if(lookahead == '(') {
return LBR;
} else if(lookahead == ')') {
return RBR;
} else {
return ERR;
}
}
void parse(enum TERMINAL *input) {
int currentState = 0;
int index = 0;
while(1) {
printf("currentState: %d\n", currentState);
switch(actionTable[currentState][input[index]].type) {
case SHIFT:
//how to shift
break;
case REDUCE:
switch(actionTable[currentState][input[index]].id) {
case 1:
//reduce by production 1
break;
case 2:
//reduce by production 2
break;
case 3:
//reduce by production 3
break;
case 4:
//reduce by production 4
break;
case 5:
//reduce by production 5
break;
case 6:
//reduce by production 6
break;
default:
printf("ERROR REDUCE\n");
exit(-1);
break;
}
break;
case ERROR:
printf("ERROR ACTION\n");
exit(-1);
break;
case ACC:
if(input[index] == END) {
printf("ACC\n");
return;
} else {
printf("ERROR ACC\n");
exit(-1);
}
break;
}
}
}
int main(int argc, char **argv) {
enum TERMINAL input[1024];
int inputIndex = 0;
FILE *fp;
enum TERMINAL temp;
if(argc < 2) {
printf("insufficient arguments");
return 1;
}
fp = fopen(argv[1], "r");
while((temp = lexical(fp)) != END) {
input[inputIndex++] = temp;
}
input[inputIndex] = END;
fclose(fp);
init();
parse(input);
return 0;
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C
1
https://gitee.com/zren/compiler-homework4.git
git@gitee.com:zren/compiler-homework4.git
zren
compiler-homework4
compiler-homework4
master

搜索帮助