Kray-G/kinx

Grammar railroad diagram

mingodad opened this issue · 2 comments

Asking this modified byacc (https://github.com/mingodad/lalr-parser-test/tree/main/byacc) to output an EBNF grammar that can be used on https://www.bottlecaps.de/rr/ui to get a railroad diagram (https://en.wikipedia.org/wiki/Syntax_diagram).

Copy and paste the EBNF shown bellow on https://www.bottlecaps.de/rr/ui in the TAB Edit Grammar then switch to the TAB View Diagram.

/* "byacc -e knix.y" the output is in y.ebnf and add the tokens manually if you want more details */
Program ::=
	 ToplevelStatementList

ToplevelStatementList ::=
	 Statement
	| ToplevelStatementList Statement

StatementList ::=
	 Statement
	| StatementList Statement

Statement ::=
	 NonSemicolonStatement
	| SemicolonStatement

NonSemicolonStatement ::=
	 BlockStatement
	| NamespaceStatement
	| EnumStatement
	| IfStatement
	| CaseStatement
	| TryCatchStatement
	| LabelStatement
	| LabelledStatement
	| IMPORT VAR NAME '=' STR ';'
	| error RBBR

SemicolonStatement ::=
	 ReturnStatement
	| YieldStatement
	| ThrowStatement
	| MixinStatement
	| ExpressionStatement
	| DefinitionStatement
	| BreakStatement
	| error ';'
	| error LBBR
	| error IF
	| error DO
	| error WHILE
	| error FOR
	| error TRY
	| error SWITCH
	| error CASE
	| error ENUM
	| error CLASS
	| error FUNCTION
	| error PRIVATE
	| error PUBLIC

LabelledStatement ::=
	 WhileStatement
	| DoWhileStatement
	| SwitchCaseStatement
	| ForStatement

BlockStatement ::=
	 LBBR RBBR
	| LBBR StatementList RBBR

NamespaceStatement ::=
	 NAMESPACE NamespaceName LBBR RBBR
	| SYSNS NamespaceName LBBR RBBR
	| NAMESPACE NamespaceName LBBR StatementList RBBR
	| SYSNS NamespaceName LBBR StatementList RBBR

NamespaceName ::=
	 NAME

EnumStatement ::=
	 ENUM LBBR EnumList Comma_Opt RBBR

EnumList ::=
	 NAME
	| NAME '=' INT
	| NAME '=' '-' INT
	| EnumList ',' NAME
	| EnumList ',' NAME '=' INT
	| EnumList ',' NAME '=' '-' INT

DefinitionStatement ::=
	 VarDeclStatement
	| FunctionDeclStatement
	| ClassDeclStatement
	| ModuleDeclStatement

LabelStatement ::=
	 NAME ':' LabelledStatement

IfStatement ::=
	 IF '(' AssignExpressionList ')' Statement
	| IF '(' AssignExpressionList ')' Statement ELSE Statement

WhileStatement ::=
	 WHILE '(' AssignExpressionList ')' Statement

DoWhileStatement ::=
	 DO Statement WHILE '(' AssignExpressionList ')' ';'

SwitchCaseStatement ::=
	 SWITCH '(' AssignExpressionList ')' BlockStatement

CaseStatement ::=
	 CASE AssignExpression ':'
	| DEFAULT ':'

ForStatement ::=
	 FOR '(' VAR DeclAssignExpressionList ';' AssignExpressionList_Opt ';' AssignExpressionList_Opt ')' Statement
	| FOR '(' AssignExpressionList ';' AssignExpressionList_Opt ';' AssignExpressionList_Opt ')' Statement
	| FOR '(' ';' AssignExpressionList_Opt ';' AssignExpressionList_Opt ')' Statement
	| FOR '(' ForInVariable IN AssignExpressionList ')' Statement
	| FOR '(' VAR ForInVariable IN AssignExpressionList ')' Statement

ForInVariable ::=
	 VarName
	| LMBR ArrayItemList RMBR

TryCatchStatement ::=
	 TRY BlockStatement CatchStatement_Opt FinallyStatement_Opt

CatchStatement_Opt ::=
	 /*empty*/
	| CATCH BlockStatement
	| CATCH '(' NAME ')' BlockStatement

FinallyStatement_Opt ::=
	 /*empty*/
	| FINALLY BlockStatement

BreakStatement ::=
	 BREAK Modifier_Opt ';'
	| BREAK NAME Modifier_Opt ';'
	| CONTINUE Modifier_Opt ';'
	| CONTINUE NAME Modifier_Opt ';'

ReturnStatement ::=
	 RETURN GetLineNumber AssignExpressionList_Opt Modifier_Opt ';'
	| SYSRET_NV ';'

YieldStatement ::=
	 YieldExpression Modifier_Opt ';'

YieldExpression ::=
	 YIELD AssignExpression
	| YIELD
	| AssignExpression '=' YIELD AssignExpression
	| AssignExpression '=' YIELD

ThrowStatement ::=
	 THROW AssignExpressionList_Opt Modifier_Opt ';'

MixinStatement ::=
	 MIXIN MixinModuleList ';'

MixinModuleList ::=
	 NAME
	| MixinModuleList ',' NAME

ExpressionStatement ::=
	 AssignExpression_Opt ';'

AssignExpression_Opt ::=
	 /*empty*/
	| AssignExpression Modifier_Opt

AssignExpressionList_Opt ::=
	 /*empty*/
	| AssignExpressionObjList

Modifier_Opt ::=
	 /*empty*/
	| IF '(' AssignExpressionList ')'

AssignExpression ::=
	 TernaryExpression
	| AssignExpression '=' AssignRightHandSide
	| AssignExpression SHLEQ AssignRightHandSide
	| AssignExpression SHREQ AssignRightHandSide
	| AssignExpression ADDEQ AssignRightHandSide
	| AssignExpression SUBEQ AssignRightHandSide
	| AssignExpression MULEQ AssignRightHandSide
	| AssignExpression DIVEQ AssignRightHandSide
	| AssignExpression MODEQ AssignRightHandSide
	| AssignExpression ANDEQ AssignRightHandSide
	| AssignExpression OREQ AssignRightHandSide
	| AssignExpression XOREQ AssignRightHandSide
	| AssignExpression LANDEQ AssignRightHandSide
	| AssignExpression LOREQ AssignRightHandSide
	| AssignExpression LUNDEFEQ AssignRightHandSide

AssignRightHandSide ::=
	 TernaryExpression
	| ObjectSpecialSyntax

ObjectSpecialSyntax ::=
	 LBBR RBBR
	| ObjectSpecialSyntax PostIncDec
	| ObjectSpecialSyntax LMBR AssignExpression RMBR
	| ObjectSpecialSyntax '.' PropertyName
	| ObjectSpecialSyntax '.' TYPEOF
	| ObjectSpecialSyntax '(' CallArgumentList_Opts ')'
	| ObjectSpecialSyntax SimpleFuncCallFactor

TernaryExpression ::=
	 FunctionExpression
	| LogicalUndefExpression '?' TernaryExpression ':' TernaryExpression

FunctionExpression ::=
	 AnonymousFunctionDeclExpression
	| LogicalUndefExpression

LogicalUndefExpression ::=
	 LogicalOrExpression
	| LogicalUndefExpression LUNDEF LogicalOrExpression

LogicalOrExpression ::=
	 LogicalAndExpression
	| LogicalOrExpression LOR LogicalAndExpression

LogicalAndExpression ::=
	 BitOrExpression
	| LogicalAndExpression LAND BitOrExpression

BitOrExpression ::=
	 BitXorExpression
	| BitOrExpression '|' BitXorExpression

BitXorExpression ::=
	 BitAndExpression
	| BitXorExpression '^' BitAndExpression

BitAndExpression ::=
	 CompareEqualExpression
	| BitAndExpression '&' CompareEqualExpression

CompareEqualExpression ::=
	 CompareExpression
	| CompareEqualExpression EQEQ CompareExpression
	| CompareEqualExpression NEQ CompareExpression

CompareExpression ::=
	 ShiftExpression
	| CompareExpression '<' ShiftExpression
	| CompareExpression LE ShiftExpression
	| CompareExpression '>' ShiftExpression
	| CompareExpression GE ShiftExpression
	| CompareExpression LGE ShiftExpression

ShiftExpression ::=
	 Expression
	| ShiftExpression SHL Expression
	| ShiftExpression SHR Expression

Expression ::=
	 Term
	| Expression '+' Term
	| Expression '-' Term

Term ::=
	 Exponentiation
	| Term '*' Exponentiation
	| Term '/' Exponentiation
	| Term '%' Exponentiation

Exponentiation ::=
	 RegexMatch
	| Exponentiation POW RegexMatch

RegexMatch ::=
	 PrefixExpression
	| RegexMatch REGEQ PrefixExpression
	| RegexMatch REGNE PrefixExpression
	| PrefixExpression DOTS2
	| PrefixExpression DOTS2 PrefixExpression
	| PrefixExpression DOTS3
	| PrefixExpression DOTS3 PrefixExpression

PrefixExpression ::=
	 CastExpression
	| '~' PrefixExpression
	| '!' PrefixExpression
	| '+' PostfixExpression
	| '-' PostfixExpression
	| '*' PrefixExpression
	| INC PostfixExpression
	| DEC PostfixExpression

CastExpression ::=
	 PostfixExpression
	| PostfixExpression AS TypeName

PostfixExpression ::=
	 Factor
	| PostfixExpression PostIncDec
	| PostfixExpression LMBR AssignExpression RMBR
	| PostfixExpression '.' PropertyName
	| PostfixExpression '.' TYPEOF
	| PostfixExpression '(' CallArgumentList_Opts ')'
	| PostfixExpression SimpleFuncCallFactorOrBlock

SimpleFuncCallFactorOrBlock ::=
	 BlockStatement
	| SimpleFuncCallFactor

SimpleFuncCallFactor ::=
	 LBBR DARROW TernaryExpression RBBR
	| LBBR '&' '(' ArgumentList_Opts ')' DARROW TernaryExpression RBBR
	| LBBR '&' '(' ArgumentList_Opts ')' RBBR
	| LBBR '&' '(' ArgumentList_Opts ')' ':' RBBR
	| LBBR '&' '(' ArgumentList_Opts ')' StatementList RBBR
	| LBBR '&' '(' ArgumentList_Opts ')' ':' StatementList RBBR

PostIncDec ::=
	 INC
	| DEC

Factor ::=
	 INT
	| DBL
	| BIGINT
	| NUL
	| VarName
	| TRUE
	| FALSE
	| SRCFILE
	| Array
	| Binary
	| Object
	| SimpleFuncCallFactor
	| Regex
	| '.' PropertyName
	| IMPORT '(' '(' STR ')' ')'
	| '(' AssignExpression ')'
	| '(' ObjectSpecialSyntax ')'
	| '(' STR ')'
	| NEW Factor
	| '@' PropertyName
	| '@' TYPEOF

VarName ::=
	 NAME
	| TYPE

PropertyName ::=
	 NAME
	| IF
	| ELSE
	| WHILE
	| DO
	| FOR
	| IN
	| TRY
	| CATCH
	| FINALLY
	| BREAK
	| CONTINUE
	| SWITCH
	| CASE
	| DEFAULT
	| NEW
	| VAR
	| CONST
	| NATIVE
	| FUNCTION
	| SYSFUNC
	| PUBLIC
	| PRIVATE
	| PROTECTED
	| CLASS
	| MODULE
	| RETURN
	| YIELD
	| THROW
	| NUL
	| TRUE
	| FALSE
	| IMPORT
	| USING
	| TYPE
	| SHL
	| SHR
	| EQEQ
	| NEQ
	| LE
	| '<'
	| GE
	| '>'
	| LGE
	| '+'
	| '-'
	| '*'
	| '/'
	| '%'
	| '&'
	| '|'
	| '^'
	| LMBR RMBR
	| '(' ')'

Array ::=
	 LMBR RMBR
	| LMBR ArrayItemList RMBR

Binary ::=
	 '<' '>'
	| '<' BinStart ArrayItemList BINEND

BinStart ::=
	 /*empty*/

Object ::=
	 LBBR KeyValueList Comma_Opt RBBR

Comma_Opt ::=
	 /*empty*/
	| ','

ArrayItemList ::=
	 ArrayItemListCore
	| CommaList ArrayItemListCore

CommaList ::=
	 ','
	| CommaList ','

ArrayItemListCore ::=
	 AssignExpression
	| DOTS3 AssignRightHandSide
	| ArrayItemListCore ','
	| ArrayItemListCore ',' AssignExpression
	| ArrayItemListCore ',' DOTS3 AssignRightHandSide

AssignExpressionList ::=
	 AssignExpression
	| AssignExpressionList ',' AssignExpression

AssignExpressionObjList ::=
	 AssignExpression
	| LBBR RBBR
	| AssignExpressionObjList ',' AssignExpression
	| AssignExpressionObjList ',' LBBR RBBR

KeyValueList ::=
	 KeyValue
	| KeyValueList ',' KeyValue

KeyValue ::=
	 '(' STR ')' ':' AssignExpression
	| '(' STR ')' ':' ObjectSpecialSyntax
	| NAME ':' AssignExpression
	| NAME ':' ObjectSpecialSyntax
	| KeySpecialName ':' AssignExpression
	| KeySpecialName ':' ObjectSpecialSyntax
	| DOTS3 AssignRightHandSide

KeySpecialName ::=
	 IF
	| ELSE
	| WHILE
	| DO
	| FOR
	| IN
	| TRY
	| CATCH
	| FINALLY
	| BREAK
	| CONTINUE
	| SWITCH
	| CASE
	| NEW
	| VAR
	| CONST
	| FUNCTION
	| SYSFUNC
	| PUBLIC
	| PRIVATE
	| PROTECTED
	| CLASS
	| MODULE
	| RETURN
	| YIELD
	| THROW
	| NUL
	| TRUE
	| FALSE
	| IMPORT
	| USING
	| TYPE
	| TYPEOF
	| SHL
	| SHR
	| EQEQ
	| NEQ
	| LE
	| '<'
	| GE
	| '>'
	| LGE
	| '+'
	| '-'
	| '*'
	| '/'
	| '%'
	| '&'
	| '|'
	| '^'
	| LMBR RMBR
	| '(' ')'

Regex ::=
	 '/' RegexStart RegexString
	| DIVEQ RegexStart RegexString
	| REGPF RegexString

RegexStart ::=
	 /*empty*/

RegexString ::=
	 '(' STR ')'

VarDeclStatement ::=
	 VAR DeclAssignExpressionList ';'
	| CONST DeclAssignExpressionList ';'

DeclAssignExpressionList ::=
	 DeclAssignExpression
	| DeclAssignExpressionList ',' DeclAssignExpression

DeclAssignExpression ::=
	 VarName
	| VarName ':' TypeName ReturnType_Opt
	| VarName '=' DeclAssignRightHandSide
	| VarName ':' TypeName ReturnType_Opt '=' DeclAssignRightHandSide
	| LMBR ArrayItemList RMBR '=' DeclAssignRightHandSide
	| LBBR ArrayItemList RBBR '=' DeclAssignRightHandSide

DeclAssignRightHandSide ::=
	 AssignRightHandSide
	| DeclAssignRightHandSide '=' AssignRightHandSide

FunctionDeclStatement ::=
	 NormalFunctionDeclStatement
	| ClassFunctionDeclStatement

NormalFunctionDeclStatement ::=
	 FUNCTION NAME '(' ArgumentList_Opts ')' BlockStatement
	| SYSFUNC NAME '(' ArgumentList_Opts ')' BlockStatement
	| NativeKeyword NativeType_Opt NAME '(' ArgumentList_Opts ')' BlockStatement

NativeKeyword ::=
	 NATIVE

NativeType_Opt ::=
	 /*empty*/
	| ':' TypeName

AnonymousFunctionDeclExpression ::=
	 FUNCTION '(' ArgumentList_Opts ')' BlockStatement
	| SYSFUNC '(' ArgumentList_Opts ')' BlockStatement
	| COROUTINE '(' ArgumentList_Opts ')' BlockStatement
	| NativeKeyword NativeType_Opt '(' ArgumentList_Opts ')' BlockStatement
	| '&' '(' ArgumentList_Opts ')' DARROW TernaryExpression
	| '&' '(' ArgumentList_Opts ')' DARROW BlockStatement
	| '&' BlockStatement

ClassFunctionDeclStatement ::=
	 PUBLIC ClassFunctionName '(' ArgumentList_Opts ')' BlockStatement
	| PRIVATE ClassFunctionName '(' ArgumentList_Opts ')' BlockStatement
	| PROTECTED ClassFunctionName '(' ArgumentList_Opts ')' BlockStatement

ClassFunctionName ::=
	 NAME
	| KeySpecialName

ClassDeclStatement ::=
	 CLASS NAME ClassArgumentList_Opts Inherit_Opt BlockStatement
	| SYSCLASS NAME ClassArgumentList_Opts Inherit_Opt BlockStatement

ModuleDeclStatement ::=
	 MODULE NAME BlockStatement
	| SYSMODULE NAME BlockStatement

Inherit_Opt ::=
	 /*empty*/
	| ':' GetLineNumber InheritFactor ClassCallArgumentList_Opts

InheritFactor ::=
	 Factor
	| InheritFactor LMBR AssignExpression RMBR
	| InheritFactor '.' PropertyName
	| InheritFactor '.' TYPEOF

ClassArgumentList_Opts ::=
	 /*empty*/
	| '(' ArgumentList_Opts ')'

ArgumentList_Opts ::=
	 /*empty*/
	| ArgumentList

ArgumentList ::=
	 Argument
	| ArgumentList ',' Argument

Argument ::=
	 VarName
	| VarName ':' TypeName ReturnType_Opt
	| LMBR ArrayItemList RMBR
	| LBBR ArrayItemList RBBR
	| DOTS3 VarName

TypeName ::=
	 TYPE ArrayLevel
	| NATIVE
	| NAME ArrayLevel

ArrayLevel ::=
	 /*empty*/
	| LMBR RMBR
	| ArrayLevel LMBR RMBR

ReturnType_Opt ::=
	 /*empty*/
	| '(' TypeName ')'

ClassCallArgumentList_Opts ::=
	 /*empty*/
	| '(' CallArgumentList_Opts ')'

CallArgumentList_Opts ::=
	 /*empty*/
	| CallArgumentList

CallArgumentList ::=
	 CallArgument
	| DOTS3 AssignRightHandSide
	| CallArgumentList ',' CallArgument
	| CallArgumentList ',' DOTS3 AssignRightHandSide

CallArgument ::=
	 AssignExpression
	| ObjectSpecialSyntax
	| STR

GetLineNumber ::=
	 /*empty*/

//Tokens
//if (strcmp(val, \("[^"]+"\).+?return \([^;]+\).+
SRCFILE ::= "__FILE__"
IMPORT ::= "_import"
SYSFUNC ::= "_function"
SYSCLASS ::= "_class"
SYSMODULE ::= "_module"
SYSNS ::= "_namespace"
COROUTINE ::= "_coroutine"
SYSRET_NV ::= "_ret_nv"
AS ::= "as"
BREAK ::= "break"
CLASS ::= "class"
CATCH ::= "catch"
CASE ::= "case"
CONTINUE ::= "continue"
CONST ::= "const"
DO ::= "do"
DEFAULT ::= "default"
ELSE ::= "else"
ENUM ::= "enum"
FUNCTION ::= "function"
FOR ::= "for"
FINALLY ::= "finally"
FALSE ::= "false"
IF ::= "if"
IN ::= "in"
IMPORT ::= "import"
TYPEOF ::= "isNull"
TYPEOF ::= "isUndefined"
TYPEOF ::= "isDefined"
TYPEOF ::= "isInteger"
TYPEOF ::= "isBigInteger"
TYPEOF ::= "isNumber"
TYPEOF ::= "isString"
TYPEOF ::= "isDouble"
TYPEOF ::= "isBinary"
TYPEOF ::= "isFunction"
TYPEOF ::= "isArray"
TYPEOF ::= "isObject"
MODULE ::= "module"
MIXIN ::= "mixin"
NUL ::= "null"
NEW ::= "new"
NATIVE ::= "native"
NAMESPACE ::= "namespace"
PUBLIC ::= "public"
PRIVATE ::= "private"
PROTECTED ::= "protected"
RETURN ::= "return"
SWITCH ::= "switch"
THROW ::= "throw"
TRY ::= "try"
TRUE ::= "true"
NUL ::= "undefined"
VAR ::= "var"
WHILE ::= "while"
YIELD ::= "yield"

Thanks for your report. I feel it is very useful to see the Grammar on Railroad Diagram. But I am afraid, can I ask what you would like me to do? Is it right that you want me to implement something into Kinx?

I am very sorry that I have not get what you meant. If it's doable for me, I will try it in the future.

I've just added this project grammar to https://mingodad.github.io/parsertl-playground/playground/ a Yacc/Lex online editor/tester (select Kinx parser (partially working) from Examples then click Parse to see a parse tree for the content in Input source).