Shunting yard algorithm

Last updated
Shunting yard algorithm
Class Parsing
Data structure Stack
Worst-case performance
Worst-case space complexity

In computer science, the shunting yard algorithm is a method for parsing arithmetical or logical expressions, or a combination of both, specified in infix notation. It can produce either a postfix notation string, also known as Reverse Polish notation (RPN), or an abstract syntax tree (AST). [1] The algorithm was invented by Edsger Dijkstra and named the "shunting yard" algorithm because its operation resembles that of a railroad shunting yard. Dijkstra first described the shunting yard algorithm in the Mathematisch Centrum report MR 34/61.

Contents

Like the evaluation of RPN, the shunting yard algorithm is stack-based. Infix expressions are the form of mathematical notation most people are used to, for instance "3 + 4" or "3 + 4 × (2 − 1)". For the conversion there are two text variables (strings), the input and the output. There is also a stack that holds operators not yet added to the output queue. To convert, the program reads each symbol in order and does something based on that symbol. The result for the above examples would be (in Reverse Polish notation) "3 4 +" and "3 4 2 1 − × +", respectively.

The shunting yard algorithm will correctly parse all valid infix expressions, but does not reject all invalid expressions. For example, "1 2 +" is not a valid infix expression, but would be parsed as "1 + 2". The algorithm can however reject expressions with mismatched parentheses.

The shunting yard algorithm was later generalized into operator-precedence parsing.

A simple conversion

  1. Input: 3 + 4
  2. Push 3 to the output queue (whenever a number is read it is pushed to the output)
  3. Push + (or its ID) onto the operator stack
  4. Push 4 to the output queue
  5. After reading the expression, pop the operators off the stack and add them to the output.
    In this case there is only one, "+".
  6. Output: 3 4 +

This already shows a couple of rules:

Graphical illustration

Shunting yard.svg

Graphical illustration of algorithm, using a three-way railroad junction. The input is processed one symbol at a time: if a variable or number is found, it is copied directly to the output a), c), e), h). If the symbol is an operator, it is pushed onto the operator stack b), d), f). If the operator's precedence is lower than that of the operators at the top of the stack or the precedences are equal and the operator is left associative, then that operator is popped off the stack and added to the output g). Finally, any remaining operators are popped off the stack and added to the output i).

The algorithm in detail

/* The functions referred to in this algorithm are simple single argument functions such as sine, inverse or factorial. *//* This implementation does not implement composite functions, functions with a variable number of arguments, or unary operators. */while there are tokens to be read:     read a token     if the token is:     - a number:         put it into the output queue     - a  function :         push it onto the operator stack      - an operatoro1:         while (             there is an operator o2 at the top of the operator stack which is not a left parenthesis,              and (o2 has greater precedence than o1or (o1 and o2 have the same precedence ando1 is left-associative))         ):             pop o2 from the operator stack into the output queue         push o1 onto the operator stack     - a ",":         while the operator at the top of the operator stack is not a left parenthesis:              pop the operator from the operator stack into the output queue     - a left parenthesis (i.e. "("):         push it onto the operator stack     - a right parenthesis (i.e. ")"):         while the operator at the top of the operator stack is not a left parenthesis:             {assert the operator stack is not empty}             /* If the stack runs out without finding a left parenthesis, then there are mismatched parentheses. */             pop the operator from the operator stack into the output queue         {assert there is a left parenthesis at the top of the operator stack}         pop the left parenthesis from the operator stack and discard it         if there is a function token at the top of the operator stack, then:             pop the function from the operator stack into the output queue /* After the while loop, pop the remaining items from the operator stack into the output queue. */while there are tokens on the operator stack:     /* If the operator token on the top of the stack is a parenthesis, then there are mismatched parentheses. */     {assert the operator on top of the stack is not a (left) parenthesis}     pop the operator from the operator stack onto the output queue

To analyze the running time complexity of this algorithm, one has only to note that each token will be read once, each number, function, or operator will be printed once, and each function, operator, or parenthesis will be pushed onto the stack and popped off the stack once—therefore, there are at most a constant number of operations executed per token, and the running time is thus O(n) — linear in the size of the input.

