Modify the grammar of Figure 2.25 so that it accepts only programs that contain at least one

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 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 +

Fantastic news! We've Found the answer you've been seeking!

Step by Step Answer:

Related Book For  book-img-for-question

Programming Language Pragmatics

ISBN: 9780124104099

4th Edition

Authors: Michael L. Scott

Question Posted: