📄 parser.c
字号:
#include "global.h"
#include "parser.h"
#include "error.h"
#include "lexer.h"
FILE * fwparser;
void parse()
{
if ((fwparser = fopen("parser.txt", "wt")) == NULL)
{
fprintf(stderr, "can't create parser.txt");
exit(-1);
}
lookahead = lexan();
while (lookahead != DONE)
{
fprintf(fwparser, "Program->id(){Slist}\n");
match(ID);
match('(');
match(')');
match('{');
Slist();
match('}');
}
}
void Slist()
{
switch(lookahead)
{
case INT:
fprintf(fwparser, "Slist->Dlist Olist\n");
Dlist();
Olist();
break;
case ID: case IF: case WHILE:
fprintf(fwparser, "Slist->Olist\n");
Olist();
break;
default:
return;
}
}
void Dlist()
{
fprintf(fwparser, "Dlist->D Dlist'\n");
declaration();
while(1)
{
switch(lookahead)
{
case INT:
fprintf(fwparser, "Dlist'->D Dlist'\n");
declaration();
continue;
default:
fprintf(fwparser, "Dlist'->ε\n");
return;
}
}
}
void declaration()//D
{
switch(lookahead)
{
case INT:
fprintf(fwparser, "D->type id;\n");
match(INT);
match(ID);
match(';');
break;
default:
error("syntax error");
}
}
void Olist()
{
fprintf(fwparser, "Olist->O Olist'\n");
operation();
while(1)
{
switch(lookahead)
{
case ID: case IF: case WHILE:
fprintf(fwparser, "Olist'->O Olist'\n");
operation();
continue;
default:
fprintf(fwparser, "Olist'->ε\n");
return;
}
}
}
void operation()
{
switch(lookahead)
{
case ID:
fprintf(fwparser, "O->id=expression;\n");
match(ID);
match('=');
expression();
match(';');
break;
case IF:
fprintf(fwparser, "O->if(expression) {Olist}O'\n");
match(IF);
match('(');
expression();
match(')');
match('{');
Olist();
match('}');
operation2();
break;
case WHILE:
fprintf(fwparser, "O->while(expression) {Olist}\n");
match(WHILE);
match('(');
expression();
match(')');
match('{');
Olist();
match('}');
break;
default:
error("syntax error");
}
}
void operation2()
{
switch(lookahead)
{
case ELSE:
fprintf(fwparser, "O'->else{Olist}\n");
match(ELSE);
match('{');
Olist();
match('}');
break;
default:
fprintf(fwparser, "O'->ε\n");
return;
}
}
void expression()
{
fprintf(fwparser, "expression->simple_expression expression'\n");
simple_expression();
while(1)
{
switch(lookahead)
{
case RELOP:
fprintf(fwparser, "expression'->relop simple_expression\n");
match(lookahead);
simple_expression();
continue;
default:
fprintf(fwparser, "expression'->ε\n");
return;
}
}
}
void simple_expression()
{
fprintf(fwparser, "simple_expression->term simple_expression'\n");
term();
while(1)
{
switch(lookahead)
{
case ADDOP:
fprintf(fwparser, "simple_expression'->addop term simple_expression'\n");
match(lookahead);
term();
continue;
default:
fprintf(fwparser, "simple_expression'->ε\n");
return;
}
}
}
void term()
{
int t;
fprintf(fwparser, "term->factor term'\n");
factor();
while (1)
{
switch(lookahead)
{
case MULOP:
fprintf(fwparser, "term'->mulop factor term'\n");
t = lookahead;
match(lookahead);
factor();
continue;
default:
fprintf(fwparser, "term'->ε\n");
return;
}
}
}
void factor()
{
switch(lookahead)
{
case '(':
fprintf(fwparser, "factor->(expression)\n");
match('(');
expression();
match(')');
break;
case ID:
fprintf(fwparser, "factor->id\n");
match(ID);
break;
case NUM:
fprintf(fwparser, "factor->num\n");
match(NUM);
break;
case '!':
fprintf(fwparser, "factor->!factor\n");
match('!');
factor();
break;
default:
error("syntax error");
}
}
void match(int t)
{
if (lookahead == t)
lookahead = lexan();
else
{
error("syntax error");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -