Question

In: Computer Science

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

Solutions

Expert Solution

1)

Dangling else is an ambiguity which is created when there are 2 if statements followed by single else. So the compiler cannot understand which if the else belongs to, so that is called dangling else.

Demo of program output with inner and outer if separately.

#include <iostream>

using namespace std;

int main() {
int a;
//input a number
cout<< "\nEnter a number";
cin>>a;
cout << "\nDemo of dangling else, outer if";
if(a>5)
{
cout<<"\na is greater than 5";
if(a>100)
cout<<"\na is greater than 100";
}
else
cout<<"\na is less than 100";
//example 2
cout << "\nDemo of dangling else, inner if";
if(a>5)
{ cout<<"\na is greater than 5";
if(a>100)
cout<<"\na is greater than 1000";
else
cout<<"\na is less than 1000";
}
}

To get rid of dangling else, put braces for the compiler to know which if is being referred for the else.

Sample output


Related Solutions

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
write pseudocode not c program If- else programming exercises 1.    Write a C program to find...
write pseudocode not c program If- else programming exercises 1.    Write a C program to find maximum between two numbers. 2.    Write a C program to find maximum between three numbers. 3.    Write a C program to check whether a number is negative, positive or zero. 4.    Write a C program to check whether a number is divisible by 5 and 11 or not. 5.    Write a C program to check whether a number is even or odd. 6.    Write...
Describe how each of Algol 60, Pascal, and Pytho 3 handles the dangling else problem.
Describe how each of Algol 60, Pascal, and Pytho 3 handles the dangling else problem.
JAVA Program 2: In Order Using an IF/ELSE IF/ELSE structure, write a program that will prompt...
JAVA Program 2: In Order Using an IF/ELSE IF/ELSE structure, write a program that will prompt the user for three numbers and displays them in ascending order. First, the program will prompt the user for each of the three numbers. Next, find the smallest value of the three. Then decide which of the other two is the next smallest. Then have the program display the three numbers in ascending order. Be sure to do the following: Determine what the input...
Program must be in C++! Write a program which: Write a program which uses the following...
Program must be in C++! Write a program which: Write a program which uses the following arrays: empID: An array of 7 integers to hold employee identification numbers. The array should be initialized with the following values: 1, 2, 3, 4, 5, 6, 7. Hours: an array of seven integers to hold the number of hours worked by each employee. payRate: an array of seven doubles to hold each employee’s hourly pay rate. Wages: an array of seven doubles to...
(17pts)Write, compile, and test a C++ program that uses an if-else structure for problem 3.7 on...
(17pts)Write, compile, and test a C++ program that uses an if-else structure for problem 3.7 on page 108.  Use the format specified earlier (initial block of comments with TCC logo, name, etc) Display instructions so that the user understands the purpose of the program and what to enter. Display the results in increasing (non-decreasing) order. Run the program for the following 6 test cases.Turn in a printout of the program and printouts of the 6 test cases.(The result should...
1. ​ a)​Write a Matlab program to define student grade program and student number with if-else...
1. ​ a)​Write a Matlab program to define student grade program and student number with if-else statement logic​​​​​​​​​ b)​Write a Matlab program for sensor selection of temperature, pressure, flow sensor with if-else statement logic​​​​​​​​​ ​ 2.​Assume there are four letters from an information source with probabilities as A1 0.5 A2 0.3 A3 0.1 A4 0.1 Generate the tag and find the corresponding gaps and binary values for each stage and the sequence of the symbols which are coded are a1a3a2a4a1.​​​...
C++: Write correct C++ code for a nested if-else if-else structure that will output a message...
C++: Write correct C++ code for a nested if-else if-else structure that will output a message based on the logic below. A buyer can pay immediately or be billed. If paid immediately, then display "a 5% discount is applied." If billed, then display "a 2% discount is applied" if it is paid in 30 days. If between 30 and 60 days, display "there is no discount." If over 60 days, then display "a 3% surcharge is added to the bill."...
For your first project, write a C program (not a C++ program!)that will read in a...
For your first project, write a C program (not a C++ program!)that will read in a given list of non-negative integers and a target integer and checks if there exist two integers in the list that sum up to the target integer. Example:List: 31, 5, 8, 28, 15, 21, 11, 2 Target: 26 Yes!, 44 No! your C program will contain the following: •Write a function that will make a copy of the values from one array to another array....
JAVA Write a program that demonstrates how various exceptions are caught with catch (Exception exception) This...
JAVA Write a program that demonstrates how various exceptions are caught with catch (Exception exception) This time, define classes ExceptionA (which inherits from class Exception) and ExceptionB (which inherits from class ExceptionA). In your program, create try blocks that throw exceptions of types ExceptionA, ExceptionB, NullPointerException and IOException. All exceptions should be caught with catch blocks specifying type Exception.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT