Question: Modify the grammar of Figure 2.25 so that it accepts only programs that contain at least one write statement. Make the same change in the
Modify the grammar of Figure 2.25 so that it accepts only programs that contain at least one write statement. Make the same change in the solution to Exercise 2.17. Based on your experience, what do you think of the idea of using the CFG to enforce the rule that every function in C must contain at least one return statement?
Figure 2.25:
1. program → stmt list $$
2. stmt list → stmt list stmt
3. stmt list → stmt
4. stmt → id := expr
5. stmt → read id
6. stmt → write expr
7. expr → term
8. expr → expr add op term
9. term → factor
10. term → term mult op factor
11. factor → ( expr )
12. factor → id
13. factor → number
14. add op → +
15. add op → -
16. mult op → *
17. mult op → /
Figure 2.17:

procedure factor.tail() case input_token of *, / : mult op(); factor(); factor_tail() +, -, ), id, read, write, $$ : skip otherwise parse_error -- epsilon production procedure factor() case input_token of id : match(id) number : match(number) (: match( (); expr(); match()) otherwise parse_error procedure add_op() case input_token of + : match(+) - : match(-) otherwise parse error procedure mult.op() case input_token of * : match(*) 7: match(/) otherwise parse_error
Step by Step Solution
3.33 Rating (156 Votes )
There are 3 Steps involved in it
Here other stmt list generates all statements prior to the first write statement Note that we have h... View full answer
Get step-by-step solutions from verified subject matter experts
