Eliminate left recursion from the original grammar

Assignment Help Theory of Computation
Reference no: EM131101909

Assignment :

Exercise 1: Consider the context-free grammar:

5 → SS + \ss* \ a

and the string aa + a*.

a) Give a leftmost derivation for the string.

b) Give a rightmost derivation for the string.

c) Give a parse tree for the string.

d) Is the grammar ambiguous or unambiguous? Justify your answer.

e) Describe the language generated by this grammar.

Exercise 2: Repeat Exercise 1 for each of the following grammars and strings:

a) S     0 5 1 1 0 1 with string 000111.

Exercise 3: Design grammars for the following languages:

a) The set of all strings of 0s and 1s such that every 0 is immediately followed by at least one 1.

b) The set of all strings of 0s and 1s that are palindromes; that is, the string reads the same backward as forward.

c) The set of all strings of 0s and 1s with an equal number of 0s and 1s.

Exercise 4: The following is a grammar for regular expressions over symbols a and b only, using + in place of I for union, to avoid conflict with the use of vertical bar as a metasymbol in grammars:

rexpr        rexpr + rterm | rterm

rterm        rterm rfactor  \ rfactor

rfactor      rfactor * / rprimary

rprimary    a|b

a) Left factor this grammar.

b) Does left factoring make the grammar suitable for top-down parsing?

c) In addition to left factoring, eliminate left recursion from the original grammar.

d) Is the resulting grammar suitable for top-down parsing?

Programming Assignment:

A moderately simple Scheme (MSS) expression is similar to those of our previous language VSS, but supports the Boolean type in addition to the floating-point type. This includes the literals true and false, in addition to Boolean and relational operators. There is also a conditional if expression. Here is the grammar:

Prog         →   expr+

expr         →   DOUBLE
               |    BOOLEAN
               |    ID
               |   '(' RATOR expr* ')'
               |   '("def ID expr ')'
               |   '("if expri exprz exprj
BOOLEAN  →   'true' | 'false'
RATOR     →   ARITHMETIC I RELATIONAL I BOOLEAN
ARITHMETIC → '+' | '-' | '*' + '/'
RELATIONAL → '=' |'>'|'<'
BOOLEAN     → '&' |'|' |'!'

The conditional expression first evaluates its first expression expr1 to obtain testVal. If testVal is true then the conditional expression evaluates to the value of expr2, and if testVal is false then it evaluates to the value of expr J (it is a semantic error if expri is not of Boolean type).

