-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreport.txt
56 lines (46 loc) · 3.04 KB
/
report.txt
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
Part A: Modifying the grammar
The grammars above describe a language that consists of expressions. Instead of expressions, one could change the language to consist of assignments. That is, instead of for example 10 * 3 + foo, it should understand input of the type fum = 10 * 3 + foo.
The postfix representation of an assignment variable = value is variable value =. For example, the postfix representation of x = 17 is x 17 =, and the postfix representation of fum = 10 * 3 + foo is fum 10 3 * foo + =.
To change the grammar from above to one that understands assignments, we first add a production for a new non-terminal called assignment. We also change the definition of list from a list of expressions to a list of assignments. The grammar now looks like this, with changes marked in red:
Assume that we want to handle both assignments and expressions. Write a grammar for that.
Answer:
start -> list eof
list -> expass ; list
| empty
expass -> expr | assignment
assignment -> id { print(id.lexeme) } = expr { print('=') }
expr -> term moreterms
moreterms -> + term { print('+') } moreterms
| - term { print('-') } moreterms
| empty
term -> factor morefactors
morefactors -> * factor { print('*') } morefactors
| / factor { print('/') } morefactors
| div factor { print('DIV') } morefactors
| mod factor { print('MOD') } morefactors
| empty
factor -> ( expr )
| id { print(id.lexeme) }
| num { print(num.value) }
Would it be possible to write a predictive recursive-descent parser for your grammar? (Hints: Is it ambiguous? Is it left-recursive? Are FIRST sets disjoint?)
Answer:
No, there are few criterias of a predictive recursive-descent parser which our grammar does not satisfy.
1. The grammar should not be ambiguous.
a: I don't think our grammar is ambiguous since there are strict rule for how a expressions and assignments should look like.
2. The grammar should not be left-recursive.
a: Our grammar is left-recursive in more than one places like list -> expass ; list and moreterms -> + term { print('+') } moreterms.
3. The FIRST sets of the grammar should be disjoint.
a: By looking at the grammar I can tell that at least FIRST set is not disjoint which is expr and assignment can both start with an ID.
PART B: Changed
PART C: This one is a bit tricky because according to guidelines we still allow variables in expressions.
So to successfully use them for calculations we need to store their values in a symbols table.
If we don't find them there then the calculation has failed and approprate message shuld be printed out.
Also I'm still not sure what does DIV mean In keywords library. So I just ignore it.
The same for MOD. I guess it can be modulo operation but I'm not sure.
I dont think our calculator looses a lot of functionality because of this.
But it would be nice to know what does it mean.
If it's nessecary I can add it later.
PART D.
It was more complicated than I thought.
The calculator part was easy but the grammar was harder.
morefactors, factor what should go efter what was tricky.