

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


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.
(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.
(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
(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.
(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 $ $ $
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
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:
State d e $ A B 0 2 1 1 shift 3 2 shift 4 3 reduce S→bBd 4 reduce S→bAe
(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.