ana icon indicating copy to clipboard operation
ana copied to clipboard

Grammar railroad diagram

Open mingodad opened this issue 3 years ago • 0 comments

Using this online tool https://www.bottlecaps.de/convert/ to convert the grammar.y in an EBNF understood by https://www.bottlecaps.de/rr/ui and adding the tokens from lexer.l manually we can have a nice railroad diagram (https://en.wikipedia.org/wiki/Syntax_diagram).

Copy and paste the EBNF shown bellow at https://www.bottlecaps.de/rr/ui on the tab Edit Grammar then click on the tab View Diagram.

/* converted on Wed Apr 13, 2022, 12:17 (UTC+02) by bison-to-w3c v0.57 which is Copyright (c) 2011-2022 by Gunther Rademacher <[email protected]> */

program  ::= statement*
statement
         ::= ( assignment_expression | throw_stmt ) ';'
           | function_def
           | class_def
           | trycatch_stmt
           | selection_stmt
selection_stmt
         ::= T_IF '(' assignment_expression ')' '{' if_statements '}' else_if_stmts? else_stmt?
           | ( T_WHILE '(' | T_FOR '(' assignment_expression ';' assignment_expression ';' | T_FOREACH '(' T_ID T_IN ) assignment_expression ')' '{' if_statements '}'
else_if_stmts
         ::= elseif+
elseif   ::= T_ELSE_IF '(' assignment_expression ')' '{' if_statements '}'
else_stmt
         ::= T_ELSE '{' if_statements '}'
if_statements
         ::= if_statement*
if_statement
         ::= ( assignment_expression | jump_statement | throw_stmt ) ';'
           | trycatch_stmt
           | selection_stmt
throw_stmt
         ::= T_THROW assignment_expression
trycatch_stmt
         ::= T_TRY '{' stmt_group '}' T_CATCH '(' T_ID ')' '{' stmt_group '}'
stmt_group
         ::= stmt*
stmt     ::= ( assignment_expression | jump_statement | throw_stmt ) ';'
           | trycatch_stmt
           | selection_stmt
function_statements
         ::= function_statement*
function_statement
         ::= ( assignment_expression | jump_statement | throw_stmt ) ';'
           | trycatch_stmt
           | selection_stmt
jump_statement
         ::= T_RETURN optional_assignment_expression
           | T_BREAK
           | T_CONTINUE
optional_assignment_expression
         ::= assignment_expression?
optional_extends
         ::= ( ':' T_ID )?
function_def
         ::= T_FUNCTION T_ID '(' optional_parameter_list ')' '{' function_statements '}'
function_expression
         ::= T_FUNCTION '(' optional_parameter_list ')' '{' function_statements '}'
class_statement
         ::= function_def
class_def
         ::= T_CLASS T_ID optional_extends '{' class_statement* '}'
primary_expression
         ::= T_ID
           | constant
           | '(' assignment_expression ')'
           | '{' opt_key_vpairs '}'
           | '[' optional_argument_list ']'
           | function_expression
opt_key_vpairs
         ::= ( keyvpair ( ',' keyvpair )* )?
keyvpair ::= key ':' assignment_expression
key      ::= T_ID
           | T_STR_LIT
optional_parameter_list
         ::= ( function_param ( ',' function_param )* )?
function_param
         ::= T_VARARG? T_ID
constant ::= T_INT
           | T_DOUBLE
           | T_STR_LIT
           | T_BT
           | T_BF
basic_expression
         ::= logical_and_expression ( T_LO logical_and_expression )*
logical_and_expression
         ::= inclusive_or_expression ( T_LA inclusive_or_expression )*
inclusive_or_expression
         ::= exclusive_or_expression ( '|' exclusive_or_expression )*
exclusive_or_expression
         ::= and_expression ( '^' and_expression )*
and_expression
         ::= equality_expression ( '&' equality_expression )*
equality_expression
         ::= relational_expression ( ( T_EQ | T_NEQ | T_IN ) relational_expression )*
relational_expression
         ::= shift_expression ( ( '<' | '>' | T_LTE | T_GTE ) shift_expression )*
shift_expression
         ::= additive_expression ( ( T_LS | T_RS ) additive_expression )*
additive_expression
         ::= multiplicative_expression ( ( '+' | '-' ) multiplicative_expression )*
multiplicative_expression
         ::= unary_expression ( ( '*' | '/' | '%' ) unary_expression )*
assignment_expression
         ::= ( unary_expression ( '=' | T_PLUS_EQUAL ) )* basic_expression
unary_expression
         ::= ( T_DEC | T_INC | '-' | '+' | '!' )* primary_expression ( '[' assignment_expression ']' | '(' optional_argument_list ')' | '.' T_ID | T_INC | T_DEC )*
optional_argument_list
         ::= ( argument ( ',' argument )* )?
argument ::= assignment_expression

//Tokens
//\("[^"]+"\)\s+{ return \(\S[^;]+\).+

T_FUNCTION ::= "function"
T_FUNCTION ::= "func"
T_CLASS ::= "class"
T_THROW ::= "throw"
T_NEW ::= "new"
T_LTE ::= "<="
T_GTE ::= ">="
T_INC ::= "++"
T_PLUS_EQUAL ::= "+="
T_DEC ::= "--"
T_EQ ::= "=="
T_NEQ ::= "!="
T_LA ::= "&&"
T_LO ::= "||"
T_BT ::= "true"
T_BF ::= "false"
T_TRY ::= "try"
T_CATCH ::= "catch"
T_IF ::= "if"
T_ELSE_IF ::= "else if"
T_ELSE ::= "else"
T_FOR ::= "for"
T_FOREACH ::= "foreach"
T_WHILE ::= "while"
T_CONTINUE ::= "continue"
T_BREAK ::= "break"
T_RETURN ::= "return"
T_IN ::= "in"
T_LS ::= "<<"
T_RS ::= ">>"
T_VARARG ::= "..."

mingodad avatar Apr 13 '22 10:04 mingodad