Question

In: Computer Science

Q6.13) Unary minuses can be added in several ways to the arithmetic expression grammar of Figure...

Q6.13) Unary minuses can be added in several ways to the arithmetic expression grammar of Figure 6.17 or Figure 6.18. Revise the BNF and EBNF for each of the cases that follow so that it satisfies the stated rule:

expr → expr + term | term

term → term * factor | factor

factor → ( expr ) | number

number → number digit | digit

digit → 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

[Figure 6.17. Revised grammar for simple integer arithmetic expressions]

expr → term { + term }

term → factor { * factor }

factor → ( expr ) | number

number → digit { digit }

digit → 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9

[Figure 6.18 EBNF rules for simple integer arithmetic expressions]

(a) At most, one unary minus is allowed in each expression, and it must come at the beginning of an expression, so -2 + 3 is legal (and equals 1) and -2 + (-3) is legal, but -2 + -3 is not.

(b) At most, one unary minus is allowed before a number or left parenthesis, so -2 + -3 and -2 * -3 are legal, but --2 and -2 + --3 are not.

(c) Arbitrarily many unary minuses are allowed before numbers and left parentheses, so everything above is legal.

Solutions

Expert Solution

Hello student, here is your detailed answer. I hope it helps.

A)

BNF

unaryminus → – expr | term
expr → expr + term | term
term → term * factor | factor
factor → ( expr ) | number
number → number digit | digit
digit → 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8| 9

EBNF

unaryminus → – expr | term
expr → term { + term }
term → factor { * factor }
factor → ( expr ) | number
number → digit { digit }
digit → 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8| 9

B)

BNF

expr → expr + term | term
unaryminus → expr + term | term
term → term * factor | factor
unaryminsus → term * factor | factor
factor → ( expr ) | number
number → number digit | digit
digit → 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8| 9

EBNF

expr → term { + term }
term → factor { * factor }
factor → ( expr ) | number
unaryminsus → ( - expr ) | number
number → digit { digit }
digit → 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8| 9

C)

BNF

unaryminus → ( – expr) | number
expr → expr + term | term
term → term * factor | factor
factor → ( expr) | number
number → number digit | digit
digit → 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8| 9

EBNF

unaryminus → ( – expr ) | number
expr → term { + term }
term → factor { * factor }
factor → ( expr ) | number
number → digit { digit }
digit → 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8| 9

THANK YOU


Related Solutions

An arithmetic expression can be represented in three different formats: infix, prefix, and postfix. In the...
An arithmetic expression can be represented in three different formats: infix, prefix, and postfix. In the infix notation, the operator comes between two operands. In the postfix notation, the operator comes after its two operands. For example, an expression a/b can be transformed to ab/. The following are some examples of the postfix expressions: 2+6*4 Its postfix notation is 2 6 4 * + 2-3+5/4*9-1 Its postfix expression is 2 3 – 5 4 / 9 * + 1 -...
describe Regulation of Gene Expression in Eukaryotes (Chapter 17). Describe 4 ways gene expression can be...
describe Regulation of Gene Expression in Eukaryotes (Chapter 17). Describe 4 ways gene expression can be regulated in Eukaryotes. (USE SENTENCES)
In Java, write class InfixToPostfixConverter co convert an ordinary infix arithmetic expression (assume a valid expression...
In Java, write class InfixToPostfixConverter co convert an ordinary infix arithmetic expression (assume a valid expression is entered) with single-digit integers (to make things easier) such as (6 + 2) • 5 - 8 / 4 to a postfix expression. The postfix version (no parentheses are needed) of this infix expression is 6 2 + 5 * 8 4 / - The program should read the expression into StringBuilder or String infix and use the Stack and Stack Interface class...
. Write a sequence of instructions to calculate the following arithmetic expression and store the result...
. Write a sequence of instructions to calculate the following arithmetic expression and store the result in register CX: 20 – 6 + (-10) - (-8) + 15 Trace the contents of registers, assume initial contents are 0000 ps(there are multiple boxes) Instruction AX BX CX DX Remark initial 0000 0000 0000 0000
Write a C++ function that takes in an arithmetic expression in prefix notation and converts it...
Write a C++ function that takes in an arithmetic expression in prefix notation and converts it into a binary tree, such that each operation is stored in a node whose left subtree stores the left operand, and whose right subtree stores the right operand.
: In this assignment you will write a C++ program that evaluates an arithmetic expression (represented...
: In this assignment you will write a C++ program that evaluates an arithmetic expression (represented with an infix notation), then outputs this expression in prefix form and also outputs the result of the calculation. The program will first convert the input infix expression to a prefix expression (using the Stack ADT) and then calculate the result (again, using the Stack ADT). The details are provided in the following sections.
Write a program (preferably in Java) that, given an arithmetic expression, first transforms it to a...
Write a program (preferably in Java) that, given an arithmetic expression, first transforms it to a postfix form, and then computes its value (by using stack-based algorithms). Assume that all the numbers in the arithmetic expression are one-digit numbers, i.e., each of these numbers is either 0, or 1, or 2, ..., or 9. For example, your program should correctly process expressions like 2+3*4, but there is no need to process expressions like 11+22.
Draw the binary tree representation of the following arithmetic expression: "( ( ( 6 + 3...
Draw the binary tree representation of the following arithmetic expression: "( ( ( 6 + 3 ) * ( 3 - 2 ) ) / ( ( 3 + 10 ) + ( ( 8 - 3 ) - 2 ) ) * 9 )" -Give an O(n)-time algorithm for computing the depth of all positions of a tree T, where n is the number of nodes of T.
a Composite class representing arithmetic expression. Participants: Expression – interface. Operations: double calculate(), boolean isTree(). Constant...
a Composite class representing arithmetic expression. Participants: Expression – interface. Operations: double calculate(), boolean isTree(). Constant – represent a number. Leaf node. Plus, Minus, Div, Mul – binary operations. Use abstract BinaryOperation class to implement common functionality Q. Create a code implementing a pattern, Implementing Composite implies multiple class. By my estimate, at least 3. In minimal Composite implementation, classes have two methods (or a constructor and 1 method). extensive coding is not required. (using Java)
a. 4 ways in which gene expression can be regulated in eukaryotes b. A substantial mutation...
a. 4 ways in which gene expression can be regulated in eukaryotes b. A substantial mutation occurs in the regulator region of a the lacI gene. What effects do you imagine this will have on the lac operon in a)the presence of lactose and b) in the absence of lactose?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT