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 #112

Open
mingodad opened this issue Aug 28, 2024 · 0 comments
Open

Grammar railroad diagram #112

mingodad opened this issue Aug 28, 2024 · 0 comments

Comments

@mingodad
Copy link

Using some online tools like https://github.com/GuntherRademacher/rr and https://github.com/GuntherRademacher/ebnf-convert we can have a nice navigable railroad diagram.

Copy and paste the EBNF shown bellow on (IPV4) https://rr.red-dove.com/ui on the tab Edit Grammar the click on the tab View Diagram to see/download a navigable railroad diagram.

I've also added chaos grammar to https://mingodad.github.io/parsertl-playground/playground/ an Yacc/Lex compatible online editor/tester (select Chaos parser from Examples then click Parse to see a parse tree for the content in Input source).

//
// 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.
//

parser::=
	  /*%empty*/
	| parser line

line::=
	  T_NEWLINE
	| import
	| stmt

expr::=
	  ident
	| basic_lit
	| binary_expr
	| unary_expr
	| paren_expr
	| incdec_expr
	| index_expr
	| composite_lit
	| call_expr

ident::=
	  T_VAR

basic_lit::=
	  T_TRUE
	| T_FALSE
	| T_INT
	| T_FLOAT
	| T_STRING

binary_expr::=
	  expr T_ADD expr
	| expr T_SUB expr
	| expr T_MUL expr
	| expr T_QUO expr
	| expr T_REM expr
	| expr T_AND expr
	| expr T_OR expr
	| expr T_XOR expr
	| expr T_SHL expr
	| expr T_SHR expr
	| bool_expr

bool_expr::=
	  expr T_EQL expr
	| expr T_NEQ expr
	| expr T_GTR expr
	| expr T_LSS expr
	| expr T_GEQ expr
	| expr T_LEQ expr
	| expr T_LAND expr
	| expr T_LOR expr

unary_expr::=
	  T_ADD expr
	| T_SUB expr
	| T_NOT expr
	| T_TILDE expr

paren_expr::=
	  T_LPAREN expr T_RPAREN

incdec_expr::=
	  T_INC expr
	| expr T_INC
	| T_DEC expr
	| expr T_DEC

alias_expr::=
	  ident
	| ident T_AS ident

alias_expr_list::=
	  alias_expr
	| alias_expr T_COMMA alias_expr_list

index_expr::=
	  expr T_LBRACK expr T_RBRACK

expr_list::=
	  expr
	| expr T_COMMA expr_list
	| expr T_COMMA T_NEWLINE expr_list

key_value_expr::=
	  basic_lit T_COLON expr

key_value_list::=
	  key_value_expr
	| key_value_expr T_COMMA key_value_list
	| key_value_expr T_COMMA T_NEWLINE key_value_list

composite_lit::=
	  T_LBRACK T_RBRACK
	| T_LBRACK expr_list T_RBRACK
	| T_LBRACK T_NEWLINE expr_list T_RBRACK
	| T_LBRACK expr_list T_NEWLINE T_RBRACK
	| T_LBRACK T_NEWLINE expr_list T_NEWLINE T_RBRACK
	| T_LBRACE T_RBRACE
	| T_LBRACE key_value_list T_RBRACE
	| T_LBRACE T_NEWLINE key_value_list T_RBRACE
	| T_LBRACE key_value_list T_NEWLINE T_RBRACE
	| T_LBRACE T_NEWLINE key_value_list T_NEWLINE T_RBRACE

selector_expr::=
	  ident T_PERIOD ident
	| selector_expr T_PERIOD ident

call_expr::=
	  ident T_LPAREN T_RPAREN
	| ident T_LPAREN expr_list T_RPAREN
	| selector_expr T_LPAREN T_RPAREN
	| selector_expr T_LPAREN expr_list T_RPAREN

decision_expr::=
	  bool_expr T_COLON call_expr
	| bool_expr T_COLON return_stmt
	| bool_expr T_COLON break_stmt

default_expr::=
	  T_DEFAULT T_COLON call_expr
	| T_DEFAULT T_COLON return_stmt
	| T_DEFAULT T_COLON break_stmt

decision_expr_list::=
	  decision_expr
	| default_expr
	| decision_expr T_COMMA decision_expr_list
	| decision_expr T_COMMA T_NEWLINE decision_expr_list

stmt::=
	  assign_stmt T_NEWLINE
	| return_stmt T_NEWLINE
	| print_stmt T_NEWLINE
	| echo_stmt T_NEWLINE
	| expr_stmt T_NEWLINE
	| decl_stmt T_NEWLINE
	| del_stmt T_NEWLINE
	| exit_stmt T_NEWLINE
	| function_table_stmt T_NEWLINE

stmt_list::=
	  stmt
	| /*%empty*/
	| stmt stmt_list
	| T_NEWLINE stmt_list

assign_stmt::=
	  expr T_ASSIGN expr

return_stmt::=
	  T_RETURN expr

print_stmt::=
	  T_PRINT expr
	| pretty_spec T_PRINT expr

echo_stmt::=
	  T_ECHO expr
	| pretty_spec T_ECHO expr

expr_stmt::=
	  expr

decl_stmt::=
	  var_decl
	| times_do_decl
	| foreach_as_list_decl
	| foreach_as_dict_decl
	| func_decl

del_stmt::=
	  T_DEL ident
	| T_DEL index_expr

exit_stmt::=
	  T_EXIT
	| T_EXIT expr

function_table_stmt::=
	  T_FUNCTION_TABLE

block_stmt::=
	  stmt_list T_END

