Question

Consider the following grammar (G1) for simple assignment statements. (The symbols in double quotation marks are...

Consider the following grammar (G1) for simple assignment statements. (The symbols in double quotation marks are terminal symbols.)

assign → id “ = ” expr

id → “A” | “B” | “C”

expr → expr “ + ” expr

| expr “ ∗ ” expr

| “(” expr “)”

| id

a) Give a (leftmost) derivation for string A = B ∗ A + C.

b) Give the parse tree for string A = B ∗ A + C.

c) Is the grammar ambiguous or unambiguous? Explain your answer.

d) Consider the following grammar (G2).

assign → id “ = ” expr

id → “A” | “B” | “C”

expr → id “ + ” expr

| id “ ∗ ” expr

| “(” expr “)”

| id

What is the difference between G2 and G1? Explain your answer

0 0
Add a comment Improve this question Transcribed image text
Answer #1

ton Sign → id = expr → A -expr B * expr + expr expr expr * expr exp expr id id

c)
Ambiguous.

Grammar is ambiguous because we have more than one parse tree is there for one string.

d)
G1 is Ambiguous
G2 is unambigous

Since we have following in G2, we will substitute something to expr only but not to id.
expr ? id “ + ” expr
| id “ * ” expr
| “(” expr “)”
| id


Where as in G1, we can substitute anything in any of the 2 expr's we have
expr ? expr “ + ” expr
which lets it to have more than one derivation for one string

Add a comment
Know the answer?
Add Answer to:
Consider the following grammar (G1) for simple assignment statements. (The symbols in double quotation marks are...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT