197k views
2 votes
Expr -> expr addop term | term

addop -> + | -
term -> term mulop factor | factor
mulop -> *
factor -> (expr) | num
PLEASE ANSWER THIS QUESTION CLEARLY WITH PAPER AND PEN
Consider the grammar above and the token string: num+num*(num+num)
a. Show a leftmost derivation of the string.
b. Show a rightmost derivation of the string.
c. Show the parse tree (not an Abstract Syntax Tree) corresponding to the leftmost derivation you gave in part (a).
d. Would the parse tree corresponding to a rightmost derivation be the same as you came up with in part (c)? Why or Why not?

User Canhazbits
by
9.1k points

1 Answer

1 vote

Leftmost derivation: Start with whole expression, then split by multiplication, then addition. Parse tree has multiplication closer to root. Rightmost derivation: Start with addition, then analyze multiplication within left term. Resulting parse trees differ due to processing order. 29 words!

Leftmost Derivation and Parse Tree

a. Leftmost Derivation:

1. **expr -> term:** This is our starting point, considering the whole string as a single expression.

2. **term -> term mulop factor:** We identify the first multiplication operator and split the term accordingly.

3. **term -> num mulop factor:** We recognize "num" as the left operand before the multiplication.

4. **factor -> num:** We recognize the second "num" after the multiplication as a factor.

5. **term -> num + (expr):** We recognize the remaining part as an expression with addition.

6. **expr -> term + (expr):** We apply the rule for addition to the overall expression.

7. **expr -> num + (term mulop factor):** We identify the multiplication within the remaining expression.

8. **expr -> num + (num mulop num):** We recognize the factors within the multiplication.

b. Rightmost Derivation:

1. **expr -> expr addop term:** We start from the entire string and recognize the addition operator first.

2. **expr -> term addop term:** We isolate the left term before the addition.

3. **term -> factor mulop factor:** We identify the first multiplication within the left term.

4. **term -> num mulop factor:** We recognize the first factor within the multiplication.

5. **term -> num mulop num:** We recognize the second factor within the multiplication.

6. **expr -> num + num:** We apply the addition rule to the remaining expression.

7. **expr -> num + num *(expr):** We identify the remaining part as an expression with multiplication.

8. **expr -> num + num * (term):** We isolate the term within the multiplication.

9. **expr -> num + num * (factor mulop factor):** We identify the multiplication within the term.

10. **expr -> num + num * (num mulop num):** We recognize the factors within the multiplication.

c. Parse Tree:

```

Expr

|

+

/ \

/ \

num *

|

num num

```

d. Similarity of Parse Trees:

No, the parse trees for the leftmost and rightmost derivations would not be the same. The main difference lies in the order of processing the multiplication operations. The leftmost derivation prioritizes the multiplication operator closer to the root of the tree, while the rightmost derivation processes the addition operator first and then analyzes the multiplication within the left term. Therefore, the structures and connections between the nodes would differ in the two parse trees.

User Alexander Arendar
by
7.8k points