mangle icon indicating copy to clipboard operation
mangle copied to clipboard

Grammar railroad diagram

Open mingodad opened this issue 3 years ago • 7 comments

Using some online tools like https://www.bottlecaps.de/rr/ui and https://www.bottlecaps.de/convert/ we can have a nice navigable railroad diagram.

Copy and paste the EBNF shown bellow on https://www.bottlecaps.de/rr/ui on the tab Edit Grammar the click on the tab View Diagram to see/download a navigable railroad diagram.

/* converted on Mon Dec 12, 2022, 11:54 (UTC+01) by antlr_4-to-w3c v0.62 which is Copyright (c) 2011-2022 by Gunther Rademacher <[email protected]> */

start    ::= program EOF
program  ::= packageDecl? useDecl* ( decl | clause )*
packageDecl
         ::= 'Package' NAME atoms? '!'
useDecl  ::= 'Use' NAME atoms? '!'
decl     ::= 'Decl' atom descrBlock? boundsBlock* constraintsBlock? '.'
descrBlock
         ::= 'descr' atoms
boundsBlock
         ::= 'bound' '[' term ( ',' term )* ']'
constraintsBlock
         ::= 'inclusion' atoms
clause   ::= atom ( ':-' clauseBody )? '.'
clauseBody
         ::= literalOrFml ( ',' literalOrFml )* ( '|>' transform )?
transform
         ::= ( 'do' term | letStmt ) ( ',' letStmt )*
letStmt  ::= 'let' VARIABLE '=' term
literalOrFml
         ::= ( term ( '=' | '!=' | '<' | '<=' ) | '!' )? term
term     ::= VARIABLE
           | CONSTANT
           | NUMBER
           | STRING
           | NAME '(' ( term ( ',' term )* )? ')'
           | '[' ( ( ( term ',' )* | term ':' ( term ',' term ':' )* ) term )? ']'
           | '{' ( term ':' term ( ',' term ':' term )* )? '}'
atom     ::= term
atoms    ::= '[' ( atom ( ',' atom )* )? ']'
_        ::= WHITESPACE
           | COMMENT
          /* ws: definition */

<?TOKENS?>

WHITESPACE
         ::= [ #x9#xD#xA#xC]+
COMMENT  ::= '#' [^#xA]*
NEWLINE  ::= #xA
LETTER   ::= [A-Za-z]
DIGIT    ::= [0-9]
NUMBER   ::= '-'? DIGIT+
VARIABLE_START
         ::= [A-Z]
VARIABLE_CHAR
         ::= LETTER
           | DIGIT
VARIABLE ::= '_'
           | VARIABLE_START VARIABLE_CHAR*
NAME_CHAR
         ::= LETTER
           | DIGIT
           | ':'
           | '_'
NAME     ::= ':'? [a-z] ( '.'? NAME_CHAR )*
CONSTANT_CHAR
         ::= LETTER
           | DIGIT
           | '.'
           | '-'
           | '_'
           | '~'
           | '%'
CONSTANT ::= ( '/' CONSTANT_CHAR+ )+
STRING   ::= SHORT_STRING
           | LONG_STRING
SHORT_STRING
         ::= "'" ( STRING_ESCAPE_SEQ | [^\'#xd#xa#xc] )* "'"
           | '"' ( STRING_ESCAPE_SEQ | [^\"#xd#xa#xc] )* '"'
LONG_STRING
         ::= '`' LONG_STRING_ITEM* '`'
LONG_STRING_ITEM
         ::= LONG_STRING_CHAR
           | STRING_ESCAPE_SEQ
LONG_STRING_CHAR
         ::= [^-]
STRING_ESCAPE_SEQ
         ::= '\' ( . | NEWLINE )
EOF      ::= $

mingodad avatar Nov 27 '22 09:11 mingodad

Thanks for the suggestion! These will look nice in our documentation. Keeping this issue open as as a reminder.

I do think some parts of the grammar may still change on our way to 1.0 release, but others should be stable.

burakemir avatar Nov 27 '22 10:11 burakemir

@mingodad @burakemir, there are railroad generation tools for ANTLR. If you can let me know which directory you want them in, I can add a script for generating them / Just insert the railroad diagrams.

rkrishnasanka avatar Nov 30 '22 23:11 rkrishnasanka

Let's maybe start with an svg for plain Datalog atoms / facts first, with recursion and negation? However the mangle syntax evolves (the syntax in first post is already outdated, since map and struct constants were added), one needs to understand this core first. (edit: formatting)

clause ::= atom (':-' clauseBody)? '.'
clauseBody ::= literal (',' literal)* 
literal ::= atom | '!' atom
atom ::=  predicateName '(' varOrConstant (',' varOrConstant)*  ')' 
varOrConstant  ::= variable | constant

burakemir avatar Dec 12 '22 10:12 burakemir

It's on a matter to pass https://raw.githubusercontent.com/google/mangle/main/parse/gen/Mangle.g4 through https://www.bottlecaps.de/convert/ and then click View Diagram as I just did and updated the EBNF from the first post https://github.com/google/mangle/issues/3#issue-1465384133

mingodad avatar Dec 12 '22 10:12 mingodad