botttom up parsing

Upload: vishnugehlot

Post on 09-Apr-2018

216 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/7/2019 botttom up parsing

    1/30

    Vishnu Kumar Gehlot(Lecturer,CSE)

  • 8/7/2019 botttom up parsing

    2/30

    Parsing TechniquesParsing Techniques

    TopTop--down parsersdown parsers (LL(1), recursive descent)(LL(1), recursive descent) Start at the root of the parse tree from the start symbol and grow towardStart at the root of the parse tree from the start symbol and grow toward

    leaves (similar to a derivation)leaves (similar to a derivation)

    Pick a production and try to match the inputPick a production and try to match the input

    Bad pickBad pick may need to backtrackmay need to backtrack

    Some grammars are backtrackSome grammars are backtrack--freefree (predictive parsing)(predictive parsing)

    BottomBottom--up parsersup parsers (LR(1), operator precedence)(LR(1), operator precedence)

    Start at the leaves and grow toward rootStart at the leaves and grow toward root

    We can think of the process as reducing the input string to the startWe can think of the process as reducing the input string to the startsymbolsymbol

    At each reduction step a particular substring matching the rightAt each reduction step a particular substring matching the right--side of aside of aproduction is replaced by the symbol on the leftproduction is replaced by the symbol on the left--side of the productionside of the production

    BottomBottom--up parsers handle a large class of grammarsup parsers handle a large class of grammars

  • 8/7/2019 botttom up parsing

    3/30

    BottomBottom--up Parsingup Parsing

    A general style of bottom-up syntax analysis, known as

    shift-reduce parsing.

    Two types of Shift Reduce Parsing:

    1. Operator-Precedence parsing

    2. LR parsing

  • 8/7/2019 botttom up parsing

    4/30

    Bottom Up ParsingBottom Up Parsing

    ShiftShift--Reduce ParsingReduce Parsing Reduce a string to the start symbol of the grammar.Reduce a string to the start symbol of the grammar.

    At every step a particular subAt every step a particular sub--string is matched (in leftstring is matched (in left--toto--right fashion) to the right side of some production and thenright fashion) to the right side of some production and thenit is substituted by theit is substituted by the nonnon--terminalterminal in the left hand side ofin the left hand side of

    the production.the production.

    Consider:

    S p aABe

    A p Abc | b

    B p d

    abbcde

    aAbcde

    aAdeaABe

    SRightmost Derivation:

    S aABe aAde aAbcde abbcde

    Reverse

    order

  • 8/7/2019 botttom up parsing

    5/30

    HandlesHandles

    Handle of a string: Substring that matches the R

    HS of some productionAND whose reduction to the non-terminal on the LHS is a step along

    the reverse of some rightmost derivation.

    Formally:

    Handle of a right sentential form K is , that satisfies the above property. i.e. A p F is a handle ofEFK at the location immediately

    after the end ofE, if:

    S => EAK => EFK A certain sentential form may have many different handles.

    Right sentential forms of a non-ambiguous grammar haveone unique handle.

    *rm rm

  • 8/7/2019 botttom up parsing

    6/30

    ExampleExample

    S aABe aAde aAbcde abbcde

    Consider:

    S p aABe

    A p Abc | b

    B p d

    It follows that:

    S p aABe is a handle of aABe in location 1.B p d is a handle of aAde in location 3.

    A p Abc is a handle of aAbcde in location 2.

    A p b is a handle of abbcde in location 2.

  • 8/7/2019 botttom up parsing

    7/30

    Handle PruningHandle Pruning

    A rightmost derivation in reverse can be obtained byA rightmost derivation in reverse can be obtained byhandlehandle--pruning.pruning.

    Apply this to the previous example.Apply this to the previous example.

    S p aABe

    A p Abc | bB p d

    abbcde

    Find the handle = b at loc. 2

    aAbcde

    b at loc. 3 is not a handle:

    aAAcde

    ... blocked.

    Also Consider:E p E + E | E * E |

    | ( E ) | id

    Derive id+id*id

    By two different Rightmostderivations

  • 8/7/2019 botttom up parsing

    8/30

    HandleHandle--pruning, Bottompruning, Bottom--up Parsersup Parsers

    The process of discovering a handle & reducing it to the

    appropriate left-hand side is called handle pruning.

    Handle pruning forms the basis for a bottom-up parsing method.

    To construct a rightmost derivationTo construct a rightmost derivation

    S= K0

    K1 K2 Kn-1 Kn = wApply the following simple algorithmApply the following simple algorithm

    for i n n to 1 by -1

    Find the handleAipFi in KiReplace Fi with Ai to generate Ki-1

  • 8/7/2019 botttom up parsing

    9/30

    Handle Pruning, IIHandle Pruning, II

    Consider the cut of a parseConsider the cut of a parse--tree of a certain right sententialtree of a certain right sententialform.form.

    S

    A

    Left part Handle (only terminals here)

    Viable prefix

  • 8/7/2019 botttom up parsing

    10/30

    Shift Reduce Parsing with a StackShift Reduce Parsing with a Stack

    Two problems:Two problems: locate a handle and

    decide which production to use (if there are more than twocandidate productions).

    General Construction: using a stack:General Construction: using a stack: shift input symbols into the stack until a handle is found on

    top of it.

    reduce the handle to the corresponding non-terminal.

    other operations:

    accept when the input is consumed and only the start symbol

    is on the stack, also: error

  • 8/7/2019 botttom up parsing

    11/30

    ExampleExample

    $

    $id

    $E

    $E +

    $E + id

    $E + E

    $E + E *

    $E + E * id

    $E + E * E

    $E + E

    $E

    id + id * id$

    + id * id$

    + id * id$

    id * id$

    * id$

    * id$

    id$

    $

    $

    $

    $

    STACK INPUT Action

    Shift

    Reduce by E p id

    Shift

    Shift

    Reduce by E p id

    Shift

    Shift

    Reduce by E p id

    Reduce by E p E * E

    Reduce by E p E + E

    Accept

    E p E + E| E * E

    | ( E ) | id

  • 8/7/2019 botttom up parsing

    12/30

    ShiftShift--reduce Parsingreduce Parsing

    Shift reduce parsers are easily built and easily understood

    A shift-reduce parser has just four actions

    Shiftnext word is shifted onto the stack

    Reduce right end of handle is at top of stack

    Locate left end of handle within the stack

    Pop handle off stack & push appropriate lhs Acceptstop parsing & report success

    Error call an error reporting/recovery routine

    Accept & Errorare simple

    Shiftis just a push and a call to the scanner

    Reduce takes |rhs| pops & 1 push

  • 8/7/2019 botttom up parsing

    13/30

    More on ShiftMore on Shift--Reduce ParsingReduce Parsing

    stmtp ifexprthen stmt

    | ifexprthen stmtelse stmt

    | other (any other statement)

    Stack Input

    if then stmt else Shift/ Reduce Conflict

    Viable prefixes:The set of prefixes of a right sentential form that can appear on thestack of a Shift-Reduce parser is called Viable prefixes.

    Conflictsshift/reduce or reduce/reduce

    Example:

    We cant tell

    whether it is a

    handle

  • 8/7/2019 botttom up parsing

    14/30

    More ConflictsMore Conflicts

    stmtp id (parameter-list)

    stmtp expr:= expr

    parameter-listpparameter-list,parameter | parameter

    parameterp id

    expr-listp expr-list, expr | expr

    exprp id | id ( expr-list)Consider the string A(I,J)

    Corresponding token stream is id(id, id)After three shifts:

    Stack = id(id Input = , id)

    Reduce/Reduce Conflict what to do?(it really depends on what is A,an array? or a procedure?

    How the symbol third from the top of stack determines the reduction to be made, even

    though it is not involved in the reduction. Shift-reduce parsing can utilize information

    far down in the stack to guide the parse.

  • 8/7/2019 botttom up parsing

    15/30

    OperatorOperator--Precedence ParserPrecedence Parser

    Operator grammar small, but an important class of grammars

    we may have an efficient operator precedence parser (a shift-reduce parser) for an operator grammar.

    In an operator grammar, no production rule can have:

    I at the right side

    two adjacent non-terminals at the right side. Ex:

    EpAB EpEOE EpE+E |

    Apa Epid E*E |

    Bp b | id Op+|*|/ E/E

    not operator grammar not operator grammar operator grammar

  • 8/7/2019 botttom up parsing

    16/30

    Precedence RelationsPrecedence Relations

    In operator-precedence parsing, we define three disjointprecedence relations between certain pairs of terminals.

    a b b has lower precedence than a

    The determination of correct precedence relations betweenterminals are based on the traditional notions ofassociativity and precedence of operators. (Unary minuscauses a problem).

  • 8/7/2019 botttom up parsing

    17/30

    Using OperatorUsing Operator--Precedence RelationsPrecedence Relations

    The intention of the precedence relations is to find thehandle of a right-sentential form,

    marking the right hand.

    In our input string $a1a2...an$, we insert the precedencerelation between the pairs of terminals (the precedencerelation holds between the terminals in that pair).

  • 8/7/2019 botttom up parsing

    18/30

    Using OperatorUsing Operator --Precedence RelationsPrecedence Relations

    E p E+E | E-E | E*E | E/E | E^E | (E) | -E | id

    The partial operator-precedence

    table for this grammar

    Then the input string id+id*id with the precedence relationsinserted will be:

    $ + * $

    idid ++ ** $$

    idid ..>> ..>> ..>>

    ++

    ** >..

    >>..

    >>$$

  • 8/7/2019 botttom up parsing

    19/30

    To Find The HandlesTo Find The Handles

    1. Scan the string from left end until the first.

    > is encountered.2. Then scan backwards (to the left) over any = until a and tothe right of the

  • 8/7/2019 botttom up parsing

    20/30

    OperatorOperator--Precedence Parsing AlgorithmPrecedence Parsing Algorithm

    The input string is w$, the initial stack is $ and a table holdsprecedence relations between certain terminals

    Algorithm:set p to point to the first symbol of w$ ;repeat forever

    if ( $ is on top of the stackandp points to $ ) then returnelse {

    let a be the topmost terminal symbol on the stack and let b be the symbolpointed to by p;

    if ( a b ) then /* REDUCE */

    repeat pop stackuntil ( the top of stack terminal is related by

  • 8/7/2019 botttom up parsing

    21/30

    OperatorOperator--Precedence Parsing AlgorithmPrecedence Parsing Algorithm ---- ExampleExample

    stack input action$ id+id*id$ $ + reduce E p id$ +id*id$ shift

    $+ id*id$ shift

    $+id *id$ id .> * reduce E p id

    $+ *id$ shift$+* id$ shift

    $+*id $ id .> $ reduce E p id

    $+* $ * .> $ reduce E p E*E$+ $ + .> $ reduce E p E+E

    $ $ acceptidid ++ ** $$idid ..>> ..>> ..>>

    ++

    ** ..>> ..>>

    $$

  • 8/7/2019 botttom up parsing

    22/30

    How to Create OperatorHow to Create Operator--Precedence RelationsPrecedence Relations

    We use associativity and precedence relations among operators.

    1. If operatorU1 has higher precedence than operatorU 2, U 1

    .> U 2 and U 2 U2

    and U2

    .> U1they are right-associative U 1 > ..>> ..>> ..>> ..>> ..>> ..>> ..>> ..>> ..>> ..>> ..>> ..>> ..>>

    ((

    $$

  • 8/7/2019 botttom up parsing

    24/30

    Handling UnaryMinusHandling UnaryMinus

    Operator-P

    recedence parsing cannot handle the unary minuswhen we also have the binary minus in our grammar.

    The best approach to solve this problem, let the lexical analyzer

    handle this problem. The lexical analyzer will return two different tokens for the unary

    minus and the binary minus.

    The lexical analyzer will need a lookhead to distinguish thebinary minus from the unary minus.

    Then, we makeThen, we make

    UU

  • 8/7/2019 botttom up parsing

    25/30

    Precedence FunctionsPrecedence Functions

    Compilers using operator precedence parsers do not need tostore the table of precedence relations.

    The table can be encoded by two precedence functions f andg that map terminal symbols to integers.

    For symbols a and b.

    f(a) < g(b) whenever a g(b) whenever a .> b

  • 8/7/2019 botttom up parsing

    26/30

    Constructing precedence functions

    Method:

    1. Create symbols fa and gb for each a that is a terminal or $.

    2. Partition the created symbols into as many groups as possible, in

    such a way that if a =. b, then fa and gb are in the same group.

    3. Create a directed graph whose nodes are the groups found in (2).For any a and b, if a b, place an edge from the group offa to that of

    gb.

    4. If the graph constructed in (3) has a cycle, then no precedence

    functions exist. If there are no cycle, let f(a) be the length of thelongest path beginning at the group offa; let g(a) be the length of the

    longest path beginning at the group ofga.

  • 8/7/2019 botttom up parsing

    27/30

    ExampleExample

    f*

    f$

    fid

    f+

    gid

    g+

    g*

    g$

    + * Id $

    f 22 44 44 00

    g 11 33 55 00

  • 8/7/2019 botttom up parsing

    28/30

    Disadvantages of OperatorPrecedence ParsingDisadvantages of OperatorPrecedence Parsing

    DisadvantagesDisadvantages:: It cannot handle the unary minus (the lexical

    analyzer should handle the unary minus).

    Small class of grammars.

    Difficult to decide which language is recognizedby the grammar.

    AdvantagesAdvantages::

    simple

    powerful enough for expressions in programminglanguages

  • 8/7/2019 botttom up parsing

    29/30

    ErrorError Recovery in OperatorRecovery in Operator--Precedence ParsingPrecedence Parsing

    Error Cases:Error Cases:1. No relation holds between the terminal on the top of stack

    and the next input symbol.

    2. A handle is found (reduction step), but there is noproduction with this handle as a right side

    Error Recovery:Error Recovery:

    1. Each empty entry is filled with a pointer to an error routine.

    2. Decides the popped handle looks like which right handside. And tries to recover from that situation.

  • 8/7/2019 botttom up parsing

    30/30