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

  How to construct an nfa

Give a construction that assumes you are given a DFA for L and show how to construct an NFA (with or without ε-moves) to recognize sort(L).

  Question 1 nbspconsider a logic function with three outputs

question 1. nbspconsider a logic function with three outputs a b and c and three inputs d e and f. the function is

  How to express correctness properties in ltl

Express the given correctness properties in LTL. Defne propositions/variables to model the events mentioned in the question. If a parent process calls the blocking waitpid() system call then it is blocked until child process terminates.

  Scrum vs plan-based software development strategies

Develop a visual rendering of each approach using Microsoft Visio or its open source alternative, Dia. Note: The graphically depicted solution is not included in the required page length.

  Create a mealy machine which produces the output

Create a Mealy Machine which produces the output of 1 whenever discrepancy in above pattern is detected, and produces the output of 0 otherwise. Write states meaningful names.

  1-is situational leadership model a useful model and how

1-is situational leadership model a useful model and how can we apply this model effectively?2-how is the

  Productions of nonterminals as right regular grammars

Rewrite the productions for each of the following nonterminals as right regular grammars: Identifier, Float. Show the moves made using the DFSA for identifiers in accepting.

  Verify that a number in base b3 can be converted to base b

Verify that a number in base b can be converted to base b3 by partitioning the digits of the base b number into groups of three consecutive digits starting at the radix point and proceeding both left and right and converting each group into a base..

  Conclude that sat is np-complete

Let  be a 3cnf-formula. An  assignment to the variables of  is one where each clause contains two literals with unequal truth values. In other words an  -assignment satisfies  without assigning three true li..

  How the computations of the new az or bearings

Compute the following Azimuths into Bearings a. 132°45'31" b. 289°12'12" c. 220°47'39" Compute the following Bearings into Azimuths a. N00°00'59"E b. S89°14'56"E c. S45°00'00"W

  Prepare a research strategy

A research strategy is a plan of action that gives direction to your efforts enabling you to conduct your research systemically rather than haphazardly.

  Te speed team at ibmsteve ward the vice president of

the speed team at ibmsteve ward the vice president of business transformation and chief information officer at ibm was

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