<<

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 their personal 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

Top-down parsers ❑ start at the root of derivation and fill in ❑ picks a production and 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

CSC 302, Second Semester. © 2018 Obienu, A. C. All rights reserved. Top Down Parser

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

CSC 302, Second Semester. © 2018 Obienu, A. C. All rights reserved. Issue with TDP

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)

CSC 302, Second Semester. © 2018 Obienu, A. C. All rights reserved. Bottom Up Parser

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

CSC 302, Second Semester. © 2018 Obienu, A. C. All rights reserved. Issues with BUP

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) CSC 302, Second Semester. © 2018 Obienu, A. C. All rights reserved. TDP REVISITED

Top Down (TDP)

CSC 302, Second Semester. © 2018 Obienu, A. C. All rights reserved. TDP

Once grammar is on its optimal form: I. Goal is to construct the 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.

CSC 302, Second Semester. © 2018 Obienu, A. C. All rights reserved. Example

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

CSC 302, Second Semester. © 2018 Obienu, A. C. All rights reserved. EXAMPLE

Then, Top down parsing

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. Example: Top Down Parser

CSC 302, Second Semester. © 2018 Obienu, A. C. All rights reserved. Classes of Top Down Parser

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.

CSC 302, Second Semester. © 2018 Obienu, A. C. All rights reserved. LL(1) PARSER

A predictive parser looks like:

CSC 302, Second Semester. © 2018 Obienu, A. C. All rights reserved. TABLE-DRIVEN PARSERS

A parser generator system often looks like:

This is true for both top-down (LL) and bottom-up (LR) parsers

CSC 302, Second Semester. © 2018 Obienu, A. C. All rights reserved. Definition of Terms

LL(k) implies scanning input from Left, retrieving

Leftmost derivation, using k lookahead symbols to reach a resolution.

Stack: A 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.

CSC 302, Second Semester. © 2018 Obienu, A. C. All rights reserved. Choosing the right production

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.

CSC 302, Second Semester. © 2018 Obienu, A. C. All rights reserved. Definition

CSC 302, Second Semester. © 2018 Obienu, A. C. All rights reserved. NEXT CLASS

For the next week, we will look at:

THE FIRST AND FOLLOW SETS

CSC 302, Second Semester. © 2018 Obienu, A. C. All rights reserved. THE END

QUESTIONS ?

E-mail: [email protected] Office hours: Mon, Wed & Fri, 1.30pm to 3.30pm Office: Power of Faith Building, Floor 2, Rm 59

CSC 302, Second Semester. © 2018 Obienu, A. C. All rights reserved.