Operator-precedence parser
In computer science, an operator-precedence parser is a bottom-up parser that interprets an operator-precedence grammar.[src] For example, most calculators use operator-precedence parsers to convert from the human-readable infix notation relying on order of operations to a format that is optimized for evaluation such as Reverse Polish notation (RPN).[src] Edsger Dijkstra's shunting yard algorithm is commonly used to implement operator-precedence parsers.[src]
In computer science, an operator-precedence parser is a bottom-up parser that interprets an operator-precedence grammar.[src] For example, most calculators use operator-precedence parsers to convert from the human-readable infix notation relying on order of operations to a format that is optimized for evaluation such as Reverse Polish notation (RPN).[src] Edsger Dijkstra's shunting yard algorithm is commonly used to implement operator-precedence parsers.[src] 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.[src] Parsing a number, for example, can require five function calls: one for each non-terminal in the grammar until reaching primary.[src] An operator-precedence parser can do the same more efficiently.[src] 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.[src] The algorithm that is presented here does not need an explicit stack; instead, it uses recursive calls to implement the stack.[src] The algorithm is not a pure operator-precedence parser like Edsger Dijkstra's shunting yard algorithm.[src] It assumes that the primary nonterminal is parsed in a separate subroutine, like in a recursive descent parser.[src] lookahead := peek next token while lookahead is a binary operator whose precedence is >= min_precedence op := lookahead advance to next token rhs := parse_primary () lookahead := peek next token while lookahead is a binary operator whose precedence is greater than op ' s, or a right-associative operator whose precedence is equal to ops rhs := parse_expression_1 (rhs, precedence of op + (1 if lookahead precedence is greater, else 0)) lookahead := peek next token lhs := the result of applying op with operands lhs and rhs return lhs Note that in the case of a production rule like this (where the operator can only appear once): equality-expression ::= additive-expression ( '==' | '!=' ) additive-expression the algorithm must be modified to accept only binary operators whose precedence is > min_precedence.[src] An example execution on the expression 2 + 3 * 4 + 5 == 19 is as follows.[src] We give precedence 0 to equality expressions, 1 to additive expressions, 2 to multiplicative expressions.[src] parse_expression_1 (lhs = 2, min_precedence = 0) the lookahead token is +, with precedence 1.[src] the outer while loop is entered.[src] op is + (precedence 1) and the input is advanced rhs is 3 the lookahead token is *, with precedence 2.[src] the inner while loop is entered.[src] parse_expression_1 (lhs = 3, min_precedence = 2) the lookahead token is *, with precedence 2.[src] the outer while loop is entered.[src] op is * (precedence 2) and the input is advanced rhs is 4 the next token is +, with precedence 1.[src] the inner while loop is not entered.[src] lhs is assigned 3*4 = 12 the next token is +, with precedence 1.[src] the outer while loop is left.[src] 12 is returned.[src] the lookahead token is +, with precedence 1.[src] the inner while loop is not entered.[src] lhs is assigned 2+12 = 14 the lookahead token is +, with precedence 1.[src] the outer while loop is not left.[src] op is + (precedence 1) and the input is advanced rhs is 5 the next token is ==, with precedence 0.[src] the inner while loop is not entered.[src] lhs is assigned 14+5 = 19 the next token is ==, with precedence 0.[src] the outer while loop is not left.[src] op is == (precedence 0) and the input is advanced rhs is 19 the next token is end-of-line, which is not an operator.[src] the inner while loop is not entered.[src] lhs is assigned the result of evaluating 19 == 19, for example 1 (as in the C standard).[src] the next token is end-of-line, which is not an operator.[src] the outer while loop is left.[src] 1 is returned.[src] include include // The command-line argument boundary is our lexer.[src] int main(int argc, char *argv[]) { int i; printf("(((("); for (i=1; i!=argc; i++) { // strlen(argv[i]) == 2 if (argv[i] && !argv[i][1]) { switch (*argv[i]) { case '(': printf("(((("); continue; case ')': printf("))))"); continue; case '^': printf(")^("); continue; case '*': printf("))*(("); continue; case '/': printf("))/(("); continue; case '+': // unary check: either first or had an operator expecting secondary argument if (i == 1 || strchr("(^*/+-", *argv[i-1])) printf("+"); else printf(")))+((("); continue; case '-': if (i == 1 || strchr("(^*/+-", *argv[i-1])) printf("-"); else printf(")))-((("); continue; } } printf("%s", argv[i]); } printf("))))\n"); return 0; } For example, running with input a * b + c ^ d / e produces ((((a))*((b)))+(((c)^(d))/((e)))) as output.[src] A limitation to this strategy is that unary operators must all have higher precedence than infix operators.[src] The "negative" operator in the above code has a higher precedence than exponentiation.[src] Running the program with this input - a ^ 2 produces this output ((((-a)^(2)))) which is probably not what is intended.[src] ==References== ==External links== Example C++ code by Keith Clarke for parsing infix expressions using the precedence climbing method Parser for expression with infix notation using precedence lists[src]
47 sentences · 47 cited inline · 0 references stored
References
No reference rows published for this article.
Cite this article
MLA
"Operator-precedence parser." encyclopegia.org, 2026, https://encyclopegia.org/wiki/operator-precedence-parser. Accessed 25 November 2026.
Chicago
"Operator-precedence parser." encyclopegia.org. Accessed November 25, 2026. https://encyclopegia.org/wiki/operator-precedence-parser.
APA
encyclopegia.org. (2026). Operator-precedence parser. Retrieved November 25, 2026, from https://encyclopegia.org/wiki/operator-precedence-parser
Accessed November 25, 2026 · CC BY-SA 4.0 · citation page · JSON