You may wish to distinguish between Boolean and floating-point values when evaluating expressions to ensure semantic correctness, but I do not require such runtime error checking. It is also not required that you distinguish between Boolean and floating-point expressions in your grammar (e.g., your grammar need not enforce that the conditional's test expression is of type Boolean). Also, it's not required that you implement the Boolean operators (and, or, and not), but you should implement the relational operators (equal, greater-than, and less-than).

I suggest that you revise the representation of values in your interpreter. One way is to define a Val class capable of wrapping a Double or a Boolean value. Your symbol table, which stores variable bindings, would bind identifiers to Val objects, literals and variables would evaluate to Val objects, and operators would input a list of Val arguments and output a Val object.

As in the previous assignment (VSS language), your interpreter evaluates the top-level expressions in order and prints the value of the last expression. For programs that contain more than one top-level expression, all but the last one are generally there for side-effect. Here are some sample runs:

> java run
(def a (if (< 5 7) 2 4))
(* a 6)
^Z
12.0

> java run

(def flag (= 6 (+ 3 5)))
(if flag (+ 1 2) (* 3 4))
^Z
12.0

This illustrates semantics involving the Boolean type:

true => true
(! true) => false
(&) => true // identity for and
(& true) => true
(& true false) => false
(|) => false // identity for or
(| false) => false
(> 7) true // each element is > than its successor
(> 7 5) =>. true
(> 7 5 2) =>. true
(> 7 8 5 3) => false
(< 4 6) =>, true
(= 4 (+ 2 2)) =>, true
(= 3 3 7) => false
(!(< (+ 2 2) (* 2 3))) => false
(if true 3 4)              => 3.0
(if (< 6 3) 5 (* 6 3))  => 18.0
(if (= 3 (+ 2 1))
(if true 4 5)

(if false 6 7)) => 4.0

Reference no: EM131101909

Questions Cloud

Discuss three reasons for the importance of data retrieval : Discuss three (3) reasons for the importance of data retrieval and analysis in health care. In your discussion include four (4) tasks that are associated with data retrieval and analysis.
Why do states more often than not abide by international law : Why is enforcement such an issue at the international level but not so much at the domestic level? At the same time, states do indeed often abide by international law. Why do states more often than not abide by international law?
Calculate the molar mass and the osmotic virial coefficient : Calculate the molar mass and the osmotic virial coefficient, B, of the fraction from the following data:
First-best level of investment : a. Draw a timeline of the game. What is the ex-ante period and the ex-post period in this model? b. What is the first-best level of investment? Explain.
Eliminate left recursion from the original grammar : Give a leftmost derivation for the string and implement the relational operators - operators would input a list of Val arguments and output a Val object.
Are resonance structures needed to describe the structure : What would you predict for the bond lengths in the C-O formate ion relative to those in CO2?
Calculate the monopolistic price : Begin your analysis by finding a benchmark socially efficient price and level of output. Then calculate the monopolistic price and level of output. Finally calculate the threshold dollar amount that the monopolist should budget for lobbying to bar..
Contemplating earning the fellow of the american college : Nathan is contemplating earning the Fellow of the American College of Healthcare Executives (FACHE) certification. He knows from experience that he can get a well-paying job with his current degree;
Create visual logic files to execute each of the tasks : Create a 1/2-page Memo using the Memo Template for each of the tasks. Each document should include: A brief description of the task.

Reviews

Write a Review

Theory of Computation Questions & Answers

  Each part of this problem that the eax register

Assume for each part of this problem that the EAX register contains 00 00 00 4F and the doubleword referenced by value contains FF FF FF 38. Determine whether each of the conditional jump statements causes a jump to dest.

  Adjust the proposal as required

Adjust the proposal as required. Post whatever you have accomplished to the folder for the GDI to review. Inform your GDI on any difficulties and show stoppers that you might encounter.

  Problem related to lcg

Consider the LCG defined by m = 16, a = 5, c = 3, and Z0 = 7. Compute until Z19 and verify that when i = 16, the exact the same order will show up. Show all results in a table.

  What is the network address

What is the network address - what is the range of host IP addresses (low to high)?

  Write down an illustration of a hypothetical situation when

question 1 explain why t1s values above 0 versus c will not matter for comparing algorithms.question 2 give an example

  Write mathematical formulation for non-terminal

Non-terminal A is useless if there is no derivation from start symbol to string of tokens in which A appears. Write a mathematical formulation of this property.

  A new manager is starting in the organisation shortly you

a new manager is starting in the organisation shortly. you have been asked to provide an outline to this new-starter so

  Find the square roots of the matrix

Compute Ak and eAt using the Cayley - Hamilton theorem. Find the square roots of the matrix, i.e., find all matrices B such that B2 = A using the Cayley - Hamilton theorem method.

  Essay is about qantas emirates alliance focus on change

essay is about qantas emirates alliance. focus on change took place in qantas airline due to this alliancepart 11.

  Exchanging the accept and reject states

If M is a DFA accepting language B, then exchanging the accept and reject states gives a new DFA accepting the complement of B.

  Write grammar for language comprising of strings

Write down the grammar for language comprising of strings which have n copies of letter a followed by same number of copies of letter b, where n > 0.

  Provide state diagram of dfas recognizing the languages

Give nondeterministic finite automata accepting the set of strings of 0's and 1's such that there are two 0's separated by a number of positions that is a multiple of 3.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd