________________________________________

CMSC 331 Summer 2002 Take Home Midterm

________________________________________

Directions

·         You have until the start of class on 24 June to hand in this extended quiz.

·         To be accepted the answers to the questions must be typed.

·         No late submissions will be accepted.

·         If any outside sources are used they must be cited. Each question is weighted equally.

·         You must work individually on this test.

o        Failure to do so will result in a grade of zero for the test !

o        No collaboration of any kind is permitted.

Questions

 

1. If possible using only the algorithm I showed you in class, remove the left recursion from the following grammar, if its not  possible, explain why you cannot remove the left recursion. (Note: Any symbol not on the left hand side of any production is a terminal symbol)

 

       <c> ->   <q>  *  <c>   |   <c>  @   <q>  |   d  <c>

       <q> ->   <q>  &  <f>     |    <q>  #    <f>  |   <f>

       <f> - >   a    |     b    |  d  |  <c>

 

2.          Rewrite the following grammar expressed in EBNF to be in BNF

 

<g> ->   [ l ]  { a { b c }  b } <m>

<l> ->   z { z }

<m>  ->  x [ y ] z

 

 

3.      If possible using only the algorithm I showed you in class, remove the left recursion from the following grammar, if it’s not possible, explain why you cannot remove the left recursion. (Note: Any symbol not on the left hand side of any production is a terminal symbol)

 

      <z>      ->      <y>  %  <x>     |  <z>  ( <y> )   |    a  <z>  

     <x>       ->     <z>  *  <x>       |    <x> / <y>    |    <w>

     <w>     ->       e   |   f  |   g   

 

 

4.         Prove that the following grammar is ambiguous

 

A->  A  ‘ A A ‘ A  | L |  ‘{‘ A ‘}’

 

 Where L is a token with lexemes a, b c.

 

5. Rewrite the grammar in Problem 4, if possible to remove the ambiguity.

 

6.      Given the following BNF:

 

          <exp> -> ‘{‘ <list> ‘}’ |   ‘(‘ <list> ’ )’  |  ‘[‘ <list> ‘]’  |  <char>

          <list> -> <list> , <exp> | <exp>

          <char> -> a  |  b | c |  d | e

 

      Show the parse tree (if possible) for

 

   ( ( [ a , b, { c  } ] , e ) c, { b } )

 

7.     Give the EBNF specification for the language L that is made up of the chars a,b and c such that sentences in the language have the form

 

                       L : sqsR

 

 where

·        s is a string of any combination of the characters a and b

·        sR  is just that same string s reversed

·        q is an  odd number of c's followed by  either an even number of b's or an even number of a’s.

 

 

8.  Translate the following grammar expressed in EBNF into BNF

 

<goal> ->   { a b [ <bar> ] } [<foo>]  (<bar>  | <baz>)

<bar> ->    b [ e ] f

<foo> ->   d {  d  e}

<baz>  ->   g  { g }

 

9.       Create the first/follow tables for the following grammar:

 

G -> S 
S -> ‘( ‘ L  ‘)’  | M
L -> L , S | S
M -> a | b

 

 

10.    Generate the parse table for the grammar in Problem 9.

 

 

11. Show the actions of the parser for the grammar in Problem 9 on the following string :

 

            ((a), (a))

 

12.    Explain why a grammar must be left factored for a predictive parser to work.

 

13.    Consider the following grammar:

 

E ->  E  *  F   |  E  / F   | F

     F ->  F   +  T   | F  -  T   | T

     T ->  Number  |  (  E  )

 

What’s the value of the following expression given that Number is any double precision number and that “*” denotes multiplication, “/” denotes” division, “+” denotes addition and “-“ denotes subtraction.

 

4.5 + 5.6 * 2.0 – 3.4 / 2.0

 

14.

+ 1 * 5.2 – (9.5 –4.2) * 6.0 / 5.6

 

 

 

15.     Consider sample grammar for the recursive descent technique Consider the example looked at in class.  You need to rework that code. In the code there was a production of the form :

 

 

 

C  -> D { '^' D }

 

If that grammar had the  ‘ operator being left associative, then make it right associative. Otherwise if the grammar had the ‘ operator being right associative, then make it left associative. You need not actually code the entire program as a  solution[1] just sketch the code that needs to change and how to change it.

 

 

Extra Credit

 

A.      [2 points] There are 12 coins, all identical in appearance, and all identical in weight except for one, which is either heavier or lighter than the remaining 11 coins. Devise a procedure to identify the counterfeit coin in only 3 weighings with a fair balance.

B.       [2 points]  Who said “It's easier to beg forgiveness than to get permission”  , and what if anything does it have to do with programming languages?

 

 

 

 

 

 

 

 

 

 

 

         



[1] although you may want to just to verify you have done it correctly.