Operator Precedence Parsing Example

In this class, We discuss Operator Precedence Parsing Example.

For Complete YouTube Video: Click Here

The reader should have prior knowledge of Operator precedence parsing. Click Here.

The example is complex. For a better understanding of Operator Precedence parsing, we do this example.

Example:

E – E + E | E * E | (E) | id | E ^ E

^ highest precedence and right-associative

‘*’ second-highest and left-associative

+ least precedence and left-associative

We need to generate an operator precedence table based on the above conditions.

To fill the parsing table, we show a few examples.

Examples:

1) Assume that we need the expressions in our programming language as (id + id).

In our OPP, we are checking the stack symbol and input symbol.

We are doing a shift operation if the stack symbol operator is less than or equal to the input symbol operator. Otherwise, reduce operation.

Assume stack top has the symbol ‘(‘ and input is pointing symbol id.

Whenever we find the open bracket, we need to evaluate the expression until we find the closed bracket.

We need to push the expression onto the stack.

We need to shift action in our parse table for [(, id]. So we place relation <.

Take one more expression id ( id + id). The expression is not valid in our language.

We place blank in the precedence table for [id, (].

2) Take the expression (id + id + id)

We convert the expression as (E+ E + E)

if we see the symbol [+, )], we need to reduce the expression to E. Based on the production E – (E).

To take reduce action we place relation > for [+, )].

Similarly, we have to construct the remaining table according to the requirement of our programming language.

The below diagram shows the complete operator precedence parsing table.

The below table shows the parsing of the input string ((id + id))

The parsing of the input string is explained in our previous class.