Presentation is loading. Please wait.

Presentation is loading. Please wait.

Operator precedence parser Lecturer: Noor Dhia 2014-2015.

Similar presentations


Presentation on theme: "Operator precedence parser Lecturer: Noor Dhia 2014-2015."— Presentation transcript:

1 Operator precedence parser Lecturer: Noor Dhia 2014-2015

2 Operator precedence parser: The operator-precedence parser is a shift –reduce parser that can be easily constructed by hand. It used for a small class of grammars which is called operator grammar. These grammars have the property: - That no production right side is ε - And no production right side has two adjacent nonterminals. Example: The following grammar for expressions: E → EAE | (E) | –E |id A → + | - | * | / | ^ Is not an operator grammar, because the right side EAE has two (in fact three) consecutive nonterminals. However, if we substitute for A each of its alternatives, we obtain the following operator grammar: E → E+E | E-E | E*E | E/E | E^E| (E) | –E | id

3 Operator precedence parser: With operator precedence parser, Firstly the relation table must constructed to define the precedence of the operation. This table is square table consist of rows and columns from terminal symbols. And the content of the table is the relation symbol which describe the precedence between row symbol ( left side) and column symbol ( right symbol). In operator-precedence parser, we define three disjoint precedence relations,, between certain pairs of terminals. These precedence relations guide the selection of handlers. If a b, a “takes precedence over” b.

4 Operator precedence parser: Now suppose we remove the nonterminals from the string and place the correct relation, between each pair of terminals and between the endmost terminal and the $’s marking the ends of the string.

5 Operator precedence parser: Example: following operator grammar: E → E+E | E*E | id The operator precedence ( relation table) can be constructed : 1- we listed the terminal symbols in the rows and columns with $. 2- compare between row symbol and column symbol and put the relation in intersection cell id with id = has the same precedence id with +.> id takes precedence over + and so on Id+*$ + * $

6 Operator precedence parser: The precedence relations are given in precedence relation table below

7 Operator precedence parser: The handle can be found by the following process: 1- Scan the string from the left end until the first.> is encountered. In example id + id * id, this occurs between the first id and +. 2- Then scan backwards (to the left) over any = until a<. Is encountered, we scan back to $ 3- The handle contains everything to the left of the first.> and to the right of the <. encountered in step (2).

8 Stack implementation of operator precedence parser: Example : Suppose we are given the expression id+id to parse we set up the stack and input as:

9 Example: Suppose we are given the expression id+id*id use the following grammar to check this input accepted : E → E+E | E*E | id precedence table: Id+*$ -.> +<..><..> *<..> $<. -

10 Example: 5 E 4 E E E E id + id * id 2 1 3 stackinputAction $ $ id $ $ + $ + id $ + $ + * $ + * id $ + * $ + $ id + id * id $ + id * id $ id * id $ * id $ id $ $ S R E → id S R E → id S R E → id R E → E * E R E → E + E Accepted

11 Home work: Using the following grammar: E → E+E | E-E | E*E | E/E | E^E| (E) | –E | id Parse the following input string using operator precedence parser. id * ( id ^ id ) – id / id


Download ppt "Operator precedence parser Lecturer: Noor Dhia 2014-2015."

Similar presentations


Ads by Google