peg and ANTLR are tools that generate parsers from grammar files. This program translates the syntax from pointlander/peg-style grammar to antlr g4 style.
go run ./ peg_input.peg [antlr_output]
Creates antlr_outputLexer.g4
and antlr_outputLexer.g4
- The peg repository has a grammar for the peg language. I used that to generate a parser for .peg files.
- Parse the peg file, turning it into an AST
- Traverse the AST and index the tokens used in the peg grammar file so we can know what Lexer definitions we need.
- Compile the AST into a ANTLR-style grammar file using various rules.
- For example, grammar rule definitions in peg are
label <- expression
while in ANTLR it'slabel: expression
. This can easily be translated by switching out the symbol. - This rules can be more advanced, such as how it handles regex. ANTLR's regex engine has less features than peg's, so it requires a little more elbow grease.
- For example, grammar rule definitions in peg are
peg_grammar.peg
, the grammar file used to parse .peg files, is shamelessly stolen from the pointlander/peg repo