Top Banner
CSC 302: COMPILER CONSTRUCTION 1 LECTURE 5 DEPARTMENT OF COMPUTER SCIENCE ON MONDAY @ 9AM; TUESDAY @ 10AM INSTRUCTOR: OBIENU, A. C. ([email protected])
23

CSC 302: COMPILER CONSTRUCTION 1 LECTURE 5oer.mciu.edu.ng/wp-content/uploads/2015/04/Obienu... · csc 302: compiler construction 1 lecture 5 department of computer science on monday

Mar 18, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CSC 302: COMPILER CONSTRUCTION 1 LECTURE 5oer.mciu.edu.ng/wp-content/uploads/2015/04/Obienu... · csc 302: compiler construction 1 lecture 5 department of computer science on monday

CSC 302: COMPILER CONSTRUCTION 1

LECTURE 5

DEPARTMENT OF COMPUTER SCIENCE

ON MONDAY @ 9AM; TUESDAY @ 10AM

INSTRUCTOR: OBIENU, A. C. ([email protected])

Page 2: CSC 302: COMPILER CONSTRUCTION 1 LECTURE 5oer.mciu.edu.ng/wp-content/uploads/2015/04/Obienu... · csc 302: compiler construction 1 lecture 5 department of computer science on monday

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.

Page 3: CSC 302: COMPILER CONSTRUCTION 1 LECTURE 5oer.mciu.edu.ng/wp-content/uploads/2015/04/Obienu... · csc 302: compiler construction 1 lecture 5 department of computer science on monday

Parser Generator

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

Page 4: CSC 302: COMPILER CONSTRUCTION 1 LECTURE 5oer.mciu.edu.ng/wp-content/uploads/2015/04/Obienu... · csc 302: compiler construction 1 lecture 5 department of computer science on monday

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.

Page 5: CSC 302: COMPILER CONSTRUCTION 1 LECTURE 5oer.mciu.edu.ng/wp-content/uploads/2015/04/Obienu... · csc 302: compiler construction 1 lecture 5 department of computer science on monday

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

Page 6: CSC 302: COMPILER CONSTRUCTION 1 LECTURE 5oer.mciu.edu.ng/wp-content/uploads/2015/04/Obienu... · csc 302: compiler construction 1 lecture 5 department of computer science on monday

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

Page 7: CSC 302: COMPILER CONSTRUCTION 1 LECTURE 5oer.mciu.edu.ng/wp-content/uploads/2015/04/Obienu... · csc 302: compiler construction 1 lecture 5 department of computer science on monday

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)

Page 8: CSC 302: COMPILER CONSTRUCTION 1 LECTURE 5oer.mciu.edu.ng/wp-content/uploads/2015/04/Obienu... · csc 302: compiler construction 1 lecture 5 department of computer science on monday

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

Page 9: CSC 302: COMPILER CONSTRUCTION 1 LECTURE 5oer.mciu.edu.ng/wp-content/uploads/2015/04/Obienu... · csc 302: compiler construction 1 lecture 5 department of computer science on monday

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)

Page 10: CSC 302: COMPILER CONSTRUCTION 1 LECTURE 5oer.mciu.edu.ng/wp-content/uploads/2015/04/Obienu... · csc 302: compiler construction 1 lecture 5 department of computer science on monday

TDP REVISITED

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

Top Down Parsing (TDP)

Page 11: CSC 302: COMPILER CONSTRUCTION 1 LECTURE 5oer.mciu.edu.ng/wp-content/uploads/2015/04/Obienu... · csc 302: compiler construction 1 lecture 5 department of computer science on monday

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.

Page 12: CSC 302: COMPILER CONSTRUCTION 1 LECTURE 5oer.mciu.edu.ng/wp-content/uploads/2015/04/Obienu... · csc 302: compiler construction 1 lecture 5 department of computer science on monday

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

Page 13: CSC 302: COMPILER CONSTRUCTION 1 LECTURE 5oer.mciu.edu.ng/wp-content/uploads/2015/04/Obienu... · csc 302: compiler construction 1 lecture 5 department of computer science on monday

EXAMPLE

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

Then, Top down parsing

Page 14: CSC 302: COMPILER CONSTRUCTION 1 LECTURE 5oer.mciu.edu.ng/wp-content/uploads/2015/04/Obienu... · csc 302: compiler construction 1 lecture 5 department of computer science on monday

Example: Top Down Parser

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

Page 15: CSC 302: COMPILER CONSTRUCTION 1 LECTURE 5oer.mciu.edu.ng/wp-content/uploads/2015/04/Obienu... · csc 302: compiler construction 1 lecture 5 department of computer science on monday

Example: Top Down Parser

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

Page 16: CSC 302: COMPILER CONSTRUCTION 1 LECTURE 5oer.mciu.edu.ng/wp-content/uploads/2015/04/Obienu... · csc 302: compiler construction 1 lecture 5 department of computer science on monday

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.

Page 17: CSC 302: COMPILER CONSTRUCTION 1 LECTURE 5oer.mciu.edu.ng/wp-content/uploads/2015/04/Obienu... · csc 302: compiler construction 1 lecture 5 department of computer science on monday

LL(1) PARSER

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

A predictive parser looks like:

Page 18: CSC 302: COMPILER CONSTRUCTION 1 LECTURE 5oer.mciu.edu.ng/wp-content/uploads/2015/04/Obienu... · csc 302: compiler construction 1 lecture 5 department of computer science on monday

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

Page 19: CSC 302: COMPILER CONSTRUCTION 1 LECTURE 5oer.mciu.edu.ng/wp-content/uploads/2015/04/Obienu... · csc 302: compiler construction 1 lecture 5 department of computer science on monday

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.

Page 20: CSC 302: COMPILER CONSTRUCTION 1 LECTURE 5oer.mciu.edu.ng/wp-content/uploads/2015/04/Obienu... · csc 302: compiler construction 1 lecture 5 department of computer science on monday

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.

Page 21: CSC 302: COMPILER CONSTRUCTION 1 LECTURE 5oer.mciu.edu.ng/wp-content/uploads/2015/04/Obienu... · csc 302: compiler construction 1 lecture 5 department of computer science on monday

Definition

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

Page 22: CSC 302: COMPILER CONSTRUCTION 1 LECTURE 5oer.mciu.edu.ng/wp-content/uploads/2015/04/Obienu... · csc 302: compiler construction 1 lecture 5 department of computer science on monday

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

Page 23: CSC 302: COMPILER CONSTRUCTION 1 LECTURE 5oer.mciu.edu.ng/wp-content/uploads/2015/04/Obienu... · csc 302: compiler construction 1 lecture 5 department of computer science on monday

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