代码拉取完成,页面将自动刷新
同步操作将从 zren/compiler-homework3 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
#include <stdio.h>
#include <ctype.h>
//#include <stack>
//E -> TE'
//E' -> +TE' | e
//T -> FT'
//T' -> *FT' | e
//F -> NUM | (E)
//FIRST(E) = {NUM, LEFTB}
//FIRST(E') = {PLUS, e}
//FIRST(T) = {NUM, LEFTB}
//FIRST(T') = {MULT, e}
//FIRST(F) = {NUM, LEFTB}
//FOLLOW(E) = {$, RIGHTB}
//FOLLOW(E') = {$, RIGHTB}
//FOLLOW(T) = {PLUS, RIGHTB, $}
//FOLLOW(T') = {PLUS, RIGHTB, $}
//FOLLOW(F) = {MULT, PLUS, RIGHTB, $}
enum TOKEN {PLUS, MULT, NUM, END, ERR, LEFTB, RIGHTB};
char lookahead;
enum TOKEN token;
int num;
int nums=0;
int error = 0;
FILE *fp;
//stack<int > nums;
void lexical(FILE *fp);
int match(enum TOKEN x);
int E();
int E_();
int T();
int T_();
int F();
//return 0 if success, return 1 otherwise
int match(enum TOKEN x) {
if(token == x) {
lexical(fp);
return 0;
} else {
error = 1;
return 1;
}
}
//E -> TE'
int E() {
//printf("%c \n",lookahead);
//printf("E -> TE' \n");
return T()+E_();
//return 0;
}
//E_ -> +TE'|e
int E_() {
if(lookahead=='+')
{
//printf("E_ -> +TE'|e \n");
match(PLUS);
int a=T()+E_();
//printf("T()+E()= %d \n",a);
return a;
}
else
{
return 0;
//error=1;
}
return 0;
}
//T -> FT'
int T() {
//printf("%c \n",lookahead);
//printf("T= FT' \n");
//F();
int a= F()*T_();
//printf("F()*T_() = %d\n",a);
return a;
//return 0;
}
//T' -> *FT' | e
int T_() {
//printf("%c \n",lookahead);
if(lookahead=='*'){
match(MULT);
//printf("T '= *FT '|e \n");
int a=F()*T_();
//printf("F()*T_() = %d \n",a);
return a;
}
else
{
//error=0;
return 1;
}
//return 0;
}
//F -> NUM | (E)
int F() {
if(num!=0){
//printf("F = NUM =%d \n",num);
int a=num;
match(NUM);
return a;
}
else if(lookahead=='(')
{
//printf("F= (E) \n");
match(LEFTB);
//printf("lookahead =%c \n",lookahead);
int a=E();
//printf("(E) = %d\n",a);
match(RIGHTB);
return a;
}
else
{
error=1;
}
return 0;
}
void lexical(FILE *fp) {
lookahead = getc(fp);
num = 0;
while(lookahead == ' ' || lookahead == '\t' || lookahead == '\n') {
lookahead = getc(fp);
}
if(isdigit(lookahead)) {
num = 0;
while(isdigit(lookahead)) {
token = NUM;
num = num * 10 + lookahead - '0';
//printf("rrr %c ",lookahead);
lookahead = getc(fp);
}
//printf("ddd %c ",lookahead);
ungetc(lookahead, fp);
//getc(fp);
//printf("sss %c ",lookahead);
return;
} else if (lookahead == '+') {
token = PLUS;
return;
} else if (lookahead == '*') {
token = MULT;
return;
} else if(lookahead == EOF) {
token = END;
return;
}else if(lookahead == '(') {
token = LEFTB;
return;
} else if(lookahead == ')') {
token = RIGHTB;
return;
} else {
error = 1;
token = ERR;
return;
}
}
int main(int argc, char **argv) {
int res = 0;
if(argc < 2) {
printf("insufficient arguments");
return 1;
}
fp = fopen(argv[1], "r");
lexical(fp);
res = E();
if(!error) {
printf("%d\n", res);
} else {
printf("There are errors\n");
}
fclose(fp);
return 0;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。