Top Banner

of 18

Bottom Up LR(0) Parsing in C.

Jun 01, 2018

Download

Documents

shashankmudgal
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 8/9/2019 Bottom Up LR(0) Parsing in C.

    1/18

    BOTTOM UP PARSING

    A bottom-up parse corresponds to the construction of a parse tree for an input stringbeginning at the leaves (the bottom) and working up towards the root (the top) . It isconvenient to describe parsing as the process of building parse trees, although a front end

    may in fact carry out a translation directly without building an explicit tree.e can think of bottom-up parsing as the process of !reducing! a string w to the start symbolof the grammar. At each reduction step, a specific substring matching the body of a

    production is replaced by the non terminal at the head of that production. "he key decisionsduring bottom-up parsing are about when to reduce and about what production to apply, asthe parse proceeds.

    LR PARSERS:"he #most prevalent type of bottom-up parser today is based on a concept called $%(k)

    parsing& the !$! is for left-to-right scanning of the input, the !%! for constructing a rightmostderivation in reverse, and the k for the number of input symbols of lookahead that are used inmaking parsing decisions.

    $% parsing is attractive because of variety of reasons'

    $% parsers can be constructed to recognie virtually all programming language

    constructs for which context-free grammars can be written. on $% context-freegrammars exist, but these can generally be avoided for typical programming-language constructs.

    "he $%-parsing method is the most general non back tracking shift-reduce parsing

    method known, yet it can be implemented as efficiently as other, more primitive shift-reduce methods .

    An $% parser can detect a syntactic error as soon as it is possible to do so on a left-to-right scan of the input.

    $% grammars can describe more languages than $$ grammars.

    ITEMS AND THE LR(0) AUTOMATION:An $% parser makes shift-reduce decisions by maintaining states to keep track of where weare in a parse. *tates represent sets of !items.! An $%(+) item (item for short) of a grammar is a production of with a dot at some position of the body. "hus, production A - /0yields the four itemsA - 1/0

    A - 1/0A - /1 0A - /01"he production A - generates only one item, A - . . Intuitively, an item indicates how much of a production we have seen at a given point in the

    parsing process. 2or example, the item A - 1/ 0 indicates that we hope to see a stringderivable from / 0 next on the input. Item A - 1 / 0 indicates that we have 3ust seen onthe input a string derivable from and that we hope next to see a string derivable from / 0.Item A -/ 01 indicates that we have seen the body / 0 and that it may be time to reduce/0 to A.

  • 8/9/2019 Bottom Up LR(0) Parsing in C.

    2/18

    CLOSURE OF ITEM SETS:

    If I is a set of items for a grammar , then 4$+*5%6(I) is the set of items constructed from Iby the two rules'

    7. Initially, add every item in I to 4$+*5%6(I).8. If A -91:; is in 4$+*5%6(I) and : - is a production, then add the item : - to

    4$+*5%6(I), if it is not already there. Apply this rule until no more new items canbe added to 4$+*5%6 (I).

    THE FUNCTION GOTO:

    "he second useful function is +"+(I, ) where I is a set of items and is a grammarsymbol. +"+ (I, ) is defined to be the closure of the set of all items

  • 8/9/2019 Bottom Up LR(0) Parsing in C.

    3/18

    ALGORITHMS USED:

    Algorithm to compute closure of an item'

    Algorithm to compute set of canonical $%(C) items'

    Algorithm to construct $%(C) >arsing "able'

  • 8/9/2019 Bottom Up LR(0) Parsing in C.

    4/18

    $% >A%*I A$+%I"?@'

    IMPLEMENTATION CODE IN C LANGUAGE:

    #include#include

    #include#include#defnesize20structstate{ charproductions[size][10],ons!"bol shortintscannedproductions[size],noo$productions,statenu"ber,shi$tin$o[10],nu"bero$shi$t structstate%lin&'t!pede$structstate% ()*+structaction{

    intstate

    charact't!pede$structactionA-)(A-)(feld[/0][1]()*+frst (33,last (33int nu"bero$states,noo$4ariables,noo$ter"inals,count1,5p0,pgoto[/0][10],tpcharclosureproductions[size][10],input[10][10],pfrst[10][10],p$ollo6[10][10],4ariables[size],ter"inals[size],p[10]4oidclosure7()*+,int%84oidite"s78()*+getnode78{ ()*+te"p

    inti te"p 7()*+8 "alloc7sizeo$7structstate88 $or7i 0 i < sizei998

  • 8/9/2019 Bottom Up LR(0) Parsing in C.

    5/18

    { strcp!7te"p:>productions[i],;0;8 te"p:>scannedproductions[i] 0 ' te"p:>noo$productions 0 te"p:>nu"bero$shi$t 0

    te"p:>lin& (33 returnte"p'4oidinsert7()*+te"p8{

    i$7frst (338 { frst te"p last te"p ' else { last:>lin& te"p

    last te"p '

    '

    4oiddotproductions7charinput[][10],intcount8{ inti 1,5,& 1 charbu=er[10]{0' 6hile7i < count8 { & 0 5 0

    6hile7input[i][&] ? 08 { i$7input[i][&] >8 { bu=er[599] > bu=er[599] . ' else bu=er[599] input[i][&] &99 ' bu=er[5] 0 strcp!7closureproductions[i],bu=er8 i99 ''int chec&$orpresenceinproductions7()*+te"p,char%bu=er8{ inti 0 6hile7te"p:>productions[i][0]8 { i$7?strc"p7te"p:>productions[i],bu=er88 return1 i99 ' return0'

    4oidaug"entgra""ar7charinput[][10]8{ input[0][0] input[1][0]

  • 8/9/2019 Bottom Up LR(0) Parsing in C.

    6/18

  • 8/9/2019 Bottom Up LR(0) Parsing in C.

    7/18

    {i$7c4ariables[i]8{

    returni

    '

    'return:1'intfndt7charc8{

    inti0$or7i0i

  • 8/9/2019 Bottom Up LR(0) Parsing in C.

    8/18

    ''

    ''

    ''

    intdetectepslon7charc8{int4ar,i4ar fnd47c8i$74ar :18

    return0$or7i0i

  • 8/9/2019 Bottom Up LR(0) Parsing in C.

    9/18

    "99 else

    brea& '

    '

    '''intfndp7char%str8{ inti

    charbu=[20]strcp!7bu=,str8bu=[strlen7str8:1]0$or7i1ions!"bolch8 {

    $or7515statenu"berfeld[te"p:>shi$tin$o[5]][i].acts

    ' ' te"pte"p:>lin&

    ''te"pfrst

    6hile7te"p?(338{

    i$7te"p:>statenu"ber?18$or7i0iproductions[i]8&::

  • 8/9/2019 Bottom Up LR(0) Parsing in C.

    10/18

    i$7te"p:>productions[i][&].8{

    lfndp7te"p:>productions[i]8"fnd47input[l][0]8$or7505statenu"ber][poster"7p$ollo6["][5]8].act(338

    {feld[te"p:>statenu"ber][poster"7p$ollo6["]

    [5]8].statelfeld[te"p:>statenu"ber][poster"7p$ollo6["]

    [5]8].actr'else{

    print$7;Ecr conflctn;,feld[te"p:>statenu"ber][poster"7p$ollo6["][5]8].act8

    getch78

    eCit708'

    '

    '

    'else{

    feld[1][poster"7F8].state1feld[1][poster"7F8].acta

    '

    te"pte"p:>lin&'

    '4oidco"putegoto78{ inti,5

    charch()*+te"p

    $or7i0istatenu"ber'

    ' te"pte"p:>lin&

    ''

    '4oidparse7char%str8

    {inti0,stac&[1],5,top 0,&,",pos:1,l0

  • 8/9/2019 Bottom Up LR(0) Parsing in C.

    11/18

    charch,action[G]{0',s!"bol[20]{0',%p,te"pstac&[0] 06hile7str[i] ?08{

    5 poster"7str[i]8

    pstr[i]i$7feld[stac&[top]][5].act a8{

    strcp!7action,;Accept;8print$7;n;8print$7;E1s ;,action8

    $or7l0l

  • 8/9/2019 Bottom Up LR(0) Parsing in C.

    12/18

    '

    'int4alidi7char%str8{ inti0

    $or7i0iEsn;,4ariables[i],pfrst[i]8 ' $or7505productions[i][0] ? 08

  • 8/9/2019 Bottom Up LR(0) Parsing in C.

    13/18

    print$7;nEs;,frst:>productions[i99]8print$7; Edn;,frst:>noo$productions8%Jite"s78te"p frst6hile7te"p ? (338

    { i 0 print$7;nEd ;,te"p:>statenu"ber8 6hile7i < te"p:>noo$productions8 print$7;nEs;,te"p:>productions[i99]8 $or7i 1i ons!"bol,te"p:>statenu"ber8 te"p te"p:>lin&'getch78co"puteaction78co"putegoto78print$7;n::::::::::::::::::::::::::3I708 LAIH(M -AN3+:::::::::::::::::::::::::::::::::;8

    print$7;n ;8$or7i0i

  • 8/9/2019 Bottom Up LR(0) Parsing in C.

    14/18

    { " 0 6hile7te"p:>productions[i]["] ? .8 "99 "99

    i$7te"p:>productions[i]["] > P te"p:>productions[i]["] productions[7%place899],closureproductions[5]8

    te"p:>noo$productions 9 1 '

    '599

    ''

    i99 ''4oidite"s78{ ()*+te"p,te"p1 (33 inti0,50,&0,place0 charch,bu=er[10]

    te"p frst

    6hile7te"p ? (338 { 6hile7i < te"p:>noo$productions8 { i$7te"p:>scannedproductions[i] 08 {

    5 0

    te"p:>scannedproductions[i] 1 6hile7te"p:>productions[i][599] ? .8

    ch te"p:>productions[i][5]

    i$7 ch ? 0ch?B8 { place 0 te"p1 getnode78 te"p1:>ons!"bol ch

    te"p1:>shi$tin$o[99te"p1:>nu"bero$shi$t] te"p:>statenu"ber strcp!7bu=er,te"p:>productions[i]8 bu=er[5:1] ch bu=er[5] . strcp!7te"p1:>productions[0],bu=er8 te"p1:>noo$productions 1 place 1

    closure7te"p1,place8 & 0 6hile7te"p:>productions[&][0] ? 08

  • 8/9/2019 Bottom Up LR(0) Parsing in C.

    15/18

    { 5 0 i$7te"p:>scannedproductions[&] 08 { 6hile7te"p:>productions[&][599] ? .8

    i$7ch te"p:>productions[&][5] 8 { te"p:>scannedproductions[&] 1 strcp!7bu=er,te"p:>productions[&]8 bu=er[5:1] ch bu=er[5] . strcp!7te"p1:>productions[place99],bu=er8 te"p1:>noo$productions 9 1 closure7te"p1,place8 '

    ' &99 ' J%$or7i 0i < te"p1:>noo$productionsi998 print$7;nEs;,te"p1:>productions[i]8%J i$7?statenotadded7te"p1,te"p:>statenu"ber88

    { insert7te"p18

    te"p1:>statenu"ber 99nu"bero$states ' else

    $ree7te"p18

    '

    i0 '

    else i99 ' te"p te"p:>lin& i 0 ''

    SAMPLE OUTPUT:

  • 8/9/2019 Bottom Up LR(0) Parsing in C.

    16/18

  • 8/9/2019 Bottom Up LR(0) Parsing in C.

    17/18

  • 8/9/2019 Bottom Up LR(0) Parsing in C.

    18/18