Compiler Construction - PowerPoint PPT Presentation

1 / 31
About This Presentation
Title:

Compiler Construction

Description:

Compiler Construction Sohail Aslam ... it is not clear how to predict A ab2 ab1 ab3 aZ b2 b1 b3 A FIRST(rhs1) = {id} FIRST(rhs2) = {id} FIRST(rhs3) = {id} ... – PowerPoint PPT presentation

Number of Views:200
Avg rating:3.0/5.0
Slides: 32
Provided by: whit1159
Category:

less

Transcript and Presenter's Notes

Title: Compiler Construction


1
Compiler Construction
  • Sohail Aslam
  • Lecture 19

2
LL(1) Table Construction
  • For each production A ? a
  • 1. for each terminal a in FIRST(a), add A ? a to
    MA,a.
  • 2. If e is in FIRST(a), add A ? a to MA,b for
    each terminal b in FOLLOW(A).

3
LL(1) Table Construction
  • For each production A ? a
  • 1. for each terminal a in FIRST(a), add A ? a to
    MA,a.
  • 2. If e is in FIRST(a), add A ? a to MA,b for
    each terminal b in FOLLOW(A).

4
LL(1) Table Construction
  • For each production A ? a
  • 1. for each terminal a in FIRST(a), add A ? a to
    MA,a.
  • 2. If e is in FIRST(a), add A ? a to MA,b for
    each terminal b in FOLLOW(A).

5
LL(1) Table Construction
  • (2 cont.)If e is in FIRST(a), and is in
    FOLLOW(A), add A ? a to MA,.

6
LL(1) Table Construction
  • Make each undefined entry of M be error.

7
LL(1) Table Construction
  • Let us apply the algorithm to the expression
    grammar.

8
LL(1) Table Construction
  • Since FIRST(TE ') FIRST(T ) (, id
    ,production E ?TE' cause ME,( and ME,id to
    get E ?TE'

9
id ( )
E E ?TE' E ?TE'
E' E' ? TE' E' ?e E' ?e
T T ?FT' T ?FT'
T' T' ?e T ?FT' T' ?e T' ?e
F F ? id F ?(E )
10
LL(1) Table Construction
  • Production E' ? TE' causes ME', to get E'
    ?TE'

11
id ( )
E E ?TE' E ?TE'
E' E' ? TE' E' ?e E' ?e
T T ?FT' T ?FT'
T' T' ?e T ?FT' T' ?e T' ?e
F F ? id F ?(E )
12
LL(1) Table Construction
  • Production E' ? e causes ME',) and ME', to
    get E' ? e since FOLLOW (E' ) ),

13
id ( )
E E ?TE' E ?TE'
E' E' ? TE' E' ?e E' ?e
T T ?FT' T ?FT'
T' T' ?e T ?FT' T' ?e T' ?e
F F ? id F ?(E )
14
LL(1) Table Construction
  • And so on.
  • The final parsing table produced is

15
LL(1) Parsing Table
id ( )
E E ?TE' E ?TE'
E' E' ? TE' E' ?e E' ?e
T T ?FT' T ?FT'
T' T' ?e T ?FT' T' ?e T' ?e
F F ? id F ?(E )
16
Left Factoring
  • Consider the grammar
  • E ? T E TT ? int int? T (E)

17
Left Factoring
  • E ? T E TT ? int int? T (E)

Impossible to predict because for T, two
productions start with int
18
Left Factoring
  • E ? T E TT ? int int? T (E)

For E, it is not clear how to predict
19
Left Factoring
  • A grammar must be left factored before use for
    predictive parsing

20
Left Factoring
  • If a ? e, replace all productions A ? ab1 ab2
    .... abn g
  • with A ? a Z g Z ? b1 b2 .... bn
  • where Z is new nonterminal

21
Left Factoring
  • If a ? e, replace all productions A ? ab1 ab2
    .... abn g
  • with A ? a Z g Z ? b1 b2 .... bn
  • where Z is new nonterminal

22
Left Factoring
  • A graphical explanation

23
Left Factoring
  • becomes

b1
b2
aZ
b3
24
Left Factoring
  • Consider following fragment of expression grammar
  • Factor ? id
  • id ExprList
  • id ( ExprList )

25
Left Factoring
  • Factor ? id
  • id ExprList
  • id ( ExprList )

FIRST(rhs1) id
FIRST(rhs2) id
FIRST(rhs3) id
26
Left Factoring
  • after left factoring
  • Factor ? id Args
  • Args ? ExprList
  • ( ExprList )
  • e

27
Left Factoring
  • Factor ? id Args
  • Args ? ExprList
  • ( ExprList )
  • e

FIRST(rhs1) id
FIRST(rhs2)
FIRST(rhs3) (
FIRST(rhs4) FOLLOW(Factor)
28
Left Factoring
id

Factor
id
ExprList
(
)
id
ExprList
No basis for choice
29
Left Factoring

Factor
ExprList
id
(
)
ExprList
correct choice possible
30
Left Factoring
  • QuestionBy eliminating left recursion and left
    factoring, can we transform an arbitrary CFG to a
    form where it meets the LL(1) condition (and can
    be parsed predictively with a single token
    lookahead?)

31
Left Factoring
  • AnswerGiven a CFG that does not meet the LL(1)
    condition, it is undecidable whether or not an
    equivalent LL(1) grammar exists
Write a Comment
User Comments (0)
About PowerShow.com