Questions tagged [shunting-yard]

The Dijkstra Shunting-yard algorithm is a very general, linear time, stack-based algorithm for parsing mathematical expressions.

The Dijkstra Shunting-yard algorithm is a very general, linear time, stack-based algorithm for parsing mathematical expressions specified in infix notation. It can be used to produce output in Reverse Polish notation (RPN) or as an abstract syntax tree (AST). It was invented by the Dutch computer science Edsger Dijkstra, and published in 1960. He named it the "shunting yard" algorithm because its operation resembles that of a railroad shunting yard.

It is not used in most production compilers, as they already have more general parsing systems that include arithmetic expressions as a special case. It is very widely used as a teaching exercise, and it can be used in compilers for special languages where the programmer can add operators or redefine operator precedence, which are easily made variable in the algorithm, unlike the parsers generally used in production compilers.

Wikipedia page

124 questions
5
votes
2 answers

Ternary Operator Parsing with the Shunting Yard Algorithm

For context, please read this question about Ternary Operators first. I am building my own programming language that allows you to define custom operators. Because I want it to have as few compiler built-ins as possible, it should allow the…
Clashsoft
  • 11,553
  • 5
  • 40
  • 79
5
votes
1 answer

Tokenizing an infix string in Java

I'm implementing the Shunting Yard Algorithm in Java, as a side project to my AP Computer Science class. I've implemented a simple one in Javascript, with only basic arithmetic expressions (addition, subtraction, multiplication, division,…
scrblnrd3
  • 7,228
  • 9
  • 33
  • 64
4
votes
1 answer

Preserving Parenthesis in Shunting Yard

I'm working on what is essentially the shunting yard algorithm, but moving infix to prefix instead of RPN I am trying to preserve parenthesis, and I'm having a devil of a time of it. Currenntly my code is String s = inFixList.get(i); …
ashreil
  • 49
  • 3
4
votes
1 answer

Shunting Yard Algorithm with Variables

I'm currently working on a modified version of the Shunting Yard Algorithm that would work with variables, but I cant figure out how to get it to work. For example, I would want the algorithm to re-write 2 * (2x + 5) - 5 to 4x + 5. Any ideas / links…
Alston Lin
  • 197
  • 4
  • 10
4
votes
3 answers

shunting yard algorithm(in Javascript),handling negative numbers

I have written shunting yard algorithm in JS which works fine for almost all the scenarios, however if I have a negative number scenario then it fails, say for example if I give this expression 9-(3*(-6)) then it won't give a result...Any hint would…
min2bro
  • 554
  • 2
  • 9
  • 19
4
votes
2 answers

Have to implement Shunting-yard algorithm, need help understanding it

I'm trying to implement an Shunting-yard algorithm with no parenthesis, but I'm having trouble understanding it. I have tried wikipedia but the entry is really bad. I should have little problem implementing the code, but if I don't get I can't…
Kalec
  • 2,681
  • 9
  • 30
  • 49
3
votes
4 answers

Shunting Yard implementation in PHP needed, interpret and parse a string perform a mathematical comparison and return a boolean result

I'm looking for something that can interpret a string in php and perform simple math calculation, and then return a boolean result as to whether the expression is true or false. For example: Sue types in "3*{mysalary}/9=10000" PHP splits this up…
Jason
  • 15,064
  • 15
  • 65
  • 105
3
votes
3 answers

Extending the shunting-yard algorithm to support the conditional ternary operator

How to extend the shunting yard algorithm, that's originally meant for binary operators to support the conditional ternary operator ("a ? b : c") ? I haven't seen an answer to this here and I have one, so I'm posting it.
3
votes
3 answers

Which of the following postfix notations correctly represents infix sum 1+2+3+4?

I am testing an infix-to-postfix-to-infix converter and found some kind of uncertainty. For example, a simple infix sum 1 + 2 + 3 + 4 can be converted to postfix one 1 2 + 3 + 4 + assuming that operators with equal precedence are not accumulated.…
Andrei
  • 10,918
  • 12
  • 76
  • 110
3
votes
2 answers

Shunting-yard: missing argument to operator

I'm implementing the shunting-yard algorithm. I'm having trouble detecting when there are missing arguments to operators. The wikipedia entry is very bad on this topic, and their code also crashes for the example below. For instance 3 - (5 + ) is…
Giovanni Funchal
  • 8,934
  • 13
  • 61
  • 110
3
votes
2 answers

Apply distributive law on AST (or RPN) => disjunctive normal form

I have expressions like the following: {1000} AND ({1001} OR {1002} OR {1003}) Allowed operators are OR and AND, expressions can be nested using parenthesis. I already managed to tokenize this string and to convert it to an abstract syntax tree…
3
votes
3 answers

Shunting-yard algorithm in c++

I need a function that takes an infix string (like "3 + 4 * 9"), and convert it to postfix (like "4 9 * 3 +"). I got it working until you throw in parentheses within parentheses. I've been working on it all day and can't figure out what I'm doing…
user1311736
  • 83
  • 2
  • 2
  • 7
2
votes
0 answers

Handling Parentheses with the Shunting Yard Algorithm using C++

I am currently writing a shunting yard algorithm implementation. I am having trouble with handling parentheses. My current method trying to check if it's seen a token with lexeme ")" in it's iteration through the operator stack, and if it has, it…
can house
  • 41
  • 2
2
votes
1 answer

Shunting-yard Algorithm

I'm working on implementing an infix-calculator in Clojure, which starts with me implementing Dijkstra's Shunting-yard Algorithm. I thought I had it down pretty well, but joke's on me, it doesn't seem to handle operators very well at all. Calling…
Andy
  • 3,132
  • 4
  • 36
  • 68
2
votes
2 answers

Haskell Function Returning Empty List

I'm really an absolute newbie at Haskell, so I'm at a total loss as to how to debug some functions I wrote. When I call shuntingYard ["3+4"] I get back [], whereas I want to get back [34+]. Any and all help would be greatly, greatly…
Andy
  • 3,132
  • 4
  • 36
  • 68
1
2
3
8 9