0% found this document useful (0 votes)
193 views

CSE 4310 - CD - Assignment II

The assignment contains 9 questions analyzing sample context-free grammars: constructing parse trees and derivations, determining ambiguity and languages generated, building recursive descent and predictive parsers, and developing LR parsing tables using SLR and LALR algorithms. Students must answer questions about parse trees, ambiguity, shift-reduce

Uploaded by

yonas
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
193 views

CSE 4310 - CD - Assignment II

The assignment contains 9 questions analyzing sample context-free grammars: constructing parse trees and derivations, determining ambiguity and languages generated, building recursive descent and predictive parsers, and developing LR parsing tables using SLR and LALR algorithms. Students must answer questions about parse trees, ambiguity, shift-reduce

Uploaded by

yonas
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Adama Science and Technology University (ASTU)

School of Electrical Engineering and Computing (SoEEC)


Department of Computer Science Engineering (CSE)
Compiler Design (CSE 4310) – Assignment II

Instruction: Attempt the following questions and prepare your assignment report handwritten
which you can submit as softcopy either by scanning or capturing. The report should be well
organized and submitted in a *.pdf format.

1. Consider the grammar

𝑆 → (𝐿 )| 𝑎
𝐿 → 𝐿, 𝑆| 𝑆
a. What are the terminals, nonterminals, and start symbol?
b. Find parse trees for the following sentences:
i. (𝑎, 𝑎)
ii. (𝑎, (𝑎, 𝑎))
iii. (𝑎, ((𝑎, 𝑎), (𝑎, 𝑎)))
c. Construct a leftmost derivation for each of the sentences in (b)
d. Construct a rightmost derivation for each of the sentences in (b)
e. What language does this grammar generate?
2. Consider the grammar
𝑆 → 𝑎𝑆𝑏𝑆 | 𝑏𝑆𝑎𝑆 | 𝜆
a. Show that this grammar is ambiguous by constructing two different leftmost
derivations for the sentence 𝒂𝒃𝒂𝒃.
b. Construct the corresponding rightmost derivations for 𝒂𝒃𝒂𝒃.
c. Construct the corresponding parse trees for 𝒂𝒃𝒂𝒃.
d. What language does this grammar generate?
3. Consider the grammar

𝑏𝑒𝑥𝑝𝑟 → 𝑏𝑒𝑥𝑝𝑟 𝒐𝒓 𝑏𝑡𝑒𝑟𝑚 | 𝑏𝑡𝑒𝑟𝑚

𝑏𝑡𝑒𝑟𝑚 → 𝑏𝑡𝑒𝑟𝑚 𝒂𝒏𝒅 𝑏𝑓𝑎𝑐𝑡𝑜𝑟 | 𝑏𝑓𝑎𝑐𝑡𝑜𝑟


𝑏𝑓𝑎𝑐𝑡𝑜𝑟 → 𝒏𝒐𝒕 𝑏𝑓𝑎𝑐𝑡𝑜𝑟 | (𝑏𝑒𝑥𝑝𝑟) | 𝒕𝒓𝒖𝒆 | 𝒇𝒂𝒍𝒔𝒆

a. Construct a parse tree for the sentence 𝒏𝒐𝒕(𝒕𝒓𝒖𝒆 𝒐𝒓 𝒇𝒂𝒍𝒔𝒆)?


b. Show that this grammar generates all Boolean expressions.
c. Is this grammar ambiguous? Why?
4. Construct a recursive-descent parser with backtracking for the grammar in question 2.
5. Construct a predictive parser for the grammar in question number 3.
6. Using the grammar in question 1,
a. Construct a rightmost derivation for (𝑎, (𝑎, 𝑎)) and show the handle of each right-
sentential form.
b. Show the steps of the shift-reduce parser corresponding to the rightmost
derivation of (a).
c. Show the steps in the bottom-up construction of a parse tree during the shift-
reduce parse of (b)
7. Consider the ambiguous grammars

𝑆 → 𝐴𝑆 | 𝑏
𝐴 → 𝑆𝐴 | 𝑏
a. Construct the collection of sets of 𝑳𝑹(𝟎) items for this grammar.
b. Construct a DFA in which each state is an 𝑳𝑹(𝟎) item from (a).
c. Construct the parsing table using the SLR algorithm.
d. Show all the moves allowed by the table from (c) on the input 𝒂𝒃𝒂𝒃.
e. Construct the canonical parsing table.
f. Construct the parsing table using the LALR algorithm.
8. Construct an SLR parsing table for the grammar in question number 3.
9. Construct a canonical LR parsing table for the following expression grammar:
E → E + E | E ∗ E | (E) | id
Obtain the sequence of configurations of the parser for the input string
w = 𝑖𝑑 ∗ (𝑖𝑑 + 𝑖𝑑).

You might also like