Live data from Hacker News

Parsers don't have to be complicated

bkaradzic.github.io

21–30 of 77 posts

Re: Parsers don't have to be complicated

#21
post #4

Unfortunately, simple URL parsing breaks on so many things. There is a reason on why every URL parsing library is at least a few thousand LOCs. One common way to test it is just to pass ipv6 url: http://[f021:d981:b487:e57d:193e:550e::]/

Is that so?

RFC 3986 Appendix B [1] "Parsing a URI Reference with a Regular Expression":

The following line is the regular expression for breaking-down a well-formed URI reference into its components.

  ^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?

      scheme    = $2
      authority = $4
      path      = $5
      query     = $7
      fragment  = $9

Let's test your URI with this regex, shall we? [2]

  $2 (scheme) = http
  $4 (authority) = [f021:d981:b487:e57d:193e:550e::]
  $5 (path) = /
Seems correct to me.

[1] https://datatracker.ietf.org/doc/html/rfc3986#appendix-B

[2] https://regexr.com/8nqop

Re: Parsers don't have to be complicated

#23
post #14

If you created a format that is so difficult to parse that it cannot be parsed with simple readable C code then the problem is the format not the parser code.

Why care about lang which doesnt really support strings well?

What do you think the libraries you use to parse these things are doing under the hood?

Maybe you don't care? Fair enough.

Re: Parsers don't have to be complicated

#24

This post doesn't touch on something that makes parsers complicated no matter how simple the grammar: good error messages. Parsing a well formed input is the easy part, but not just spitting out a byte index but actually telling the user why their input is not good and what they could do to make it conform is super hard. The Rust compiler is a common example of a compiler that does a good job here, and I think it is…

I will provide some context from having done a lot of that work.

The Rust grammar is actually quite regular, that's why we have things like the turbofish for type parameters (`binding.method::()`): it makes the grammar unambiguous (a naïve parser would with a complicated grammar that accepts chained comparisons would have to deal with differentiating between `binding.method ()` and `binding.method()`). But that doesn't mean the rustc parser doesn't do the work of supporting some the more complex grammar in order to provide better diagnostics. I like to say that rustc actually knows about meta-Rust, a daughter language that goes crazier in its features. I also joke that rustc isn't done until you can paste code from another language and following the suggestions you end up with valid Rust code without loss of the user's intent.

