Question

In: Computer Science

Show that the following attempt to solve the dangling else ambiguity is still ambiguous. statement →...

Show that the following attempt to solve the dangling else ambiguity is still ambiguous.


statement → if ( exp ) statement | matched-stmt
matched-stmt → if ( exp ) matched-stmt else statement | other
exp → 0 | 1

Solutions

Expert Solution

Solution

Consider the following string
if ( 0 ) if ( 0 ) other else if ( 0 ) other else other

We can show two possible leftmost derivations for this, demonstrating the grammar’s ambiguity

Derivation 1

statement -> if (exp)      statement

                    -> if (0)          statement

-> if (0)           matched-stmt

-> if (0)     if    (exp)    matched-stmt else   statement

-> if (0)    if (0)           matched-stmt else   statement

-> If (0)     if (0)             other                else   statement

  -> if (0)    if (0)             other                 else matched-stmt

  ->   if (0)     if (0)             other                 else if (exp)   matched-stmt else statement

  -> if (0)     if (0)             other                 else if (0)      matched-stmt   else statement

   -> if (0)      if (0)             other                  else if (0)      other                    else statement

-> if (0)        if (0)            other                  else if (0)      other                  else matched-stmt

-> if (0)       if (0)            other                  else if (0)      other                    else other

Derivation 2

statement           ->matched-stmt

->if        (exp)    matched-stmt   else        statement

->if        (0)          matched-stmt   else        statement

->if         (0)          if (exp) matched-stmt   else        statement           else        statement

->if         (0)          if (0)     matched-stmt   else        statement           else        statement

->if         (0)          if (0)      other     else       statement           else        statement

->if         (0)          if (0)     other    else       if (exp) statement           else        statement

->if         (0)          if (0)     other    else        if (0)     statement           else        statement

->if         (0)          if (0)      other     else       if (0)     matched-stmt   else        statement

->if         (0)         if (0)     other    else       if (0)     other    else       statement

->if         (0)          if (0)      other     else       if (0)      other     else       matched-stmt

->if         (0)          if (0)     other    else       if (0)      other     else        other

---

i wrote the solution in word

after pasted here, format and alignment changed, really sorry for that

all the best


Related Solutions

1)Write a C++ program which clearly demonstrates how the dangling else ambiguity is resolved. Your program...
1)Write a C++ program which clearly demonstrates how the dangling else ambiguity is resolved. Your program should print distinct results depending on if C++ uses inner-if or outer-if resolution. Turn in a listing of your program, the output, and a description of how your program demonstrates the semantic resolution of the ambiguity. 2) Give a context-free grammar describing the syntax of the following; Non-empty strings of 0’s and 1’s that represent binary numbers that have odd parity
True/false/ambiguous. For each of the following, indicate whether the statement is true, false, or ambiguous, and...
True/false/ambiguous. For each of the following, indicate whether the statement is true, false, or ambiguous, and briefly explain your answer. If your answer depends on any assumptions, state them clearly. Use graphs or equations to illustrate your answer whenever it is helpful. 1. A worker should never trust an implicit contract with an employer that pays them less than their marginal product when they first join the firm, but more than their marginal product after many years with the firm,...
True/false/ambiguous. For each of the following, indicate whether the statement is true, false, or ambiguous, and...
True/false/ambiguous. For each of the following, indicate whether the statement is true, false, or ambiguous, and briefly explain your answer. If your answer depends on any assumptions, state them clearly. Use graphs or equations to illustrate your answer whenever it is helpful. 11. In efficiency wage models, employers take the market wage as given, and then choose the level of effort at which employees are required to work. 12. Mandatory retirement only makes sense in a delayed compensation (or underpayment-overpayment)...
Question 4 Is the following statement true, false, or ambiguous? Justify your answer. “Since long-run average...
Question 4 Is the following statement true, false, or ambiguous? Justify your answer. “Since long-run average cost cannot exceed short-run average cost, the long- run average cost curve is the locus of all of the minimum points of the short- run average cost curves.”
Answer the following questions in detail on company "Vitamin Shoppe". (Note: Do not attempt to solve...
Answer the following questions in detail on company "Vitamin Shoppe". (Note: Do not attempt to solve if you cannot answer all questions.) 1) What is the current organizational structure? How would you label or describe it? 2) What are the strengths and weaknesses of this type of structure? 3) How do the strengths and weaknesses of this structure align with the new strategy or strategies? 4) Does the current organizational structure serve the new strategy? If so, how could it...
Answer the following questions on company "Vitamin Shoppe". (Note: Do not attempt to solve if you...
Answer the following questions on company "Vitamin Shoppe". (Note: Do not attempt to solve if you cannot answer all.) 1) What is the current organizational structure? How would you label or describe it? 2) What are the strengths and weaknesses of this type of structure? 3) How do the strengths and weaknesses of this structure align with the new strategy or strategies? 4) Does the current organizational structure serve the new strategy? If so, how could it be improved to...
Write an if-else statement for the following: If numDifference is less than -20, execute totalDifference =...
Write an if-else statement for the following: If numDifference is less than -20, execute totalDifference = -25. Else, execute totalDifference = numDifference. import java.util.Scanner; public class NumberDifference {    public static void main (String [] args) {       int totalDifference;       int numDifference;       Scanner scnr = new Scanner(System.in);       numDifference = scnr.nextInt(); // Program will be tested with values: -19, -20, -21, -22. //solution goes here//       System.out.println(totalDifference);    } }
(Note: Do not attempt to solve if you cannot answer all) Please answer the following discussion...
(Note: Do not attempt to solve if you cannot answer all) Please answer the following discussion questions in detail on company "Vitamin Shoppe": 1) Review the organizational structure for "Vitamin Shoppe" and determine if and how it needs to be adjusted in order to be aligned with the new strategy or strategies.
solve the following LP. Formulate and algebraically solve the problem. Show all steps. what is the...
solve the following LP. Formulate and algebraically solve the problem. Show all steps. what is the new optimal z value max z=65x1+35x2+20x3 8x1+6x2+x3<=48 4x1+2x2+1.5x3<=20 2x1+1x2+0.5x3<=8 x2<=5 x1,x2,x3>=0 interpret the meaning of the shadow prices
Solve the following problem. Solve the follwing problem. Horizontal analysis of income statement For 20Y2, McDade...
Solve the following problem. Solve the follwing problem. Horizontal analysis of income statement For 20Y2, McDade Company reported a decline in net income. At the end of the year, T. Burrows, the president, is presented with the following condensed comparative income statement: McDade Company Comparative Income Statement For the Years Ended December 31, 20Y2 and 20Y1 1 20Y2 20Y1 2 Sales $6,787,200.00 $6,060,000.00 3 Cost of goods sold 2,444,200.00 2,020,000.00 4 Gross profit $4,343,000.00 $4,040,000.00 5 Selling expenses $1,125,000.00 $900,000.00...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT