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
I need help to solve a simple problem using Grover’s algorithm, where the solution is not necessarily known beforehand. The problem is a 2×2 binary sudoku with two rules: • No column may contain the same value twice. • No row may contain the same value twice.   Each square in the sudoku is assigned to a variable as follows:   We want to design a quantum circuit that outputs a valid solution to this sudoku. While using Grover’s algorithm for this task is not necessarily practical, the goal is to demonstrate how classical decision problems can be converted into oracles for Grover’s algorithm.   Turning the Problem into a Circuit   To solve this, an oracle needs to be created that helps identify valid solutions. The first step is to construct a classical function within a quantum circuit that checks whether a given state satisfies the sudoku rules.   Since we need to check both columns and rows, there are four conditions to verify: v0 ≠ v1   # Check top row   v2 ≠ v3   # Check bottom row…
I need help to solve a simple problem using Grover’s algorithm, where the solution is not necessarily known beforehand. The problem is a 2×2 binary sudoku with two rules: • No column may contain the same value twice. • No row may contain the same value twice.   Each square in the sudoku is assigned to a variable as follows:   We want to design a quantum circuit that outputs a valid solution to this sudoku. While using Grover’s algorithm for this task is not necessarily practical, the goal is to demonstrate how classical decision problems can be converted into oracles for Grover’s algorithm.   Turning the Problem into a Circuit   To solve this, an oracle needs to be created that helps identify valid solutions. The first step is to construct a classical function within a quantum circuit that checks whether a given state satisfies the sudoku rules.   Since we need to check both columns and rows, there are four conditions to verify: v0 ≠ v1   # Check top row   v2 ≠ v3   # Check bottom row…
using r language
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