-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab1.html
588 lines (482 loc) · 15.9 KB
/
lab1.html
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
<!--
//Skeleton file
//Author: Prof. Hing Leung, PhD, New Mexico State University
//Last modified: unknown
-->
<html>
<body>
<h1>CS 370 (Spring 2016)<br> Lab 1 <br>
</h1>
<p>
Due: (Friday) Sept 2, 2016 at 11:59pm BEFORE class
</p>
<p>
Submit all the source codes (including .h files) as a single zip file
to CANVAS</tt>. Also include a README file that
explains how well your program works.
</p>
<p>
We will learn to write a set of mutually recursive functions
(using the recursive descent parsing method)
to evaluate simple arithmetic expressions.
<br>
<br>
Following are the programs for solving the problem:
<br>
<br><a href="expr.c">expr.c</a>
<br><a href="expr.h">expr.h</a>
<br><a href="input_token.c">input_token.c</a>
<br><a href="input_token.h">input_token.h</a>
<br><a href="main.c">main.c</a>
<br><a href="error.c">error.c</a>
<br><a href="error.h">error.h</a>
<br>
<p>
<p>A simple tokenizer (<tt>input_token.h</tt> and <tt>input_token.c</tt>)
has been written for you. Your job is to complete the codes given
in <tt>expr.c</tt>
<p>
Later, we will solve the problem again using Lex and Yacc programs.
</p>
<p>
In machine/assembly language, there is no support for
the evaluation of complex arithmetic expression
involving parentheses and operators of different precedence.
Such arithmetic expression needs to be converted into
a series of simple arithmetic operations before given
to the computer for execution.
<p>
How do we human evaluate an arithmetic expression?
<p>
For example, consider the expression
<pre>
( 78 + 34 * 9 * ( 45 * ( 23 - 15 * 4 ) - 8 ) )
</pre>
We read the expression from left to right.
<pre>
( 78 + 34 * 9 * ( 45 * ( 23 - 15 * 4 ) - 8 ) )
( 78 + 34 * 9 * ( 45 * ( 23 - 15 * 4 ) - 8 ) )
( 78 + 34 * 9 * ( 45 * ( 23 - 15 * 4 ) - 8 ) )
( 78 + 34 * 9 * ( 45 * ( 23 - 15 * 4 ) - 8 ) )
( 78 + 34 * 9 * ( 45 * ( 23 - 15 * 4 ) - 8 ) )
( 78 + 34 * 9 * ( 45 * ( 23 - 15 * 4 ) - 8 ) )
( 78 + 306 * ( 45 * ( 23 - 15 * 4 ) - 8 ) )
( 78 + 306 * ( 45 * ( 23 - 15 * 4 ) - 8 ) )
( 78 + 306 *
( 45 * ( 23 - 15 * 4 ) - 8 ) )
( 78 + 306 *
( 45 * ( 23 - 15 * 4 ) - 8 ) )
( 78 + 306 *
( 45 *
( 23 - 15 * 4 ) - 8 ) )
( 78 + 306 *
( 45 *
( 23 - 15 * 4 ) - 8 ) )
( 78 + 306 *
( 45 *
( 23 - 15 * 4 ) - 8 ) )
( 78 + 306 *
( 45 *
( 23 - 15 * 4 ) - 8 ) )
( 78 + 306 *
( 45 *
( 23 - 15 * 4 ) - 8 ) )
( 78 + 306 *
( 45 *
( 23 - 60 ) - 8 ) )
( 78 + 306 *
( 45 *
( 23 - 60 ) - 8 ) )
( 78 + 306 *
( 45 * -37 - 8 ) )
( 78 + 306 *
( -1665 - 8 ) )
( 78 + 306 *
( -1665 - 8 ) )
( 78 + 306 *
( -1665 - 8 ) )
( 78 + 306 *
( -1665 - 8 ) )
( 78 + 306 * -1673 )
( 78 + -511938 )
( -511860 )
( -511860 )
-511860
</pre>
So the answer is -511860.
<br>
<br>
We write a program that simulates the above procedure
for evaluating expression. Warning: the above example has not
exhausted all the different cases that need to be handled.
<br>
<br>
We assume that each expression must begin and end with parentheses,
which we also called brackets below.
That is,
<pre>
78 + 34 * 9 * ( 45 * ( 23 - 15 * 4 ) - 8 )
</pre>
is not considered to be a valid expression, but
<pre>
( 78 + 34 * 9 * ( 45 * ( 23 - 15 * 4 ) - 8 ) )
</pre>
is.
<br>
<br>
Next, we assume that each operand in an expression is a
non-negative integer.
That is,
<pre>
( 56 * -23 )
</pre>
is not a valid expression. it has to be rewritten as
<pre>
( 56 * (0 - 23) )
</pre>
First, we need to write a library that tokenizes the
input expression. the function provided is
<pre>
int get_token(int *w)
</pre>
See <tt>input_token.h</tt>
for a detailed description of the function <tt>get_token</tt>.
<br>
<br>
Also, we assume that the input file holds only ONE expression
which may be given over several lines.
<br>
<br>
Let
<pre>
( 56 * -23 )
</pre>
be the given input. <tt>get_token</tt> returns
<pre>
OpenBracket
Operand: 56
Mult
Minus
Operand: 23
CloseBracket
</pre>
as the token sequence.
<br>
<br>
We need a library that reports the errors.
This is given in <tt>error.h</tt> and <tt>error.c</tt>.
Once an error is reported, we abort the program execution
by an exit statement. Currently, the error reporting
message is very brief. It can be extended easily
to include a corresponding detailed message for every
type of error.
<br>
<br>
The library <tt>expr.h</tt> and <tt>expr.c</tt> provides a function
<pre>
int evaluate_expression()
</pre>
that evaluates an expression given in the input.
<br>
<br>
Consider expr.c.
How do we write the functions that evaluate an expression?
We will be writing a set of mutually recursive functions.
After we have read an open parenthesis,
we execute <tt>eval_expr</tt> which assumes that
( has been just read. If we read an operand, then we will call
<tt>eval_expr_1(opn1)</tt> to continue. Next if we read an operator,
we will further call <tt>eval_expr_2(opn1, opr1)</tt> to continue.
Altogether, we need five mutually recursive functions:
<pre>
eval_expr()
eval_expr_1(int opn1)
eval_expr_2(int opn1,int opr1)
eval_expr_3(int opn1,int opr1, int opn2)
eval_expr_4(int opn1,int opr1, int opn2, int opr2)
</pre>
The evaluation of the expression should observe the precedence rules:
<ol>
<li>* and / are of the same precedence</li>
<li>+ and - are of the same precedence</li>
<li>* and / are of higher precedence than + and -</li>
</ol>
</pre>
The following are the test cases:<br>
<br><a href="data1">data1</a>
<br><a href="data2">data2</a>
<br><a href="data3">data3</a>
<br><a href="data4">data4</a>
<br><a href="data5">data5</a>
<br><a href="data6">data6</a>
<br>
<br>
The commands to compile and execute the program is as follows:
<pre><blockquote>
gcc -c expr.c
gcc -c input_token.c
gcc -c error.c
gcc -c btree.c
gcc main.c expr.o input_token.o error.o btree.o -o expression
</blockquote></pre>
<br>
<br>
The executable of the program is placed in <tt>expr</tt>.
To test the program on the test case given in <tt>data1</tt>, type
<pre>
<blockquote>
./expression data1
</blockquote>
</pre>
</p>
<br>
<br>
In this lab, we want to build an expression tree for a given arithmetic
expression.
Modify the C codes <tt>expr.c</tt>
so that the function <tt>eval_expr()</tt> returns an expression tree
which is of the type <tt>BTree</tt> (instead of returning an integer).
Also, you need to modify the main program <tt>main.c</tt> so that
instead of printing out the integer value for the given
arithmetic expression, the program
<ol>
<li>
prints out the expression tree by using an in-order traversal
using a recursive function
<pre>
void in_order_traversal( BTree t )
</pre>
</li>
<li>
prints out the expression tree by using an post-order traversal,
using a recursive function
<pre>
void post_order_traversal( BTree t )
</pre>
</li>
<li>
evaluate the expression tree to return an integer value using a recursive function
<pre>
int eval( BTree t )
</pre>
</li>
</ol>
Suppose the expression is
<pre>
( 78 + 34 * 9 * ( 45 * ( 23 - 15 * 4 ) - 8 ) )
</pre>
the expression tree is
<pre>
+
/ \
78 *
/ \
* \
/ \ \
34 9 -
/ \
* 8
/ \
45 -
/ \
23 *
/ \
15 4
</pre>
the in-order traversal would print out
<pre>
(78 +((34*9)*((45*(23-(15*4)))-8)))
</pre>
The post-order traversal of the expression tree given in step 1
gives out the same expression in postfix notation which is
<pre>
78 34 9 * 45 23 15 4 * - * 8 - * +
</pre>
The integer value evaluated is -511860.
<br>
<br>
<br>
<br>
<br>
<br>
<br>
SUPPLEMENTARY NOTES (nothing to turn in for this section)
<br>
<br>
We can re-write the mutually recursive functions to evaluate arithmetic expressions
into a non-recursive program using a stack.
<br>
<br>
Below is an illustration to evaluate an arithmetic expression
using a stack.
<br>
<br>
For example, consider the expression
<pre>
( 78 + 34 * 9 * ( 45 * ( 23 - 15 * 4 ) - 8 ) )
</pre>
<pre>
INPUT:
stack bottom -> ( 78 + 34 * 9 * ( 45 * ( 23 - 15 * 4 ) - 8 ) )
stack bottom -> ( 78 + 34 * 9 * ( 45 * ( 23 - 15 * 4 ) - 8 ) )
stack bottom -> ( 78 + 34 * 9 * ( 45 * ( 23 - 15 * 4 ) - 8 ) )
stack bottom -> ( 78 + 34 * 9 * ( 45 * ( 23 - 15 * 4 ) - 8 ) )
stack bottom -> ( 78 + 34 * 9 * ( 45 * ( 23 - 15 * 4 ) - 8 ) )
stack bottom -> ( 78 + 34 * 9 * ( 45 * ( 23 - 15 * 4 ) - 8 ) )
stack bottom -> ( 78 + 306 * ( 45 * ( 23 - 15 * 4 ) - 8 ) )
stack bottom -> ( 78 + 306 * ( 45 * ( 23 - 15 * 4 ) - 8 ) )
stack bottom -> ( 78 + 306 *
stack top -> ( 45 * ( 23 - 15 * 4 ) - 8 ) )
stack bottom -> ( 78 + 306 *
stack top -> ( 45 * ( 23 - 15 * 4 ) - 8 ) )
stack bottom -> ( 78 + 306 *
( 45 *
stack top -> ( 23 - 15 * 4 ) - 8 ) )
stack bottom -> ( 78 + 306 *
( 45 *
stack top -> ( 23 - 15 * 4 ) - 8 ) )
stack bottom -> ( 78 + 306 *
( 45 *
stack top -> ( 23 - 15 * 4 ) - 8 ) )
stack bottom -> ( 78 + 306 *
( 45 *
stack top -> ( 23 - 15 * 4 ) - 8 ) )
stack bottom -> ( 78 + 306 *
( 45 *
stack top -> ( 23 - 15 * 4 ) - 8 ) )
stack bottom -> ( 78 + 306 *
( 45 *
stack top -> ( 23 - 60 ) - 8 ) )
stack bottom -> ( 78 + 306 *
( 45 *
stack top -> ( 23 - 60 ) - 8 ) )
unget_token -37
stack bottom -> ( 78 + 306 *
stack top -> ( 45 * -37 - 8 ) )
stack bottom -> ( 78 + 306 *
stack top -> ( 45 * -37 - 8 ) )
stack bottom -> ( 78 + 306 *
stack top -> ( -1665 - 8 ) )
stack bottom -> ( 78 + 306 *
stack top -> ( -1665 - 8 ) )
stack bottom -> ( 78 + 306 *
stack top -> ( -1665 - 8 ) )
stack bottom -> ( 78 + 306 *
stack top -> ( -1665 - 8 ) )
unget_token -1673
stack bottom -> ( 78 + 306 * -1673 )
stack bottom -> ( 78 + 306 * -1673 )
stack bottom -> ( 78 + -511938 )
stack bottom -> ( -511860 )
stack bottom -> ( -511860 )
unget_token -511860
-511860
</pre>
So the answer is -511860.
</p>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
SUPPLEMENTARY NOTES (nothing to turn in for this section)
<br>
<br>
<p>
The problem can be solved easily using Lex and Yacc programs
<a HREF="hw1.l">hw1.l</a> and <a HREF="hw1.y">hw1.y</a>.
<!--
The programs can actually be simplified to
<a HREF="expression/hw1.alt.l">hw1.alt.l</a> and <a HREF="expression/hw1.alt.y">hw1.alt.y</a>.
-->
Commands to run the programs are: <br>
<pre>
<blockquote>
flex hw1.l
yacc hw1.y
gcc y.tab.c -lfl -ly
a.out < data1
</blockquote>
</pre>
Note that + and - are of lower precedence than * and /.
Also, +, -, * and / are left associate.
Indeed, Yacc provides a facility to handle operators easily
so that the user does not have to specify the grammar rules.
The Yacc program can be rewritten as
<a HREF="hw1a.y">hw1a.y</a>
to take advantage of the facility.
Soon after the specification of the token <tt>INTdenotation</tt> in the
<a HREF="hw1a.y">hw1a.y</a>
program, the operators are listed with the lower precedence operators (+ and -)
presented first, followed by the higher precedence operators (* and /) given in
the next line. Within each line, it begins with the word <tt>left</tt>
indicating that the operators are left associative.
<!--
Notice that we have further
simplified the Lex and Yacc programs by removing the concept of <tt>num</tt>
from both programs.
-->
<!--
But, in our future Yacc programs, we need to use the
full features of Yacc as given in the original
<a HREF="hw1.l">hw1.l</a> and <a HREF="hw1.y">hw1.y</a>
programs.
-->
<br>
<p>
We can augment <a href="hw1.y">hw1.y</a> to give
<a href="hw1b.y">hw1b.y</a> to print the order for which
the grammar rules are applied.
<p>
Example: we run <a href="hw1b.y">hw1b.y</a> on the test data
<a href="data1">data1</a> which is the same example input
<tt>( 78 + 34 * 9 * ( 45 * ( 23 - 15 * 4 ) - 8 ) )</tt>
generated by the C program we developed for evaluating arithmetic expressions.
<pre><blockquote>
flex hw1.l
yacc hw1b.y
gcc y.tab.c -lfl -ly
a.out < data1 > data1.out
</blockquote></pre>
<p>
Click <a href="data1.out">here</a> for the outputs.
<!--
<pre><blockquote>
primary : INTdenotation (78)
term : primary (78)
sum : term (78)
primary : INTdenotation (34)
term : primary (34)
primary : INTdenotation (9)
306 = 34 * 9
primary : INTdenotation (45)
term : primary (45)
primary : INTdenotation (23)
term : primary (23)
sum : term (23)
primary : INTdenotation (15)
term : primary (15)
primary : INTdenotation (4)
60 = 15 * 4
-37 = 23 - 60
primary : '(' sum (-37) ')'
-1665 = 45 * -37
sum : term (-1665)
primary : INTdenotation (8)
term : primary (8)
-1673 = -1665 - 8
primary : '(' sum (-1673) ')'
-511938 = 306 * -1673
-511860 = 78 + -511938
program : '(' sum (-511860) ')'
</blockquote></pre>
-->
Next, you should draw the parse tree for the input. Then
pay attention to the order that the grammar rules are applied,
and compare that to the order in which the expression are processed
by the C program. In fact, the order shows that the YACC parser employs the
bottom-up parsing technique.
</body>
</html>