antlr / grammars-v4

Grammars written for ANTLR v4; expectation that the grammars are free of actions.
MIT License
10.15k stars 3.7k forks source link

Please add grammar for Graphviz/gvpr #3803

Open steveroush opened 11 months ago

steveroush commented 11 months ago

roughly defined here: https://www.graphviz.org/pdf/gvpr.1.pdf

kaby76 commented 11 months ago

The implementation of gvpr, and the grammar for it, is spread across two different codes. (1) A parser for the top-level syntax of the language is here. The "man-page" describes a grammar, but if you want to know exactly what the recursive top-down parser implements, read parseProg() function. "Actions" in the grammar are parsed as a block of text. Further parsing of that text is done elsewhere! parseBracket() simply creates a token for the entire action, and that is parsed/compiled in another function called compile(). (2) The parsing of an "action" is done via a Bison parser. That grammar is here. The Bison (yacc) grammar can be converted easily to an Antlr grammar using this script, which uses the Trash toolkit. The only difficulty with the grammar is understanding that the lexer for that Bison grammar is parse context specific. So, a number of tokens from the lexer are just ID's, but with a different token type (ARRAY, DECLARE, DYNAMIC, FUNCTION, LABEL).

kaby76 commented 11 months ago

@steveroush If you can, please check out the grammar for a PR at https://github.com/kaby76/grammars-v4/tree/35f00edee27b4080a803df5b1ed34662db226d64/gvpr . This grammar is derived from the C-source code for gvpr graphviz. I fished around for some interesting examples (some are pretty interesting, such as reachable.gvpr and tsort.gvpr). I had no idea about gvpr, but it is a really nice tool.

The grammar was derived from the implementation, but I had to modify the Bison grammar to parse statements with an optional ; at the end of a statement_list in a block { ... }. I couldn't get the gvpr C-source code to build in Windows and use a good debugger to find out how and/or why what the Bison parser is doing. I do plan to use the try and true "printf" method on linux to figure this out. It just doesn't seem possible the Bison grammar is correct, but gvpr parses fine (except for one which is a regression test for flagging a parse error) all the examples I added to test the grammar.

If you can pass me other examples, that would be helpful.

steveroush commented 11 months ago

Wow! I am overwhelmed with the fast turn-around.
Yes, gvpr is a really nice tool. An interesting mixture of awk, C, the innards of the dot language, and a mish-mash of Bell Labs Research internal goodies. (I was not involved)

It will take me a few days to a few weeks to test the grammar. (I am even older than you, rusty, have no experience with antlr, and I have no real experience w/ "compiler writing" in this century.)

Here (https://gitlab.com/graphviz/graphviz/-/tree/main/cmd/gvpr/lib) is a pointer to 38 gvpr programs that are buried in the Graphviz repository. They are small-to-mid sized (for gvpr programs), and (unfortunately?) all coded by one or two individuals (not me) so only one or two coding styles.

If you search stackoverflow for gvpr (https://stackoverflow.com/search?tab=newest&q=gvpr&searchOn=3) you will find more small-to-mid sized examples, some written by me.

And here (https://gist.github.com/steveroush/60d9a850a545ec4e02b0482c7bac3ad5) is a pointer to a shell archive that contains a somewhat longer gvpr program and some test files.

I have 100-ish other gvpr programs if you want any specific coverage help.

thanks again, steve roush