Part of the problem is that the places where incorrect code can fail is in more places than the parser. The chained comparisons example is one that is easy for Rust (as it doesn't support them), so the parser itself can produce a "missing turbofish" suggestion with high certainty, but for truly ambiguous expressions, the errors will happen later, during name resolution ("expected a value and found a type") or when checking the number of arguments. A production compiler needs to account for not only the original error, but also silence every knock-down error too. The simplest strategies are to just stop if at the end of a given stage there are errors (which leads to the "wave of errors" experience of fixing the "last" error leading to a ton of new ones) or fully replacing entire blocks of code that had a parse error with an AST node that acts as a tombstone marking that that later stages need to ignore it. The first option leads to a bad experience, and the latter is insufficient. A recent example of looking at this is https://github.com/rust-lang/rust/pull/159689, where `Arc::new(RwLock::new(HashMap::default()));` currently produces

  error[E0423]: expected value, found struct `HashMap`
    --> $DIR/suggest-turbofish-parsed-as-comparisons.rs:11:34
     |
  LL |     let _ = Arc::new(RwLock::new(HashMap::default()));
     |                                  ^^^^^^^
     |
    --> $SRC_DIR/std/src/collections/hash/map.rs:LL:COL
    ::: $SRC_DIR/std/src/collections/hash/map.rs:LL:COL
     |
     = note: `HashMap` defined here
  
  error[E0423]: expected value, found builtin type `i32`
    --> $DIR/suggest-turbofish-parsed-as-comparisons.rs:11:42
     |
  LL |     let _ = Arc::new(RwLock::new(HashMap::default()));
     |                                          ^^^ not a value
  
  error[E0423]: expected value, found builtin type `i64`
    --> $DIR/suggest-turbofish-parsed-as-comparisons.rs:11:47
     |
  LL |     let _ = Arc::new(RwLock::new(HashMap::default()));
     |                                               ^^^ not a value
  
  error[E0425]: cannot find external crate `default` in the crate root
    --> $DIR/suggest-turbofish-parsed-as-comparisons.rs:11:53
     |
  LL |     let _ = Arc::new(RwLock::new(HashMap::default()));
     |                                                     ^^^^^^^ not found in the crate root
  
  error[E0061]: this function takes 1 argument but 2 arguments were supplied
    --> $DIR/suggest-turbofish-parsed-as-comparisons.rs:11:22
     |
  LL |     let _ = Arc::new(RwLock::new(HashMap::default()));
     |                      ^^^^^^^^^^^              --------------- unexpected argument #2 of type `bool`
     |
  note: associated function defined here
    --> $SRC_DIR/std/src/sync/poison/rwlock.rs:LL:COL
  help: remove the extra argument
     |
  LL -     let _ = Arc::new(RwLock::new(HashMap::default()));
  LL +     let _ = Arc::new(RwLock::new(HashMap
This is because the expression is syntactically correct as

  RwLock::new( HashMap  ::default() );
  ^^^^^^^^^^^^ ------- - ---^ --- - ----------- ^
  |            |       | |  | |   | |
  |            |       | |  | |   | a function call to `default` in the crate root
  |            |       | |  | |   a more than binop
  |            |       | |  | a value to be compared
  |            |       | |  the separator of the second argument to `RwLock::new()`
  |            |       | a value to be compared
  |            |       a less than binop
  |            a value to be compared
  an associated function call
 
but after that PR it would only be the following, even though the parser hasn't changed:

  error: can't compare two types
    --> $DIR/suggest-turbofish-parsed-as-comparisons.rs:24:41
     |
  LL |     let _ = Arc::new(RwLock::new(HashMap::default()));
     |                                         ^        ^ these are parsed as "less than" and "greater than"
     |
  help: you likely intended to write type `HashMap` with type parameters, but type parameters in expression contexts require the use of the "turbofish" `::`
     |
  LL |     let _ = Arc::new(RwLock::new(HashMap::::default()));
     |                                         ++
I think that there's a lot of work needed in the parser itself to produce good diagnostics. There are other strategies, like performing multiple parses at a given point when you've reached a known bad state (you've seen a flag-post that shouldn't be there, but that is a signal for a handful of other known cases), or fully consuming the rest of a block when an unrecoverable parse occurred (we're half-way through parsing function arguments, but failed? consume the rest of the statement or of the parent block, accounting for sub-scopes). The latter can cause the rest of the file to be consumed, but that's an edge-case that in practice is much better than a deluge of irrelevant errors.

Another added complexity is how some easy-to-hit errors occur during lexing, which means the compiler has barely any information about the user's code. Mismatched braces/parens is one of those. rustc tries to provide context by keeping a queue of seen open delimiters to point at, and explicitly checking for their indentation level as a heuristic to detect where the user's intent diverged from the code, but that's overly reliant on the code being sanely formatted (thanks to rustfmt-on-save, that's a good bet for many users). For an example of the things rustc can do even in the lexer, you can look at https://github.com/rust-lang/rust/pull/160592.

Re: Parsers don't have to be complicated

#25

This post doesn't touch on something that makes parsers complicated no matter how simple the grammar: good error messages. Parsing a well formed input is the easy part, but not just spitting out a byte index but actually telling the user why their input is not good and what they could do to make it conform is super hard. The Rust compiler is a common example of a compiler that does a good job here, and I think it is…

It actually does touch on this:

Built-in line and column tracking. Any movement across a newline updates the line number, including a backwards seek. getLine and getColumn are always available and both are one-based, which makes decent error messages nearly free.

That doesn't sound like much, but having hand-written plenty of recursive descent parsers, it's most of what you need for good error messages. Just being able to pinpoint where the error occurred is usually 80% of the battle; but keeping track of lines and columns in a hand-written parser is a pain.

Sure, for something like Rust, you need vastly more than that, but parsing is a tiny fraction of what the Rust compiler is doing -- type-checking and borrow checking is much more complicated and much more important.

A tiny library like this is a great fit for something like an INI file parser.

Re: Parsers don't have to be complicated

#26

The hardest thing about writing a parser is cognitively accepting what is going to be considered valid input. You can make the best parser that is fast and well specified but invariably someone will (ab)use it in an unexpected way. Famous examples: despite so many initial good intentions, html tags don’t need to be closed, JSON numbers are too often encoded as strings, YAML can look like what most people expect or it…

I think the second-hardest thing is to accept that CS spent decades optimizing parsing algorithms and grammars, and this is still a significant part of CS curricula in many places. But the practical reality is that parsing is almost never a bottleneck. If what you're parsing is within the capacity of humans to interact with (so in the range of tens of kilobytes), a grammar that requires an O(N^2) parser is totally fi…

I don't think it is difficult to accept that fundamentals should be taught.

We spend years learning basic arithmetic like the addition of integers. You could very well argue that there is no need for that either because everyone has a calculator app on their phone. This is how dark ages begin.

Re: Parsers don't have to be complicated

#27
post #17

Earlier quoted context omitted.

I think the second-hardest thing is to accept that CS spent decades optimizing parsing algorithms and grammars, and this is still a significant part of CS curricula in many places. But the practical reality is that parsing is almost never a bottleneck. If what you're parsing is within the capacity of humans to interact with (so in the range of tens of kilobytes), a grammar that requires an O(N^2) parser is totally fi…

An O(n^2) parser is not fine for the mere reason that I don't know how one would make such a mess of the job in the first place. A simple recursive-descent parser is easy to write by hand and runs in linear time.

I think the point was that even if you managed to make a O(n*2) parser it will ve fast enough for human entered problems.

Re: Parsers don't have to be complicated

#28
post #10
post #6

Earlier quoted context omitted.

But the preceding clause says it handles \r\n. If you're already handling \r\n, what remaining sources of \r are there, that you'd actually want to silently ignore?

Someone else (possibly you) already split on \n.

Fair point. I think if something is getting mangled like that I'd rather fail loudly, but it depends on the use case I suppose.

Re: Parsers don't have to be complicated

#29

This post doesn't touch on something that makes parsers complicated no matter how simple the grammar: good error messages. Parsing a well formed input is the easy part, but not just spitting out a byte index but actually telling the user why their input is not good and what they could do to make it conform is super hard. The Rust compiler is a common example of a compiler that does a good job here, and I think it is…

It actually does touch on this: Built-in line and column tracking. Any movement across a newline updates the line number, including a backwards seek. getLine and getColumn are always available and both are one-based, which makes decent error messages nearly free. That doesn't sound like much, but having hand-written plenty of recursive descent parsers, it's most of what you need for good error messages. Just being ab…

I don't really agree. Many top-down parsers find an error at an unexpected token. That token is often not the error. Quite often something is missing at that point, or there has been a mistake some way back. Translating e.g. "unexpected semicolon" into "keyword 'if' should be the identifier 'f'" is not easy.

Re: Parsers don't have to be complicated

#30
post #4

Unfortunately, simple URL parsing breaks on so many things. There is a reason on why every URL parsing library is at least a few thousand LOCs. One common way to test it is just to pass ipv6 url: http://[f021:d981:b487:e57d:193e:550e::]/

Is that so? RFC 3986 Appendix B [1] "Parsing a URI Reference with a Regular Expression": The following line is the regular expression for breaking-down a well-formed URI reference into its components. ^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))? scheme = $2 authority = $4 path = $5 query = $7 fragment = $9 Let's test your URI with this regex, shall we? [2] $2 (scheme) = http $4 (authority) = [f021:d981:b…

Hm, but I think he's right. The problem comes when you try to break down the authority portion into host and port; TFA's parser treats the first colon as introducing the port, which is wrong.

https://github.com/bkaradzic/bx/blob/0b001f5f36579e8aea07efa...

Post reply on HN