


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



Compulsory Question 1: Question 1. [10 marks]
Consider the following grammar:
a) Calculate nullable, FIRST and FOLLOW for this grammar: [5]
b) Construct the LL(1) parsetable. [4]
c) Why is this grammar a LL(1) grammar? Give reasons for your answer by referring to your LL(1) parsetable. [1]
Answer: a) nullable, FIRST and FOLLOW:
nullable FIRST FOLLOW S no (?! ā Session no (?! )$ ā Fact no!!? ā Facts yes!! ā Question no? )$ (^) ā
b) LL(1) parsing table
( ) $!? string S S ā Session $^ S ā Session $^ S ā Session $ Session Session ā ( Session ) Session
Session ā Facts Question
Session ā Facts Question
Fact Fact ā! string Facts Facts ā Fact Facts Facts ā Question Question? string
1/2 mark for each correct entry, max 4 marks.
c) This is not a LL(1) grammar because there are duplicate entries in the LL(1) parsing table. ( 1mark)
Choose between Question 2 and Question 3 below: Question 2 [10 marks]
a) Construct the LR(0) and SLR parsetables for the following grammar and its given LR(0)-DFA: [10]
Question 3. [10 marks]
Describe the Visitor Pattern. Supply the java code for the grammar above to illustrate this design pattern [8] Answer: A visitor implements an interpretation ā an object that contains a visit method for each syntax tree class. Each syntax tree class contains an accept method. An accept method serves as a hook for interpretations. It is called by a visitor class, and passes control back to an appropriate method in the visitor class. It allows the addition of a new interpretation without editing and recompiling existing classes, providing that each of the appropriate classes has an accept method, etc ā¦. Mark code on discretion.