Lecture #12

18
Lecture #12 Parsing Types

description

Lecture #12. Parsing Types. Parsing. Parsing = process of determining if a string of tokens can be generated by a grammar For any CF grammar there is a parser that takes at most O ( n 3 ) time to parse a string of n tokens - PowerPoint PPT Presentation

Transcript of Lecture #12

Page 1: Lecture #12

Lecture #12

Parsing Types

Page 2: Lecture #12

2

Parsing

• Parsing = process of determining if a string of tokens can be generated by a grammar

• For any CF grammar there is a parser that takes at most O(n3) time to parse a string of n tokens

• Linear algorithms suffice for parsing programming language source code

• Top-down parsing “constructs” a parse tree from root to leaves

• Bottom-up parsing “constructs” a parse tree from leaves to root

Page 3: Lecture #12

3

Parsing

• Top-down (C-F grammar with restrictions)– Recursive descent (predictive parsing)– LL (Left-to-right, Leftmost derivation) methods

• Bottom-up (C-F grammar with restrictions)– Operator precedence parsing– LR (Left-to-right, Rightmost derivation) methods

• SLR, canonical LR, LALR

Page 4: Lecture #12

4

Predictive Parsing

• Recursive descent parsing is a top-down parsing method– Every nonterminal has one (recursive) procedure responsible

for parsing the nonterminal’s syntactic category of input tokens

– When a nonterminal has multiple productions, each production is implemented in a branch of a selection statement based on input look-ahead information

• Predictive parsing is a special form of recursive descent parsing where we use one lookahead token to unambiguously determine the parse operations

Page 5: Lecture #12

5

Example Predictive Parser (Grammar)

type simple |^ id | array [ simple ] of typesimple integer | char | num dotdot num

Page 6: Lecture #12

6

Example Predictive Parser (Program Code)

procedure match(t : token);begin if lookahead = t then lookahead := nexttoken() else error()end;

procedure type();begin if lookahead in { ‘integer’, ‘char’, ‘num’ } then simple() else if lookahead = ‘^’ then match(‘^’); match(id) else if lookahead = ‘array’ then match(‘array’); match(‘[‘); simple(); match(‘]’); match(‘of’); type() else error()end;

procedure simple();begin if lookahead = ‘integer’ then match(‘integer’) else if lookahead = ‘char’ then match(‘char’) else if lookahead = ‘num’ then match(‘num’); match(‘dotdot’); match(‘num’) else error()end;

Page 7: Lecture #12

7

Example Predictive Parser (Execution Step 1)

type()

match(‘array’)

array [ num numdotdot ] of integerInput:

lookahead

Check lookaheadand call match

Page 8: Lecture #12

8

Example Predictive Parser (Execution Step 2)

match(‘array’)

array [ num numdotdot ] of integerInput:

lookahead

match(‘[’)

type()

Page 9: Lecture #12

9

Example Predictive Parser (Execution Step 3)

simple()match(‘array’)

array [ num numdotdot ] of integerInput:

lookahead

match(‘[’)

match(‘num’)

type()

Page 10: Lecture #12

10

Example Predictive Parser (Execution Step 4)

simple()match(‘array’)

array [ num numdotdot ] of integerInput:

lookahead

match(‘[’)

match(‘num’) match(‘dotdot’)

type()

Page 11: Lecture #12

11

Example Predictive Parser (Execution Step 5)

simple()match(‘array’)

array [ num numdotdot ] of integerInput:

lookahead

match(‘[’)

match(‘num’) match(‘num’)match(‘dotdot’)

type()

Page 12: Lecture #12

12

Example Predictive Parser (Execution Step 6)

simple()match(‘array’)

array [ num numdotdot ] of integerInput:

lookahead

match(‘[’) match(‘]’)

match(‘num’) match(‘num’)match(‘dotdot’)

type()

Page 13: Lecture #12

13

Example Predictive Parser (Execution Step 7)

simple()match(‘array’)

array [ num numdotdot ] of integerInput:

lookahead

match(‘[’) match(‘]’) match(‘of’)

match(‘num’) match(‘num’)match(‘dotdot’)

type()

Page 14: Lecture #12

14

Example Predictive Parser (Execution Step 8)

simple()match(‘array’)

array [ num numdotdot ] of integerInput:

lookahead

match(‘[’) match(‘]’) type()match(‘of’)

match(‘num’) match(‘num’)match(‘dotdot’)

match(‘integer’)

type()

simple()

Page 15: Lecture #12

15

FIRST

FIRST() is the set of terminals that appear as thefirst symbols of one or more strings generated from

type simple | ^ id | array [ simple ] of typesimple integer | char | num dotdot num

FIRST(simple) = { integer, char, num }FIRST(^ id) = { ^ }FIRST(type) = { integer, char, num, ^, array }

Page 16: Lecture #12

16

How to use FIRST

expr term rest rest + term rest | - term rest |

A |

When a nonterminal A has two (or more) productions as in

Then FIRST () and FIRST() must be disjoint forpredictive parsing to work

procedure rest();begin if lookahead in FIRST(+ term rest) then match(‘+’); term(); rest() else if lookahead in FIRST(- term rest) then match(‘-’); term(); rest() else returnend;

We use FIRST to write a predictive parser as follows

Page 17: Lecture #12

17

Predictive Parsing

• Eliminate left recursion from grammar• Left factor the grammar• Compute FIRST and FOLLOW• Two variants:– Recursive (recursive-descent parsing)– Non-recursive (table-driven parsing)

Page 18: Lecture #12

18

Predictive Parsing Program (Driver)

push($)push(S)a := lookaheadrepeat

X := pop()if X is a terminal or X = $ then

match(X) // moves to next token and a := lookaheadelse if M[X,a] = X Y1Y2…Yk then

push(Yk, Yk-1, …, Y2, Y1) // such that Y1 is on top… invoke actions and/or produce IR output …

else error()endif

until X = $