TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Parsing command line arguments using a finite state machine and backtracking

43 pointsby elasticdogover 10 years ago

4 comments

userbinatorover 10 years ago
That was interesting to read and certainly a different approach to solving a problem, but the biggest question I had in my mind while reading the article was &quot;is it really necessary to create all this complexity --- and how does it compare to the traditional getopt()?&quot;<p>I&#x27;ve used getopt() many times, even implemented its functionality for fun, and parsing commandline arguments has never been something I thought was particularly difficult or complex.
评论 #8967338 未加载
评论 #8967528 未加载
评论 #8968322 未加载
fintlerover 10 years ago
I ended up writing a blog post on how to handle CP arguments a while back. I started coding before really thinking about it -- I mean, it&#x27;s CP, how hard could it be (famous last words)?<p><a href="http://blog.typeobject.com/thinking-out-loud-file-copy-tool-arguments" rel="nofollow">http:&#x2F;&#x2F;blog.typeobject.com&#x2F;thinking-out-loud-file-copy-tool-...</a><p>It turns out the arguments to CP are really tough to reason about if you just naively dive into it. The approach in this article is much better than mine. It feels like I was fumbling around in the dark.<p>If you&#x27;re interested in seeing my implementation (it&#x27;s buggy, but handles most cases), it can be found at:<p><a href="https://github.com/hpc/dcp/blob/master/src/handle_args.c" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;hpc&#x2F;dcp&#x2F;blob&#x2F;master&#x2F;src&#x2F;handle_args.c</a><p>The entry point to handle_args.c is DCOPY_parse_path_args, which is called by the main() in:<p><a href="https://github.com/hpc/dcp/blob/master/src/dcp.c" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;hpc&#x2F;dcp&#x2F;blob&#x2F;master&#x2F;src&#x2F;dcp.c</a>
评论 #8967556 未加载
nine_kover 10 years ago
Why write an FSM and backtracking explicitly? Use parser combinators, they basically do the same thing, just express it in a clearer, grammar-friendly way: <a href="http://theorangeduck.com/page/you-could-have-invented-parser-combinators" rel="nofollow">http:&#x2F;&#x2F;theorangeduck.com&#x2F;page&#x2F;you-could-have-invented-parser...</a>
评论 #8967468 未加载
TheLoneWolflingover 10 years ago
How does this avoid the exponential-time worst-case complexity of backtracking approaches to walking NFAs?