TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Shunting-yard algorithm

75 点作者 lkurusa大约 6 年前

8 条评论

chubot大约 6 年前
My blog links to some runnable Python code for this algorithm, with tests:<p><i>Code for the Shunting Yard Algorithm</i> <a href="http:&#x2F;&#x2F;www.oilshell.org&#x2F;blog&#x2F;2017&#x2F;04&#x2F;22.html" rel="nofollow">http:&#x2F;&#x2F;www.oilshell.org&#x2F;blog&#x2F;2017&#x2F;04&#x2F;22.html</a><p><a href="https:&#x2F;&#x2F;github.com&#x2F;bourguet&#x2F;operator_precedence_parsing" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;bourguet&#x2F;operator_precedence_parsing</a><p>It seems like there are 2 common algorithms for expressions: the shunting yard algorithm and Pratt parsing [1]. As best as I can tell, which one you use is a coin toss. I haven&#x27;t been able to figure out any real difference.<p>They&#x27;re both nicer than grammar-based approaches when you have many levels of precedence, like C.<p>But it doesn&#x27;t seem like there is any strict relationship between the two algorithms, which is interesting. I guess there are other places where that happens, like there being at least two unrelated algorithms for topological sort.<p>Trivia: The original C compilers used the shunting yard algorithm. IIRC the source made reference to Dijikstra.<p>[1] <a href="http:&#x2F;&#x2F;www.oilshell.org&#x2F;blog&#x2F;2017&#x2F;03&#x2F;31.html" rel="nofollow">http:&#x2F;&#x2F;www.oilshell.org&#x2F;blog&#x2F;2017&#x2F;03&#x2F;31.html</a>
评论 #19196335 未加载
cdoxsey大约 6 年前
This algorithm is great.<p>I was working on a new metric alert evaluation system and built an expression parser for things like:<p><pre><code> system.disk.free{*} &#x2F; 1024 by {host} </code></pre> The naive approach to parsing will result in the wrong order of operations, since they&#x27;ll just be done in the order they appear:<p><pre><code> x + y * z =&gt; (x + y) * z </code></pre> Rather than:<p><pre><code> x + (y * z) </code></pre> As it should be. The shunting yard algorithm will rearrange the expressions.<p>After implementing it I noticed my results were different than the reference system... and that&#x27;s when I discovered we did arithmetic wrong in the main app and had for years.<p>So I had to hard code a toggle for whether to do math properly :(. It&#x27;s a sort of Hippocratic oath when it comes to these things... first do no harm, and even if it was wrong, people were relying on the existing functionality, and changing it would likely result in sudden alerts for folks.<p>In the end we did fix it in the main app, but you always feel kind of dirty writing code like that.
评论 #19196114 未加载
评论 #19197421 未加载
rootbear大约 6 年前
I implemented this as a class assignment in Univac 1100 assembly language in the late 70s. I always thought it was a cool algorithm. I had an HP scientific calculator at the time and was a fan of RPN.
Insanity大约 6 年前
It&#x27;s a neat algorithm :) When learning Go I made an implementation of Shunting-Yard at some point: <a href="https:&#x2F;&#x2F;github.com&#x2F;DylanMeeus&#x2F;GoPlay&#x2F;blob&#x2F;master&#x2F;ExpressionParser&#x2F;Infix&#x2F;infixparser.go" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;DylanMeeus&#x2F;GoPlay&#x2F;blob&#x2F;master&#x2F;ExpressionP...</a><p>As part of something else I was trying. As I was just learning Go at that point, the code is not that clean :D But AFAIK it did work. My memory is a bit hazy on that :)
akhilcacharya大约 6 年前
This is one of those algorithms that everyone should understand or memorize. I had variations of this algorithm in the coding interviews or coding challenges of 6 companies (!) my last cycle.
评论 #19196122 未加载
bhoeting大约 6 年前
Hah, a few years ago I learned Go by building a simple programming language (super basic with Lua-like syntax) and definitely used this article to help me. I made a slight modification so it could support functions with multiple arguments. Fun times.<p><a href="https:&#x2F;&#x2F;github.com&#x2F;bhoeting&#x2F;blast&#x2F;blob&#x2F;master&#x2F;parser.go#L82" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;bhoeting&#x2F;blast&#x2F;blob&#x2F;master&#x2F;parser.go#L82</a>
IIAOPSW大约 6 年前
I was hoping for an algorithm that solves railway shunting problems. Anyone know of something that does that?<p><a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Train_shunting_puzzle" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Train_shunting_puzzle</a>
评论 #19195921 未加载
评论 #19195913 未加载
评论 #19197879 未加载
User23大约 6 年前
The original paper is a good read to get some insight into the thought process: <a href="https:&#x2F;&#x2F;www.cs.utexas.edu&#x2F;~EWD&#x2F;MCReps&#x2F;MR35.PDF" rel="nofollow">https:&#x2F;&#x2F;www.cs.utexas.edu&#x2F;~EWD&#x2F;MCReps&#x2F;MR35.PDF</a>