3 ms·
I usually use re2c or yacc with lex. Return some object from the parser for each token type and put it in a tree node. With re2c the code is quite clean with i
by rightbyte 8y ago
I usually use re2c or yacc with lex. Return some object from the parser for each token type and put it in a tree node.
With re2c the code is quite clean with inlined reg ex:s and logic in tagged comments.
These function chaining approaches gets out of hand for any non trivial problem you could have solved with just writing some loops anyway by hand.
How are you supposed to read code like this example with all these parentheses.
I wonder how the compiler optimizes the chains. You would want the char literals to be immediate literals in the machine code and I am not too sure the compiler are able to produce that from this. I also wonder about stack depth.
- intertextuality 8y ago> How are you supposed to read code like this example with all these parentheses. Once you get to the end, the examples are quite readable. The initial implementation was left in to demonstrate an iterative approach. If you look Pom, which is an actual crate that uses these sort of parser combinators, the code is quite readable also. [0]: https://github.com/J-F-Liu/pom/blob/master/src/parser.rs https://github.com/J-F-Liu/pom/blob/master/src/parser.rs [1]: https://github.com/J-F-Liu/pom/blob/master/examples/json.rs https://github.com/J-F-Liu/pom/blob/master/examples/json.rs