CS536 Homework 2
Fall 2018
Due by 11:59 pm on Friday, November 16, 2018 (not accepted late)

Questions

Homework assignments must be done individually. Collaboration on homework assignments is not allowed.

Question 1:

Here's a CFG for declarations of structs in a language simliar to C. (Classes in C++ and Java are generalizations of structs.)

strt → STRUCT ID LCURLY members RCURLY SEMICOLON
members → decllist | λ
declList → declList decl | decl
decl	→ TYPE ID SEMICOLON | TYPE ID ASSIGN INTLIT SEMICOLON
  1. Although the CFG given above is not LL(1), some correct struct declarations can be parsed by a predictive parser. This is because certain inputs may never cause the parser to look at a table entry that contains two or more CFG rules. However, some declaration cannot be parsed by a predictive parser because the CFG is not LL(1).

    1. Give a struct declaration that is in the language of the CFG and can be parsed by a predictive parser.
    2. Give the shortest struct declaration that is the language of the CFG but cannot be parsed by a predictive parser.
  2. What is the earliest point in the course at which the parser broke? (i.e., a sequence of tokens that is a prefix of a valid input, but for which the parser would not know how to continue to build the parse tree top-down because it looks at a table entry that contains two or more CFG rules)? To answer this question, give all of the following:

    1. The sequence of tokens (a prefix of a valid input).
    2. The CFG rules that the predictive parser can't choose between to continue to grow the parse tree.
  3. One problem with the CFG is that it has a common prefix problem (it has not been left factored). Find the CFG rules with a common prefix and transform them by doing left factoring.

  4. Another problem with the CFG is that is has left recursion. Find the CFG rules that cause this and remove the left recursion.

Question 2:

This question concerns the following grammar.

program -> { stmts }
stmts ->  stmt  stmts 
       | λ
stmt -> ID = exp ; 
exp -> ID tail
tail -> + exp 
      | λ

Fill in the LL(1) Parse Table for this grammar.

{ } ID + ; = EOF
program
stmts
stmt
exp
tail

Handing in

Please include your name at the top your file.

Put your answers to all questions into one file named Homework2 with the appropriate file extension, e.g., Homework1.pdf

Electronically submit your work to the Homework 2 tab on Canvas.

Last Updated: 7/31/2018     © 2018 Charles Fischer