Question

This BNF grammar defines expressions with three operations, *, -, and +, and the variables “a”, “b”, “c”, and “d”. <e...

This BNF grammar defines expressions with three operations, *, -, and +, and the variables “a”, “b”, “c”, and “d”.

<expr> ::= <thing> | <thing> * <expr>
<object> ::= <element> | <element> – <object>
<thing> ::= <object> | <thing> + <object>
<element> ::= a|b|c|d|(<object>)

a) Give the order of precedence among the three operations.

b) Give the order (left-to-right or right-to-left) of execution for each operation.

c) Explain how the parentheses defined for the nonterminal <element> may be used in these expressions. Describe their limitations.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

ANSWER:

For better understanding, re-writing the grammar as:

< expr >::=< thing > * < erpr > \ < thing > < thing >..=< thing > + < object > \ < object > < object >:<element > - < object

a)

If we draw the parse tree for an expression, ' - ' sign will occur at the bottom with ' + ' above it then ' * ' .

Since the rule producing ' - ' is at the lowest level, so it will have highest precedence, followed by ' + ' and then ' * ' .

\\-> + > *

b)

< expr >..=< thing > * < expr > < object >..=< element >-< object >

These productions are right recursive, so ' * ' and ' - ' are right recursive means their order of execution will be right to left.

< thing >::=< thing > + < object >

This production is left recursive so order of execution of '+' is left to right.

c)

Parenthesis can be used to give the expression inside the parenthesis higher order of precedence.

But on observing the grammar here (<object>), parenthesis can only contain expression involving subtractions as <object> can only produce subtraction part.

So limitation is that parenthesis can only have expressions that involve subtraction.

Add a comment
Know the answer?
Add Answer to:
This BNF grammar defines expressions with three operations, *, -, and +, and the variables “a”, “b”, “c”, and “d”. <e...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1. Write a BNF description of the logical expressions and the relational expressions in C++. Make...

    1. Write a BNF description of the logical expressions and the relational expressions in C++. Make sure that the BNF reflects the order of precedence of the operators, as well as, the associativity rules. 2. Using the BNF rules in 1., give a rightmost derivation and show a parse tree for the expression below. 3. Prove that the following grammar is ambiguous and rewrite the grammar to remove ambiguity «newexp> → «newexp> @ <newexp> ulvl w I <other> <other> →

  • 4. Assume the following rules of associativity and precedence for expressions: Precedence Highest *, /, not...

    4. Assume the following rules of associativity and precedence for expressions: Precedence Highest *, /, not +,-,&, mod - (unary) =,/=,<,<=,>=,> and or, xor Lowest Associativity Left to right Show the order of evaluation of the following expressions by parenthesizing all sub expressions and placing a superscript on the right parenthesis to indicate order. For example, for the expression a+b*c+d the order of evaluation would be represented as ((a+(b*c)1)2 +d)3 a * b - 1 + c a * (b...

  • Given the grammar G6 from the book: G6 Extend this grammar to include three additional operators (your answer must be o...

    Given the grammar G6 from the book: G6 Extend this grammar to include three additional operators (your answer must be one grammar that adds all of): A. Subtraction, -, with left associativity and precedence equal to that of addition (+) [2pt] B. Division, /, with left associativity and precedence equal to that of multiplication () [2pt] C. Unary negation, , with right associativity and precedence higher than multiplication but lower than parentheses 0 [3 pt] Unary negation is (typically) an...

  • (10] Eliminate left recursion from the grammar A Ba |Aa c B Bb | Ab 1...

    (10] Eliminate left recursion from the grammar A Ba |Aa c B Bb | Ab 1 d A Ad IB A BA ASJAE Consider the following grammar G: S'S S (S)S|e fa) (10] Construct the collection of the sets of LR(0) items (b) [5] When constructing the action table of SLR parser of G what are the rules to determine the parsing actions? That is, what is the rule for a shift action at state /? What is the rule...

  • Stacks are used by compilers to help in the process of evaluating expressions and generating machine...

    Stacks are used by compilers to help in the process of evaluating expressions and generating machine language code.In this exercise, we investigate how compilers evaluate arithmetic expressions consisting only of constants, operators and parentheses. Humans generally write expressions like 3 + 4and 7 / 9in which the operator (+ or / here) is written between its operands—this is called infix notation. Computers “prefer” postfix notation in which the operator is written to the right of its two operands. The preceding...

  • Write a C++ program that takes two sets ’A’ and ’B’ as input read from the...

    Write a C++ program that takes two sets ’A’ and ’B’ as input read from the file prog1 input.txt. The first line of the file corresponds to the set ’A’ and the second line is the set ’B’. Every element of each set is a character, and the characters are separated by space. Implement algorithms for the following operations on the sets. Each of these algorithms must be in separate methods or subroutines. The output should be written in the...

  • 5. A three-heap with n elements can be stored in an array A, where A[O] contains...

    5. A three-heap with n elements can be stored in an array A, where A[O] contains the root of the tree. a) Draw the three-heap that results from inserting 5, 2, 8, 3, 6, 4, 9, 7, 1 in that order into an initially empty three-heap. You do not need to show the array representation of the heap. You are only required to show the final tree, although if you draw intermediate trees. b) Assuming that elements are placed in...

  • 1. (p. 2-3.) Which of the following is NOT a reason for studying concepts of programming...

    1. (p. 2-3.) Which of the following is NOT a reason for studying concepts of programming languages according to Sebesta? a. Increased capacity to express ideas. b. Improved background for choosing appropriate languages. c. Increased ability to design new languages. d. Increased ability to learn new languages. 2. (p. 5-6.) What programming language has dominated scientific computing over the past 50 years? a. FORTRAN b. ALGOL c. SNOBOL d. PL/I 3. (p. 6.) What programming language has dominated artificial intelligence...

  • Three Hfr strains of E. coli, each with genotype StrS, lacZ+, A, B, C, D are...

    Three Hfr strains of E. coli, each with genotype StrS, lacZ+, A, B, C, D are mated to an F- strain with the genotype StrR, lacZ-, a, b, c, d. Samples of conjugating strains are collected and disrupted every minute for one hour using a blender. Cells are plated under conditions that allow the lacZ+, A, B, C, and D alleles to be identified. The time at which each dominant allele is transferred is shown in the table. lacZ A...

  • 2. Three charges are fixed at positions along the a-axis as positions -d, o, and +d....

    2. Three charges are fixed at positions along the a-axis as positions -d, o, and +d. The charges at -d and d are both and the charge at O is positive. (a) A positively charged object of mass m is placed on the x-axis between O and d, close to the positlicn the three charges described above do not move as a result of this new charged object, describe the mois object after it is released as it moves in...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT