top-down parsing.pdf

Upload: ajith-suresh

Post on 03-Apr-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/28/2019 Top-Down Parsing.pdf

    1/6

    4/19/13 Top-Down Parsing

    compilerdesigndetails.blogspot.in/2012/02/top-down-parsing.html

    2

    Top-Down Parsing 1

    Top-Down Parsing Parsing is the process of determining if a string of tokens can be generated by a

    grammar. For any context-free grammar there is a parser that takes at most (n3) time to

    parse a string of n tokens.

    Top-down parsers build parse trees from the top (root) to the bottom (leaves).

    Two top-down parsing are to be discussed:

    oRecursive Descent Parsing

    o An efficient non-backtracking parsing called Predictive Parsing for

    LL(1) grammars.

    2.5 Recursive Descent Parsing Recursive descent parsing is a top-down method of syntax analysis in which a set

    recursive procedures to process the input is executed.

    A procedure is associated with each nonterminal of a grammar.

    Top-down parsing can be viewed as an attempt to find a leftmost derivation for an

    input string.

    Equivalently, it attempts to construct a parse tree for the input starting from the root

    and creating the nodes of the parse tree in preorder.

    Recursive descent parsing involves backtracking.

    Example 2.5.1 (backtracking)

    Consider the grammar

    S cAd

    A ab | a

    (Grammar 2.5.1)

    and the input string w = cad

    To construct a parse tree for this string using top-down approach, initially create a

    tree consisting of a single node labeled S.

    [http://3.bp.blogspot.com/--

    t37MR7yQlo/TzYJOBC3zHI/AAAAAAAAAGY/OUIDZ8dnhgA/s1600/ss.JPG]

    An input pointer points to c, the first symbol of w.

    Then use the first production for S to expand the tree and obtain the tree (as

    in Fig 2.5(a))

    The leftmost leaf, labeled c, matches the first symbol of w.

    Next input pointer to a, the second symbol of w.

    Consider the next leaf, labeled A.

    Expand A using the first alternative for A to obtain the tree (as in Fig

    Top-Down Parsing

    SidebarSidebar

    principles of compilerprinciples of compiler search

    Send feedback

    http://3.bp.blogspot.com/--t37MR7yQlo/TzYJOBC3zHI/AAAAAAAAAGY/OUIDZ8dnhgA/s1600/ss.JPGhttp://3.bp.blogspot.com/--t37MR7yQlo/TzYJOBC3zHI/AAAAAAAAAGY/OUIDZ8dnhgA/s1600/ss.JPGhttp://3.bp.blogspot.com/--t37MR7yQlo/TzYJOBC3zHI/AAAAAAAAAGY/OUIDZ8dnhgA/s1600/ss.JPGhttp://3.bp.blogspot.com/--t37MR7yQlo/TzYJOBC3zHI/AAAAAAAAAGY/OUIDZ8dnhgA/s1600/ss.JPGhttp://3.bp.blogspot.com/--t37MR7yQlo/TzYJOBC3zHI/AAAAAAAAAGY/OUIDZ8dnhgA/s1600/ss.JPGhttp://3.bp.blogspot.com/--t37MR7yQlo/TzYJOBC3zHI/AAAAAAAAAGY/OUIDZ8dnhgA/s1600/ss.JPGhttp://3.bp.blogspot.com/--t37MR7yQlo/TzYJOBC3zHI/AAAAAAAAAGY/OUIDZ8dnhgA/s1600/ss.JPGhttp://3.bp.blogspot.com/--t37MR7yQlo/TzYJOBC3zHI/AAAAAAAAAGY/OUIDZ8dnhgA/s1600/ss.JPGhttp://3.bp.blogspot.com/--t37MR7yQlo/TzYJOBC3zHI/AAAAAAAAAGY/OUIDZ8dnhgA/s1600/ss.JPGhttp://www.google.com/support/forum/p/bloggerhttp://3.bp.blogspot.com/--t37MR7yQlo/TzYJOBC3zHI/AAAAAAAAAGY/OUIDZ8dnhgA/s1600/ss.JPGhttp://3.bp.blogspot.com/--t37MR7yQlo/TzYJOBC3zHI/AAAAAAAAAGY/OUIDZ8dnhgA/s1600/ss.JPGhttp://3.bp.blogspot.com/--t37MR7yQlo/TzYJOBC3zHI/AAAAAAAAAGY/OUIDZ8dnhgA/s1600/ss.JPGhttp://3.bp.blogspot.com/--t37MR7yQlo/TzYJOBC3zHI/AAAAAAAAAGY/OUIDZ8dnhgA/s1600/ss.JPGhttp://3.bp.blogspot.com/--t37MR7yQlo/TzYJOBC3zHI/AAAAAAAAAGY/OUIDZ8dnhgA/s1600/ss.JPGhttp://3.bp.blogspot.com/--t37MR7yQlo/TzYJOBC3zHI/AAAAAAAAAGY/OUIDZ8dnhgA/s1600/ss.JPGhttp://3.bp.blogspot.com/--t37MR7yQlo/TzYJOBC3zHI/AAAAAAAAAGY/OUIDZ8dnhgA/s1600/ss.JPGhttp://3.bp.blogspot.com/--t37MR7yQlo/TzYJOBC3zHI/AAAAAAAAAGY/OUIDZ8dnhgA/s1600/ss.JPGhttp://3.bp.blogspot.com/--t37MR7yQlo/TzYJOBC3zHI/AAAAAAAAAGY/OUIDZ8dnhgA/s1600/ss.JPGhttp://compilerdesigndetails.blogspot.com/2012/02/top-down-parsing.htmlhttp://compilerdesigndetails.blogspot.in/http://www.google.com/support/forum/p/bloggerhttp://compilerdesigndetails.blogspot.in/http://compilerdesigndetails.blogspot.in/http://compilerdesigndetails.blogspot.com/2012/02/top-down-parsing.htmlhttp://3.bp.blogspot.com/--t37MR7yQlo/TzYJOBC3zHI/AAAAAAAAAGY/OUIDZ8dnhgA/s1600/ss.JPGhttp://compilerdesigndetails.blogspot.com/2012/02/top-down-parsing.htmlhttp://compilerdesigndetails.blogspot.com/2012/02/compiler-design.htmlhttp://compilerdesigndetails.blogspot.com/2012/02/v-behaviorurldefaultvmlo.htmlhttp://compilerdesigndetails.blogspot.com/2012/02/grouping-of-phases.htmlhttp://compilerdesigndetails.blogspot.com/2012/02/buffering.htmlhttp://compilerdesigndetails.blogspot.com/2012/02/specification-of-tokens.htmlhttp://compilerdesigndetails.blogspot.com/2012/02/role-of-parser.htmlhttp://compilerdesigndetails.blogspot.com/2012/02/writing-grammars.htmlhttp://compilerdesigndetails.blogspot.com/2012/02/context-free-grammars-cfg-or-simply.htmlhttp://compilerdesigndetails.blogspot.com/2012/02/bottom-up-parsing-shift-reducing.htmlhttp://compilerdesigndetails.blogspot.com/2012/02/operator-precedence-parsing.html
  • 7/28/2019 Top-Down Parsing.pdf

    2/6

    4/19/13 Top-Down Parsing

    compilerdesigndetails.blogspot.in/2012/02/top-down-parsing.html

    2.5(b)).

    Now have a match for the second input symbol. Then advance to the next

    input pointer d, the third input symbol and compare d against the next leaf,

    labeled b. Since b does not match d, report failure and go back to A to see

    whether there is another alternative. (Backtracking takes place).

    If there is another alternative for A, substitute and compare the input symbol

    with leaf.

    Repeat the step for all the alternatives of A to find a match using

    backtracking. If match found, then the string is accepted by the grammar.

    Else report failure.

    A left-recursive grammar can cause a recursive-descent parser, even one

    with backtracking, to go into an infinite loop.

    As discussed above, an easy way to implement a recursive descent parsing

    with backtracking is to create a procedure for each nonterminals.

    For the grammar(Grammar 2.5.1) above write the recursive procedure for

    each nonterminal S and A.

    Procedure S

    procedure S()begin

    if input symbol = c then

    begin

    ADVANCE( );

    if A( ) then

    if input symbol = d then

    begin ADSVANCE( ); return true end

    end;

    return false

    end

    Procedure A

    procedure A( )

    beginisave := input-pointer;

    if input symbol = a then

    begin

    ADVANCE( );

    if input symbol = b then

    begin ADVANCE( ); return true end

    end

    input-pointer := isave;

    /* failure to find ab */

    if input symbol = a then

    begin ADVANCE( ); return true end

    else return false

    end

    A parser that uses a set of recursive procedures to recognize its input with

    no backtracking is called a recursive descent parser.

    Exercise 2.5.1

    o * Write recursive descent parsing for the (Grammar 2.2.3) after

    eliminating the left recursion. (refer reference book 1, for solution,

    page 181.)

    2.6 Predictive Parsing Apredictive parseris an efficient way of implementing recursive-descent parsing

    by handling the stack of activation records explicitly.

  • 7/28/2019 Top-Down Parsing.pdf

    3/6

    4/19/13 Top-Down Parsing

    compilerdesigndetails.blogspot.in/2012/02/top-down-parsing.html

    [http://1.bp.blogspot.com/-

    nFzw1HTDdOM/TzYJe679qKI/AAAAAAAAAGg/s7MeZ5NbTr4/s1600/ss1.JPG]

    The predictive parser has an input, a stack, a parsing table, and an output.

    The input contains the string to be parsed, followed by $, the right endmarker.

    The stack contains a sequence of grammar symbols, preceded by $, the bottom-of-

    stack marker.

    Initially the stack contains the start symbol of the grammar preceded by $.

    The parsing table is a two dimensional array M[A,a], where A is a nonterminal, and

    a is a terminal or the symbol $.

    The parser is controlled by a program that behaves as follows:

    The program determinesX, the symbol on top of the stack, and a, the current input

    symbol.

    These two symbols determine the action of the parser.

    There are three possibilities:

    o 1. IfX= a = $, the parser halts and announces successful completion

    of parsing.

    o 2. IfX = a $, the parser pops X off the stack and advances the

    input pointer to the next input symbol.

    o 3. IfX is a nonterminal, the program consults entry M[X,a] of the

    parsing table M. This entry will be either an X-production of the

    grammar or an error entry.

    If M[X,a] = {X UVW}, the parser replacesXon top of the

    stack by WVU(with Uon top).

    If M[X,a] = error, the parser calls an error recovery routine.

    Construction of Parsing Table:

    o Before constructing the parsing table, two functions are to be

    performed to fill the entries in the table.

    FIRST( ) and FOLLOW( ) functions.

    o These functions will indicate proper entries in the table for a grammar

    G.

    o To compute FIRST(X) for all grammar symbols X, apply the

    following rules until no more terminals or can be added to any

    FIRST set.

    1. IfXis terminal, then FIRST(X) is {X}.2. IfXis nonterminal andX a is a production, then add a to

    FIRST(X). IfX is a production, then add to FIRST(X).

    3. IfX Y1, Y2, , Yk is a production, then for all I such that

    all ofY1, , Yi-1 are nonterminals and FIRST(Yj) contains for

    j = 1,2, , i-1 (i.e., Y1, Y2, . Yi-1 ), add every non-

    symbol in FIRST(Yi) to FIRST(X). If is in FIRST(Yj) for all j =

    1, 2, , k, than add to FIRST(X).

    o To compute FOLLOW(A) for all nonterminals A, apply the

    following rules until nothing can be added to any FOLLOW set.

    1. $ is in FOLLOW(S), where S is the start symbol.

    http://1.bp.blogspot.com/-nFzw1HTDdOM/TzYJe679qKI/AAAAAAAAAGg/s7MeZ5NbTr4/s1600/ss1.JPG
  • 7/28/2019 Top-Down Parsing.pdf

    4/6

    4/19/13 Top-Down Parsing

    compilerdesigndetails.blogspot.in/2012/02/top-down-parsing.html

    2. If there is a production A B, , the everything in

    FIRST() but is in FOLLOW(B).

    3. If there is a production A B, or a production A B

    where FIRST() contains (i.e., ), then everything in

    FOLLOW(A) is in FOLLOW(B).

    oExample 2.6.1

    Consider the following grammar

    E E + T | T

    T T * F | F

    F ( E ) | id(Grammar 2.6.1)

    Compute the FIRST and FOLLOW function for the above

    grammar.

    Solut ion:

    Here the (Grammar 2.6.1) is in left-recursion, so

    eliminate the left recursion for the (Grammar 2.6.1)

    we get;

    E TE`

    E` +TE` |

    T FT`

    T` *FT` |

    F ( E ) | id

    (Grammar 2.6.2) Then:

    FIRST(E) = FIRST(T) = FIRST(F) = {(,

    id}.

    FIRST(E ) = {+,}

    FIRST(T ) = {*,}

    FOLLOW(E) = FOLLOW(E`) = {),$}

    FOLLOW(T) = FOLLOW(T`) = {+,),$}

    FOLLOW(F) = {+,*,),$}

    oExercise 2.6.1

    Consider the grammar

    S iCtSS` | a

    S` eS |

    C b(Grammar 2.6.3)

    Compute the FIRST and FOLLOW for the

    (Grammar 2.6.3)

    o Construction of Predictive Parsing Table:

    The following algorithm can be used to construct a predictive

    parsing table for a grammar G

    Algorithm 2.6.1 Constructing a predictive parsing table

    Input: Grammar G

    Output: Parsing table M

    Method:

    1. For each production A of the grammar, do

    step 2 and 3.

    2. For each terminal a in FIRST(), add A toM[A,a].

    3. If is in FIRST(), add A to M[A,b] for

    each terminal b in FOLLOW(A). If is in FIRST()

    and $ is in FOLLOW(A), add A to M[A,$].

    4. Make each undefined entry of M error.

    Example 2.6.2

    Construct the predictive parsing table for the (Grammar

    2.6.2) using theAlgorithm 2.6.1

    Table 2.6.1 Predictive parsing table for the (Grammar 2.6.2)

  • 7/28/2019 Top-Down Parsing.pdf

    5/6

    4/19/13 Top-Down Parsing

    compilerdesigndetails.blogspot.in/2012/02/top-down-parsing.html

    Exercise 2.6.2

    Construct the predictive parsing table for the

    (Grammar 2.6.3)

    Moves by Predictive parser using the input string

    oPredictive parsing program

    repeat

    begin

    letXbe the top stack symbol and a the next input symbol;ifXis a terminal or $ then

    ifX= a then

    popXfrom the stack and remove a from the input

    else

    ERROR( )

    else /*Xis a nonterminal */

    ifM[X,a] =XY1, Y2, , Ykthen

    begin

    popXfrom the stack;

    push Yk, Yk-1, ,Y1 onto the stack, Y1 on top

    end

    else

    ERROR( )

    end

    until

    X= $ /* stack has emptied */

    Example 2.6.3

    o For the input string id + id * id, using the (Grammar 2.6.2) do the

    predictive parsing. Use the Table 2.6.1

    [http://4.bp.blogspot.com/-

    y9XqFSchQio/TzYJz6pHenI/AAAAAAAAAGw/ur1ex50Wiw8/s1600/ss2.JPG]

    LL(1) Grammars:

    o When there is a situation that the parsing table consists of atleast

    one multiply defined entries, then the easiest recourse is to

    transform the grammar by eliminating all left-recursion and then

    left-factoring whenever possible, to produce a grammar for

    which the parsing table has no multiply-defined entries.

    o A grammar whose parsing table has no multiply-defined entries is

    said to beLL(1) grammar.

    o A grammar isLL(1) if and only if whenever A | are two

    distinct productions of G the following conditions hold:

    1. For no terminal a do and derive strings beginning

    with a.

    2. At most one of and can derive the empty string.

    3. If , then does not derive any strings beginning

    http://4.bp.blogspot.com/-y9XqFSchQio/TzYJz6pHenI/AAAAAAAAAGw/ur1ex50Wiw8/s1600/ss2.JPG
  • 7/28/2019 Top-Down Parsing.pdf

    6/6

    4/19/13 Top-Down Parsing

    compilerdesigndetails.blogspot.in/2012/02/top-down-parsing.html

    with a terminal in FOLLOW(A).

    o Exercise 2.6.3 check whether the (Grammar 2.6.2) and

    (Grammar 2.6.3) areLL(1).

    Error recovery in predictive parsing

    o An error is detected during predictive parsing when the terminal

    on top of the stack does not match input symbol or when

    nonterminal A is on top of the stack, a is the next input symbol,

    and the parsing table entry M[A,a] is empty.

    oPanic-mode error recovery:

    It is based on the idea of skipping symbols on the input

    until a token in a selected set of synchronizing tokens

    appears.

    Its effectiveness depends on the choice of synchronizing

    set.

    The sets should chosen so that the parser recovers quickly

    from errors that are likely to occur in practice.

    oPhrase=level recovery:

    It is implemented by filling in the blank entries in the

    predictive parsing table with pointers to error routines.

    These routines may change, insert, or delete symbols on

    the input and issue appropriate error messages.

    They may also pop from the stack.

    In any event, sure that there is no possibility of an infinite

    loop.

    Checking that any recovery action eventually results in an

    input symbol being consumed is a good way to protect

    against such loops.

    Posted 11th February 2012 byArun Patrick

    Labels: recursive desent parsing,compiler design,types of parsing,parsingtypes,top down parsing,Specification of Tokens

    1 View comments

    roger layo 28 November 2012 00:30

    nice ..... knowleageable enough....

    Reply

    http://compilerdesigndetails.blogspot.com/2012/02/top-down-parsing.html?showComment=1354091456037#c8897290281001264910http://www.blogger.com/profile/06638162870325586824http://compilerdesigndetails.blogspot.in/search/label/Specification%20of%20Tokenshttp://compilerdesigndetails.blogspot.in/search/label/top%20down%20parsinghttp://compilerdesigndetails.blogspot.in/search/label/parsing%20typeshttp://compilerdesigndetails.blogspot.in/search/label/types%20of%20parsinghttp://compilerdesigndetails.blogspot.in/search/label/compiler%20designhttp://compilerdesigndetails.blogspot.in/search/label/recursive%20desent%20parsinghttps://plus.google.com/109760498258772693215