Skip to content

Grammar railroad diagram #628

@mingodad

Description

@mingodad

Using this prompt with copilot:

extract an ebnf for railroad diagram generation in one file, the railroad generator uses "::=" as rule separator, no semicolon at the end of a rule, and "?+*" for repetiton instead of "[]{}" and uses C comments instead of pascal comments

I've got the EBNF shown bellow that using https://github.com/GuntherRademacher/rr can generate a nice navigable railroad diagram (see bellow with instructions at the top.

I needed to do small manual fixes and it's not complete but it's a good start point to make it reflect the whole ChaiScript language.

//
// EBNF to be viewd at
//    (IPV6) https://www.bottlecaps.de/rr/ui
//    (IPV4) https://rr.red-dove.com/ui
//
// Copy and paste this at one of the urls shown above in the 'Edit Grammar' tab
// then click the 'View Diagram' tab.
//

/* ChaiScript EBNF for railroad diagrams */

statements           ::= (def | try | if | while | class | for | switch | return | break | continue | equation | block | eol)+

def                  ::= "def" id ("::" id)? "(" decl_arg_list ")" eos* (":" operator)? eol* block
lambda               ::= "fun" ("[" id_arg_list "]" |) "(" decl_arg_list ")" eol* block
try                  ::= "try" eol* block (catch+)? (finally)?
catch                ::= "catch" ("(" arg ")")? eol* block
finally              ::= "finally" eol* block

if                   ::= "if" "(" equation (eol equation)? ")" eol* block (("else" (if | eol* block))*)
while                ::= "while" "(" operator ")" eol* block

for                  ::= "for" "(" (for_guards ")" | (equation ":" equation ")")) eol* block
for_guards           ::= (equation eol)+ (equation eol)? equation

class                ::= "class" id eol* class_block
class_block          ::= "{" class_statements* "}"
class_statements     ::= (def | var_decl) | eol

switch               ::= "switch" "(" operator ")" eol* "{" (case | default)+ "}"
case                 ::= "case" "(" operator ")" eol* block
default              ::= "default" eol* block

block                ::= "{" statements* "}"
return               ::= "return" operator?
break                ::= "break"
continue             ::= "continue"
eol                  ::= "\n" | "\r\n" | ";"

equation             ::= operator (("=" | ":=" | "+=" | "-=" | "*=" | "/=" | "%=" | "<<=" | ">>=" | "&=" | "^=" | "|=") equation)?
operator             ::= prefix | value | (operator binary_operator operator)+
prefix               ::= ("++" | "--" | "-" | "+" | "!" | "~") operator
binary_operator      ::= "?" | "||" | "&&" | "|" | "^" | "&" | "==" | "!=" | "<" | "<=" | ">" | ">=" | "<<" | ">>" | "+" | "-" | "*" | "/" | "%"

value                ::= var_decl | dot_fun_array | prefix
dot_fun_array        ::= (lambda | num | quoted_string | single_quoted_string | paren_expression | inline_container | id) (fun_call | array_call | dot_access)*
fun_call             ::= "(" arg_list ")"
array_call           ::= "[" operator "]"
dot_access           ::= "." id

var_decl             ::= ("auto" | "var") (reference | id) | "global" (reference | id) | "attr" id ("::" id)?
reference            ::= "&" id

paren_expression     ::= "(" operator ")"
inline_container     ::= "[" container_arg_list "]"
container_arg_list   ::= value_range | map_pair ("," map_pair)* | operator ("," operator)*

quoted_string        ::= '"' (char | escape | interpolation)* '"'
single_quoted_string ::= "'" (char | escape)* "'"

id_arg_list          ::= arg ("," arg)*
decl_arg_list        ::= arg ("," arg)*
arg_list             ::= equation ("," equation)*
arg                  ::= id id?    /* type allowed */

id                   ::= [a-zA-Z_] [a-zA-Z0-9_]* | "`" char+ "`"
num                  ::= hex | binary | float | int
hex                  ::= "0" ("x" | "X") [0-9a-fA-F]+ int_suffix*
binary               ::= "0" ("b" | "B") [01]+
float                ::= [0-9]+ "." [0-9]+ (("e"|"E") ("+"|"-")? [0-9]+)? float_suffix*
int                  ::= [0-9]+ int_suffix*

int_suffix           ::= "l" | "L" | "u" | "U"
float_suffix         ::= "l" | "L" | "f" | "F"

quoted_string        ::= '"' (char | escape | interpolation)* '"'
single_quoted_string ::= "'" (char | escape)* "'"

escape               ::= "\" ("'" | '"' | "?" | "\" | "a" | "b" | "f" | "n" | "r" | "t" | "v" | "$" | "x" hex_digit+ | "u" hex_digit hex_digit hex_digit hex_digit | "U" hex_digit hex_digit hex_digit hex_digit hex_digit hex_digit hex_digit hex_digit | octal_digit+)
octal_digit          ::= [0-7]
hex_digit            ::= [0-9a-fA-F]
interpolation        ::= "$" "{" equation "}"

value_range          ::= operator ".." operator
map_pair             ::= operator ":" operator

/* Utility */
eos                  ::= eol | ";"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions