I'm in the planning stage of making a code beautifier (similar to AStyle or Uncrustify) - originally I was going to just contribute to one of those projects,
but reviewing their source led me to the conclusion that I have different design goals and that their source is written in a way that makes it difficult for an outsider to easily contribute. AStyle, for example, instead of building some sort of AST, uses over 100 state variables such as isInComment
, foundClassHeader
, isLineReady
, etc.
I'm deciding between using scanner and parser generators (such as flex and bison) and writing my own parsing system. Which would be a better approach? I've been through a compilers class in university, so I do have some experience with scanning and parsing theory. Below are the advantages I've thought of for each:
Generators
- Arguably simpler/faster to develop
- Possibly more optimized than my initial stab at a custom parser, though a custom one might leave more room for improvement
Custom parser
- Arguably easier to debug
- Programmers seems to prefer custom lexers and parsers in past questions - "Anyone who wants a decent lexer doesn't use Lex"
- Fewer dependencies
- Could possibly allow for "fuzzier" parsing - since I'm just developing a beautifier, the parser wouldn't need to be nearly as strict or detailed as one for a compiler
- Parallelization - flex and bison (or what I've seen of them) use global state. If I want to parse multiple files at once (in separate threads), a custom solution would be simpler to compartmentalize