Skip to content

Grammar railroad diagram #5

@mingodad

Description

@mingodad

Asking copilot to generate an EBNF understood by https://github.com/GuntherRademacher/rr to generate a nice navigable railroad diagram I've got the EBNF shown bellow (with instructions at the top).
Prompt:

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 .

The EBNF:

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

/* Compilation Unit (Top Level) */
compilation_unit ::= (import_declaration | load_declaration | type_alias_declaration | declaration_statement)+

/* Import Declaration */
import_declaration ::= "import" ( "{" STRING+ "}" | STRING )

/* Load Declaration */
load_declaration ::= "load" ( "{" STRING+ "}" | STRING )

/* Type Alias */
type_alias_declaration ::= "type" IDENTIFIER "=" type

/* Declaration Statement (Top Level Declarations) */
declaration_statement ::= function_declaration
                       | operator_function_declaration
                       | field_declaration
                       | destructuring_field_declaration
                       | compiletime_constants_declaration
                       | structure_declaration
                       | enum_declaration
                       | declarations_directive

/* Field Declaration */
field_declaration ::= "var" IDENTIFIER ( ":" type ("=" expression)? | "=" expression ) ";"

/* Destructuring Declaration */
destructuring_field_declaration ::= "var" "(" destructuring_field ("," destructuring_field)* ")" "=" expression ";"
destructuring_field ::= IDENTIFIER ( ":" type )?

/* Compiletime Constants */
compiletime_constants_declaration ::= "const" IDENTIFIER "=" expression ";"

/* Structure Declaration */
structure_declaration ::= "struct" IDENTIFIER generic_parameters? "{" struct_field_declaration* "}"
struct_field_declaration ::= IDENTIFIER type ";"

/* Enum Declaration */
enum_declaration ::= "enum" IDENTIFIER ( ":" type )? "{" enum_field ("," enum_field)* "}"
enum_field ::= IDENTIFIER ( "=" expression )?

/* Function Declaration */
function_declaration ::= function_prototype ( "=" expression ";" | block_statement )
function_prototype ::= "fun" IDENTIFIER generic_parameters? "(" parameter_list? ")" type? (";" | block_statement)
generic_parameters ::= "<" IDENTIFIER ("," IDENTIFIER)* ">"
parameter_list ::= parameter ("," parameter)*
parameter ::= IDENTIFIER type

/* Operator Function Declaration */
operator_function_declaration ::= "operator" operator_token "(" parameter_list? ")" type? ( "=" expression ";" | block_statement )
operator_token ::= /* See language spec: e.g. + - * / == etc. */

/* Block Statement */
block_statement ::= "{" statement* "}"
statement ::= field_declaration
           | destructuring_field_declaration
           | compiletime_constants_declaration
           | if_statement
           | for_statement
           | while_statement
           | switch_statement
           | return_statement
           | defer_statement
           | break_statement
           | continue_statement
           | block_statement
           | statements_directive
           | expression_statement

/* If Statement */
if_statement ::= "if" "(" expression ")" statement ( "else" ( if_statement | statement ) )*

/* For Statement */
for_statement ::= "for" ( "{" statement "}" | "(" (IDENTIFIER ( "," IDENTIFIER )? ":" )? expression ( ".." expression ( ":" expression )? )? ")" statement )

/* While Statement */
while_statement ::= "while" "(" expression ")" statement

/* Switch Statement */
switch_statement ::= "switch" "(" expression switch_operator ")" "{" switch_case* "}"
switch_operator ::= "," operator_token | /* empty */
switch_case ::= ( expression ("," expression)* | "else" ) "->" statement

/* Return Statement */
return_statement ::= "return" expression? ";"

/* Defer Statement */
defer_statement ::= "defer" expression ";"

/* Break Statement */
break_statement ::= "break" expression? ";"

/* Continue Statement */
continue_statement ::= "continue" expression? ";"

/* Expression Statement */
expression_statement ::= expression ";"

