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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Learning Parser Combinators with Rust

289 点作者 chwolfe大约 6 年前

11 条评论

YeGoblynQueenne大约 6 年前
&gt;&gt; The novice programmer will ask, &quot;what is a parser?&quot;<p>&gt;&gt; The intermediate programmer will say, &quot;that&#x27;s easy, I&#x27;ll write a regular expression.&quot;<p>&gt;&gt; The master programmer will say, &quot;stand back, I know lex and yacc.&quot;<p>The Prolog programmer will write a Definite Clause Grammar [1], which is both a grammar and a parser, two-in-one. So you only have to do the easy and fun bit of writing a parser, which is defining the grammar.<p>Leaves plenty of time to get online and brag about the awesome power of Prolog or get embroiled in flamewars with functional programming folks [2].<p>______________<p>[1] <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Definite_clause_grammar" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Definite_clause_grammar</a><p>[2] Actually, DCGs are kiiind of like parser combinators. Ish. In the sense that they&#x27;re executable grammars. But in Prolog you can run your programs backwards so your DCG is both a recogniser <i>and</i> a generator.
评论 #19703780 未加载
评论 #19712033 未加载
intertextuality大约 6 年前
On the reddit discussion of this [0], someone mentioned using a type of<p>fn parse(&amp;self, input: &amp;mut &amp;str) -&gt; Option&lt;Output&gt;<p>instead of the article&#x27;s<p>fn parse(&amp;self, input: &amp;&#x27;a str) -&gt; Result&lt;(&amp;&#x27;a str, Output), &amp;&#x27;a str&gt;<p>for composability. I found the article fascinating and plan on going back to see what an xml parsing implementation based on the former would act like.<p>[0]: <a href="https:&#x2F;&#x2F;www.reddit.com&#x2F;r&#x2F;rust&#x2F;comments&#x2F;bepi63&#x2F;learning_parser_combinators_with_rust&#x2F;" rel="nofollow">https:&#x2F;&#x2F;www.reddit.com&#x2F;r&#x2F;rust&#x2F;comments&#x2F;bepi63&#x2F;learning_parse...</a>
评论 #19699384 未加载
xymostech大约 6 年前
This was such a wonderful read! I&#x27;ve been getting into Rust recently, and the sections on dealing with challenges that are specific to Rust were particularly useful. The way they created a new trait to turn `Fn(&amp;str) -&gt; Result&lt;(&amp;str, T), &amp;str&gt;` into `Parser&lt;T&gt;` was insightful, and the discussion of how they dealt with the growing sizes of types was something that I can imagine myself running into in the future.<p>Most importantly though, when they started writing `and_then`, my eyes lit up and I said &quot;It&#x27;s a Monad!&quot; I think this is the first time I&#x27;ve really identified a Monad out in the wild, so I enjoyed that immensely.
louthy大约 6 年前
It doesn&#x27;t <i>feel</i> very declarative in Rust. Personally, I&#x27;m finding it hard to see the intent (I haven&#x27;t written a line of Rust in my life, so take that with a pinch of salt, but I am a polyglot programmer).<p>Really, Haskell&#x27;s do notation is the big winner when it comes to parser combinators, as the direction of the flow of the parser is easy to follow, but also you can capture variables mid-flight for use later in the expression without obvious nested scope blocks.<p>It&#x27;s possible to capture variables with `and_then` by the looks of it, but any suitably complex parser will start to end up quite an ugly mess of nested scopes.<p>I ported Haskell&#x27;s Parsec to C# [1], it has LINQ which is similar to Haskell&#x27;s Do notation. Simple parsers [2] are beautifully declarative, and even complex ones, like this floating point number parser [3], are trivial to follow.<p>[1] <a href="https:&#x2F;&#x2F;github.com&#x2F;louthy&#x2F;language-ext" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;louthy&#x2F;language-ext</a><p>[2] <a href="https:&#x2F;&#x2F;github.com&#x2F;louthy&#x2F;language-ext&#x2F;blob&#x2F;master&#x2F;LanguageExt.Parsec&#x2F;Parsers&#x2F;Prim.cs#L452" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;louthy&#x2F;language-ext&#x2F;blob&#x2F;master&#x2F;LanguageE...</a><p>[2] <a href="https:&#x2F;&#x2F;github.com&#x2F;louthy&#x2F;language-ext&#x2F;blob&#x2F;master&#x2F;LanguageExt.Parsec&#x2F;Parsers&#x2F;Token.cs#L287" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;louthy&#x2F;language-ext&#x2F;blob&#x2F;master&#x2F;LanguageE...</a>
评论 #19703938 未加载
评论 #19708296 未加载
xixixao大约 6 年前
Nice article. I finally gave Rust a recently. It&#x27;s really interesting how new languages evolve, and what &quot;deficiencies&quot; they exert. The article for example uses closures, but it&#x27;s currently impossible in stable Rust to accept a closure that itself accepts a closure as an argument (while you can easily rewrite the same pattern with structs). The borrow checker could still do better on suggesting fixes to common problems (otherwise it&#x27;s actually quite elegant). What struck me while reading this was the use of assert_eq!(expected, actual), as I&#x27;ve mostly seen the other order. Sure enough I checked and the macro does not define the order. That&#x27;s unfortunate, as testing against &quot;fixed&quot; &quot;expected&quot; outcome is very common, and leads to more friendly testing devx (which in general while supported out of the box isn&#x27;t great).<p>On the other hand, Rust&#x27;s IDE support, built-in linting, is seriously impressive.
评论 #19699343 未加载
评论 #19699273 未加载
评论 #19699163 未加载
评论 #19699615 未加载
评论 #19702705 未加载
评论 #19699356 未加载
norswap大约 6 年前
If someone wants to have a look at the code of a cutting-edge parser combinator framework with focus on features + usability, I&#x27;ll plug this here (it&#x27;s in Java)<p><a href="https:&#x2F;&#x2F;github.com&#x2F;norswap&#x2F;autumn4" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;norswap&#x2F;autumn4</a><p>WIP but 1.0.0 will land somewhere within the next two months, with a full user-guide (half of it is already written and available).<p>Constructive feedback welcome!
评论 #19703515 未加载
tiuPapa大约 6 年前
Okay, I am interested in this topic. Does anyone know of any good resources for exploring parser combinators further?
评论 #19701930 未加载
amelius大约 6 年前
What is the class of languages that can be parsed with such parsers, in the sense of [1]?<p>[1] <a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Context-free_grammar#Subclasses" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Context-free_grammar#Subclasse...</a>
评论 #19700626 未加载
lelf大约 6 年前
<a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=19694793" rel="nofollow">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=19694793</a>
k0t0n0大约 6 年前
nice read; Hi I also wrote a SQL dump parser using rust here the code.<p>&gt; <a href="https:&#x2F;&#x2F;github.com&#x2F;ooooak&#x2F;sql-split" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;ooooak&#x2F;sql-split</a>
vmchale大约 6 年前
I don&#x27;t like Rust for this purposes. It doesn&#x27;t have higher-kinded types and thus no applicatives or monads, which sort of misses the point.<p>I also object to the idea that parser combinators are an alternative to parser generators. They&#x27;re each useful in different scenarios. But for something like XML the parser combinators will be slower.<p>I&#x27;d also be curious to see how the efficiency of parser combinators is affected by the absence of laziness in Rust. I seem to recall that laziness makes the analysis more complicated than you&#x27;d expect, but I need to find a source...
评论 #19708111 未加载