Context-Free Grammar - Advanced Compiler Design - Exam, Exams for Computer Science. Allahabad University
dharmaa
dharmaa19 March 2013

Context-Free Grammar - Advanced Compiler Design - Exam, Exams for Computer Science. Allahabad University

PDF (138.7 KB)
4 pages
1000+Number of visits
Description
These are the Exam of Advanced Compiler Design which includes Regular Expression, Lex Program, Optional Decimal Point, Context-Free Grammar, Parsing Table, Action and Goto Table, Item Set Construction, New Operators, Inf...
20points
Download points needed to download
this document
Download the document
Preview3 pages / 4

U

n

d

e

r

g

r

a

d

u

a

t

e

E

x

a

m

CSE-304 Compiler Design

Fall '98

Mid-term Exam

Oct 27, 1998.

Duration: 1 hours, 15 minutes.

ID Number :

Name :

INSTRUCTIONS

Read the following carefully before answering any question.

 MAKE SURE YOU HAVE FILLED IN YOUR NAME AND ID NUMBER IN THE SPACE ABOVE.

 The exam for undergraduate students is di erent from the exam for the graduate students. Make sure

you are reading the correct question paper.

 Doubts about the questions will be answered only in the rst 15 minutes of the exam. So, read the

questions carefully at the beginning of the exam.

 Write your answers in the space provided.

 Keep your answers brief and precise.

 The exam consists of 5 questions, in 8 pages (including this page) for a total of 110 points.

You may attempt all questions. However, the maximum you can score in this exam is 100 points.

Each of questions 1, 2, and 5 are on a single page.

Questions 3 and 4 have multiple parts and are over two pages each.

GOOD LUCK!

Question Points

1.

2.

3.

4.

5.

Total

1

U

n

d

e

r

g

r

a

d

u

a

t

e

E

x

a

m

1. [Total: 28 points] Consider the following two languages:

L

1

: strings over fa; bg such that they contain even number of a's.

L

2

: strings over fa; bg such that the number of a's is a multiple of 3.

a. (7 points) Write a regular expression corresponding to L

1

.

b. (7 points) Write a regular expression corresponding to L

2

.

c. (7 points) Draw an NFA that accepts L

1

.

d. (7 points) Draw an NFA that accepts L

2

.

2

Docsity.com

U

n

d

e

r

g

r

a

d

u

a

t

e

E

x

a

m

2. [20 points] In text les (plain ASCII les containing lower-case or upper-case english letters,

numbers and common english punctuation symbols) quotations are, by convention, written

by enclosing them between a pair of double quotes ("). Since text les are read only by

humans, there are no escape sequences within quotations and quotations can span multiple

lines.

Write a Lex speci cation that copies a le from stdin to stdout, replacing the double quotes

in the beginning of each quotation to two backward quotes (i.e., ``) and the double quotes

that end each quotation to two single quotes (i.e., '').

For instance, for the input text:

He turned and said to me, "The answer is no!", and then he left.

the output should be:

He turned and said to me, ``The answer is no!'', and then he left.

3

U

n

d

e

r

g

r

a

d

u

a

t

e

E

x

a

m

3. [Total: 25 points] Consider the context-free grammar G

3

:

S �! aSbS

S �! bSaS

S �! 

a. (6 points) Show that G

3

is ambiguous by drawing two di erent parse trees for the string

abab.

b. (6 points) Write down two right-most derivations of the string abab. (Each derivation

will correspond to one of the two parse trees of part (a)).

4

Docsity.com

U

n

d

e

r

g

r

a

d

u

a

t

e

E

x

a

m

c. (5 points) Compute FIRST (S) and FOLLOW (S).

d. (4 points) Is G

3

LL(1)? Justify.

e. (4 points) Is G

3

SLR(1)? Justify.

[Hint: You need not build the SLR(1) parsing table!]

5

U

n

d

e

r

g

r

a

d

u

a

t

e

E

x

a

m

4. [Total: 25 points] Consider the following simpli ed grammar, G

4

, of a formatting language

for math equations:

T �! T exp T (Exponent)

T �! sqrt T (Square root)

T �! T frac T (Fraction)

T �! ( T )

T �! id

a. (8 points) When grammar G

4

is speci ed in bison/yacc, we get 6 shift/reduce con icts.

In the .output le produced by bison/yacc, one of the states with con icts is reported

as:

state 10

T -> T . TOK_exp T (rule 1)

T -> T TOK_exp T . (rule 1)

T -> T . TOK_frac T (rule 3)

TOK_exp shift, and go to state 7

TOK_frac shift, and go to state 8

TOK_exp [reduce using rule 1 (t)]

TOK_frac [reduce using rule 1 (t)]

$default reduce using rule 1 (t)

What entries in the parsing table (i.e., what state and terminal symbols) do the above

con icts correspond to?

6

Docsity.com

U

n

d

e

r

g

r

a

d

u

a

t

e

E

x

a

m

b. (10 points) The shift/reduce con icts can be eliminated by specifying the precedence

and associativity information for the operators, i.e., exp, sqrt and frac, in G

4

. The

rules for precedence and associativity are:

 exp is right associative, frac is left associative and sqrt is nonassociative.

 exp has highest precedence, followed by sqrt, followed by frac (which has the

lowest precedence).

Specify, in the form of yacc/bison declarations, the operator precedence and associativity

information for G

4

.

c. (7 points) How will the shift/reduce con icts in state 10 (from part (a) of this question)

be resolved by bison/yacc if the precedences and associativity you de ned in part (b)

were added to the bison/yacc speci cations?

7

U

n

d

e

r

g

r

a

d

u

a

t

e

E

x

a

m

5. [Total 12 points] Let G

5

be an LL(1) grammar with  as the set of terminal symbols and

 as the set of nonterminal symbols.

a. (6 points) Estimate the size of LL(1) parsing table for G

5

.

b. (6 points) Estimate the size of the parse tree produced by G

5

for a string of length n.

You may assume that G

5

has no -productions, and each production in G

5

is of the form

A �! B C (where B and C are nonterminals) or A �! a (where a is a terminal).

END OF EXAM

8

Docsity.com

comments (0)
no comments were posted
be the one to write the first!
This is only a preview
See and download the full document
Docsity is not optimized for the browser you're using. In order to have a better experience please switch to Google Chrome, Firefox, Internet Explorer 9+ or Safari! Download Google Chrome