sheet2

# sheet2 -...

This preview shows page 1. Sign up to view the full content.

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS 160: Practice Sheet 2 (do not turn in)      #1  You  are  trying  to  build  a  small  tool  for  people  to  turn  Boolean  expressions  into  hardware.  As  part  of  this  tool,  you  need  to  parse  out  Boolean  expressions,  and you have come up with the following grammar.      bexpr  →    bexpr or bexpr     |     bexpr and bexpr     |    not bexpr     |    (bexpr)     |    true     |    false    The  problem  is  that  you  need  to  support  precedence  (“not”  is  applied  first,  followed  by  “and”,  and  finally  “or”).    Modify  the  above  grammar  to  be  unambiguous  and  correctly  handle  precedence  (don’t  worry  about  left  recursion).      #2  Consider  the  following  grammar  of  lists  (looks  kind  of  like  Lisp).    Note  that  there are four different terminals: “(“, “)”, “a”, and “,”     1 S→() 2 |a 3 | (A) 4 A→S 5 | A,S     a) Show that the grammar is not LL(1) {hint: you should be able to do this by  computing some of the FIRST sets}    b) Rewrite the grammar so that it is suitable for recursive decent parsing    c) If you were going to build a table driven top‐down parser for this grammar  from (b), how big would the table be (i.e., how many entries would it have)      #3 Eliminate the left recursion from the grammar A B C → | | → | | → | B A C B B d h g d ff B d f f #4 Compute the FIRST and FOLLOW sets for each of the non-terminals in the following grammar A B C D → | → | → | → | BCm C DB ε k ε p r #5 Which of the following is true about an LR(1) parser generator such as yacc: a) Generates a top-down parser b) Generates a shift-reduce parser c) Generates a parser whose actions correspond to a leftmost derivation d) Generates a bottom-up parser e) Takes a regular expression as input f) Generates a parser whose actions correspond to a rightmost derivation g) Takes a grammar as an input     #6 Consider the following grammar which describes a language of set operations  (what the language “means” is not important for these questions).  This language  has three different operators: “∪ ”, “∩ ”, and “∇ ”.  Examine this grammar and  answer the questions listed below    1 S→A 2 A → A ∪B 3 |B 4 B → C ∩B 5 |C 6 C → D ∇C 7 |D 8 D → list 9 | null a) What are the non‐terminals in this grammar? What are the terminals?      b) According to the above grammar, what is the order of precedence of the 3  operators (list the highest precedence first)?      c) Which operator or operators (if any) are left‐associative?      #7 Bottom Up Parsing    The ACTION and GOTO tables (below) parse simple expressions with multiply  and add.  Use the ACTION and GOTO tables, with the grammar below, to parse  the string “num * num + num”.    Grammar:    1 S→E 2 E → E + num 3 | E * num 4 | num               Solution:      Stack | Input | index | entry ---------------------------+--------------------------+------------+--------   ...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online