The shunting yard algorithm can also be applied to produce prefix notation (also known as Polish notation). To do this one would simply start from the end of a string of tokens to be parsed and work backwards, reverse the output queue (therefore making the output queue an output stack), and flip the left and right parenthesis behavior (remembering that the now-left parenthesis behavior should pop until it finds a now-right parenthesis). And changing the associativity condition to right.

Detailed examples

Input: 3 + 4 × 2 ÷ ( 1 − 5 ) ^ 2 ^ 3

OperatorPrecedenceAssociativity
^4Right
×3Left
÷3Left
+2Left
2Left

The symbol ^ represents the power operator.

TokenActionOutput
(in RPN)
Operator
stack
Notes
3Add token to output3
+Push token to stack3+
4Add token to output3 4+
×Push token to stack3 4× +× has higher precedence than +
2Add token to output3 4 2× +
÷Pop stack to output3 4 2 ×+÷ and × have same precedence
Push token to stack3 4 2 ×÷ +÷ has higher precedence than +
(Push token to stack3 4 2 ×( ÷ +
1Add token to output3 4 2 × 1( ÷ +
Push token to stack3 4 2 × 1− ( ÷ +
5Add token to output3 4 2 × 1 5− ( ÷ +
)Pop stack to output3 4 2 × 1 5 −( ÷ +Repeated until "(" found
Pop stack3 4 2 × 1 5 −÷ +Discard matching parenthesis
^Push token to stack3 4 2 × 1 5 −^ ÷ +^ has higher precedence than ÷
2Add token to output3 4 2 × 1 5 − 2^ ÷ +
^Push token to stack3 4 2 × 1 5 − 2^ ^ ÷ +^ is evaluated right-to-left
3Add token to output3 4 2 × 1 5 − 2 3^ ^ ÷ +
endPop entire stack to output3 4 2 × 1 5 − 2 3 ^ ^ ÷ +

Input: sin ( max ( 2, 3 ) ÷ 3 × π )

TokenActionOutput
(in RPN)
Operator
stack
Notes
sinPush token to stacksin
(Push token to stack( sin
maxPush token to stackmax ( sin
(Push token to stack( max ( sin
2Add token to output2( max ( sin
,Ignore2( max ( sinThe operator at the top of the stack is a left parenthesis
3Add token to output2 3( max ( sin
)Pop stack to output2 3( max ( sinRepeated until "(" is at the top of the stack
Pop stack2 3max ( sinDiscarding matching parentheses
Pop stack to output2 3 max( sinFunction at top of the stack
÷Push token to stack2 3 max÷ ( sin
3Add token to output2 3 max 3÷ ( sin
×Pop stack to output2 3 max 3 ÷( sin
Push token to stack2 3 max 3 ÷× ( sin
πAdd token to output2 3 max 3 ÷ π× ( sin
)Pop stack to output2 3 max 3 ÷ π ×( sinRepeated until "(" is at the top of the stack
Pop stack2 3 max 3 ÷ π ×sinDiscarding matching parentheses
Pop stack to output2 3 max 3 ÷ π × sinFunction at top of the stack
endPop entire stack to output2 3 max 3 ÷ π × sin

See also

Related Research Articles

Forth is a procedural, concatenative, stack-oriented programming language and interactive development environment designed by Charles H. "Chuck" Moore and first used by other programmers in 1970. Although not an acronym, the language's name in its early years was often spelled in all capital letters as FORTH. The FORTH-79 and FORTH-83 implementations, which were not written by Moore, became de facto standards, and an official standardization of the language was published in 1994 as ANS Forth. A wide range of Forth derivatives existed before and after ANS Forth. The free software Gforth implementation is actively maintained, as are several commercially supported systems.

In computer science, LR parsers are a type of bottom-up parser that analyse deterministic context-free languages in linear time. There are several variants of LR parsers: SLR parsers, LALR parsers, canonical LR(1) parsers, minimal LR(1) parsers, and generalized LR parsers. LR parsers can be generated by a parser generator from a formal grammar defining the syntax of the language to be parsed. They are widely used for the processing of computer languages.

In mathematics, an operand is the object of a mathematical operation, i.e., it is the object or quantity that is operated on.

Polish notation (PN), also known as normal Polish notation (NPN), Łukasiewicz notation, Warsaw notation, Polish prefix notation or simply prefix notation, is a mathematical notation in which operators precede their operands, in contrast to the more common infix notation, in which operators are placed between operands, as well as reverse Polish notation (RPN), in which operators follow their operands. It does not need any parentheses as long as each operator has a fixed number of operands. The description "Polish" refers to the nationality of logician Jan Łukasiewicz, who invented Polish notation in 1924.

Rebol is a cross-platform data exchange language and a multi-paradigm dynamic programming language designed by Carl Sassenrath for network communications and distributed computing. It introduces the concept of dialecting: small, optimized, domain-specific languages for code and data, which is also the most notable property of the language according to its designer Carl Sassenrath:

Although it can be used for programming, writing functions, and performing processes, its greatest strength is the ability to easily create domain-specific languages or dialects

In computer science, an LL parser is a top-down parser for a restricted context-free language. It parses the input from Left to right, performing Leftmost derivation of the sentence.

Lexical tokenization is conversion of a text into meaningful lexical tokens belonging to categories defined by a "lexer" program. In case of a natural language, those categories include nouns, verbs, adjectives, punctuations etc. In case of a programming language, the categories include identifiers, operators, grouping symbols and data types. Lexical tokenization is not the same process as the probabilistic tokenization, used for a large language model's data preprocessing, that encodes text into numerical tokens, using byte pair encoding.

<span class="mw-page-title-main">Infix notation</span> Mathematics notation with operators between operands

Infix notation is the notation commonly used in arithmetical and logical formulae and statements. It is characterized by the placement of operators between operands—"infixed operators"—such as the plus sign in 2 + 2.

In mathematics and computer programming, the order of operations is a collection of rules that reflect conventions about which operations to perform first in order to evaluate a given mathematical expression.

Parsing, syntax analysis, or syntactic analysis is the process of analyzing a string of symbols, either in natural language, computer languages or data structures, conforming to the rules of a formal grammar. The term parsing comes from Latin pars (orationis), meaning part.

In programming language theory, the associativity of an operator is a property that determines how operators of the same precedence are grouped in the absence of parentheses. If an operand is both preceded and followed by operators, and those operators have equal precedence, then the operand may be used as input to two different operations. The choice of which operations to apply the operand to, is determined by the associativity of the operators. Operators may be associative, left-associative, right-associative or non-associative. The associativity and precedence of an operator is a part of the definition of the programming language; different programming languages may have different associativity and precedence for the same type of operator.

dc is a cross-platform reverse-Polish calculator which supports arbitrary-precision arithmetic. It was written by Lorinda Cherry and Robert Morris at Bell Labs. It is one of the oldest Unix utilities, preceding even the invention of the C programming language. Like other utilities of that vintage, it has a powerful set of features but terse syntax. Traditionally, the bc calculator program was implemented on top of dc.

In computer programming, operators are constructs defined within programming languages which behave generally like functions, but which differ syntactically or semantically.

In computer science, an operator precedence parser is a bottom-up parser that interprets an operator-precedence grammar. 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).

In computer science, a simple precedence parser is a type of bottom-up parser for context-free grammars that can be used only by simple precedence grammars.

An operator precedence grammar is a kind of grammar for formal languages.

A binary expression tree is a specific kind of a binary tree used to represent expressions. Two common types of expressions that a binary expression tree can represent are algebraic and boolean. These trees can represent expressions that contain both unary and binary operators.

A shift-reduce parser is a class of efficient, table-driven bottom-up parsing methods for computer languages and other notations formally defined by a grammar. The parsing methods most commonly used for parsing programming languages, LR parsing and its variations, are shift-reduce methods. The precedence parsers used before the invention of LR parsing are also shift-reduce methods. All shift-reduce parsers have similar outward effects, in the incremental order in which they build a parse tree or call specific output actions.

exp4j is a small Java library for evaluation of mathematical expressions. It implements Dijkstra's Shunting-yard algorithm to translate expressions from infix notation to Reverse Polish notation and calculates the result using a simple Stack algorithm.

In mathematics and computer science, a stack-sortable permutation is a permutation whose elements may be sorted by an algorithm whose internal storage is limited to a single stack data structure. The stack-sortable permutations are exactly the permutations that do not contain the permutation pattern 231; they are counted by the Catalan numbers, and may be placed in bijection with many other combinatorial objects with the same counting function including Dyck paths and binary trees.

References

  1. Theodore Norvell (1999). "Parsing Expressions by Recursive Descent". www.engr.mun.ca. Retrieved 2020-12-28.