←back to thread

429 points rui314 | 3 comments | | HN request time: 0.002s | source
Show context
peter303 ◴[] No.10732006[source]
Long ago UNIX had compiler writing tools like yacc and lex. I wonder if they are useful for exercises like this.
replies(6): >>10732067 #>>10732084 #>>10732219 #>>10732591 #>>10732595 #>>10732964 #
1. DSMan195276 ◴[] No.10732067[source]
For writing a general compiler (or anything similar to that), they're extremely useful because they produce very good lexers and/or parsers. The GNU versions of those two are 'bison' and 'flex'. Really, just about anything that requires parsing text can gain from using both of them.

Noting that though, for this specific exercise they're not as useful because the author intended for this compiler to be self-hosting. It would be hard to be self-hosting if the compiler have to be able to compile the C code from yacc or lex, which may do any number of strange things.

replies(1): >>10732293 #
2. vidarh ◴[] No.10732293[source]
With the huge caveat that "nobody" uses these tools for production compilers because decent error handling becomes a nightmare.

There are exceptions, but if you dig into most larger compilers, they usually sooner or later end up adopting their own handwritten recursive descent parsers.

replies(1): >>10733910 #
3. DSMan195276 ◴[] No.10733910[source]
That's a fair point. Those tools have their uses, definitely - for a 'toy' compiler like this one, a simple lex lexer or yacc parser would/could be sufficient. Once you get more complex they start to become a limiting factor.