chapter 3_syntax analysis.pptx

Upload: anonymous-zdrmfipgf

Post on 06-Jul-2018

220 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    1/88

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    2/88

    Contents (Session-1)

    ntro!u"tionContext-#ree $rammar%eri&ationParse 'reeAmbi$uityesol&in$ Ambi$uity

    mme!iate n!ire"t *e#t e"ursionEliminatin$ mme!iate n!ire"t *e#t e"ursion

    *e#t +a"torin$,on-Context +ree *an$ua$e Constru"ts

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    3/88

    Abstra"t representations o# the input pro$ram "oul! be

    abstract-syntax tree/parse tree . symbol tableinterme!iate "o!eob/e"t "o!e

    Syntax analysis is !one by the parser0Pro!u"es a parse tree #rom whi"h interme!iate "o!e "an be

    $enerate!By !ete"tin$ whether the pro$ram is written #ollowin$ the

    $rammar rules0eports syntax errors attempts error "orre"tion an!

    re"o&ery

    Colle"ts in#ormation into symbol tables

    ntro!u"tion

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    4/88

    Parsers "an be 'op-!own or Bottom-up

    ntro!u"tion2

    Sour"e

    pro$ram

    *exi"al

    analyzer

    toen

    e4uest#or toen

    parser

    est o##ront en!

    Symboltable

    nt0

    "o!e

    Parse

    tree

    Error

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    5/88

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    6/88

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    7/88

    BNF (Backus Normal Form or Backus–Naur Form) a

    notation te"hni4ues #or "ontext-#ree $rammars o#tenuse! to !es"ribe the syntax o# lan$ua$es use! in"omputin$as many extensions an! &ariants

    Extended Backus–Naur orm (EB,+) !ugmented Backus–Naur orm (AB,+)0A B,+ spe"i8"ation is a set o# !eri&ation rules written assymbol@ ::= expression

    B,+ #or &ali! arithmeti" expression

    expr@ ::= expr@ op@ expr@expr@ ::= ( expr@ )

    expr@ ::= - expr@

    expr@ ::= i!

    op@ ::= . 9 - 9 ; 9 <

    Context +ree 5rammars(C+5)2

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    8/88

    A se4uen"e o# repla"ements o# non-terminalsymbols to obtain strin$s

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    9/88

    %eri&ate strin$ :(i!.i!) #rom 51

    E ⇒ -E ⇒ -(E) ⇒ -(E.E) ⇒ -(i!.E) ⇒ -(i!.i!) (*D%)

    E ⇒ -E ⇒ -(E) ⇒ -(E.E) ⇒ -(E.i!) ⇒ -(i!.i!) (D%) At ea"h !eri&ation step we "an "hoose any o# the non-

    terminal in the sentential #orm o# 5 #or the repla"ement0

    # we always "hoose the le#t-most non-terminal in ea"h!eri&ation step this !eri&ation is "alle! as left-mostderivation(LM!0

    # we always "hoose the ri$ht-most non-terminal in ea"h!eri&ation step this !eri&ation is "alle! as ri"#t-mostderivation($M!0

    %eri&ation2

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    10/88

    A parse tree "an be seen as a $raphi"alrepresentation o# a !eri&ationnner no!es o# a parse tree are non-terminal

    symbols0 'he lea&es o# a parse tree are terminal symbols0

    Parse 'ree

    E ⇒ -E EE-

    E

    E

    EE

    E

    .

    -

    ( )

    E

    E

    E-

    ( )

    E

    E

    i!

    E

    E

    E .

    -

    ( )

    i!

    E

    E

    E

    EE .

    -

    ( )

    i!

    ⇒ -(E) ⇒ -(E.E)

    ⇒ -(i!.E)   ⇒ -(i!.i!)

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    11/88

    An ambi$uous $rammar is one that pro!u"esmore than one *D% or more than one D% #orthe same senten"e0E ⇒ E.E

    ⇒ i!.E⇒ i!.E;E

    ⇒ i!.i!;E⇒ i!.i!;i!

    Ambi$uity

    E

    E .

    i! E

    E

    ; E

    i! i!

    E ⇒ E;E⇒ E.E;E

    ⇒ i!.E;E⇒ i!.i!;E⇒ i!.i!;i!

    E

    i!

    E .

    i!

    i!

    E

    E

    ; E

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    12/88

    +or the most parsers the $rammar must beunambi$uous0

    # a $rammar unambi$uous $rammar then there areuni4ue sele"tion o# the parse tree #or a senten"e=e shoul! eliminate the ambi$uity in the $rammar

    !urin$ the !esi$n phase o# the "ompiler0

    An unambi$uous $rammar shoul! be written toeliminate the ambi$uity0=e ha&e to pre#er one o# the parse trees o# a

    senten"e ($enerate! by an ambi$uous $rammar)to !isambi$uate that $rammar to restri"t to this"hoi"e0

    Ambi$uity2

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    13/88

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    14/88

    'ption % a!! a meta-rule e0$0 pre"e!en"e an!

    asso"iati&ity rules+or example Helse asso"iates with "losest pre&ious ifIwors eeps ori$inal $rammar inta"ta! ho" an! in#ormal

    'ption & rewrite the $rammar to resol&e ambi$uityexpli"itlystmt →   matchedstmt $ unmatchedstmt 

    matchedstmt →   if   expr then  matchedstmt else  matchedstmt $

    otherstmtsunmatchedstmt →  if   expr then  stmt $

      if   expr then  matchedstmt else  unmatchedstmt 

    #ormal no a!!itional rules beyon! syntaxsometimes obs"ures ori$inal $rammar

    esol&in$ Ambi$uity

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    15/88

    'ption ) re!esi$n the lan$ua$e to remo&ethe ambi$uity

    Stmt ::= ... |

    if Expr then Stmt end  |

    if Expr then Stmt else Stmt end  

    #ormal "lear ele$ant

    allows se4uen"e o#Stmts

     inthen

     an!else

     bran"hes no > ? nee!e!extra end  re4uire! #or e&ery if

    esol&in$ Ambi$uity

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    16/88

    A $rammar is left recursive  i# it has a non-

    terminal A su"h that there is a !eri&ation0

     A ⇒ Aα #or some strin$ α 

     'op-!own parsin$ te"hni4ues cannot han!le le#t-re"ursi&e $rammars0

    So we ha&e to "on&ert our le#t-re"ursi&e $rammarinto an e4ui&alent $rammar whi"h is not le#t-re"ursi&e0

     'wo types o# le#t-re"ursionimmediate left-recursion - appear in a sin$le step o# the

    !eri&ation ()%ndirect left-recursion - appear in more than one step o# the

    !eri&ation0

    *e#t e"ursion

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    17/88

    Eliminatin$ mme!iate *e#t e"ursion

    A → A α 9 β  where β !oes not start with A⇓   eliminate imme!iate le#t

    re"ursion

    A → β AJ

    AJ → α AJ  9 ε 

    A → A α1 9 000 9 A αm 9 β1 9 000 9 βn  where β1 000 βn !o

    not start with A

    ⇓ eliminate imme!iate le#t re"ursionA → β1 AJ 9 000 9 βn AJ

    AJ

     → α1 AJ

      9 000 9 αm AJ

      9 ε  an e4ui&alent $rammar

    n $eneral

    A → β AJ 9 β

    AJ → α AJ  9 α

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    18/88

    emo&e le#t re"ursion #rom the $rammarbelowE → E.' 9 '

     ' → ';+ 9 +

    + → i! 9 (E)Answer

    E → ' EJ

    EJ → .' EJ 9 ε ' → + 'J

     'J → ;+ 'J  9 ε+ → i! 9 (E)

    Eliminatin$ *e#t e"ursion2

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    19/88

    A $rammar "annot be imme!iately le#t-re"ursi&ebut it still "an be le#t-re"ursi&e0

    By /ust eliminatin$ the imme!iate le#t-re"ursionwe may not $et a $rammar whi"h is not le#t-

    re"ursi&e0S → Aa 9 bA → S" 9 !'his $rammar is not imme!iately le#t-

    re"ursi&e

    but it is still le#t-re"ursi&e0

    S ⇒ Aa ⇒ S"a orA ⇒ S" ⇒ Aa" "auses to a le#t-re"ursion

    So we ha&e to eliminate all le#t-re"ursions #rom our

    $rammar

    n!ire"t *e#t-e"ursion

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    20/88

    Arran$e non-terminals in some or!er A1 000 An

    we will remo&e in!ire"t le#t re"ursion by"onstru"tin$ an e4ui&alent $rammar 5J su"h that- i# Ai → A /a  is any pro!u"tion o# 5J then i /

    +or ea"h non-terminal in turn !o+or ea"h terminal *i su"h that %+ +i an! we ha&e a

    pro!u"tion rule o# the #orm Ai → A /α  where the A /pro!u"tions are A / → β1 9 29Bn  !oepla"e the pro!u"tion rule Ai → A / α with the rule Ai

    → β1α 9 29Bnα   Eliminate any imme!iate le#t re"ursion amon$ the

    pro!u"tions αβ1 

    Eliminatin$ n!ire"t *e#t-e"ursion

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    21/88

    xample %

    S → Aa 9 bA → A" 9 S! 9 # 

    - r!er o# non-terminals S 6 A1 A 6 AFA1 → AF a 9 bAF → AF " 9 A1 ! 9 # 

     'he only pro!u"tion with /i is AF → A1 !#or A- epla"e it with AF → AF a! 9 b!

    AF → AF " 9 AF a! 9 b! 9 # 

    - Eliminate the imme!iate le#t-re"ursion in AAF → b!AJ9b!AJAJ → "AJ 9 a!AJ9 ε

    So the resultin$ e4ui&alent $rammar whi"h is not le#t-re"ursi&e isS → Aa 9 bA → b!AJ 9 #AJ

    AJ → "AJ 9 a!AJ 9 ε

    Eliminatin$ n!ire"t *e#t-e"ursion2

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    22/88

    Example F A1 →  A2 A3 A2 →  A3 A1 | b

     A3 →  A1 A1 | a

    Replace A3 →  A1 A1 by A3 →  A2 A3 A1

     and then replace this by

     A3 →  A3 A1 A3 A1 and A3 → b  A3 A1Eliminating direct left recursion in the above,

    gives: A3 → a | b  A3 A1

    ! → A1 A3 A1 | ε

     'he resultin$ $rammar is then   A1 →  A2 A3  A2 →  A3 A1 | b

     A3 → a | b  A3 A1

    ! → A1 A3 A1 | ε

    Eliminatin$ n!ire"t *e#t-e"ursion2

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    23/88

    A pre!i"ti&e parser (a top-!own parser without

    ba"tra"in$) insists that the $rammar must beleft-factored0

    stmt →  if   expr then  stmt else  stmt $

     if   expr then  stmt

    when we see if we "annot now whi"hpro!u"tion rule to "hoose to re-write stmt  in the!eri&ation0

    n $eneral

    A →  αβ1  9 αβF  where α is non-empty an! the 8rstsymbols o# β1 an! βF (i# they ha&e one)are !iLerent

    when pro"essin$ α we "annot now whether to expan!

    A to αβ1  or A to αβF 

    *e#t +a"torin$

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    24/88

    But i# we re-write the $rammar as #ollows

     A →  αAJ AJ → β1  9 βF  so we "an imme!iately expan! A to αAJ

    *e#t +a"torin$ : Al$orithm

    +or ea"h non-terminal A with two or morealternati&es (pro!u"tion rules) with a "ommonnon-empty pre8x let say

     A →  αβ1 9 000 9 αβn 9 γ 1 9 000 9 γ m "on&ert it intoA →  αAJ 9 γ 1 9 000 9 γ m AJ → β1 9 000 9 βn

    *e#t +a"torin$2

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    25/88

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    26/88

     'here are some lan$ua$e "onstru"tions in the

    pro$rammin$ lan$ua$es whi"h are not "ontext-#ree0 'his means that we "annot write a "ontext-#ree$rammar #or these "onstru"tions0

    *1 6 > ω"ω 9 ω is in (a9b);? is not "ontext-#ree!e"larin$ an i!enti8er an! "he"in$ whether it is!e"lare! or not later0 =e "annot !o this with a"ontext-#ree lan$ua$e0 =e nee! semanti" analyzer(whi"h is not "ontext-#ree)0

    *F 6 >anbm"n!m 9 n≥1 an! m≥1 ? is not "ontext-#ree!e"larin$ two #un"tions (one with n parameters the

    other one with m parameters) an! then "allin$ them

    with a"tual parameters0

    ,on-Context +ree *an$ua$e Constru"ts

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    27/88

    Contents(Session-F)

     'op %own Parsin$e"ursi&e-%es"ent Parsin$Pre!i"ti&e Parsere"ursi&e Pre!i"ti&e Parsin$

    ,on-e"ursi&e Pre!i"ti&e Parsin$**(1) Parser : Parser A"tionsConstru"tin$ **(1) - Parsin$ 'ables

    Computin$ +S' an! +**= #un"tions**(1) 5rammarsProperties o# **(1) 5rammars

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    28/88

     'op-!own parsin$ in&ol&es "onstru"tin$ a parse tree #or

    the input strin$ startin$ #rom the rootBasi"ally top-!own parsin$ "an be &iewe! as 8n!in$ a

    le#tmost !eri&ation #or an input strin$0How it works? Start with the tree o# one no!e labele! with

    the start symbol an! repeat the #ollowin$ steps until the #rin$eo# the parse tree mat"hes the input strin$10 At a no!e labele! A sele"t a pro!u"tion with A on its *S

    an! #or ea"h symbol on its S "onstru"t the appropriate"hil!

    F0 =hen a terminal is a!!e! to the #rin$e that !oesnMt mat"hthe input strin$ ba"tra"

    30 +in! the next no!e to be expan!e!

    ! Minimize the number of backtracks as much as

     possible

     'op %own Parsin$

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    29/88

     'wo types o# top-!own parsin$

    Recursiveescent "arsin#Ba"tra"in$ is nee!e! (# a "hoi"e o# a pro!u"tion rule

    !oes not wor we ba"tra" to try other alternati&es0)t is a $eneral parsin$ te"hni4ue but not wi!ely use!

    be"ause it is not e"ient"re$ictive "arsin#no ba"tra"in$ an! hen"e e"ientnee!s a spe"ial #orm o# $rammars (**(1) $rammars)0

     'wo typesRecursive "re$ictive "arsin# is a spe"ial #orm o#&ecursive 'escent (arsing without ba"tra"in$0

    %onRecursive &'able riven( (redictive (arser isalso nown as **(1) parser0

     'op %own Parsin$2

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    30/88

    t tries to 8n! the le#t-most !eri&ation0

    Ba"tra"in$ is nee!e!Example

    S → aB"

    B → b" 9 binput ab"

    e"ursi&e-%es"ent Parsin$

    A left-recursive "rammar "an "ause a

    re"ursi&e-!es"ent parser e&en one withba"tra"in$ to $o into an in8nite loop0 'hat is when we try to expan! a non-terminal B

    we may e&entually 8n! oursel&es a$ain tryin$

    to expan! B without ha&in$ "onsume! any input0

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    31/88

    A $rammar     a $rammar suitable #or pre!i"ti&e

      eliminate le#t  parsin$ (a **(1) $rammar)  le#t re"ursion #a"tor   no )*++ guarantee0

    =hen re-writin$ a non-terminal in a !eri&ation step apre!i"ti&e parser "an uni4uely "hoose a pro!u"tionrule by /ust looin$ the "urrent symbol in the inputstrin$0

    stmt → if 000000 9  while  000000 9

      begin 000000 9

      for 00000

    owe&er e&en thou$h we eliminate the le#tre"ursion in the $rammar an! le#t #a"tor it it may not

    be suitable #or pre!i"ti&e parsin$ (not **(1) $rammar)0

    Pre!i"ti&e Parser

    Note =hen we aretryin$ to write the non-terminal stmt  we "an

    uni4uely "hoose thepro!u"tion rule by /ustlooin$ the "urrenttoen0

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    32/88

    Pre!i"ti&e Parsin$ "an be re"ursi&e or non-re"ursi&e

    n re"ursi&e pre!i"ti&e parsin$ ea"h non-terminal"orrespon!s to a pro"e!ure

    "ase o# the "urrent toen >

    NaJ - mat"h the "urrent toen with a an! mo&e to the next toenO

      - "all NBJO

      - mat"h the "urrent toen with b an! mo&e to the next toenO

    NbJ - mat"h the "urrent toen with b an! mo&e to the next toenO

      - "all NAJO

      - "all NBJO

    ?

    ?

    e"ursi&e Pre!i"ti&e Parsin$

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    33/88

    =hen to apply ε-pro!u"tions

    A → aA 9 bB 9 l

    # all other pro!u"tions #ail we shoul! apply an l-pro!u"tion0 +or example i# the "urrent toen is nota or b we may apply the ε-pro!u"tion0

    Most correct choice) =e shoul! apply a l-pro!u"tion #or a non-terminal A when the "urrenttoen is in the follo. set o# A (whi"h terminals "an#ollow A in the sentential #orms)0

    e"ursi&e Pre!i"ti&e Parsin$2

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    34/88

    e"ursi&e Pre!i"ti&e Parsin$2

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    35/88

     A non-re"ursi&e pre!i"ti&e parser "an be built

    by maintainin$ a sta" expli"itly rather thanimpli"itly &ia re"ursi&e "alls

    %onRecursive pre$ictive parsin# is a table-!ri&en top-!own parser0

    ,on-e"ursi&e Pre!i"ti&e Parsin$

    Do!el o# a table-!ri&enpre!i"ti&e parser

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    36/88

     nput buLerour strin$ to be parse!0 =e will assume that its en! is mare!

    with a spe"ial symbol Q0

    utputa pro!u"tion rule representin$ a step o# the !eri&ation

    se4uen"e (le#t-most !eri&ation) o# the strin$ in the input

    buLer0

    Sta""ontains the $rammar symbolsat the bottom o# the sta" there is a spe"ial en! marer

    symbol Q0initially the sta" "ontains only the symbol Q an! the startin$

    symbol S0when the sta" is emptie! (i0e0 only Q le#t in the sta") the

    parsin$ is "omplete!0

    Parsin$ tablea two-!imensional array DRAa

    ,on-e"ursi&e Pre!i"ti&e Parsin$2

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    37/88

     'he symbol at the top o# the sta" (say T) an! the

    "urrent symbol in the input strin$ (say a) !eterminethe parser a"tion0  'here are #our possible parser a"tions0

    10 # T an! a are Q  parser halts (su""ess#ul"ompletion)

    F0 # T an! a are the same terminal symbol (!iLerent#rom Q)

     parser pops T #rom the sta" an! mo&es thenext symbol in the input buLer0

    30 # T is a non-terminal parser loos at the parsin$ table entry DRTa0# DRTa hol!s a pro!u"tion rule T→ U1 UF000U itpops T #rom the sta" an! pushes UU-1000U1 into

    the sta"0 'he parser also outputs the pro!u"tion

    **(1) Parser : Parser A"tions

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    38/88

    **(1) Parser : Example1

    S → aBa **(1) Parsin$B → bB 9 ε   'able

    stack input outputQS abbaQ S → aBaQaBa abbaQ

    QaB bbaQ B → bBQaBb bbaQ

    QaB baQ B → bBQaBb baQQaB aQ B → εQa aQ

    Q Q a""ept su""ess#ul "ompletion

    a b $

    S S → aBa

    B B → ε B → bB*e will see

    how to

    construct parsin#

    table +er,soon

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    39/88

    **(1) Parser : ExampleF

    E → 'EJ

    EJ → .'EJ 9 ε ' → +'J

     'J → ;+'J 9 ε

    + → (E) 9 i!id + * ( ) $E E → TE’ E → TE’

    E’ E’ → +TE’ E’ → ε E’ → ε

    T T → FT’ T → FT’

    T’ T’ → ε T’ → *FT’ T’ → ε T’ → εF F → id F → (E)

    E is start symbol

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    40/88

     

    **(1) Parser : ExampleF

    stack inputoutput

    QE i!.i!Q E → 'EJ

    QEJ ' i!.i!Q ' → +'J

    QEJ 'J+ i!.i!Q + → i!

    Q EJ 'Ji! i!.i!Q

    Q EJ 'J .i!Q 'J → εQ EJ  .i!Q EJ → .'EJ

    Q EJ '. .i!Q

    Q EJ ' i!Q ' → +'J

    Q EJ 'J + i!Q + → i!Q EJ 'Ji! i!Q

    Q EJ 'J Q  'J → ε

    Q EJ Q EJ → ε

    Q Q a""ept

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    41/88

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    42/88

     'wo #un"tions are use! in the "onstru"tion o#

    **(1) parsin$ tables+S'+**=

    F0$3( !  is a set o# the terminal symbols whi"ho""ur as 8rst symbols in strin$s !eri&e! #rom α where α is any strin$ o# $rammar symbols0i# α !eri&es to ε then ε is also in +S'(α) 0

    F'LL'4(*! is the set o# the terminals whi"ho""ur imme!iately a#ter the non-terminal !  inthe strin$s !eri&e! #rom the startin$ symbol0

    a terminal a is in +**=(A) i# S ⇒ αAaβ

    Constru"tin$ **(1) Parsin$ 'ables

    C S # S i

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    43/88

    10 # T is a terminal symbol then+S'(T)6>T?

    F0 # T is ε then +S'(T)6>ε?

    30 # T is a non-terminal symbol an! T → ε isa pro!u"tion rule then a!! ε in +S'(T)0

    V0 # T is a non-terminal symbol an! T →  U1 UF00Un is a pro!u"tion rule theni# a terminal a in +S'(Ui) an! ε is in all

    +S'(U /) #or /61000i-1 then a is in+S'(T)0

    Compute +S' #or a Strin$ T

    C +S' # S i T

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    44/88

    xample

    E → 'EJEJ → .'EJ 9 ε

     ' → +'J

     'J → ;+'J9 ε+ → (E) 9 i!

    From $ule %+S'(i!) 6 >i!?

    From $ule &+S'(ε) 6 >ε?

    From $ule ) and5+irst(+) 6 >( i!?+irst('J) 6 >; ε? 

    Compute +S' #or a Strin$ T2

    +S'(E’) 6 >+ ε?+S'(E) 6 >(id?

    't#ers

    +S'('EJ) 6 >(i!?+S'(.'EJ ) 6 >.?+S'(+'J) 6 >(i!?+S'(;+'J) 6 >;?

    +S'((E)) 6 >(?

    C t +**= (# t i l )

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    45/88

    10 Q is in +**=(S) i# S is the start symbol

    F0 *oo at the o""urren"e o# a nonWterminal on theS o# a pro!u"tion whi"h is #ollowe! bysomethin$

    i# A → αBβ  is a pro!u"tion rule then e&erythin$ in+S'(β) ex"ept ε is +**=(B)

    30 *oo at B on the S that is not #ollowe! by

    anythin$# ( A → αB is a pro!u"tion rule ) or ( A → αBβ is

    a pro!u"tion rule an! ε is in +S'(β) ) thene&erythin$ in +**=(A) is in +**=(B)0

    Compute +**= (#or non-terminals)

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    46/88

    xample

    i0 E → 'EJii0 EJ → .'EJ 9 εiii0  ' → +'J

    i&0  'J → ;+'J 9 ε

    &0 + → (E) 9 i!+**=(E) 6 > Q ) ? be"ause.+rom 8rst rule +ollow (E) "ontains Q.+rom ule F +ollow(E) is 8rst(!) #rom the pro!u"tion + → 

    (E)

    +**=(EJ) 6 > Q ) ? 20 ule 3+**=(') 6 > . ) Q ?. +rom ule F . is in +**=('). +rom ule 3 E&erythin$ in +ollow(E) is in +ollow(') sin"e

    +irst(EJ) "ontains ε

    +**=(+) 6 >. ; ) Q ? 2same reasonin$ as

    Compute +**= (#or non-terminals)

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    47/88

    +or ea"h pro!u"tion rule A → α  o# a $rammar510 #or ea"h terminal a in +S'(α)

      a!! A → α  to DRAa

    F0 # ε in +S'(α)

      #or ea"h terminal a in +**=(A) a!! A → α to DRAa

    30 # ε in +S'(α) an! Q in +**=(A)

    Constru"tin$ **(1) Parsin$ 'able -- Al$orithm

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    48/88

    E → 'EJ +S'('EJ)6>(i!?    E → 'EJ  into DRE( an! DREi!

    EJ → .'EJ +S'(.'EJ )6>.?    EJ → .'EJ into DREJ. EJ → ε +S'(ε)6>ε?    none

    but sin"e ε in +S'(ε)an! +**=(EJ)6>Q)?  EJ → ε  into DREJQ an! DREJ)

     ' → +'J +S'(+'J)6>(i!?    ' → +'J into DR'( an! DR'i! 

     'J → ;+'J +S'(;+'J )6>;?    'J → ;+'J into DR'J; 

     'J → ε  +S'(ε)6>ε?    nonebut sin"e ε in +S'(ε)an! +**=('J)6>Q).?    'J → ε  into DR'JQ DR'J)

    DR'J.

    + → (E) +S'((E) )6>(?    + → (E) into DR+(

    Constru"tin$ **(1) Parsin$ 'able -- Example

    **(1) 5rammars

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    49/88

    **(1) 5rammarsA $rammar whose parsin$ table has no multiple-!e8ne!

    entries is sai! to be **(1) $rammar0+irst * re#ers input s"anne! #rom le#t the se"on! * re#ers le#t-

    most !eri&ation an! 1 re#ers one input symbol use! as a loo-hea! symbol !o !etermine parser a"tion input s"anne! #rom le#tto ri$ht

    A $rammar 5 is **(1) i# an! only i# the #ollowin$ "on!itions

    hol! #or two !istin"ti&e pro!u"tion rules A → α  an! A → β10 Both α an! β "annot !eri&e strin$s startin$ with same terminals0F0 At most one o# α an! β "an !eri&e to ε030 # β "an !eri&e to ε then α "annot !eri&e to any strin$ startin$

    with a terminal in +**=(A)0

    rom * " 0 e can say that irst1 α  2 % irst1 β  2 3 +

    rom 40 means that if ε  is in irst1 β  20 then irst1 α  2 % ollo1!2 3 +an! the lie

    A 5rammar whi"h is not **(1)

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    50/88

    A 5rammar whi"h is not **(1) 'he parsin$ table o# a $rammar may "ontain more than one

    pro!u"tion rule0n this "ase we say that it is not a **(1) $rammar0

    S → i C t S E 9 aE → e S 9 εC → b

    +S'(iCtSE) 6 >i?

    +S'(a) 6 >a?

    +S'(eS) 6 >e?

    +S'(ε) 6 >ε?

    +S'(b) 6 >b?+**=(S) 6 > Qe ?

    +**=(E) 6 > Qe ?

    +**=(C) 6 > t ? two pro!u"tion rules #or DREe

      6roblem  ambi"uity

    a b e i t $

    S S → a S → iCtSE

    E E → e S

    E → ε

    E → ε

    C C → b

    A 5rammar whi"h is not **(1)

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    51/88

    A 5rammar whi"h is not **(1)=hat !o we ha&e to !o i# the resultin$ parsin$ table "ontains

    multiply !e8ne! entries

    Eliminate le#t re"ursion in the $rammar i# it is not eliminate!A → Aα 9 β  any terminal that appears in +S'(β) also appears

    +S'(Aα) be"ause Aα ⇒ βα0 # β is ε any terminal that appears in +S'(α) also appears

    in +S'(Aα) an! +**=(A)0*e#t #a"tor the $rammar i# it is not le#t #a"tore!0A $rammar is not le#t #a"tore! it "annot be a **(1) $rammar

    A → αβ1 9 αβF

    any terminal that appears in +S'(αβ1) also appears in+S'(αβ

    # its (new $rammarJs) parsin$ table still "ontains multiply!e8ne! entries that $rammar is ambi$uous or it is inherentlynot a **(1) $rammar0

    An ambi$uous $rammar "annot be a **(1) $rammar0

    Error e"o&ery in Pre!i"ti&e Parsin$

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    52/88

    Error e"o&ery in Pre!i"ti&e Parsin$

    An error may o""ur in the pre!i"ti&e parsin$

    (**(1) parsin$)i# the terminal symbol on the top o# sta" !oes

    not mat"h with the "urrent input symbol0i# the top o# sta" is a non-terminal A the

    "urrent input symbol is athe parsin$ table entry DRAa is empty0

    =hat shoul! the parser !o in an error "ase 'he parser shoul! be able to $i&e an error

    messa$e (as mu"h as possible meanin$#ul errormessa$e)0

    t shoul! re"o&er #rom that error "ase an! it

    C t t (S i 3)

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    53/88

    Contents (Session-3)

    Bottom Xp Parsin$an!le Prunin$mplementation o# A Shi#t-e!u"e

    Parser* Parsers* Parsin$ Al$orithm

    A"tions o# A *-ParserConstru"tin$ S* Parsin$ 'ablesS*(1) 5rammar

    Error e"o&ery in * Parsin$

    Bottom Xp Parsin$

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    54/88

    Bottom-Xp Parsin$A bottom-up parser "reates the parse tree o# the

    $i&en input startin$ #rom lea&es towar!s the root0A bottom-up parser tries to 8n! the D% o# the $i&en

    input in the re&erse or!er0

    Bottom-up parsin$ is also nown as s#ift-reduceparsin" be"ause its two main a"tions are shi#t an!re!u"e0At ea"h s#ift action the "urrent symbol in the input

    strin$ is pushe! to a sta"0At ea"h reduction step the symbols at the top o# the

    sta" will be repla"e! by the non-terminal at the le#tsi!e o# that pro!u"tion0

    *ccept Su""ess#ul "ompletion o# parsin$0rror Parser !is"o&ers a syntax error an! "alls an

    error re"o&ery routine0

    Bottom Xp Parsin$

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    55/88

    Bottom-Xp Parsin$2A shi#t-re!u"e parser tries to re!u"e the $i&en input

    strin$ into the startin$ symbol0a strin$   the startin$ symbol

      re!u"e! to

    At ea"h re!u"tion step a substrin$ o# the input

    mat"hin$ to the ri$ht si!e o# a pro!u"tion rule isrepla"e! by the non-terminal at the le#t si!e o# thatpro!u"tion rule0

    # the substrin$ is "hosen "orre"tly the ri$ht most

    !eri&ation o# that strin$ is "reate! in the re&erse or!er0

    i$htmost %eri&ation S ⇒ ω

    Shi#t-e!u"e Parser 8n!s ω ⇐ 000 ⇐ Srm rm

    rm

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    56/88

    an!le

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    57/88

    an!len#ormally a #andle o# a strin$ is a substrin$ that

    mat"hes the ri$ht si!e o# a pro!u"tion rule0But not e&ery substrin$ mat"hes the ri$ht si!e o# apro!u"tion rule is han!le

    A #andle o# a ri$ht sentential #orm γ  (≡ αβω) is  a pro!u"tion rule A → β an! a position o# γ where the strin$ β may be #oun! an! repla"e! by A topro!u"e

    the pre&ious ri$ht-sentential #orm in a ri$htmost!eri&ation o# γ 0 

    S ⇒ αAω ⇒ αβω

    # the $rammar is unambi$uous then e&ery ri$ht-sentential #orm o# the $rammar has exa"tly one

    han!le0

    rm rm

    an!le Prunin$

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    58/88

    an!le Prunin$A ri$ht-most !eri&ation in re&erse "an be

    obtaine! by #andle-prunin"0

    S ⇒ γ Y ⇒ γ 1 ⇒ γ F ⇒ 000 ⇒ γ n-1 ⇒ γ n6 ω

    input strin$

    Start #rom γ n 8n! a han!le An→βn in γ n an!repla"e βn by An to $et γ n-10

     'hen 8n! a han!le An-1→βn-1 in γ n-1 an! repla"e βn-1 in by An-1 to $et γ n-F0

    epeat this until we rea"h S0

    rm rm rm rm rm

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    59/88

    A St l t ti # A Shi#t ! P

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    60/88

    A Sta" mplementation o# A Shi#t-e!u"e Parser

    3tack 0nput *ction

    Q i!.i!;i!Q shi#t

    Qi! .i!;i!Qre!u"e by + → i!

    Q+ .i!;i!Qre!u"e by ' → +

    Q ' .i!;i!Qre!u"e by E → ' 6arse ree 

    QE .i!;i!Qshi#t

    QE. i!;i!Q shi#t

    QE.i!;i!Q re!u"e by + → i!

    QE.+ ;i!Q re!u"e by ' → +

    QE.' ;i!Q shi#t

    QE.'; i!Q shi#t

    QE.';i! Q re!u"e by + → i!

    QE. ';+ Q re!u"e by ' → ';+

    QE.' Q re!u"e by E → E.'

    QE Q a""ept

    nitial sta" /ust"ontains only the

    en!-marer Q the en! o# theinput strin$ ismare! by theen!-marer Q0

    Shi#t e!u"e Parsers

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    61/88

    Shi#t-e!u"e Parsers

     'he most pre&alent type o# bottom-up parser

    to!ay is base! on a "on"ept "alle! *() parsin$O

    le#t to ri$ht ri$ht-most loohea! ( is omitte!  it is 1)

    L$-6arsers o&ers wi!e ran$e o# $rammars0Simple * parser (S* )*oo Ahea! * (*A*)most $eneral * parser (* )

    S* * an! *A* wor same only their parsin$tables are !iLerent0

    S*

    C+5

    *

    *A*

    * Parsers

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    62/88

    * Parsers* parsin$ is attra"ti&e be"ause

    * parsers "an be "onstru"te! to re"o$nize &irtually allpro$rammin$-lan$ua$e "onstru"ts #or whi"h "ontext-

    #ree $rammars "an be written0* parsin$ is most #eneral nonbacktrackin# shift

    re$uce parsin# yet it is still e"ient0

     'he "lass o# $rammars that "an be parse! usin$ *metho!s is a proper superset o# the "lass o# $rammars

    that "an be parse! with pre!i"ti&e parsers0LL(%!-7rammars ⊂ L$(%!-7rammars

    An *-parser "an !ete"t a syntactic error as soon asit is possible to !o so a le#t-to-ri$ht s"an o# the input0

    %rawba" o# the * metho! is that it is too mu"hwor to "onstru"t an * parser by han!0Xse tools e0$0 ya""

    * Parsin$ Al$orithm

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    63/88

    * Parsin$ Al$orithm

    Sm

    Xm

    Sm-1Xm-1

      .

      .

    S1X1

    S0

    a1

     ... ai

     ... an

    $

    Action Table

      terminal and $t !o"r di!!erenta actionte

    #oto Table

      non-terminalt eac item ia a tate n"mber te

    LR Parsing Algorithm

    sta"

    input

    output

      Al$orithm

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    64/88

    Al$orithm

    A "on8$uration o# a * parsin$ is

    ( So S1 000 Sm ai ai.1 000 an Q )

    Sta" est o# nput

    Sm an! ai  !e"i!es the parser a"tion by "onsultin$ the

    parsin$ a"tion table0 (%nitially Stack   "ontains /ust So )

    A "on8$uration o# a * parsin$ represents the ri$htsentential #orm

    T1 000 Tm ai ai.1 000 an QTi is the $rammar symbol represente! by state si

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    65/88

    * parsin$ al$orithm

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    66/88

     *-parsin$ al$orithm

      5rammar

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    67/88

    5rammar

    state id + * ( ) $ E T F0 % & 1 '

    1 acc

    ' r' r' r'

    r& r& r& r&

    & % & '

    % r r r r

    % & ,

    % & 10

    11

    , r1 r1 r1

    10 r r r r

    *ction able 7oto ableExpression

    5rammar

    1) E → E.'F) E → '

    3) ' → ';+V) ' → +Z) + → (E)

    [) + → i!

      Example

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    68/88

    Example

    stack input action output

    Y i!;i!.i!Q shi#t Z

    Yi!Z ;i!.i!Q re!u"e by +→i! +→i!

    Y+3 ;i!.i!Q re!u"e by '→+ '→+

    Y'F ;i!.i!Q shi#t \

    Y'F;\ i!.i!Q shi#t ZY'F;\i!Z .i!Q re!u"e by +→i! +→i!

    Y'F;\+1Y (2).i!Q re!u"e by '→ ';+ '→ ';+

    Y'F .i!Q re!u"e by E→ ' E→ '

    YE1 .i!Q shi#t [

    YE1.[ i!Q shi#t Z

    YE1.[i!Z Q re!u"e by +→i! +→i!

    YE1.[+3 Q re!u"e by '→+ '→+

    YE1.['] (22) Q re!u"e by E→E.' E→E.'

    YE1 Q a""ept

    For id2id1id

    b

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    69/88

    ConGi"ts %urin$ Shi#t e!u"e Parsin$

     'here are "ontext-#ree $rammars #or whi"h shi#t-

    re!u"e parsers "annot be use!0Sta" "ontents an! the next input symbol may

    not !e"i!e a"tions#ift/reduce con8ict =hether mae a shi#t

    operation or a re!u"tion0reduce/reduce con8ict 'he parser "annot

    !e"i!e whi"h o# se&eral re!u"tions to mae0

    # a shi#t-re!u"e parser "annot be use! #or a$rammar that $rammar is "alle! as non-*()$rammar0

    An ambi$uous $rammar "an ne&er be a *

    $rammar

      :*(Y) tem

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    70/88

    *(Y) temAn * parser maes shi#t-re!u"e !e"isions by maintainin$ states to

    eep tra" o# where we are in a parse0

    An L$(9! item o# a $rammar 5 is a pro!u"tion o# 5 a !ot at the someposition o# the ri$ht si!e0

    Ex A → aBb Possible *(Y) tems A → 0aBb

    (#our !iLerent possibility)   A → a0Bb

      A → aB0b

      A → aBb0Sets o# *(Y) items will be the states o# action an! "oto table o# theS* parser0i0e0 States represent sets o# ^items0H

    A "olle"tion o# sets o# *(Y) items (t#e canonical L$(9! collection) is

    the basis #or "onstru"tin$ S* parsers0

    Constru"tin$ S* Parsin$ 'ables

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    71/88

    Constru"tin$ S* Parsin$ 'ables2 'o "onstru"t the canonical L$(9! collection #or a

    $rammar we !e8ne an augmented grammar an! two#un"tions0 567S8&E and G797.

     !ugmented Grammar 5J is 5 with a new pro!u"tion rule SJ→S where SJ is the new

    startin$ symbol0

    (urpose to pro&i!e a sin$le pro!u"tion that when re!u"e!si$nals the en! o# parsin$

    #   /  is a set o# *(Y) items #or a $rammar 5 thenclosure&/(  is the set o# *(Y) items "onstru"te! #rom

    by the two rules10 nitially e&ery *(Y) item in is a!!e! to "losure()0F0 # A → α0Bβ  is in "losure() #or all pro!u"tion rules B→γ  in 5

    a!! B→0γ   in the "losure()0*e will appl, this rule until no more new 0R&1( items

    can be a$$e$ to closure&/(2

    Closure () Example

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    72/88

    Closure () 20 Example

    5i&e a $rammarE → E . ' 9 '

     ' → ' ; + 9 ++ →( E ) 9 i!#en,

    Closure(>' → ' 0; +?) 6 >' → ' 0 ; +?Closure (>' → ' 0; + ' → ' ; 0+?) 6 >' → ' 0; + '

    → ' ; 0+ + → 0(E ) + → 0i!?Closure (>+ →( 0E ) ? ) 6 >+ →( 0E ) E → 0 E . ' E

    → 0 ' ' → 0 ' ; + ' → 0 + + → 0( E ) + → 0 i! ?"losure(>EJ → 0E?) 6>EJ → 0E E → 0E.' E → 0' ' → 0';+ ' → 0+ + → 0(E) + → 0i!?

    5oto peration

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    73/88

    5oto peration# is a set o# L$(9! items an! T is a $rammar symbol (terminal or non-terminal) then "oto(0,:! is

    !e8ne! as #ollows

    # A → α0Tβ  in then e&ery item in closure(;* →  :0  EJ → E0 E → E0.' ?) 6 > EJ → E0 E → E0.' ?)

    $oto(') 6 "losure(> E → '0 ' → '0;+ ?) 6 >E → '0 ' → '0;+? 

    $oto(+) 6 "losure(> ' → +0 ?) 6 > ' → +0

     ?)

    $oto(() 6 "losure(> + → (0E)?) 6 >+ → (0E) E → 0E.' E → 0 ' ' → 0 ';+ ' → 0+ + → 0(E) + → 0i! ?

    $oto(i!) 6 "losure( > + → i!0 ?) 6 > + → i!0 ?5oto(>EJ → E 0 E → E . 0'?.) 6 "losure(> ?) 6 > ?

     Colle"tion

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    74/88

    Colle"tion

     'o "reate the S* parsin$ tables #or a

    $rammar 5 we will "reate the "anoni"al *(Y)"olle"tion o# the $rammar 5J0

     3l#orithm7oi! items(5J) >

    C 6 > C*SXE(>SJ→0S?) ?repeat

    for (ea"h set o# items in C)

      for(ea"h $rammar symbol T)

      if  ($oto(T) is not emptyan! not in C(

      a!! $oto(T) to C

      =ntil no new sets o# items are a!!e!

      Example

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    75/88

    Example

    C 6 >"losure(>EJ → 0E?) ? 6 >EJ → 0 E E →0 E . ' E

    → 0 ' ' → 0 ' ; + ' →0 + + → 0( E ) + → 0 i!?0 'his $i&es us the items #or the 8rst state (stateY :

    Y) o# our %+A

    ,ow we nee! to "ompute 7oto #un"tions #or all

    o# the rele&ant symbols in the set0n this "ase we "are about the symbols F (

    an! id sin"e those are the symbols that ha&e a0symbol in #ront o# them in some item o# the set C0

    +or symbol E 5oto(Y E) 6 "losure(>EJ → E 0 E → E 0 . '?)

    6 >EJ → E 0 E → E 0 . '? 6 call it 0%

    +or symbol ' 5oto(Y ') 6 "losure(>E→

     ' 0 '→

     

    +or symbol + 5oto(Y +) 6"losure(>' → +0?) 6 >' → 

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    76/88

    +0? 6 0)

    +or symbol ( 5oto(Y () 6 "losure(>+ → ( 0E ) ?) 6 >+

    →(0E ) E → 0 E . ' E → 0 ' ' → 0 ' ; + ' → 0 + + → 0(E ) + → 0 !? 6 05

    +or symbol i! 5oto(Y i!) 6 "losure(>+ → i!0?) 6 >+→ i!0? 6 0>

    epeat this step #or newly "reate! states (1 F 3 V Z)

    till 0 o""ures at the en! o# ernal o# ea"h state0+or symbol . 5oto(1 .) 6 "losure(>E → E .0 '?) 6 >E

    → E .0 ' ? ' → 0 ' ; + ' → 0 + + → 0( E ) + → 0 !? 6 0?+or symbol ; 5oto(F ;) 6 "losure(>' → ' ; 0+?)6 >' →  ' ; 0+ + → 0( E ) + → 0 !? 6 0@A

    +or symbol E 5oto(V E) 6 "losure(>+ → ( E0 ) E → E 0. '?)

    6 6

    Summary o# states obtaine! an! to whi"h state ea"h! i i

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    77/88

    pro!u"tion in a state $oes

     +un"tion

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    78/88

    +un"tion

     *(Y) automaton #or the Example

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    79/88

    EJ → E "losure(>EJ → 0E?) 6

    E → E.' > EJ → 0E ule 1

    E → ' E → 0E.' ule F

     ' → ';+ E → 0 ' ule F

     ' → + ' → 0 ';+ ule F

     Example

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    80/88

    ExampleBe#ore we start "onstru"tion o# S* a"tionQ).?3 ' → ' ; + +ollow(') 6 >Q).?V ' → + +ollow(+) 6 >Q).;?

    Z + →( E )[ + → i!Ea"h terminal is "olumn #or a"tion table ea"h non-

    terminal is "olumn #or 5' table an! ea"h state is

    row #or both tables

    Constru"tin$ S* Parsin$ 'able 2

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    81/88

    $ $

    10 Constru"t the "anoni"al "olle"tion o# sets o# *(Y)items #or 5J0 C←>Y000n?

    F0 Create the parsin$ a"tion table as #ollows

    F010 # a is a terminal A→α0aβ in i an! $oto(ia)6 / then a"tionRi a is shift 4 A

    F0F0 # A→α0 is in i then a"tionRia is re$uce 3  #orall a in +**=(A) where A≠SJ0

    F030 # SJ→S0 is in i then a"tionRiQ is accept 0

    F0V0 # any "onGi"tin$ a"tions $enerate! by these rulesthe $rammar is not S*(1)0

    30 Create the parsin$ $oto table. #or all non-terminals A i# $oto(iA)6 /  then $otoRiA6/

    V0 All entries not !e8ne! by (F) an! (3) are errors0

    Z0 nitial state o# the parser "ontains SJ→0S

     Example

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    82/88

    Example

    From Rule 5262

     'ae + → 0( E ) #rom Y 5oto(Y ( ) 6 V then a"tionRY ( 6 shi#tV  'ae E → E 0 . ' #rom 1 5oto(1 .) 6 [ then a"tionR1 . 6

    shi#t [  'ae ' → ' 0 ; + #rom F 5oto(F ;) 6 \ then a"tionRF ; 6 shi#t

    \

    2other shifts can be populate$ in the same wa,7

    From Rule 5252  'ae E → '0 #rom F +ollow(') 6 >Q).? a"tionRFQ 6 re!u"e F (F E → ' ) A"tionRF ) 6 re!u"e F A"tionRF .) 6 re!u"e F

    7other re$uces can be $one in the same wa,7

    From Rule 52-2 EJ → E 0 is 1 a"tionR1Q 6 accept 

     Example

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    83/88

    Example

    +rom 3 - Creatin$ the parsin$ $oto table

    o  'ae E in Y $oto(YE)61  then $otoRYE61.  'ae ' in Y $oto(Y')6 F  then $otoRY ' 6 F

    .  'ae ' in Y $oto(Y+)6 3  then $otoRY + 6 3

    .  'ae E in V $oto(VE)6_  then $otoRVE6_

    .  'ae ' in V $oto(V')6 F  then $otoRV ' 6 F

    .  'ae + in V $oto(V+)6 3  then $otoRV + 6 3

    .  'ae ' in [ $oto([')6 ]  then $otoR[ ' 6 ]

    .  'ae + in [ $oto([+)6 3  then $otoR[ + 6 3

    .  'ae + in \ $oto(\+)6 1Y  then $otoR\ + 6 1Y

      x5rammar

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    84/88

    5rammar

    state

    id + * ( ) $ E T F

    0 % & 1 '

    1 acc

    ' r' r' r'

    r& r& r& r&& % & '

    % r r r r

    % & ,

    % & 10

    11

    , r1 r1 r1

    10 r r r r

    "tion 'able 5oto 'able

    Exer"ise

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    85/88

    Exer"ise

    Constru"t S* Parse table #or the au$mente!$rammar an! show how the parser a""epts thestrin$ or input () ()

    1 SJ →S

    FS →(S)S3S →lAnswer

    S*(1) 5rammar

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    86/88

    S*(1) 5rammar

    An * parser usin$ S*(1) parsin$ tables #or a$rammar 5 is "alle! as the S*(1) parser #or 50

    # a $rammar 5 has an S*(1) parsin$ table it is"alle! S*(1) $rammar (or S* $rammar in short)0

    E&ery S* $rammar is unambi$uous but e&eryunambi$uous $rammar is not a S* $rammar0

    # the S* parsin$ table o# a $rammar 5 has a"onGi"t we say that that $rammar is not S*$rammar0shi#t

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    87/88

    An * parser will !ete"t an error when it

    "onsults the parsin$ a"tion table an! 8n!san error entry0 All empty entries in the a"tiontable are error entries0missin$ operan!

    unbalan"e! ri$ht parenthesisErrors are ne&er !ete"te! by "onsultin$ the

    $oto table0

    Some error re"o&ery are%is"ar! zero or more input symbols until a symbol

    a is #oun!By marin$ ea"h empty entry in the a"tion table

    with a spe"i8" error routine0

    Assi$nment 3

  • 8/17/2019 Chapter 3_Syntax analysis.pptx

    88/88

    5i&en the #ollowin$ $rammar where a b "

    are terminals an! S T U are non-terminalsS ` TaUb 9 U 9l

    T ` aU 9 "

     U ` bT 9 aBuil! **(1) Parsin$ table #or the $rammar (Show

    all the ne"essary steps)0=hat "an you say about ambi$uity o# the $rammarshow how the parser a""epts