Programming Language Pragmatics, Fourth Edition
Programming Language Pragmatics, Fourth Edition
4th Edition
ISBN: 9780124104099
Author: Michael L. Scott
Publisher: Elsevier Science
Expert Solution & Answer
Book Icon
Chapter 2, Problem 13E

Explanation of Solution

a.

Parse tree for the string foo (a, b):

Programming Language Pragmatics, Fourth Edition, Chapter 2, Problem 13E

Explanation of Solution

b.

Canonical (rightmost) derivation:

  • Canonical derivation is a leftmost or rightmost derivations that may not be unique but they allow describing a derivation by the sequence of production rules taken.
  • In the rightmost derivation, the rightmost nonterminal on the right hand side of a rule is replaced by a suitable rule.
  • The complete derivation in this case is processed in the rightmost way.

stmt  subr_callid ( arg_list_ )id ( expr args_tail_ )id ( expr , arg_list_ )

        id ( expr , expr args_tail_ )id ( expr , expr_ )id ( expr , primary expr_tail_ )id ( expr , primary_ )

        id ( expr_ , id )id ( primary expr_tail_ , id )id ( primary_ , id )id ( id , id )

Explanation of Solution

c.

Proof:

The EPS, FIRST, and FELLOW sets table for the above grammar:

Terminals and non-terminalsEPSFIRSTFELLOW
stmtFalseidempty
assignmentFalseidempty
subr_callFalseidempty
exprFalseid, (‘,’, )
expr_tailTrue+, -, *, /‘,’, )
primaryFalseid, (+, -, *, /
opFalse+, -, *, /id, (
arg_listFalseid, ()
args_tailTrue‘,’)

Now add the predict sets to the grammar above:

1. stmt assignment               {id}2. stmt subr_call                 {id}  3. assignment id := expr     {id}4. subr_call id ( arg_list )   {id}

5. expr primary expr_tail   {id, (}6. expr_tail op expr           {+, -, *, /}7. expr_tail ε                     {',', ) }8. primary id                      {id}

9. primary subr_call           {id}10. primary ( expr )            {(}11. op + | - | * | /                 {+, -, *, /}12. arg_list expr args_tail  {id, (}

13. args_tail  , arg_list       {','}14. args_tail ε                   {)}

Parse table for the above grammar:

 Id:=(+-*/,)
stmt1, 2        
assignment3        
subr_call4        
expr5 5      
expr_tail-  666677
primary8, 9 10      
op-  11111111  
arg_list12 12      
args_tail-      1314
  • The entries in the above table indicates the production of predict. The “-” means an error.
  • If the top-of-stack is terminal, the appropriate action is always to match it against an incoming token from the scanner.

The given grammar can’t be LL (1) because of confusion with “stmt” and “id”.

Explanation of Solution

d.

Modified grammar to make it is LL (1):

Factor out “id” from productions with “stmt” and “expr”.

1. stmt id  stmt_tail               2. stmt_tail :=expr                 3. stmt_tail ( arg_list )      4. expr primary  expr_tail   

5. expr_tail op expr     6. expr_tail ε       7. primary id primary_tail                     8. primary_tail  ε                   

9. primary (arg_list)          10. primary ( expr )           11. op + | - | * | /                12. arg_list expr args_tail 

13. args_tail  , arg_list     14. args_tail ε                  

Want to see more full solutions like this?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Knowledge Booster
Background pattern image
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education