I’m looking for a bibtex grammar in ANTLR to use in a hobby project. I don’t want to spend my time for writing ANTLR grammar (this may take some time for me because it will involve a learning curve). So I’d appreciate for any pointers.
Note: I’ve found bibtex grammars for bison and yacc but couldn’t find any for antlr.
Edit: As Bart pointed the I don’t need to parse the preambles and tex in the quoted strings.
Here’s a (very) rudimentary BibTex grammar that emits an AST (contrary to a simple parse tree):
(if you don’t want the AST, remove all
->and everything to the right of it and remove both theoptions{...}andtokens{...}blocks)which can be tested with the following class:
and the following example Bib-input (file:
test.bib):Run the demo
If you now generate a parser & lexer from the grammar:
and compile all
.javasource files:and finally run the
Mainclass:*nix/MacOS
Windows
You’ll see some output on your console which corresponds to the following AST:
(click the image to enlarge it, generated with graphviz-dev.appspot.com)
To emphasize: I did not properly test the grammar! I wrote it a while back and never really used it in any project.