Compiler Designs and Constructions - PowerPoint PPT Presentation

1 / 42
About This Presentation
Title:

Compiler Designs and Constructions

Description:

Scan from Left (L), Leftmost derivation (L), Look a head 1 Token. Chapter 6: Top-Down Parser ... Top: Synchronization Token (ST) ST: is a token that can end ... – PowerPoint PPT presentation

Number of Views:31
Avg rating:3.0/5.0
Slides: 43
Provided by: scie6
Category:

less

Transcript and Presenter's Notes

Title: Compiler Designs and Constructions


1
Compiler Designs and Constructions
  • Chapter 6 Top-Down Parser
  • Objectives
  • Types of Parsers
  • Backtracking Vs. Non-backtracking
  • PDA
  • Dr. Mohsen Chitsaz

2
Definition
  • Alphabet Set of Char (token)
  • Language Set of Token
  • Sentence a group of token
  • Grammar a set of (productions) that control the
    order of the occurrence of words
  • Syntax a set of rules
  • Parser software to check the syntax
  • Parsing Process of breaking down the lexemes

3
Types of Parser
  • Universal Parsing Method example
  • Cocke-Younger-Kasami Algorithm
  • Can parse any grammar
  • too inefficient to use in compiler production
  • Top-Down Parsing
  • Left most derivation
  • Bottom-up Parsing
  • Right most derivation

4
Top-Down Parsing
  • Non-backtracking Vs. Backtracking
  • Large Plant   
  • Green Plant
  • Recursive Descent Parsing (Predictive Parser)
  • We execute a set of recursive procedures to
    process the input
  • There is one procedure associated with each
    non-terminal of a grammar

5
Example
  • lttypegt ? ltsimplegt
  • lttypegt ? id
  • lttypegt ? array ltsimplegt of lttypegt ltsimplegt
    integer
  • ltsimplegt ? char
  • ltsimplegt ? num .. num

6
Procedure Match (Ttoken)
  • Begin
  • If LookaHeadT then
  •             LookaHeadNextToken
  • Else
  •             Error()
  • End

7
Procedure Type( )
  • Begin
  • If  LookaHead in integer, char, num then
  •              Simple()
  •  Else
  •        If LookaHead then
  •          Begin
  •                       Match ()
  •                       Match (id)
  •              End
  •              Else
  •            If LookaHead array then
  •        

8
  •             Begin
  •                    Match (array)
  •                     Match ()
  •                        Simple()
  •                        Match ()
  •                        Match (of)
  •                        Type()
  •              End
  •              Else
  •                        Error()
  •  End

9
Procedure Simple()
  • Begin
  •             If LookaHead integer then
  •             Match (integer)
  •             Else
  •                         If LookaHead char then
  •                                     Match (char)
  •                         Else
  •                         If LookaHead
    num then
  •                                     Begin
  •                                                
    Match (num)
  •                                                
    Match (..)
  •                                                
    Match (num)
  •                                     End
  •                         Else     Error()
  • End

10
Example
  • lttypegt ? ltsimplegt
  • lttypegt ? id
  • lttypegt ? array ltsimplegt of lttypegt ltsimplegt
    integer
  • ltsimplegt ? char
  • ltsimplegt ? num .. num

11
Example
  • Which production should we use?
  • integer
  • lttypegt ? id
  • lttypegt ? ltsimplegt
  • ltsimplegt ? integer
  • ltsimplegt ? char

12
Example
  • First (simple) integer, char, num
  • First (id)
  • First (array ltsimplegt of type array)
  • First (type) , array, integer, char, num

13
Definition
  • A ? ab
  • First (A) a
  • A ? ?
  • A ? ?
  • First of ? ? must be disjoint

14
Push Down Automata(Non-recursive predictive
Parsing)
15
Push Down Automata(Non-recursive predictive
Parsing)
  •  PDA Q, ?, H,?, q0, Z, F
  • Q finite set of States
  • ? set of input alphabet
  • H set of stack symbols
  • ? transition function (q, a, z)
  • q0 starting state
  • Z starting stack
  • F final states

16
Operation on ?
  • state operation
  • ? change()
  • ? stay()
  • stack operation 
  • ? push()
  • ? pop()
  • ? replace()
  • ? none()
  • input operation
  • ? advance()
  • ? retain()

17
Example
INPUT
  • ? (, ), -
  • H A,
  • q0 S
  • Z

S T A R T
 
18
  • If input is (())-
  •  Stack           Input
  •                       (())-
  • A                   ())-
  • AA                 ))-
  • A                   )-
  •                       -

19
Derivation Tree
  •  S ? (S) ?

20
LL(1) Grammar (push down machine)
  • Scan from Left (L), Leftmost derivation (L), Look
    a head 1 Token

21
Definition
  • A production is called NULL if the RHS of that
    production is Ø
  • A ? ?
  • A production is call NULLABLE if the RHS can be
    reduced to Ø
  • B ? A
  • A ? ?

22
Example of LL(1) Grammar
  • 1- S ? AbB- First(AbB-) a,e,g, ?
  • 2- S ? d First(d) d
  • 3- A ? C Ab First(C Ab) a,e
  • 4- A ? B First(B) g, ?
  • 5- B ? gSd First(gSd) g
  • 6- B ? ? First (Ø)
  • 7- C ? a First (a)        a
  • 8- C ? ed First (ed)    e

23
  • First (?) a ? gta?
  • Set of terminal symbols that occur at the
    beginning of string derived from ?

