Can you implement the shunting yard algorithm in terms of regular expressions?
-
Out of curiosity is this something you'd actually want to do or is it more of an academic curiosity? – shuttle87 Feb 15 '12 at 17:24
-
1It's a tribute question to http://stackoverflow.com/questions/4161553/can-the-shunting-yard-algorithm-parse-posix-regular-expressions/4161681#4161681 – Prof. Falken Feb 16 '12 at 07:24
2 Answers
I do not think so. Regular expressions can only match regular languages (See Regular language), while infix expressions are a kind of context-free language (See Context-free language). For example, you cannot match expressions made of properly matched parentheses with a regular expression.

- 1,463
- 1
- 11
- 13
I believe this has been answered here: Can the shunting yard algorithm parse POSIX regular expressions?
I will say that the answer to your question is "no, you cannot implement the shunting yard algorithm using a regular expression." This is for the same reason you cannot parse arbitrary HTML using regular expressions. Which boils down to this:
Regular expressions do not have a stack. Because the shunting yard algorithm relies on a stack (to push and pop operands as you convert from infix to RPN), then regular expressions do not have the computational "power" to perform this task.
This glosses over many details, but a "regular expression" is one way to define a regular language. When you "use" a regular expression, you are asking the computer to say: "Look at a body of text and tell me whether or not any of those strings are in my language. The language that I defined using a regular expression." I'll point to this most excellent answer which you and everyone reading this should upvote for more on regular languages.
So now you need some mathematical concept to augment "regular languages" in order to create more powerful languages. If you were to characterize the shunting yard algorithm as an realization of a model of computational power, then you might say that the algorithm would be described as a context-free grammar (hey what do you know, that link uses an expression parse tree as an example.) A push-down automata. Something with a stack.
If you are less-than-familiar with automata theory and complexity classes, then those wikipedia articles are probably not that helpful without explaining them from the ground up.
The point being, you may be able to use regex to help writing shunting yard. But regex are not very good at doing operations that have an arbitrary depth, which this problem has. So I would not spend too much time going down the regex avenue for this problem.

- 1
- 1

- 18,726
- 23
- 95
- 134