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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
#include "parser.h"
#include <stdio.h>
struct ast *parse(enum parser_status *status, struct lexer *lexer)
{
struct token t = lexer_peek(lexer);
*status = PARSER_OK;
if (t.type == TOKEN_EOF)
{
lexer_pop(lexer);
return NULL;
}
else
{
struct ast *exp = parse_exp(status, lexer);
if (*status != PARSER_OK)
{
return NULL;
}
struct token t = lexer_pop(lexer);
if (*status == PARSER_OK && t.type == TOKEN_EOF)
{
return exp;
}
else if (t.type != TOKEN_EOF)
{
*status = PARSER_UNEXPECTED_TOKEN;
fprintf(stderr, "parser: unexpected token\n");
}
ast_free(exp);
return NULL;
}
}
struct ast *parse_exp(enum parser_status *status, struct lexer *lexer)
{
struct ast *left = parse_sexp(status, lexer);
if (*status == PARSER_UNEXPECTED_TOKEN)
return NULL;
struct token t = lexer_peek(lexer);
if (t.type != TOKEN_PLUS && t.type != TOKEN_MINUS)
{
*status = PARSER_OK;
return left;
}
else
{
struct ast *root = left;
do
{
left = root;
t = lexer_pop(lexer);
if (t.type == TOKEN_PLUS)
root = ast_new(AST_PLUS);
else
root = ast_new(AST_MINUS);
root->left = left;
root->right = parse_sexp(status, lexer);
if (*status == PARSER_UNEXPECTED_TOKEN)
{
ast_free(root);
return NULL;
}
t = lexer_peek(lexer);
} while (t.type != TOKEN_EOF
&& (t.type == TOKEN_PLUS || t.type == TOKEN_MINUS));
return root;
}
}
struct ast *parse_sexp(enum parser_status *status, struct lexer *lexer)
{
struct ast *left = parse_texp(status, lexer);
if (*status == PARSER_UNEXPECTED_TOKEN)
return NULL;
struct token t = lexer_peek(lexer);
if (t.type != TOKEN_MUL && t.type != TOKEN_DIV)
{
*status = PARSER_OK;
return left;
}
else
{
struct ast *root = left;
do
{
left = root;
t = lexer_pop(lexer);
if (t.type == TOKEN_MUL)
root = ast_new(AST_MUL);
else
root = ast_new(AST_DIV);
root->left = left;
root->right = parse_texp(status, lexer);
if (*status == PARSER_UNEXPECTED_TOKEN)
{
ast_free(root);
return NULL;
}
t = lexer_peek(lexer);
} while (t.type != TOKEN_EOF
&& (t.type == TOKEN_MUL || t.type == TOKEN_DIV));
return root;
}
}
struct ast *parse_texp(enum parser_status *status, struct lexer *lexer)
{
struct token t = lexer_pop(lexer);
if (t.type == TOKEN_NUMBER)
{
struct ast *res = ast_new(AST_NUMBER);
res->value = t.value;
return res;
}
else if (t.type == TOKEN_MINUS)
{
t = lexer_peek(lexer);
if (t.type != TOKEN_NUMBER && t.type != TOKEN_LEFT_PAR)
{
*status = PARSER_UNEXPECTED_TOKEN;
fprintf(stderr, "parser: unexpected token\n");
return NULL;
}
struct ast *unary = ast_new(AST_NEG);
unary->left = parse_texp(status, lexer);
return unary;
}
else if (t.type == TOKEN_LEFT_PAR)
{
struct ast *exp = parse_exp(status, lexer);
t = lexer_pop(lexer);
if (t.type != TOKEN_RIGHT_PAR)
{
*status = PARSER_UNEXPECTED_TOKEN;
fprintf(stderr, "parser: expected closing parenthesis\n");
ast_free(exp);
return NULL;
}
return exp;
}
else
{
*status = PARSER_UNEXPECTED_TOKEN;
fprintf(stderr, "parser: unexpected token\n");
return NULL;
}
}
|