Homework 3 Solution

Question 1 | Question 2 | Question 3


Question 1

Part a

The grammer in CNF (note: choice of names for introduced non-terminals is not unique):

id → ID
eq → =
sem → ;
add → +
sub → -
stmt → id st1
st1 → eq st2
st2 → exp sem
exp → id tail 
    | ID
tail → add exp 
     | sub exp

Part b

Solution


Question 2

Part a

  1. The shortest input that can be parsed is: NAME IDNUM EOF
  2. The parse tree is:

Part b

  1. The shortest sequence of tokens that would stump the parser is: NAME IDNUM INTLIT
  2. The parse tree is:
  3. The parser cannot choose between these two rules:
    grades → oneGrade
    grades → oneGrade COMMA grades
    

Part c

The rules with a common prefix are the ones with grades on the left. Here are the transformed rules:

grades → oneGrade grades'
grades' → ε | COMMA grades

Part d

The rules with immediate left recursion are the ones with stars on the left. Here are the transformed rules:

stars → STAR stars'
stars' → STAR stars' | ε

Question 3

Nonterminal X FIRST(X) FOLLOW(X)
program { EOF
stmts ID, IF, ε }
stmt ID, IF ID, IF, }
exp ID ;, )
tail +, -, ε ;, )

Last Updated: 3/8/2024     © 2024 CS 536