break_stmt::=
	  T_BREAK

type_spec::=
	  T_VOID
	| T_VAR_BOOL
	| T_VAR_NUMBER
	| T_VAR_STRING
	| T_VAR_ANY
	| T_VAR_LIST
	| T_VAR_DICT
	| T_VOID sub_type_spec
	| T_VAR_BOOL sub_type_spec
	| T_VAR_NUMBER sub_type_spec
	| T_VAR_STRING sub_type_spec
	| T_VAR_ANY sub_type_spec

sub_type_spec::=
	  T_VAR_LIST
	| T_VAR_DICT
	| T_VAR_LIST sub_type_spec
	| T_VAR_DICT sub_type_spec

pretty_spec::=
	  T_PRETTY

var_decl::=
	  type_spec ident T_ASSIGN expr

func_decl::=
	  func_type block_stmt
	| func_type block_stmt decision_block

times_do_decl::=
	  expr T_TIMES_DO T_ARROW call_expr
	| expr T_TIMES_DO ident T_ARROW call_expr
	| expr T_TIMES_DO T_AS ident T_ARROW call_expr

foreach_as_list_decl::=
	  T_FOREACH expr T_AS ident T_ARROW call_expr
	| T_FOREACH expr T_AS ident T_COMMA ident T_ARROW call_expr

foreach_as_dict_decl::=
	  T_FOREACH expr T_AS ident T_COLON ident T_ARROW call_expr
	| T_FOREACH expr T_AS ident T_COMMA ident T_COLON ident T_ARROW call_expr

import::=
	  T_IMPORT module_selector
	| T_IMPORT module_selector T_AS ident
	| T_FROM module_selector T_IMPORT asterisk_spec
	| T_FROM module_selector T_IMPORT alias_expr_list

module_selector::=
	  ident
	| ident T_PERIOD module_selector
	| ident T_QUO module_selector
	| ident T_BACKSLASH module_selector
	| parent_dir_spec
	| parent_dir_spec T_PERIOD module_selector
	| parent_dir_spec T_QUO module_selector
	| parent_dir_spec T_BACKSLASH module_selector
	| parent_dir_spec module_selector

parent_dir_spec::=
	  T_PERIOD T_PERIOD

asterisk_spec::=
	  T_MUL

field_spec::=
	  type_spec ident

optional_field_spec::=
	  type_spec ident T_ASSIGN expr

field_list_spec::=
	  field_spec
	| field_spec T_COMMA field_list_spec
	| field_spec T_COMMA T_NEWLINE field_list_spec
	| field_spec T_COMMA optional_field_list_spec
	| field_spec T_COMMA T_NEWLINE optional_field_list_spec

optional_field_list_spec::=
	  optional_field_spec
	| optional_field_spec T_COMMA optional_field_list_spec
	| optional_field_spec T_COMMA T_NEWLINE optional_field_list_spec

func_type::=
	  type_spec T_DEF ident T_LPAREN T_RPAREN T_NEWLINE
	| type_spec T_DEF ident T_LPAREN field_list_spec T_RPAREN T_NEWLINE

decision_block::=
	  T_LBRACE decision_expr_list T_RBRACE
	| T_LBRACE T_NEWLINE decision_expr_list T_RBRACE
	| T_LBRACE decision_expr_list T_NEWLINE T_RBRACE
	| T_LBRACE T_NEWLINE decision_expr_list T_NEWLINE T_RBRACE

//Tokens

T_NEWLINE ::= "\n"
T_ASSIGN ::= "="
T_ADD ::= "+"
T_SUB ::= "-"
T_MUL ::= "*"
T_QUO ::= "/"
T_REM ::= "%"
T_BACKSLASH ::= "\\"
T_LPAREN ::= "("
T_RPAREN ::= ")"
T_LBRACK ::= "["
T_RBRACK ::= "]"
T_LBRACE ::= "{"
T_RBRACE ::= "}"
T_COMMA ::= ","
T_PERIOD ::= "."
T_EQL ::= "=="
T_NEQ ::= "!="
T_GTR ::= ">"
T_LSS ::= "<"
T_GEQ ::= ">="
T_LEQ ::= "<="
T_LAND ::= "&&"|"and"
T_LOR ::= "||"|"or"
T_NOT ::= "!"|"not"
T_AND ::= "&"
T_OR ::= "|"
T_XOR ::= "^"
T_TILDE ::= "~"
T_SHL ::= "<<"
T_SHR ::= ">>"
T_INC ::= "++"
T_DEC ::= "--"
T_COLON ::= ":"
T_ARROW ::= "->"
T_EXIT ::= "exit"
T_EXIT ::= "quit"
T_PRINT ::= "print"
T_ECHO ::= "echo"
T_PRETTY ::= "pretty"
T_TRUE ::= "true"
T_FALSE ::= "false"
T_FUNCTION_TABLE ::= "function_table"
T_DEL ::= "del"
T_RETURN ::= "return"
T_DEFAULT ::= "default"

T_TIMES_DO ::= "times"[ \t]+"do"
T_END ::= "end"
T_FOREACH ::= "foreach"
T_AS ::= "as"
T_FROM ::= "from"

T_VAR_BOOL ::= "bool"
T_VAR_NUMBER ::= "num"
T_VAR_STRING ::= "str"
T_VAR_LIST ::= "list"
T_VAR_DICT ::= "dict"
T_VAR_ANY ::= "any"
T_VOID ::= "void"
T_DEF ::= "def"
T_IMPORT ::= "import"
T_BREAK ::= "break"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant