Precedence Climbing Method
The precedence climbing method is the name for an algorithm that was first described by Keith Clarke in a post to comp.compilers on May 26, 1992.
An infix-notation expression grammar in EBNF format 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 | '-' primaryWith 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 reaching 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.
Read more about this topic: Operator-precedence Parser
Famous quotes containing the words precedence, climbing and/or method:
“What is line? It is life. A line must live at each point along its course in such a way that the artists presence makes itself felt above that of the model.... With the writer, line takes precedence over form and content. It runs through the words he assembles. It strikes a continuous note unperceived by ear or eye. It is, in a way, the souls style, and if the line ceases to have a life of its own, if it only describes an arabesque, the soul is missing and the writing dies.”
—Jean Cocteau (18891963)
“Flee from the press and dwell with soothfastness;
Suffice unto thy good though it be small,
For hoard hath hate and climbing ticklishness,
Press hath envy and weal blent overall;”
—Geoffrey Chaucer (1340?1400)
“Too poor for a bribe, and too proud to importune,
He had not the method of making a fortune.”
—Thomas Gray (17161771)