-
Notifications
You must be signed in to change notification settings - Fork 0
/
express.cpp
79 lines (75 loc) · 1.64 KB
/
express.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include "express.h"
#include "support.h"
#include "statement.h"
extern int symbol;
//表达式
//<表达式>::=[+|-]<项>{<加法运算符><项>}
AST_node express(AST_node parent)
{
AST_node t = makeNode(EXPRESSION, parent);
printf("----------------EXPRESSION BEGINS--------------\n");
match(MINUS,t);
match(PLUS,t);
term(t);
while (match(MINUS,t) || match(PLUS,t))
{
term(t);
}
printf("----------------EXPRESSION END--------------\n");
return t;
}
//项
//<项>::=<因子>{<乘法运算符><因子>}
AST_node term(AST_node parent)
{
AST_node t = makeNode(TERM, parent);
factor(t);
while (match(TIMES,t) || match(SLASH,t))
{
factor(t);
}
return t;
}
//因子
//<因子>::=<标识符>|<标识符>'['<表达式>']'|<无符号整数>|'('<表达式>')'|<函数调用语句>
AST_node factor(AST_node parent)
{
AST_node t = makeNode(FACTOR, parent);
LexType type = (LexType)0;
switch (symbol)
{
case NUM: match(NUM, t); type = INT; break;
case IDENT:
match(IDENT,t);
if (match(LBRACKET,t))
{
//<标识符>'['<表达式>']'
express(t);
if (!match(RBRACKET,t))
{
error("Missing right bracket");
recovery(7, TIMES, SLASH, MINUS, PLUS, END, SEMICOLON, THEN);
}
}
else if (symbol == LPARENT)
{
//函数调用语句
//<函数调用语句>::=<标识符>[<实在参数表>]
arg_list(t);
}
break;
case LPARENT:
match(LPARENT,t);
express(t);
if (!match(RPARENT,t))
{
error("Missing right parenthesis");
recovery(7, TIMES, SLASH, MINUS, PLUS, END, SEMICOLON, THEN);
}
break;
default:
error("Not a factor");
recovery(8, TIMES, SLASH, MINUS, PLUS, END, SEMICOLON, THEN, RPARENT);
}
return t;
}