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 4, Problem 18E

Explanation of Solution

a.

Context-free grammar for “case” or “switch” statements:

  • The context-free grammar (CFG) generates the context-free language (CFL).
  • Production rules of CFG are simple replacement.
  • Parsing is a method of finding a tree which has a given yield.
  • Rules can also be applied in reverse to check whether a string is grammatically correct according to the grammar.

In the following grammar, the input is error-free if and only if S.dup = :

 switch ( expr ) { arm_list }    S.dup := arm_list.dupsarm_list  case_list stmt more_arms arm_list.out := case_list.out  more_arms.out         arm_list.dups := case_list.dups  more_arms.dups (case_list.out  more_arms.out)

case_list  case expr : more_cases case_list.out := {expr.val}  more_cases.out         case_list.dups :=  more_cases.dups ({expr.val}  more_cases.out)    

more_cases  case_list more_cases.out := case_list.out    more_cases.dups := case_list.dupsmore_cases   more_cases.out :=                     more_arms.dups := 

more_arms  arm_list more_arms.out := more_list.out     more_arms.dups := arm_list.dupsmore_arms   more_arms.out :=         more_arms.dups := 

Explanation of Solution

b.

Replacing the semantic functions with action routines:

  • Action routines are a semantic function that the user instructs the compiler to execute at a specific part in the parse.
  • An ad hoc translation scheme which is inserted with parsing takes the form of a set of action routine.
  • Many parser generators allow the user to identify action routines.

Assumes exponents in the input are all positive integers. The grammar is as follows:

 switch (expr){arm_list} {S.dups := arm_list.dups}arm_list  case_list stmt more_arms {arm_list.out := case_list.out  more_arms.out;arms_list.dups := case_list.dups  more_arms.dups (case_list.out  more_arms.out)}  

case_list  case expr : more_cases { case_list.out := {expr.val}more_cases.out ({exp.val}  more_cases.out) 

more_cases  case_list {more_cases.out := case_list.out; more_cases.dups := case_list.dups}  {more_cases.out := ; more_cases.dups := }

more_arms  arm_list {more_arm.out := arm_list.out; more_arms.dups := arm_list.dups}  {more_arms.out := ; more_arms.dups := }

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!
Students have asked these similar questions
Show all the work
Construct a frequency polygon density estimate for the sample in Question 1, using bin width determined by Sturges’ Rule.
Show all the work
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
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