recursive descent parsing

14
Recursive descent parsing Programming Language Design and Implementatio n (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 3.4

Upload: hyatt-sutton

Post on 31-Dec-2015

68 views

Category:

Documents


3 download

DESCRIPTION

Recursive descent parsing. Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 3.4. Recursive descent parsing overview. A simple parsing algorithm - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Recursive descent parsing

Recursive descent parsing

Programming Language Design and Implementation (4th Edition)

by T. Pratt and M. Zelkowitz

Prentice Hall, 2001

Section 3.4

Page 2: Recursive descent parsing

2

Recursive descent parsing overview

A simple parsing algorithm Shows the relationship between the formal description of a progr

amming language and the ability to generate executable code for programs in the language.

Use extended BNF for a grammar, e.g., expressions: <arithmetic expression>::=<term>{[+|-]<term>}* Consider the recursive procedure to recognize this: procedure Expression; begin Term; /* Call Term to find first term */ while ((nextchar=`+') or (nextchar=`-')) do begin nextchar:=getchar; /* Skip over operator

*/ Term end end

Page 3: Recursive descent parsing

3

Generating code

Assume each procedure outputs its own postfix (Section 8.2, to be discussed later)

To generate code, need to output symbols at appropriate places in procedure.

procedure Expression; begin Term; /* Call Term to find first term */ while ((nextchar=`+') or (nextchar=`-')) do begin nextchar:=getchar; /* Skip over oper

ator */ Term; output previous ‘+’ or ‘-’; end end

Page 4: Recursive descent parsing

4

Generating code (continued)

Each non-terminal of grammar becomes a procedure. Each procedure outputs its own postfix. Examples: procedure Term; begin Primary; while ((nextchar=`*') or (nextchar=`/')) do begin nextchar:=getchar; /* Skip over oper

ator */ Primary; output previous ‘*’ or ‘/’; end end Procedure Identifier; begin if nextchar= letter output letter else error; nextchar=getchar; end Figure 3.13 of text has complete parser for expressions.

Page 5: Recursive descent parsing

5

Recursive Descent ParsingRecall the expression grammar, after transformation

This produces a parser with sixmutually recursive routines: • Goal • Expr • EPrime • Term • TPrime • Factor

Each recognizes one NT or T

The term descent refers to thedirection in which the parse treeis built.

Page 6: Recursive descent parsing

6

Recursive Descent ParsingA couple of routines from the expression parser

Page 7: Recursive descent parsing

7

Transition diagrams for the grammar

0 102E :T

1E'

3E' :+

4T

1065E'

7 109T :F

8T'

10T' : * 11F

101312T'

14F :(

15E

101716)

id

EE'T

T'F

TE'+TE' | FT'*FT' | (E) | id

Grammar

Page 8: Recursive descent parsing

8

Simplified transition diagrams.

3E' :+

4T

5

106

3E' :+

4

T

106

3E :+

4

T

106

0T

3E :

+

106

0T

(a) (b)

(c) (d)

Page 9: Recursive descent parsing

9

Simplified transition diagrams for arithmetic expressions.

*

7 1013T :F

8

14F :(

15E

101716)

id

+

0 106E :T

3

Page 10: Recursive descent parsing

10

Example transition diagrams

An expression grammar with left recursion and ambiguity removed:

E’ -> + T E’ | ε T -> F T’ T’ -> * F T’ | ε F -> ( E ) | id E -> T E’

Corresponding transition diagrams:

Page 11: Recursive descent parsing

11

Predictive parsing without recursion To get rid of the recursive procedure calls, we maintain

our own stack.

Page 12: Recursive descent parsing

12

Example Use the table-driven predictive parser to parse

id + id * id Assuming parsing table

Initial stack is $E Initial input is id + id * id $

Page 13: Recursive descent parsing

13

LR parsing

Page 14: Recursive descent parsing

14

LR parsing example

Grammar: 1. E -> E + T 2. E -> T 3. T -> T * F 4. T -> F 5. F -> ( E ) 6. F -> id