Compiler Design Practice, Exercises of Design

Questions on Compiler Design subject

Typology: Exercises

2018/2019

Uploaded on 06/23/2023

aditya-singh-79
aditya-singh-79 🇮🇳

1 document

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
VELLORE INSTITUTE OF TECHNOLOGY
SCHOOL OF COMPUTER SCIENCE AND ENGINEERING
BCSE305L COMPILER DESIGN
DIGITAL ASSIGNMENT
1. Below is a Context Free Grammar G= ({S}, {(,)}, P, S) for strings of balanced
parentheses where P is as follows
S (S)S
S ε
Construct the LR (0) configurating sets for this grammar. Note that when dealing with
the production S ε, there is only one LR (0) item, which is S →·
2. Consider the below CFG
Stmts→Stmt
Stmts→Stmts; Stmt
Stmt →Var = E
Var →id [E]
Var→id
E →id
E → (E)
a) Construct the set of LR (0) items and SLR(1) parsing table
b) Construct the set of LR (1) items
c) Show that the grammar is both CLR(1) and LALR(1)
3. Consider the following grammar productions. Assume you have an attribute E.odd
which can be set to either true, false, or unknown, and an attribute CONST.val which is
the value of the constant.
E CONST { E.odd = ?? }
| ID { E.odd = unknown }
| E1 + E2 { E.odd = ?? }
| E1 - E2 { E.odd = ?? }
| E1 * E2 { E.odd = ?? }
| ( E1 ) { E.odd = ?? }
| - E1 { E.odd = ?? }
Add rules to the attribute grammar to calculate E.odd for each grammar production.
4. Consider the following grammar, which generates expressions formed by applying
”+” to integer and floating point constants. When two integers are added, the result is
integer, otherwise, it is a float.
pf2

Partial preview of the text

Download Compiler Design Practice and more Exercises Design in PDF only on Docsity!

VELLORE INSTITUTE OF TECHNOLOGY

SCHOOL OF COMPUTER SCIENCE AND ENGINEERING

BCSE305L – COMPILER DESIGN

DIGITAL ASSIGNMENT

  1. Below is a Context Free Grammar G= ({S}, {(,)}, P, S) for strings of balanced parentheses where P is as follows S →(S)S S → ε Construct the LR (0) configurating sets for this grammar. Note that when dealing with the production S → ε, there is only one LR (0) item, which is S →·
  2. Consider the below CFG Stmts→Stmt Stmts→Stmts; Stmt Stmt →Var = E Var →id [E] Var→id E →id E → (E) a) Construct the set of LR (0) items and SLR(1) parsing table b) Construct the set of LR (1) items c) Show that the grammar is both CLR(1) and LALR(1)
  3. Consider the following grammar productions. Assume you have an attribute E.odd which can be set to either true, false, or unknown, and an attribute CONST.val which is the value of the constant. E → CONST { E.odd = ?? } | ID { E.odd = unknown } | E1 + E2 { E.odd = ?? } | E1 - E2 { E.odd = ?? } | E1 * E2 { E.odd = ?? } | ( E1 ) { E.odd = ?? } | - E1 { E.odd = ?? } Add rules to the attribute grammar to calculate E.odd for each grammar production.
  4. Consider the following grammar, which generates expressions formed by applying ”+” to integer and floating point constants. When two integers are added, the result is integer, otherwise, it is a float.

E → E + T | T

T → num. num | num (a) Give a syntax-directed definition to determine the type of each subexpression. Assign each symbol an attribute” type”. (b) Provide the parse tree for the expression 5 + 4 + 3.2 + 1 and show the computation E.type and T.type at each point.

  1. Consider the following syntax-directed definition over the grammar defined by G = ({S, A, Sign}, S, {‘,’, ‘-‘, ‘+’, ‘n’}, P) with P as follows S → A Sign Sign → + Sign → - A → n A → A1, n a) Write the SDD to compute the maximum or minimum of a sequence of integers depending on the suffix ‘-‘ or ‘+’. b) Give an attributed parse tree for the source string“5,2,3-“ and evaluate the attributes in the attributed parse tree depicting the order in which the attributes need to be evaluated.