24
  • Follow (?) Set of input symbols that can
    immediately follow ?
  • Follow (A) b
  • Follow (B) -, b, d
  • 1              4
  • S ---gtAbB ---gtBbB
  • 1              5                   
    1
  • S ---gtAbB ---gtAbgSd ---gtAbgAbBd

25
SELECT (PREDICT)
  • SELECT (A-gt?) First (?)
  •                         If ? Ø First (?) U
    Follow (A) 
  • SELECT (1) a,e,g U Follow(A)a, e, g ,b
  • SELECT (2) d
  • SELECT (3) a,e
  • SELECT (4) First(4) U Follow (A) g,b
  • SELECT (5) g
  • SELECT (6) First(6) U Follow (B) -,b,d
  • SELECT (7)a
  • SELECT (8) e

26
Is this grammar LL(1)?
  • S SELECT (1) ? SELECT (2) a,e,g,b ?
    d Ø
  • A SELECT (3) ? SELECT (4) a,e ?
    g,b Ø
  • B SELECT (5) ? SELECT (6) g ?
    -,b,d Ø
  • C SELECT (7) ? SELECT (8) a ? e Ø
  • Def Grammar is LL(1), IIF production with the
    same LHS have disjoint prediction set.

27
Creating the Table
  • Stack symbolsrows
  • Input symbolscolumns
  • A? b ?
  • For row A, column b
  • REPLACE(?r), ADVANCE
  • A ??
  • For row A, column(selection set)
  • REPLACE (?r), RETAIN
  • A ? b
  • For row A, column b
  • POP, ADVANCE

28
Creating the Table
  • Row b, column b POP, ADVANCE
  • Row ?, column -ACCEPT
  • All other are ERROR

29
Parsed Table
 
30
  • 1- Replace (? B b A), Retain
  • 2- Pop, Advance
  • 3- Replace (bAC), Retain
  • 4- Replace (B), Retain
  • 5- Replace (dS), Advance
  • 6- Pop, Retain
  • 7- Pop, Advance
  • 8- Replace(d), Advance

31
Input        b g d d -
32
Parse Tree
33
Recovery in a Top-Down Parser
  • Advantage of TDP is efficient error recovery.
  • Error If Top of stack (token) is not the same as
    Look a Head (token)
  • Recovery
  • Pop stack until Top Synchronization Token (ST)
            ST is a token that can end blocks of
    codes Read input symbol until current LookaHead
    symbol matches the symbol at the top of the stack
    or reaches the end of input
  • If not end of input you are recovered

34
LL(1) Grammars Parser
  • facts
  • CFG
  • Leftmost Parser
  • Unambiguous
  • O(n)
  • Can be used for automatically generated parser

35
Making Grammar LL(1)
  • 1-Remove Common Prefixed (Left Factor)
  • S --gt aBCd S --gt aBD
  • S --gt aB D --gt
  • D --gt Cd
  • ltsgt --gt if ltexpgt then ltactiongt endif
  • ltsgt --gt if ltexpgt then ltactiongt else ltactiongt
    endif
  • ltsgt --gt if ltexpgt then ltactiongt ltrestgt
  • ltrestgt --gt endif
  • ltrestgt --gt else ltactiongt endif

36
2-Remove Left RecursionE
  • E --gt E TE --gt TT --gt id
  • A--gt A?A--gtB
  • A--gtA1 A2A1--gtBA2--gt?A2A2--gt
  • E--gtE1E2 E1--gtT E2--gtT E2 T--gtid E2--gt

37
3-Remove Unreachable Products
  • S--gta S --gta
  • B--gtS

38
4-Corner Substitution
  • S --gt a
  • S --gt AS
  • A --gt ab
  • A --gt cA
  • S --gt aB
  • B --gt
  • B --gt bS
  • A --gt ab
  • A --gt cA
  • S --gt cAS
  • S --gt AB?
  • B --gt?1
  • B --gt?2
  • B --gt?3
  • S --gt A?1?
  • S --gt A?2?
  • S --gt A?3?

39
5-Singleton Substitution
  • S--gt?B?
  • S--gt ?S'?S'--gt B
  • ltSgt --gt ltLABELgt ltUNLABLEgt ltLABELgt --gt id
    ltLABELgt --gt ltUNLABLEgt --gt id ltEXPgt

40
Singleton Substitution
  • -ltSgt --gt id ltUNLABLEgt ltSgt --gt ltUNLABLEgt
    ltUNLABLEgt --gt idltEXPgt
  • -ltSgt --gt idltUNLABLEgt ltSgt --gt id ltEXPgt
    ltUNLABLEgt --gt id ltEXPgt
  • -ltSgt --gt id ltid-restgt ltid-restgt --gt ltUNLABLEgt
    ltid-restgt --gt ltEXPgt ltUNLABLEgt --gt id
    ltEXPgt

41
Some Grammars Can Not be LL(1)
  • If _ Then _ Else If ltexpgt then ltactiongt If
    ltexpgt then ltactiongt else ltactiongt
  • S --gt aBcS S --gt aBcSeS B --gt d S --gt b

42
  • Q-Grammar
  • A --gt a?1
  • A --gt b?1
  • A --gt?
  • S-Grammar
  • A --gt a?1
  • A --gt b?2
  • A and S grammar are a LL(1) and we can make a
    push down Machine
Write a Comment
User Comments (0)
About PowerShow.com