CSC 302: COMPILER CONSTRUCTION 1
LECTURE 5
DEPARTMENT OF COMPUTER SCIENCE
ON MONDAY @ 9AM; TUESDAY @ 10AM
INSTRUCTOR: OBIENU, A. C. ([email protected])
CSC 302
SECOND SEMESTER
© 2018 Obienu, A. C. All rights reserved.Students enrolled in Csc 302 at MCIU and other educational institutions have explicit permission to make copies of these materials for theirpersonal use, provided this copyright notice is preserved.
Parser Generator
CSC 302, Second Semester. © 2018 Obienu, A. C. All rights reserved.
PARSER
PARSERS
Top Down Parser (TDP) Bottom Up Parser (BUP)
TDP with full TDP without
Backtracking Backtracking
Bruteforce Recursive Non-Recursive
Method descent descent (LL(1))
Operator precedence LL Parsers
Parser
LR(0) SLR(0) LALR(1) CLR(1)
CSC 302, Second Semester. © 2018 Obienu, A. C. All rights reserved.
TDP VERSUS BUP
CSC 302, Second Semester. © 2018 Obienu, A. C. All rights reserved.
Top-down parsers
❑ start at the root of derivation tree and fill in
❑ picks a production and tries to match the input
❑ may require backtracking
❑ some grammars are backtrack-free (predictive)
Bottom-up parsers
❑ start at the leaves and fill in
❑ start in a state valid for legal first tokens
❑ as input is consumed, change state to encode possibilities (recognize
valid prefixes)
❑ use a stack to store both state and sentential forms
Top Down Parser
CSC 302, Second Semester. © 2018 Obienu, A. C. All rights reserved.
S → aABe w = abbcde
A → Abc | b
B → D
S
Top Down a A B e
A b c d
b
NB: TDP uses leftmost derivation
Issue with TDP
CSC 302, Second Semester. © 2018 Obienu, A. C. All rights reserved.
S → aABe w = abbcde
A → Abc | b
B → D
S
Top Down a A B e
A b c d
b
(Issue : What to use)
Bottom Up Parser
CSC 302, Second Semester. © 2018 Obienu, A. C. All rights reserved.
S → aABe w = abbcde
A → Abc | b
B → D
S
Bottom Up B
A
A
a b b c d e
NB: BUP follow rightmost derivation
Issues with BUP
CSC 302, Second Semester. © 2018 Obienu, A. C. All rights reserved.
S → aABe w = abbcde
A → Abc | b
B → D
S
Bottom Up B
A
A
a b b c d e
(Issue : When to reduced)
TDP REVISITED
CSC 302, Second Semester. © 2018 Obienu, A. C. All rights reserved.
Top Down Parsing (TDP)
TDP
CSC 302, Second Semester. © 2018 Obienu, A. C. All rights reserved.
Once grammar is on its optimal form:
I. Goal is to construct the parse tree for some input (token) string
a) starting with the root,
b) recursively descending through the tree,
c) while building the parse tree on preorder form (depth-first).
2. Find the left-most derivation to guide the preorder construction.
3. At each step of the parse tree construction:
a) determine which (unique) production to be applied,
b) match terminal symbols in the production body with the input (token)
string symbols.
Example
CSC 302, Second Semester. © 2018 Obienu, A. C. All rights reserved.
First, bring grammar on optimal form
E → E + T | T
T → T * F | F
F → (E) |id
Can be rewritten (left-recursion elimination):
E → T E’
E’ → + T E’ / 𝜀T → F T ’
T → *F T ‘ / 𝜀F → ( E ) / id
EXAMPLE
CSC 302, Second Semester. © 2018 Obienu, A. C. All rights reserved.
Then, Top down parsing
Example: Top Down Parser
CSC 302, Second Semester. © 2018 Obienu, A. C. All rights reserved.
Example: Top Down Parser
CSC 302, Second Semester. © 2018 Obienu, A. C. All rights reserved.
Classes of Top Down Parser
CSC 302, Second Semester. © 2018 Obienu, A. C. All rights reserved.
Recursive-Descent Parser: general form of top-down parsers.
❑ May require backtracking.
Predictive Parser special case of recursive-descent parsers.
❑ No backtracking necessary.
❑ Constructed from LL(k) grammars, k >= 1.
An LL(k) grammar implies scanning input from Left, retrieving
Leftmost derivation, using k lookahead symbols to reach a
resolution.
LL(1) PARSER
CSC 302, Second Semester. © 2018 Obienu, A. C. All rights reserved.
A predictive parser looks like:
TABLE-DRIVEN PARSERS
CSC 302, Second Semester. © 2018 Obienu, A. C. All rights reserved.
A parser generator system often looks like:
This is true for both top-down (LL) and bottom-up (LR) parsers
Definition of Terms
CSC 302, Second Semester. © 2018 Obienu, A. C. All rights reserved.
LL(k) implies scanning input from Left, retrieving
Leftmost derivation, using k lookahead symbols to reach a resolution.
Stack: A data structure used for the procedure of parsing
Input Buffer holds the input string
NB: Both Stack and Input buffer has a “$”, which is used as
a stopping criterium.
Choosing the right production
CSC 302, Second Semester. © 2018 Obienu, A. C. All rights reserved.
Key parsing problem: determining which production to apply
whilst constructing the parse tree.
FIRST and FOLLOW sets of a grammar helps choose a production
based on the next input symbol.
Definition
CSC 302, Second Semester. © 2018 Obienu, A. C. All rights reserved.
NEXT CLASS
CSC 302, Second Semester. © 2018 Obienu, A. C. All rights reserved.
For the next week, we will look at:
THE FIRST AND FOLLOW SETS
THE END
CSC 302, Second Semester. © 2018 Obienu, A. C. All rights reserved.
QUESTIONS ?
E-mail: [email protected]
Office hours: Mon, Wed & Fri, 1.30pm to 3.30pm
Office: Power of Faith Building, Floor 2, Rm 59