Docsity

Midterm Exam 1 - Introduction to Compilers | CMSC 430, Papers of Computer Science

Toggle sidebar
bg1
CMSC 430
Spring ’09 Midterm 1
You have until 4:45pm to complete the midterm.
Feel free to ask questions on the midterm.
In grammars, capital letters represent nonterminals,
lower case letters represent terminals.
One sentence answers are sufficient for the “essay” questions.
1. (4 points) Compiler front ends.
(a) What is the primary interaction between the scan-
ner and parser?
Scanner converts input program into a stream of
tokens for the parser, which converts it into the
intermediate representation.
(b) What do scanner generators (e.g., JLex) and parser
generators (e.g., CUP) take as input? What do
they output?
They take regular expressions for tokens, and context-
free grammars & syntax-directed actions as input,
respectively. They output code implementing scan-
ners and parsers.
2. (4 points) Regular expressions and context-free gram-
mars.
Consider the language of all strings composed of the let-
ters aand bthat end in a(e.g., baba is in the language,
aab and are not).
(a) Write a regular expression for the language.
One solution: (a|b)∗a
(b) Write a context-free grammar for the language.
One solution: S→Ea
E→|aE |bE
3. (8 points) Parsing.
(a) What is a left-most derivation? Name one useful
property of left-most derivations for a compiler.
A derivation where the left-most non-terminal is
expanded at each step. Unique for non-ambiguous
grammars.
(b) Name one advantage and disadvantage for bottom-
up parsing (compared to top-down parsing).
Bottom-up parsing is more powerful (accepts more
grammars), allows left-recursion, and can handle
some ambiguous grammars by specifying precedence
and associativity. It is less intuitive and harder to
add clear error messages.
(c) Give an example grammar which can be processed
by a LL(2) parser, but not by an LL(1) parser.
S→aa |ab.
(d) Give an example grammar which can be processed
by a LR(0) parser, but not by an LL(k) parser.
S→Sa |b.
4. (14 points) Top-down parsers.
Consider the following grammar:
A→B b |a
B→c A |
(a) Calculate FIRST and FOLLOW for A, B:
Nonterminals FIRST FOLLOW
A{a,c,b} { b,$}
B{c, } { b}
(b) Construct a LL(1) parser for this grammar.
a b c $
A A →a A →Bb A →Bb
B B →B→cA
(c) Show the steps taken by your LL(1) parser to parse
the input ”cbb”.
Stack Input
$A cbb$
$bB cbb$
$ bAc c b b $
$ bA b b $
$ bbB b b $
$ bb b b $
$ b b $
$ $
pf3

Partial preview of the text

Download Midterm Exam 1 - Introduction to Compilers | CMSC 430 and more Papers Computer Science in PDF only on Docsity!

CMSC 430

Spring ’09 Midterm 1

You have until 4:45pm to complete the midterm.

Feel free to ask questions on the midterm.

In grammars, capital letters represent nonterminals, lower case letters represent terminals.

One sentence answers are sufficient for the “essay” questions.

  1. (4 points) Compiler front ends.

(a) What is the primary interaction between the scan- ner and parser? Scanner converts input program into a stream of tokens for the parser, which converts it into the intermediate representation. (b) What do scanner generators (e.g., JLex) and parser generators (e.g., CUP) take as input? What do they output? They take regular expressions for tokens, and context- free grammars & syntax-directed actions as input, respectively. They output code implementing scan- ners and parsers.

  1. (4 points) Regular expressions and context-free gram- mars. Consider the language of all strings composed of the let- ters a and b that end in a (e.g., baba is in the language, aab and  are not).

(a) Write a regular expression for the language. One solution: (a | b)∗a (b) Write a context-free grammar for the language. One solution: S → Ea E →  | aE | bE

  1. (8 points) Parsing.

(a) What is a left-most derivation? Name one useful property of left-most derivations for a compiler. A derivation where the left-most non-terminal is expanded at each step. Unique for non-ambiguous grammars. (b) Name one advantage and disadvantage for bottom- up parsing (compared to top-down parsing). Bottom-up parsing is more powerful (accepts more grammars), allows left-recursion, and can handle some ambiguous grammars by specifying precedence and associativity. It is less intuitive and harder to add clear error messages.

(c) Give an example grammar which can be processed by a LL(2) parser, but not by an LL(1) parser. S → aa | ab. (d) Give an example grammar which can be processed by a LR(0) parser, but not by an LL(k) parser. S → Sa | b.

  1. (14 points) Top-down parsers. Consider the following grammar: A → B b | a B → c A | 

(a) Calculate FIRST and FOLLOW for A, B: Nonterminals FIRST FOLLOW A { a , c, b } { b, $ } B { c,  } { b } (b) Construct a LL(1) parser for this grammar.

a b c $ A A → a A → Bb A → Bb B B →  B → cA

(c) Show the steps taken by your LL(1) parser to parse the input ”cbb”.

Stack Input

$ A c b b $ $ bB c b b $ $ bAc c b b $ $ bA b b $ $ bbB b b $ $ bb b b $ $ b b $ $ $

  1. (4 points) Shift-reduce parsing.

Consider the following ACTION/GOTO tables:

Action Goto State a b $ A B 0 shift 1 shift 4 6 4

1 reduce reduce reduce 0 5 A → a B → aB A → a 2 reduce shift 5 accept 1 5 B → Ab 3 reduce shift 1 reduce 3 5 A → b A →  4 shift 1 reduce accept 5 3 A → b 5 reduce shift 3 reduce 3 2 B → A B → bA 6 reduce shift 2 4 1 A → a

Show the contents of the stack and input buffer for the shift-reduce parse of ”b a”, assuming State 0 is the start state:

Stack Input Action $ 0 b a $ action[0,b] → shift 4 $ 0 b 4 a $ action[4,a] → shift 1 $ 0 b 4 a 1 $ action[1,$] → reduce A → a $ 0 b 4 A $ goto[4,A] = 5 $ 0 b 4 A 5 $ action [5,$] = reduce B → bA $ 0 B $ goto[0,B] = 4 $ 0 B 4 $ action [4,$] = accept

  1. (16 points) LR(1) parse table construction.

Consider the following (already augmented) grammar:

P1 S → E P2 E → a E P3 E → a

(a) Derive its canonical sets of LR(1) items

(b) Given the following sets of LR(1) states, build its LR(1) parse table:

ACTION GOTO

State d e $ A B 0 2 1 1 shift 3 2 shift 4 3 reduce S→bBd 4 reduce S→bAe

  1. (4 points) LR(1) conflicts. Consider the following possible sets of LR(1) items in the states of a shift/reduce parser: State 0: State 1: [ A → a • , b ] [ A → b • , a ] [ B → a • b, b ] [ B → b • b , b ] [ C → b a • , a ] [ C → a b • , a ]

(a) For the items in State 0, list any conflicts that exist and describe for what lookaheads they occur. A shift/reduce conflict occurs for lookahead ’b’ be- cause the parser can either shift on the production for B or reduce on the productions for A. (b) For the items in State 1, list any conflicts that exist and describe for what lookaheads they occur. A reduce/reduce conflict occurs for lookahead ’a’ because the parser can either perform a reduction to either A or C.

  1. (6 points) LALR parsers