-
Notifications
You must be signed in to change notification settings - Fork 2
/
parser.c
196 lines (179 loc) · 3.69 KB
/
parser.c
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*************************************************************************
> File Name: parser.c
> Author: ellochen
> Mail: [email protected]
> Created Time: Wed May 8 23:24:27 2013
************************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "parser.h"
#include "lex.h"
#include "token.h"
#include "symtbl.h"
/*
Statement:
Identifier = Expression
Expression
Expression:
Term
Expression + Term
Expression - Term
Term:
Primary
Term * Primary
Term / Primary
Primary:
Identifier
Number
( Expression )
- Primary
+ Primary
Number:
double
*/
extern char *CURSOR;
extern SymTbl *SYMTBL;
static double expression();
static double primary()
{
char *back = CURSOR;
struct Token token = getNextToken();
if (TOKEN_DOUBLECONST == token.type) {
return token.var.d;
}else if (TOKEN_LPAREN == token.type) {
double var = expression();
back = CURSOR;
token = getNextToken();
if (TOKEN_RPAREN == token.type) {
return var;
}else {
printf("括号不匹配,错误字符:%d\n",token.type);
exit(0);
}
}else if (TOKEN_PLUS == token.type) {
return primary();
}else if (TOKEN_MINUS == token.type) {
return -primary();
}else if (TOKEN_ID == token.type) {
Symbol *pSymbol = (Symbol *)malloc(sizeof(Symbol));
pSymbol->key = token.var.p;
if (symtblLookup((void **)&pSymbol) < 0) {
printf("变量未定义\n");
exit(0);
}else {
return pSymbol->var;
}
}else {
CURSOR = back;
return 1;
}
}
static double term()
{
double var = primary();
char *back = CURSOR;
struct Token token = getNextToken();
while(1) {
if (TOKEN_STAR == token.type) {
var *= primary();
back = CURSOR;
token = getNextToken();
}else if (TOKEN_SLASH == token.type) {
double temp = primary();
if (0 == temp) {
printf("除数不能为0\n");
return -1;
}else {
var /= temp;
}
back = CURSOR;
token = getNextToken();
}else if (TOKEN_LINEEND == token.type) {
break;
}else {
CURSOR = back;
break;
}
}
return var;
}
static double expression()
{
double var = term();
char *back = CURSOR;
struct Token token = getNextToken();
while(1) {
if (TOKEN_PLUS == token.type){
var += term();
back = CURSOR;
token = getNextToken();
}else if (TOKEN_MINUS == token.type) {
var -= term();
back = CURSOR;
token = getNextToken();
}else if (TOKEN_LINEEND == token.type) {
break;
}else {
CURSOR = back;
return var;
}
}
return var;
}
#define BUCKET_NUM 256
static int hash(const void *dat)
{
const Symbol *pSymbol = (Symbol *)dat;
const char *ptr = (const char *)pSymbol->key;
unsigned int val = 0;
while (*ptr != '\0') {
unsigned int tmp;
val = (val << 4) + (*ptr);
if (tmp = (val & 0xf0000000)) {
val = val ^ (tmp >> 24);
val = val ^ tmp;
}
ptr++;
}
return val % BUCKET_NUM;
}
static int match(const void *dat1, const void *dat2)
{
const Symbol *pSymbol1 = (Symbol *)dat1;
const Symbol *pSymbol2 = (Symbol *)dat2;
return strcmp(pSymbol1->key, pSymbol2->key);
}
static void destroy(void *dat)
{
Symbol *pSymbol = (Symbol *)dat;
free(pSymbol->key);
free(pSymbol);
}
double statement()
{
char *back = CURSOR;
struct Token token = getNextToken();
if (TOKEN_ID == token.type && TOKEN_EQUAL == getNextToken().type) {
double var = expression();
Symbol *pSymbol = (Symbol *)malloc(sizeof(Symbol));
pSymbol->var = var;
pSymbol->key = token.var.p;
if (symtblInsert((void *)pSymbol) < 0) {
symtblUpdate((void *)pSymbol);
}
return var;
}else {
CURSOR = back;
return expression();
}
}
int parserInit()
{
SYMTBL = (SymTbl *)malloc(sizeof(SymTbl));
symtblInit(BUCKET_NUM, hash, match, destroy);
}
int parserDestroy()
{
symtblDestroy();
}