Hacker News new | ask | show | jobs
by haileys 113 days ago
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.

3 comments

> A simple recursive-descent parser is easy to write by hand and runs in linear time.

Recursive descenrs parsers are not linear.

They are generally O(n^2) and can even can go exponential with some grammars if written naively.

It can be pretty easy to do adverserival attacks on most naive descent parser and bring it to its knees.

Packrat parser [^1] are linear, but they are by no means "trivial 200 lines" type of parsers.

[^1]: https://arxiv.org/abs/cs/0603077

Recursive descent isnt guaranteed linear time

In the face of backtracking the time depends on the complexity of the grammar, since it's basically a brute force search through all the rules.

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.
Not for C++ code generated by whole program optimizing compilers. Your "human entered" is doing the heavy lifting. Now that AI is writing code your assertion might be on shaky ground.
C++ can be slow to compile, but as I said, parsing is not the bottleneck. Even for really huge automatically generated C++ files, or old-school concatenated "unity builds", the parsing step is generally tiny compared to everything else.
> Not for C++ code generated by whole program optimizing compilers.

I'd be quite surprised if an optimizing compiler generated C++ code somewhere in its pipeline!

Take a look at Felix.

https://felix-lang.github.io/felix/

Ignore the 'scripting' language claim.

Oh, that is certainly not what I was expecting at all. I stand corrected!

I do have to wonder though - do you know what proportion of the C++ compiler time is spent parsing your generated C++ code vs. optimizing it?

Unfortunately no.

Felix is quite old at this point. It's a very interesting language, with many interesting ideas. It did not quite take off though.

C++26 reflection?
Oh, true! That's on me for not being specific enough. I was thinking about the optimization pipeline.