PlanetMath (more info)
 Math for the people, by the people.
Encyclopedia | Requests | Forums | Docs | Wiki | Random | RSS  
Login
create new user
name:
pass:
forget your password?
Main Menu
Owner confidence rating: High Entry average rating: Very low
[parent] shunting yard algorithm (Algorithm)

To convert from standard infix notation to reverse Polish notation, the Dutch computer programmer Edsger Dijkstra came up with the shunting yard algorithm.

Given a string of tokens, a token reader that ignores whitespace (except to delimit numbers), a number stack, an operator stack and an output queue execute the following steps:

1. Read the tokens in order until end of line. Put number tokens on the output queue. Put operator tokens on the operator stack: if an operator is associative, and has lesser precedence than the operator below it on the operator stack, pop the lower operator off the operator stack and put it on the output queue. If the operator is a left parenthesis, push it onto the operator stack. If the operator is a right parenthesis, start popping operators off the operator stack and onto the output queue until a left parenthesis is encountered. If no left parenthesis is found, throw a mismatched parenthesis exception. 2. At end of line, pop all operators off the operator stack and onto the output queue. If a left parenthesis is encountered during this process, throw a mismatched parenthesis exception. 3. Send the contents of the output queue to the appropriate output device.

What the mismatched parenthesis exception does depends on the device. In the case of a palm calculator, the exception might just turn on the error "E" and ignore input until the error clear key is pressed. In the case of a calculator implemented on a computer, the exception could give an appropriate message and ask the user to try again.



"shunting yard algorithm" is owned by Mravinci.
(view preamble)

View style:


This object's parent.
Log in to rate this entry.
(view current ratings)

Cross-references: clear, calculator, right, onto, push, pop, associative, line, order, queue, operator, stack, numbers, string, reverse Polish notation, infix notation
There are 2 references to this entry.

This is version 1 of shunting yard algorithm, born on 2006-08-16.
Object id is 8256, canonical name is ShuntingYardAlgorithm.
Accessed 2970 times total.

Classification:
AMS MSC68N17 (Computer science :: Software :: Logic programming)
 03B70 (Mathematical logic and foundations :: General logic :: Logic in computer science)

Pending Errata and Addenda
None.
Discussion
Style: Expand: Order:
forum policy

No messages.

Interact
post | correct | update request | add example | add (any)