Compiler Design Chapter 3 - PowerPoint PPT Presentation

1 / 30
About This Presentation
Title:

Compiler Design Chapter 3

Description:

Compiler Design - Chapter 3. Parsing - Predictive Parsing. Predicative Parsing ... Predicative Parsing. Predicative Parsing. Conflicts. Recursive Descent ... – PowerPoint PPT presentation

Number of Views:1850
Avg rating:3.0/5.0
Slides: 31
Provided by: jian9
Category:

less

Transcript and Presenter's Notes

Title: Compiler Design Chapter 3


1
Compiler Design - Chapter 3
Parsing - Predictive Parsing
2
Predicative Parsing
  • Recursive decent simple algorithm easily
    parse some grammars
  • Each production turns into a clause of recursive
    function
  • Recursive Descent Parser for this grammar
  • One function per non-terminal symbol
  • One clause per production

3
Predicative Parsing
4
Predicative Parsing
Conflicts
5
Recursive Descent
  • Recursive Descent / Predictive Parsing
  • Works only when first terminal symbol of each
    subexpression provides enough information to
    choose
  • FIRST sets
  • Parser generators rather use LR(1) parsing
    (later in course)
  • Predictive parsing algorithm simple enough
    to construct parsers manually (without tools)

6
FIRST sets
Let ? be a string with terminal and nonterminal
symbols FIRST(?) the set of all terminal
symbols that can begin any string derived from ?
? T F, FIRST(T F) id, num,
(
7
Overlapping FIRST sets
Consider
X ? ?1
X ? ?2
If FIRST(?1) and FIRST(?2) overlaps, recursive-des
cent parsing cannot be used
If terminal symbol I is in FIRST(?1) and also
FIRST(?2), the function X() do not know what to
do (i.e., which production to use) given I.
8
FIRST sets and nullable symbols
FIRST(X Y Z) not always the same as FIRST( X )
  • Y produces empty string therefore
  • X produces empty string therefore
  • FIRST( X Y Z ) must include FIRST( Z )
  • nullable symbols
  • - Symbols that can produce empty string
  • Must know what might follow nullable symbols

9
nullable, FIRST and FOLLOW sets
  • Let ? be a string with terminal and nonterminal
    symbols
  • nullable(X) is true if X can derive empty string
  • FIRST(?) the set of all terminal symbols that
    can begin any string derived from ?
  • FOLLOW(X) the set of all terminal symbols that
    can immediately follow X.
  • t ? FOLLOW(X) if there is any derivation
    containing Xt
  • t ? FOLLOW(X) if derivation contains X Y Zt
    where Y and Z both derive e (empty string)

10
Nullable, FIRST and FOLLOW sets
Smallest sets having the following properties
11
Nullable, FIRST and FOLLOW sets Algorithm
Three relations do not need to be computed
simultaneously
12
Example
Initially
13
Example - First iteration
14
Example Second iteration

15
Generally
? is nullable if each symbol in ? is nullable
16
Constructing a Predictive Parser
Can construct 2-dimnsinal predictive parsing
table indexed by nonterminals X and terminals T
to choose the right production for a input token
17
Constructing a Predictive Parser
  • Consider each X ? ?
  • enter X ? ? in row X column T for each T ?
    FIRST(?)
  • For Z ? d FIRST(d) d
  • For Z ? X Y Z FIRST(X Y Z) a,c,d
  • For Y ? c FIRST(c) c
  • For X ? Y FIRST(Y) c
  • For X ? a FIRST(a) a
  • if ? is nullable, enter X ? ? in row X column T
    for each T ? FOLLOW(X)
  • For Y ? FOLLOW(Y) a,c,d
  • For X ? Y FOLLOW(X) a,c,d

18
Duplicate entries in Predictive Parsing Table
  • Predictive parser will not work if there are
    duplicate entries in the predictive parsing
    table.
  • Grammar is ambiguous
  • Sentence d more than one parsing table

19
LL(1) Grammar
  • Grammar with no duplicate entries in predictive
    parsing table LL(1) grammar
  • Stands for
  • Left-to-right parse,
  • Left-most derivation,
  • 1 symbol lookahead
  • LL(k) parsing table rows are non-terminals,
    columns every sequence of k terminals
  • Rarely done large tables
  • No ambiguous grammar is LL(k) for any k

20
Left Recursion
  • cause duplicate entries since any tokenin
    FIRST(T) will also be in FIRST(ET)
  • E appears as the first right-hand-side symbol in
    an E-production
  • This is called left recursion
  • Left-recursive grammars cannot be LL(1)

21
Eliminating Left Recursion
Introduce a new nonterminal E and re-write using
right recursion
22
Generally
a does not start with X
23
Example
?1
?2
a1
Eliminate left-recursion
a1
?1
?2
24
Nullable, FIRST FOLLOW
25
Predictive Parser
enter X ? g in row X column T for each T ?
FIRST(g)
if g is nullable, enter X ? g in row X column T
for each T ? FOLLOW(X)
  • For S?E FIRST(E ) ( id num
  • For E?T E FIRST(T E) ( id num
  • For E?TE FIRST(TE)
  • For E?-TE FIRST(-TE) -
  • For E? FOLLOW(E) )

etc.
  • For T? FOLLOW(T) )-

-
etc.
E?-TE
T?
26
Left Factoring
  • When two productions for the same non-terminal
    symbols start with the same symbol
  • Cause problems in predictive parsing

Left factor
take the allowable endings and make a new
nonterminal X
27
Error Handling
Blank entry in table syntax error
T() does not expect to see other tokens
28
Error Handling
  • If an error is found can raise exception and
    quit parsing, which is not user friendly
  • Error Recovery by deleting, replacing, or
    inserting tokens.
  • Error Recovery by insertion pretend num token
    was found, print error message and return
    normally so that more errors can be found
  • Cascading errors might cause infinite looping

29
Error Handling
  • Error Recovery by deletion skip tokens until a
    token in the FOLLOW set is reached

MINUS,
30
Error Handling
  • Recursive Descent error recovery must be
    adjusted to avoid a long cascade of error-repair
    messages
Write a Comment
User Comments (0)
About PowerShow.com