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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
|
%{
include <ctype.h>
define YYMAXDEPTH 32
define YYOPLEN 1
define yyparse unit_parse
define SZ_SHORTSTR 31
%L
include "parseunits.com"
char units[SZ_FNAME]
int num_unstr()
pointer mul_unstr(), div_unstr(), pow_unstr(), set_unstr()
%}
%token Y_WRONG Y_DONE Y_LPAR Y_RPAR Y_CU Y_SQ Y_ID Y_NUM
%left Y_DIV
%left Y_MUL
%right Y_POW
%%
unit : expr Y_DONE {
# Normal exit. Return pointer to units structure
if (debug == YES)
call eprintf ("\n")
tun = Memi[$1]
return (OK)
}
| error {
# Syntax error
if (debug == YES)
call eprintf ("\n")
return (ERR)
}
;
expr : Y_LPAR expr Y_RPAR {
# Parenthesized expression
Memi[$$] = Memi[$1]
}
| expr Y_MUL expr {
# Multiply two units expressions
Memi[$$] = mul_unstr (Memi[$1], Memi[$3])
call free_unstr (Memi[$1])
call free_unstr (Memi[$3])
if (debug == YES) {
call str_unstr (Memi[$$], units, SZ_FNAME)
call eprintf ("Units are %s\n")
call pargstr (units)
}
}
| expr Y_DIV expr {
# Divide two units expressions
Memi[$$] = div_unstr (Memi[$1], Memi[$3])
call free_unstr (Memi[$1])
call free_unstr (Memi[$3])
if (debug == YES) {
call str_unstr (Memi[$$], units, SZ_FNAME)
call eprintf ("Units are %s\n")
call pargstr (units)
}
}
| expr Y_POW Y_NUM {
# Raise expression to a power
Memi[$$] = pow_unstr (Memi[$1], num_unstr (Memc[Memi[$3]]))
call free_unstr (Memi[$1])
if (debug == YES) {
call str_unstr (Memi[$$], units, SZ_FNAME)
call eprintf ("Units are %s\n")
call pargstr (units)
}
}
| termlist {
# List of terms
Memi[$$] = Memi[$1]
}
;
termlist: termlist term {
# Implicit multiplication
Memi[$$] = mul_unstr (Memi[$1], Memi[$2])
call free_unstr (Memi[$1])
call free_unstr (Memi[$2])
if (debug == YES) {
call str_unstr (Memi[$$], units, SZ_FNAME)
call eprintf ("Units are %s\n")
call pargstr (units)
}
}
| term {
# Simple term
Memi[$$] = Memi[$1]
}
;
term : Y_ID Y_POW Y_NUM {
# Raise units to a power
Memi[$$] = set_unstr (abrev, Memc[Memi[$1]],
num_unstr (Memc[Memi[$3]]))
if (debug == YES) {
call str_unstr (Memi[$$], units, SZ_FNAME)
call eprintf ("Units are %s\n")
call pargstr (units)
}
}
| Y_ID Y_NUM {
# Implicitly raise to a power
Memi[$$] = set_unstr (abrev, Memc[Memi[$1]],
num_unstr (Memc[Memi[$2]]))
if (debug == YES) {
call str_unstr (Memi[$$], units, SZ_FNAME)
call eprintf ("Units are %s\n")
call pargstr (units)
}
}
| Y_CU Y_ID {
# Cubic prefix
Memi[$$] = set_unstr (abrev, Memc[Memi[$2]], 3)
if (debug == YES) {
call str_unstr (Memi[$$], units, SZ_FNAME)
call eprintf ("Units are %s\n")
call pargstr (units)
}
}
| Y_SQ Y_ID {
# Square prefix
Memi[$$] = set_unstr (abrev, Memc[Memi[$2]], 2)
if (debug == YES) {
call str_unstr (Memi[$$], units, SZ_FNAME)
call eprintf ("Units are %s\n")
call pargstr (units)
}
}
| Y_ID {
# Simple name
Memi[$$] = set_unstr (abrev, Memc[Memi[$1]], 1)
if (debug == YES) {
call str_unstr (Memi[$$], units, SZ_FNAME)
call eprintf ("Units are %s\n")
call pargstr (units)
}
}
;
%%
# PARSE_UNITS -- Parse a units string into the internal format
pointer procedure parse_units (ab, units)
pointer ab # i: abbreviation hash table
char units[ARB] # i: expression to be parsed
#--
include "parseunits.com"
int len, fd
pointer sp
string syntax "Syntax error in units"
bool yydebug
int strlen(), stropen(), yyparse()
int get_token()
extern get_token
begin
len = strlen (units) + 1
fd = stropen (units, len, READ_ONLY)
call smark (sp)
call salloc (tokbuf, SZ_FNAME, TY_CHAR)
debug = NO
yydebug = (debug == YES)
nxttok = 0
abrev = ab
tun = NULL
if (yyparse (fd, yydebug, get_token) == ERR)
call tuniterr (syntax, units)
call close (fd)
call sfree (sp)
return (tun)
end
# GET_TOKEN -- Retrieve next token from units string
int procedure get_token (fd, value)
int fd # i: File containing expression to be lexed
pointer value # o: Address on parse stack to store token
#--
include "parseunits.com"
char ch
int type, index, powers[4]
pointer sp, typename, token
string pownames "sq,square,cu,cubic"
data powers / Y_SQ, Y_SQ, Y_CU, Y_CU /
bool streq()
int getc(), word_match()
begin
call smark (sp)
call salloc (typename, SZ_FNAME, TY_CHAR)
token = tokbuf + nxttok
Memi[value] = token
repeat {
ch = getc (fd, ch)
} until (ch != ' ' && ch != '\t')
if (ch == EOF) {
type = Y_DONE
call strcpy ("END", Memc[typename], SZ_FNAME)
} else if (IS_ALPHA (ch)) {
type = Y_ID
call strcpy ("IDENT", Memc[typename], SZ_FNAME)
while (IS_ALPHA (ch)) {
Memc[tokbuf+nxttok] = ch
nxttok = nxttok + 1
ch = getc (fd, ch)
}
call ungetc (fd, ch)
Memc[tokbuf+nxttok] = EOS
index = word_match (Memc[token], pownames)
if (index > 0) {
type = powers[index]
call strcpy ("POWER", Memc[typename], SZ_FNAME)
} else if (streq (Memc[token], "per")) {
type = Y_DIV
call strcpy ("DIV", Memc[typename], SZ_FNAME)
}
} else if (ch == '-' || IS_DIGIT (ch)) {
type = Y_NUM
call strcpy ("NUMBER", Memc[typename], SZ_FNAME)
Memc[tokbuf+nxttok] = ch
nxttok = nxttok + 1
ch = getc (fd, ch)
while (IS_DIGIT (ch)) {
Memc[tokbuf+nxttok] = ch
nxttok = nxttok + 1
ch = getc (fd, ch)
}
call ungetc (fd, ch)
} else {
Memc[tokbuf+nxttok] = ch
nxttok = nxttok + 1
switch (ch) {
case '*':
ch = getc (fd, ch)
if (ch == '*') {
type = Y_POW
call strcpy ("EXPON", Memc[typename], SZ_FNAME)
Memc[tokbuf+nxttok] = ch
nxttok = nxttok + 1
} else {
type = Y_MUL
call strcpy ("MUL", Memc[typename], SZ_FNAME)
call ungetc (fd, ch)
}
case '/':
type = Y_DIV
call strcpy ("DIV", Memc[typename], SZ_FNAME)
case '^':
type = Y_POW
call strcpy ("EXPON", Memc[typename], SZ_FNAME)
default:
type = Y_WRONG
call strcpy ("ERROR", Memc[typename], SZ_FNAME)
}
}
Memc[tokbuf+nxttok] = EOS
nxttok = nxttok + 1
if (debug == YES) {
call eprintf ("Token is %s [%s]\n")
if (Memc[token] == EOS) {
call pargstr ("EOS")
} else {
call pargstr (Memc[token])
}
call pargstr (Memc[typename])
}
call sfree (sp)
return (type)
end
|