/* Expressions (Operator Precedence) */
expression ::= assignment_expression
assignment_expression ::= logical_or_expression ( assignment_operator assignment_expression )?
assignment_operator ::= "=" | "+=" | "-=" | "*=" | "/=" | "%=" | "&=" | "|=" | "^=" | "<<=" | ">>="

logical_or_expression ::= logical_and_expression ( "||" logical_and_expression )*
logical_and_expression ::= bitwise_and_expression ( "&&" bitwise_and_expression )*
bitwise_and_expression ::= bitwise_xor_expression ( "&" bitwise_xor_expression )*
bitwise_xor_expression ::= bitwise_or_expression ( "^" bitwise_or_expression )*
bitwise_or_expression ::= equality_expression ( "|" equality_expression )*
equality_expression ::= comparison_expression ( ("==" | "!=") comparison_expression )*
comparison_expression ::= bitwise_shift_expression ( ("<" | "<=" | ">" | ">=") bitwise_shift_expression )*
bitwise_shift_expression ::= term_expression ( ("<<" | ">>") term_expression )*
term_expression ::= factor_expression ( ("+" | "-") factor_expression )*
factor_expression ::= enum_access_expression ( ("*" | "/" | "%") enum_access_expression )*
enum_access_expression ::= infix_call_expression ( "::" IDENTIFIER )?
infix_call_expression ::= prefix_expression ( IDENTIFIER infix_call_expression )?
prefix_expression ::= ( unary_operator prefix_expression ) | prefix_call_expression
unary_operator ::= "+" | "-" | "!" | "~" | "++" | "--"
prefix_call_expression ::= IDENTIFIER generic_arguments? prefix_expression | postfix_increment_or_decrement
generic_arguments ::= "<" type ( "," type )* ">"
postfix_increment_or_decrement ::= call_or_access_expression ( "++" | "--" )?
call_or_access_expression ::= enumeration_attribute_expression ( "." IDENTIFIER | "." INT | generic_arguments "(" argument_list? ")" | "(" argument_list? ")" | "[" expression "]" )*
enumeration_attribute_expression ::= postfix_call_expression ( "." IDENTIFIER )?
postfix_call_expression ::= initializer_expression ( IDENTIFIER generic_arguments? )?
initializer_expression ::= IDENTIFIER ( "(" argument_list? ")" | "{" lambda_expression "}" | generic_arguments )? | function_call_with_lambda_argument
function_call_with_lambda_argument ::= IDENTIFIER "{" lambda_expression "}" | primary_expression
argument_list ::= expression ( "," expression )*

/* Primary Expressions */
primary_expression ::= NUMBER
                    | CHARACTER
                    | STRING
                    | "true"
                    | "false"
                    | "null"
                    | IDENTIFIER
                    | "(" expression ( "," expression )* ")" /* tuple/group */
                    | "[" expression ( "," expression )* "]" /* array */
                    | "{" lambda_expression "}"
                    | if_expression
                    | switch_expression
                    | cast_expression
                    | type_size_expression
                    | type_align_expression
                    | value_size_expression
                    | expressions_directive

/* Lambda Expression */
lambda_expression ::= "{" ( "(" parameter_list? ")" type? "->" )? block_statement "}"

if_expression ::= "if" "(" expression ")" "{" expression "}" ( "else" ( if_expression | "{" expression "}" ) )*

switch_expression ::= "switch" "(" expression switch_operator ")" "{" ( case_expression ( "," case_expression )* "->" expression )* ( "else" "->" expression )? "}"
case_expression ::= NUMBER | enum_element

cast_expression ::= "cast" "(" type ( "," expression )? ")"

type_size_expression ::= "type_size" "(" type ")"
type_align_expression ::= "type_align" "(" type ")"
value_size_expression ::= "value_size" "(" expression ")"

/* Types */
type ::= /* See type parsing rules, e.g. primitive_type | struct_type | array_type | ... */

/* Terminals */
IDENTIFIER ::= /* letter ( letter | digit | "_" )* */
STRING ::= /* " ... " */
NUMBER ::= /* integer | float */
CHARACTER ::= /* 'c' */

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions