Vous êtes sur la page 1sur 14

BOTTOM-UP PARSING

Conflict during shift-reduce parsing


As they were for top-down parsers, ambiguous grammars are problem for bottom-up parsers because these grammars could yield more than one handle under some circumstances. These types of grammars create either shift-reduce or reduce-reduce conflicts. The former refers to a state where the parser cannot decide whether to shift or reduce. The latter refers to a state where the parser has more than one choice of production for reduction Example: stmt if expr then stmt
| if expr then stmt else stmt | other (any other statement) Stack if then stmt Input else Shift/ Reduce Conflict

LR Parser
LR parsing is a bottom up syntax analysis technique

that can be applied to a large class of context free grammars. L is for left to right scanning of the input and R for constructing rightmost derivation in reverse.

LR parser is attractive for a variety of reasons


LR parser can be constructed to recognize virtually all programming languages constructs for which CFG can be written. LR parsing method is more general than operator precedence or any of the other common shift reduce techniques. LR parser can detect syntactic errors as soon as it is possible to do so on a left-to-right scan of the input.

The LR parser contain


The LR parser has an input, a stack, and a parsing

table. The input read from left to right one symbol at a time. The stack contains a string of the form s0X1s1X2s2.Xmsm , where sm is on the top of the stack. Each Xi is a grammar symbol and each si is symbol called a state. The parsing table consists of two parts a parsing action function ACTION and goto function GOTO

Model of an LR parser

LR works
The program driving the LR parser behaves as follows. It determines sm the state currently on the top of the stack and ai, the current input symbol. It then consults ACTION[sm, a], the parsing action table entry for state sm and input ai. It can have one of four values: 1. shift s 2. reduce A 3. accept 4. error

The configurations resulting after each of the types of move are as follows:
If ACTION [sm, ai ] = shift s, the parser executes a shift move, entering

the configuration(s0 X1 s1 X2 s2.Xmsm ai s, ai+1 . an $) . It shifted the current input symbol ai and the next state s = GOTO[ sm, ai ] onto the stack ai + 1 becomes the new current input symbol. then the parser executes a reduce move, entering the configuration (s0 X1 s1 X2 s2.Xm- r sm-r ai s, ai+1 . an $) . where s = GOTO [ sm-r, A ] and r is the length of the right side of production.
If ACTION [sm, ai ] = reduce A

If ACTION [sm, ai ] = accept parsing is completed.


If ACTION [sm, ai ] = error the parser has discovered an error and

calls an error recovery routine.

Example:
Consider the following grammar

1. S S 2. S aABe 3. A Abc 4. A b 5. B d

Table for parsing action and goto function for the given grammar is:-

The codes for the actions are:


si means shift and stack state i,

rj means reduce by production numbered j'


acc means accept, blank means error.

Solution
STATE
0 0a2 0a2b4 0a2A3 0a2A3b6 0a2A3b6c9 0a2A3 0a2A3b6 0a2A3b6c9 0a2A3 0a2A3d7 0a2A3B5 0a2A3B5e8 0S1

input
abbcbcde$ bbcbcde$ bcbcde$ bcbcde$ cbcde$ bcde$ bcde$ cde$ de$ de$ e$ e$ $ $

action
shift shift Ab shift shift A Abc shift shift A Abc shift Bd shift S aABe accepted

Example 2
Example 4.45

page 251

The LR parsing algorithm is:

Vous aimerez peut-être aussi