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
2
votes
1 answer

Modify the Shunting Yard Algorithm to include a wall operator |

How would the standard Shunting Yard Algorithm be modified to include a 'wall' symbol, |, representing the end of the function parameters? That is, to support a modification of postfix notation (Reverse Polish Notation) which allows functions with…
bitsmcgee77
  • 391
  • 3
  • 10
2
votes
1 answer

Parse propositional logic string

I have a propositional logic formula ((a or b) and !d) or e -> c How is it possible to parse this string, so I can make a truth tree? I guess I should split my string by ->, and and or, but it will mess up the parentheses. How do I protect each…
Jamgreen
  • 10,329
  • 29
  • 113
  • 224
2
votes
1 answer

Shunting-Yard VS Recursive Descent Parser

I am building an advanced mathematical parser and would like to know the difference between Shunting-Yard and the other available parser algorithms like "Descent Parser" knowing that I prefer to store the formula in RPN notation. Thanks in advance,
2
votes
0 answers

Unary operators, binary expression tree and shunting yard algorithm

I am writing the mathematics expression solver which takes infix expression and solves them, both binary expression tree and shunting-yard are doing good for me (I have even solved the problem of handling unary and ternary operators). Encountered an…
A.B.
  • 1,554
  • 1
  • 14
  • 21
2
votes
1 answer

Postfix to infix with unary/binary operators

I am trying to make a converter from postfix to infix notation and need some help. There is already a question about infix-to-postfix conversion, which gives an example I am failing to convert back. (Note: a minus sign is missing there!) The…
Andrei
  • 10,918
  • 12
  • 76
  • 110
2
votes
1 answer

complicated expressions in Shunting-yard algorithm causing calculator error

I have implemented the shunting-yard algorithm as can be seen here: #!/usr/bin/env python import sys import string import operator import signal class InvalidStackError(Exception): pass class BadParenError(Exception): pass def hook_ctrl_c(signal,…
DTSCode
  • 1,062
  • 9
  • 24
2
votes
0 answers

Shunting-yard Algorithm with functions debugging

I want to implement "functions" in the shunting-yard algorithm beside operators and make a little interpreter from the resulting algorithm but syntactic incorrect usage of tokens is ignored by the default algorithm. is there anyone that has written…
Tom
  • 221
  • 1
  • 4
  • 16
2
votes
1 answer

Trouble with the Shunting Yard Algorithm

EDIT: Complete code with interactivity: http://jsfiddle.net/LDEGe/2/ I am an introductory CS student in high school, and as a side project unrelated to the class, I'm trying to create a simple math equation parser using the Shunting-Yard Algorithm.…
scrblnrd3
  • 7,228
  • 9
  • 33
  • 64
2
votes
2 answers

Get negative numbers from expression

I'm trying to separate the tokens on a string expression. The expression looks like this: -1-2+-3 This is the regex I'm using: [\d\.]+|[-][\d\.]+|\+|\-|\*|\/|\^|\(|\) This brings me these matches: -1 -2 + -3 I was expecting: -1 - 2 + -3 Any…
robert
  • 609
  • 6
  • 14
2
votes
2 answers

Is there a simple way I can tokenize a string without a full-blown lexer?

I'm looking to implement the Shunting-yard Algorithm, but I need some help figuring out what the best way to split up a string into its tokens is. If you notice, the first step of the algorithm is "read a token." This isn't exactly a non-trivial…
KingNestor
  • 65,976
  • 51
  • 121
  • 152
2
votes
2 answers

shunting yard algorithm with trigonometric functions

I am working on implementing shunting yard algorithm in C#. Although it parses mathematical expressions with symbols( + , * - / and ^) pretty well.But for some reason it is not working for sine cosine functions . Like for example if I try to…
InspiredCoder
  • 394
  • 6
  • 22
1
vote
2 answers

Unary negation ignored in user input in my pemdas calculator using python

I'm having issues with my PEMDAS calculator built in python. The issue that I'm encountering is mainly that I can't find a way to make my code recognize the "-" sign at the beginning of the user's input. So when a user submits an input like -5+5 ,…
Mave360
  • 11
  • 3
1
vote
2 answers

cannot convert from 'void' to 'Token' (C++)

Edit: Added token struct/enum to code block I'm new to c++, so forgive me if I missed something obvious. I'm trying to write a c++ version of the Shunting Yard Algorithm, but it won't compile because it gives me the error: "cannot convert from…
GregoryComer
  • 740
  • 8
  • 18
1
vote
1 answer

infix to postfix converstion in C with multiple digits input

What I'm trying to obtain is a calculator that will take infix notation, ignore insignificant whitespace characters like " " or '@', then convert that infix notaion into postfix notation and do simple calculations like addition, subtraction etc. So…
1
vote
2 answers

Adding unary operator to shunting yard algorithm

I am creating a calculator-type program that parses an input into postfix notation and then evaluates the expression. It works for +,-,*,/, and ^, but I cannot get the unary - to work. Currently I am just trying to get the unary - to work at the…
Defqon
  • 117
  • 11
1 2
3
8 9