E E
/|\ /|\
/ | \ / | \
E + E E + E
/|\ | | /|\
/ | \ | | / | \
E + E A A E + E
| | | | | |
| | | | | |
A A c a A A
| | | |
| | | |
a b b c
There are also are causes of ambiguity: the associativity of the concatenation and the priority
of the various operators.
E ::= E+A | A
A ::= AB | B
B ::= B* | C
C ::= a | b | c | (E)
Exp ::= max(Exp,Exp)
case "max" : {
int k1 = eval(t->get_left(), r);
int k2 = eval(t->get_right(), r);
if (k1 > k2)
return k1;
else
return k2;
}
}
Another possibility would have been to represent max, in the parse trees, as another operator.
In that case it would be sufficient to add the following case in switch(n->get_op()):
case "max" : {
if (k1 > k2)
return k1;
else
return k2;
}
}
caller of p p I A.R. where p is declared
^ ------------------ ^
CL |___|_ | |
| | |
------------------ |
| _|______| SL
| |
------------------
|-->| ----- |<-|---|----|
| | x | 1 | | | | |
| | ----- | | | |
| | y | 2 | | | | |
| | ----- | | | |
CL | ------------------ | | |
| | | |
| q I | | |
| ------------------ | | |
|___|_ | | | |
| | | | |
------------------ | | |
| _|__|SL | |
| | | |
------------------ | |
|-->| ----- | | |
| | y | 1 | | | |
| | ----- | | |
CL | ------------------ | |
| | |
| r I | |
| ------------------ | |
|___|_ | | |
| | | |
------------------ | |
| _|______| SL |
| | |
------------------ |
|-->| ----- | |
| | x | 2 | | |
| | ----- | |
CL | ------------------ |
| |
| Q II |
| ------------------ |
|___|_ | |
| | |
------------------ |
| _|___________| SL
| |
------------------
| ----- |
| y | 3 | |
| ----- |
------------------
CL = Control Link
SL = Static Link