-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpar1.c
176 lines (154 loc) · 3.49 KB
/
par1.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "scan.h"
#define TRUE 1
#define FALSE 0
Symbol sym;
void nextsym() {
sym = scan();
printf("> ");
printsymb(sym);
};
void error(const char msg[]) {
printf("error: %s\n", msg);
printsymb(sym);
};
int accept(Symbol s) {
if (sym == s) {
nextsym();
return TRUE;
}
return FALSE;
}
int expect(Symbol s) {
if (accept(s)) {
return TRUE;
}
error("expected symbol: ");
printsymb(s);
return FALSE;
}
void program();
void block();
void statement();
void condition();
void expression();
void term();
void factor();
// <factor> = <ident> | <number> | "(" <expression> ")";
void factor() {
if (accept(IDENT)) {
;
} else if (accept(NUMBER)) {
;
} else if (accept(LPAREN)) {
expression();
expect(RPAREN);
} else {
error("factor: syntax error");
nextsym();
}
}
// <term> = <factor> {("*"|"/") <factor>};
void term() {
factor();
while (sym == TIMES || sym == SLASH) {
nextsym();
factor();
}
}
// <expression> = [ "+"|"-"] <term> { ("+"|"-") <term>};
void expression() {
if (sym == PLUS || sym == MINUS)
nextsym();
term();
while (sym == PLUS || sym == MINUS) {
nextsym();
term();
}
}
// <condition> = "odd" <expression>
// | <expression> ("="|"#"|"<"|"<="|">"|">=") <expression> ;
void condition() {
if (accept(ODDSYM)) {
expression();
} else {
expression();
if (sym == EQL || sym == NEQ || sym == LSS || sym == LEQ || sym == GTR || sym == GEQ) {
nextsym();
expression();
} else {
error("condition: invalid operator");
nextsym();
}
}
}
// <statement> = [ <ident> ":=" <expression>
// | "call" <ident>
// | "read" <ident> // NOT IMPL!
// | "write" <expression> // NOT IMPL!
// | "begin" <statement> {";" <statement> } "end"
// | "if" <condition> "then" <statement>
// | "while" <condition> "do" <statement> ];
void statement() {
if (accept(IDENT)) {
expect(BECOMES);
expression();
} else if (accept(CALLSYM)) {
expect(IDENT);
} else if (accept(BEGINSYM)) {
do {
statement();
} while (accept(SEMICOLON));
expect(ENDSYM);
} else if (accept(IFSYM)) {
condition();
expect(THENSYM);
statement();
} else if (accept(WHILESYM)) {
condition();
expect(DOSYM);
statement();
} else {
error("statement: syntax error");
nextsym();
}
}
// <block> = [ "const" <ident> "=" <number> {"," <ident> "=" <number>} ";"]
// [ "var" <ident> {"," <ident>} ";"]
// { "procedure" <ident> ";" <block> ";" } <statement> ;
void block() {
if (accept(CONSTSYM)) {
do {
expect(IDENT);
expect(EQL);
expect(NUMBER);
} while (accept(COMMA));
expect(SEMICOLON);
}
if (accept(VARSYM)) {
do {
expect(IDENT);
} while (accept(COMMA));
expect(SEMICOLON);
}
while (accept(PROCSYM)) {
expect(IDENT);
expect(SEMICOLON);
block();
expect(SEMICOLON);
}
statement();
}
// <program> = <block> "." ;
void program() {
nextsym();
block();
expect(PERIOD);
}
int main() {
program();
return 0;
}
/* EOF */