0% found this document useful (0 votes)
28 views

Parsing

The document describes an algorithm called precedence climbing that can parse infix notation more efficiently than a traditional recursive descent parser. It works by left-associating arithmetic operations of equal precedence and using recursive calls to implicitly implement an expression evaluation stack. The key steps are to parse primaries, then repeatedly apply binary operators of equal or higher precedence to the left-hand side, recursively parsing the right-hand side as needed.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

Parsing

The document describes an algorithm called precedence climbing that can parse infix notation more efficiently than a traditional recursive descent parser. It works by left-associating arithmetic operations of equal precedence and using recursive calls to implicitly implement an expression evaluation stack. The key steps are to parse primaries, then repeatedly apply binary operators of equal or higher precedence to the left-hand side, recursively parsing the right-hand side as needed.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 1

Example algorithm known as precedence climbing to parse infix notation

An EBNF grammar that parses infix notation will usually look like this:
expression ::= equality-expression
equality-expression ::= additive-expression ( ( '==' | '!=' ) additive-expression ) *
additive-expression ::= multiplicative-expression ( ( '+' | '-' ) multiplicative-expression ) *
multiplicative-expression ::= primary ( ( '*' | '/' ) primary ) *
primary ::= '(' expression ')' | NUMBER | VARIABLE | '-' primary

With many levels of precedence, implementing this grammar with a predictive recursive-descent parser
can become inefficient. Parsing a number, for example, can require five function calls (one for each
non-terminal in the grammar, until we reach primary).

An operator-precedence parser can do the same more efficiently. The idea is that we can left associate
the arithmetic operations as long as we find operators with the same precedence, but we have to save a
temporary result to evaluate higher precedence operators. The algorithm that is presented here does not
need an explicit stack: instead, it uses recursive calls to implement the stack.

The algorithm is not a pure operator-precedence parser like the Dijkstra shunting yard algorithm. It
assumes that the primary nonterminal is parsed in a separate subroutine, like in a recursive descent
parser.

Pseudo-code

The pseudo-code for the algorithm is as follows. The parser starts at function parse_expression.
Precedence levels are greater or equal to 0.
parse_expression ()
return parse_expression_1 (parse_primary (), 0)

parse_expression_1 (lhs, min_precedence)


while the next token is a binary operator whose precedence is >= min_precedence
op := next token
rhs := parse_primary ()
while the next token is a binary operator whose precedence is greater
than op's, or a right-associative operator
whose precedence is equal to op's
lookahead := next token
rhs := parse_expression_1 (rhs, lookahead's precedence)
lhs := the result of applying op with operands lhs and rhs
return lhs

You might also like