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.
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...
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...
(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."...
Module/Week 3 ASSIGNMENT (CONTROL STRUCTURES . IF ..ELSE) Write a C++ program that computes a student’s...
Module/Week 3 ASSIGNMENT (CONTROL STRUCTURES . IF ..ELSE) Write a C++ program that computes a student’s grade for an assignment as a percentage given the student’s score and total points. The final score must be rounded up to the nearest whole value using the ceil function in the <cmath> header file. You must also display the floating-point result up to 5 decimal places. The input to the program must come from a file containing a single line with the score...
Your task is to write a C program which performs encryption and decryption of a message...
Your task is to write a C program which performs encryption and decryption of a message using the substitution cipher algorithm. Write a C program which performs encryption and decryption using the substitution cipher algorithm. Your program must be fully automated (ie: it does not take any interactive user input) and either encrypt or decrypt depending on the files which exist in the program’s directory. If message.txt exists your program should read that file, encrypt the contents, and write the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT