PackCC


PackCC is a parser generator for C. Its main features are as follows:
The grammar of an output parser can be described in a PEG. The PEG is a top-down parsing language, and is similar to the regular expression grammar. Compared with a bottom-up parsing language, like Yacc's one, the PEG is much more intuitive and cannot be ambiguous. The PEG does not require tokenization to be a separate step, and tokenization rules can be written in the same way as any other grammar rules.
The generated parser can parse inputs very efficiently by packrat parsing. The packrat parsing is the recursive descent parsing algorithm that is accelerated using memoization. By using packrat parsing, any input can be parsed in linear time. Without it, however, the resulting parser could exhibit exponential time performance in the worst case due to the unlimited look-ahead capability.
Unlike common packrat parsers, PackCC can support direct and indirect left-recursive grammar rules. This makes grammar rules much more intuitive.
The generated code is beautified and as ease-of-understanding as possible. Actually, it uses lots of goto statements, but the control flows are much more traceable than goto spaghetti storms generated by some other parser generators.
PackCC itself is under MIT license, but the generated code can be distributed under any license or can be used in proprietary software.

Input file example

A desktop calculator. Note that left-recursive grammar rules are included.

%prefix "calc"
statement <- _ e:expression _ EOL
/ * EOL
expression <- e:term
term <- l:term _ '+' _ r:factor
/ l:term _ '-' _ r:factor
/ e:factor
factor <- l:factor _ '*' _ r:unary
/ l:factor _ '/' _ r:unary
/ e:unary
unary <- '+' _ e:unary
/ '-' _ e:unary
/ e:primary
primary <- < + >
/ ''
_ <- *
EOL <- '\n' / '\r\n' / '\r' / ';'
%%
int main