Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Grammar railroad diagram #1435

Closed
mingodad opened this issue Jul 19, 2021 · 2 comments
Closed

Grammar railroad diagram #1435

mingodad opened this issue Jul 19, 2021 · 2 comments

Comments

@mingodad
Copy link

Using the sql output of the lemon parser generator with the query shown bellow give us an EBNF understood by https://www.bottlecaps.de/rr/ui to generate railroad diagrams.

lemon -S zc-parse.lemon
select name || ' ::=' || group_concat(
	case when hasOr == 1 then ' (' || trim(rtxt) || ')'
	else
		case when length(rtxt) == 0 then ' /* empty */' else rtxt end
	end, '
	|')
from (
	select lhs, name, substr(txt, instr(txt, tbl.sep) + length(tbl.sep)) rtxt, (instr(txt, '|') > 0) hasOr
	from rule left join symbol on lhs=id, (select '::=' as sep) tbl
) as t
group by lhs;

We can see a railroad diagram for the grammar in src/common/scripting/frontend/zcc-parse.lemon , copy and paste the EBNF shown bellow on https://www.bottlecaps.de/rr/ui in the tab Edit Grammar then switching to the tab View Diagram.

It can be made nicer if we add the tokens to it, see zephir-lang/php-zephir-parser#106

declarator ::= decl_flags type_list_or_void variables_or_function
opt_func_body ::= SEMICOLON
    | function_body
function_body ::= compound_statement
main ::= translation_unit
translation_unit ::= /* empty */
    | translation_unit external_declaration
    | error
    | translation_unit EOF
external_declaration ::= mixin_definition
    | class_definition
    | struct_def
    | enum_def
    | const_def
    | include_def
mixin_definition ::= mixin_class_definition
class_definition ::= class_head class_body
struct_def ::= STRUCT IDENTIFIER struct_flags LBRACE opt_struct_body RBRACE opt_semicolon
    | EXTEND STRUCT IDENTIFIER LBRACE opt_struct_body RBRACE opt_semicolon
enum_def ::= ENUM IDENTIFIER enum_type LBRACE opt_enum_list RBRACE opt_semicolon
const_def ::= CONST IDENTIFIER EQ expr SEMICOLON
include_def ::= INCLUDE string_constant
opt_semicolon ::= /* empty */
    | SEMICOLON
opt_comma ::= /* empty */
    | COMMA
opt_expr ::= /* empty */
    | expr
expr ::= expr ADD expr
    | expr SUB expr
    | expr MUL expr
    | expr DIV expr
    | expr MOD expr
    | expr POW expr
    | expr CROSSPROD expr
    | expr DOTPROD expr
    | expr LSH expr
    | expr RSH expr
    | expr URSH expr
    | expr DOTDOT expr
    | expr LT expr
    | expr GT expr
    | expr LTEQ expr
    | expr GTEQ expr
    | expr LTGTEQ expr
    | expr IS expr
    | expr EQEQ expr
    | expr NEQ expr
    | expr APPROXEQ expr
    | expr AND expr
    | expr XOR expr
    | expr OR expr
    | expr ANDAND expr
    | expr OROR expr
    | expr EQ expr
    | expr ADDEQ expr
    | expr SUBEQ expr
    | expr MULEQ expr
    | expr DIVEQ expr
    | expr MODEQ expr
    | expr LSHEQ expr
    | expr RSHEQ expr
    | expr URSHEQ expr
    | expr ANDEQ expr
    | expr OREQ expr
    | expr XOREQ expr
    | expr SCOPE expr
    | expr QUESTION expr COLON expr
    | unary_expr
string_constant ::= STRCONST
    | string_constant STRCONST
class_head ::= EXTEND CLASS IDENTIFIER
    | CLASS IDENTIFIER class_ancestry class_flags
class_innards ::= /* empty */
    | class_innards class_member
class_member ::= declarator
    | mixin_statement
    | enum_def
    | struct_def
    | states_def
    | default_def
    | const_def
    | property_def
    | flag_def
    | staticarray_statement
class_body ::= SEMICOLON class_innards EOF
    | LBRACE class_innards RBRACE
class_ancestry ::= /* empty */
    | COLON dottable_id
class_flags ::= /* empty */
    | class_flags ABSTRACT
    | class_flags NATIVE
    | class_flags UI
    | class_flags PLAY
    | class_flags REPLACES dottable_id
    | class_flags VERSION LPAREN STRCONST RPAREN
dottable_id ::= IDENTIFIER
    | dottable_id DOT IDENTIFIER
    | dottable_id DOT DEFAULT
    | dottable_id DOT COLOR
mixin_statement ::= MIXIN IDENTIFIER SEMICOLON
property_def ::= PROPERTY IDENTIFIER COLON identifier_list SEMICOLON
flag_def ::= FLAGDEF IDENTIFIER COLON IDENTIFIER COMMA INTCONST SEMICOLON
states_def ::= STATES states_opts scanner_mode LBRACE states_body RBRACE
default_def ::= DEFAULT LBRACE RBRACE
    | DEFAULT LBRACE default_statement_list RBRACE
    | DEFAULT LBRACE error RBRACE
staticarray_statement ::= STATICCONST type IDENTIFIER LBRACKET RBRACKET EQ LBRACE expr_list RBRACE SEMICOLON
    | STATICCONST type LBRACKET RBRACKET IDENTIFIER EQ LBRACE expr_list RBRACE SEMICOLON
opt_struct_body ::= /* empty */
    | error
    | struct_body
struct_body ::= struct_body struct_member
    | struct_member
struct_member ::= declarator
    | enum_def
    | const_def
    | staticarray_statement
identifier_list ::= IDENTIFIER
    | states_opt COMMA IDENTIFIER
states_opt ::= IDENTIFIER
    | states_opt COMMA IDENTIFIER
struct_flags ::= /* empty */
    | struct_flags UI
    | struct_flags PLAY
    | struct_flags CLEARSCOPE
    | struct_flags NATIVE
    | struct_flags VERSION LPAREN STRCONST RPAREN
enum_list ::= error
    | enum_list COMMA enumerator
    | enumerator
opt_enum_list ::= /* empty */
    | enum_list opt_comma
enumerator ::= IDENTIFIER
    | IDENTIFIER EQ expr
enum_type ::= /* empty */
    | COLON int_type
int_type ::= SBYTE
    | BYTE
    | SHORT
    | USHORT
    | INT
    | UINT
mixin_class_definition ::= MIXIN CLASS IDENTIFIER LBRACE mixin_class_body RBRACE
mixin_class_body ::= /* empty */
    | mixin_class_body mixin_class_member
mixin_class_member ::= declarator
    | enum_def
    | struct_def
    | states_def
    | default_def
    | const_def
    | property_def
    | flag_def
    | staticarray_statement
states_body ::= /* empty */
    | error
    | states_body state_line
    | states_body state_label
    | states_body state_flow
state_line ::= NWS NWS expr state_opts state_action
state_label ::= NWS COLON
state_flow ::= state_flow_type scanner_mode SEMICOLON
state_flow_type ::= STOP
    | WAIT
    | FAIL
    | LOOP
    | GOTO dottable_id state_goto_offset
    | GOTO IDENTIFIER SCOPE dottable_id state_goto_offset
    | GOTO SUPER SCOPE dottable_id state_goto_offset
state_goto_offset ::= /* empty */
    | ADD expr
state_action ::= LBRACE statement_list scanner_mode RBRACE
    | LBRACE scanner_mode RBRACE
    | LBRACE error scanner_mode RBRACE
    | state_call scanner_mode SEMICOLON
state_call ::= /* empty */
    | IDENTIFIER state_call_params
state_call_params ::= /* empty */
    | LPAREN func_expr_list RPAREN
state_opts ::= /* empty */
    | state_opts BRIGHT
    | state_opts FAST
    | state_opts SLOW
    | state_opts NODELAY
    | state_opts CANRAISE
    | state_opts OFFSET LPAREN expr COMMA expr RPAREN
    | state_opts LIGHT LPAREN light_list RPAREN
states_opts ::= /* empty */
    | LPAREN states_opt RPAREN
scanner_mode ::= /* empty */
light_list ::= STRCONST
    | light_list COMMA STRCONST
statement_list ::= statement
    | statement_list statement
func_expr_list ::= func_expr_list COMMA func_expr_item
    | func_expr_item
default_statement_list ::= default_statement
    | default_statement_list default_statement
default_statement ::= SEMICOLON
    | error SEMICOLON
    | property_statement
    | flag_statement
property_statement ::= dottable_id expr_list SEMICOLON
    | dottable_id SEMICOLON
flag_statement ::= ADD dottable_id
    | SUB dottable_id
expr_list ::= expr_list COMMA expr
    | expr
type_name ::= type_name1
    | IDENTIFIER
    | ATSIGN IDENTIFIER
    | READONLY LT IDENTIFIER GT
    | READONLY LT ATSIGN IDENTIFIER GT
    | DOT dottable_id
type_name1 ::= BOOL
    | FLOAT
    | DOUBLE
    | VECTOR2
    | VECTOR3
    | NAME
    | SOUND
    | STATE
    | COLOR
    | LET
    | int_type
aggregate_type ::= MAP LT type_or_array COMMA type_or_array GT
    | ARRAY LT type_or_array GT
    | CLASS class_restrictor
type ::= type_name
    | aggregate_type
type_list ::= type_list COMMA type_or_array
    | type_or_array
type_list_or_void ::= VOID
    | type_list
type_or_array ::= type array_size
    | type
class_restrictor ::= /* empty */
    | LT dottable_id GT
array_size ::= array_size array_size_expr
    | array_size_expr
array_size_expr ::= LBRACKET opt_expr RBRACKET
variables_or_function ::= IDENTIFIER LPAREN func_params RPAREN func_const opt_func_body
    | variable_list SEMICOLON
    | error SEMICOLON
decl_flags ::= /* empty */
    | decl_flags decl_flag
    | decl_flags ACTION states_opts
    | decl_flags DEPRECATED LPAREN STRCONST opt_deprecation_message RPAREN
    | decl_flags VERSION LPAREN STRCONST RPAREN
func_params ::= /* empty */
    | VOID
    | func_param_list COMMA ELLIPSIS
    | func_param_list
func_const ::= /* empty */
    | CONST
variable_list ::= variable_list COMMA variable_name
    | variable_name
variable_name ::= IDENTIFIER
    | IDENTIFIER array_size
decl_flag ::= NATIVE
    | STATIC
    | PRIVATE
    | PROTECTED
    | LATENT
    | FINAL
    | META
    | TRANSIENT
    | READONLY
    | INTERNAL
    | VIRTUAL
    | OVERRIDE
    | ABSTRACT
    | VARARG
    | UI
    | PLAY
    | CLEARSCOPE
    | VIRTUALSCOPE
opt_deprecation_message ::= /* empty */
    | COMMA STRCONST
func_param_list ::= func_param_list COMMA func_param
    | func_param
func_param ::= func_param_flags type IDENTIFIER
    | func_param_flags type IDENTIFIER EQ expr
func_param_flags ::= /* empty */
    | func_param_flags IN
    | func_param_flags OUT
    | func_param_flags OPTIONAL
primary ::= IDENTIFIER
    | SUPER
    | constant
    | LPAREN expr COMMA expr COMMA expr RPAREN
    | LPAREN expr COMMA expr RPAREN
    | LPAREN expr RPAREN
    | primary LPAREN func_expr_list RPAREN
    | LPAREN CLASS LT IDENTIFIER GT RPAREN LPAREN func_expr_list RPAREN
    | primary LBRACKET expr RBRACKET
    | primary DOT IDENTIFIER
    | primary ADDADD
    | primary SUBSUB
    | LPAREN error RPAREN
unary_expr ::= SUB unary_expr
    | ADD unary_expr
    | SUBSUB unary_expr
    | ADDADD unary_expr
    | TILDE unary_expr
    | BANG unary_expr
    | SIZEOF unary_expr
    | ALIGNOF unary_expr
    | primary
constant ::= INTCONST
    | UINTCONST
    | FLOATCONST
    | NAMECONST
    | FALSE
    | TRUE
    | NULLPTR
    | string_constant
func_expr_item ::= /* empty */
    | named_expr
named_expr ::= IDENTIFIER COLON expr
    | expr
compound_statement ::= LBRACE RBRACE
    | LBRACE statement_list RBRACE
    | LBRACE error RBRACE
statement ::= SEMICOLON
    | labeled_statement
    | compound_statement
    | expression_statement SEMICOLON
    | assign_statement SEMICOLON
    | local_var SEMICOLON
    | error SEMICOLON
    | staticarray_statement
    | selection_statement
    | iteration_statement
    | jump_statement
labeled_statement ::= CASE expr COLON
    | DEFAULT COLON
expression_statement ::= expr
selection_statement ::= if_front
    | if_front ELSE statement
    | SWITCH LPAREN expr RPAREN statement
iteration_statement ::= while_or_until LPAREN expr RPAREN statement
    | DO statement while_or_until LPAREN expr RPAREN
    | FOR LPAREN for_init SEMICOLON opt_expr SEMICOLON for_bump RPAREN statement
jump_statement ::= CONTINUE SEMICOLON
    | BREAK SEMICOLON
    | RETURN SEMICOLON
    | RETURN expr_list SEMICOLON
assign_statement ::= LBRACKET expr_list RBRACKET EQ expr
local_var ::= type variable_list_with_init
while_or_until ::= WHILE
    | UNTIL
for_init ::= local_var
    | for_bump
for_bump ::= /* empty */
    | expression_statement
    | for_bump COMMA expression_statement
if_front ::= IF LPAREN expr RPAREN statement
variable_list_with_init ::= variable_list_with_init COMMA var_init
    | var_init
var_init ::= IDENTIFIER
    | IDENTIFIER array_size
    | IDENTIFIER EQ expr
    | IDENTIFIER EQ LBRACE expr_list RBRACE
    | IDENTIFIER array_size EQ LBRACE expr_list RBRACE
    | IDENTIFIER EQ LBRACE error RBRACE

@coelckers
Copy link
Member

This needs to be implemented by someone with a bit more knowledge about the subject matter.

@coelckers
Copy link
Member

I don't think this needs to be kept open, considering it got no response in half a year.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants