Docsity

Grammar - Code Generation - Solved Exam, Exams of Computer Science

Toggle sidebar
bg1
1
Compulsory Question 1:
Question 1. [10 marks]
Consider the following grammar:
S → Session $
Session → Facts Question
Session → ( Session ) Session
Facts → Fact Facts
Facts →
Fact → ! string
Question →? string
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)
pf3
pf4

Partial preview of the text

Download Grammar - Code Generation - Solved Exam and more Exams Computer Science in PDF only on Docsity!

Compulsory Question 1: Question 1. [10 marks]

Consider the following grammar:

S → Session $

Session → Facts Question

Session → ( Session ) Session

Facts → Fact Facts

Facts →

Fact →! string

Question →? string

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]

  1. What is the difference between a Concrete Parse Tree and an Abstract Parse Tree? [2] Answer : Concrete Parse Tree – represents the concrete syntax of the source language. Abstract Syntax – conveys the phrase structure of the source program with all parsing issues resolved but without any semantic interpretation.
  2. Consider the grammar below describing the abstract syntax of expressions.

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.