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

Open
mingodad opened this issue Jul 2, 2024 · 6 comments
Open

Grammar railroad diagram #361

mingodad opened this issue Jul 2, 2024 · 6 comments
Labels
enhancement New feature or request

Comments

@mingodad
Copy link

mingodad commented Jul 2, 2024

Based on https://github.com/schibsted/jslt/blob/master/core/src/main/jjtree/jslt.jjt with a bit of manual changes we can have a nice navigable railroad diagram:

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

/** Root production. */
Start ::=
	Import* (Let | FunctionDecl)* Expr //<EOF>


///** Root production for module files */
//Module ::=
//	Import* (Let | FunctionDecl)* Expr? //<EOF>
//

Expr ::=
	OrExpr (PipeOperator OrExpr)*


OrExpr ::=
	AndExpr (OR OrExpr)?


AndExpr ::=
	ComparativeExpr (AND AndExpr)?


ComparativeExpr ::=
	AdditiveExpr (Comparator AdditiveExpr)?


// not necessary, but makes the tree easier to traverse
Comparator ::=
	EQUALS
	| UNEQUALS
	| BIGOREQ
	| BIGGER
	| SMALLER
	| SMALLOREQ


PipeOperator ::=
	PIPE


AdditiveExpr ::=
	MultiplicativeExpr (AdditiveOperator MultiplicativeExpr)*


// not necessary, but makes the tree easier to traverse
AdditiveOperator ::=
	PLUS
	| MINUS


MultiplicativeExpr ::=
	BaseExpr (MultiplicativeOperator BaseExpr)*


// not necessary, but makes the tree easier to traverse
MultiplicativeOperator ::=
	STAR
	| SLASH


BaseExpr ::=
	//(LOOKAHEAD(2)
	NULL
	| INTEGER
	| DECIMAL
	| STRING
	| TRUE
	| FALSE
	| Chainable
	| Parenthesis
	| IfStatement
	| Array
	//|(LOOKAHEAD(2)
	| Object
	| ObjectComprehension


Chainable ::=
	(FunctionCall | VARIABLE | DOT (IDENT | STRING)?) ChainLink?


ChainLink ::=
	(DotKey | ArraySlicing) ChainLink?


Parenthesis ::=
	LPAREN Expr RPAREN


DotKey ::=
	DOT (IDENT | STRING)


ArraySlicing ::=
	LBRACKET
	(
		Expr (Colon Expr?)?
		| Colon Expr
	)
	RBRACKET


Colon ::=
	COLON // need this to make parse tree manageable


ArrayElem /*#void*/  ::=
	Expr (COMMA ArrayElem?)?


Array ::=
	LBRACKET
	(
		FOR LPAREN Expr RPAREN Let* Expr
			(IF LPAREN Expr RPAREN)?
		| ArrayElem?
	)
	RBRACKET


Object ::=
	LCURLY Let* (Pair | Matcher)? RCURLY


Matcher ::=
	STAR MatcherMinus? COLON Expr


MatcherMinus ::=
	MINUS (IDENT | STRING) (COMMA (IDENT | STRING))*


Pair ::=
	Expr COLON Expr (COMMA (Pair | Matcher)?)?


ObjectComprehension ::=
	LCURLY FOR LPAREN Expr RPAREN Let* Expr COLON Expr
		(IF LPAREN Expr RPAREN)?
	RCURLY


IfStatement ::=
	IF LPAREN Expr RPAREN Let* Expr ElseBranch?


// not necessary, but makes it easier to walk the parse tree
ElseBranch ::=
	ELSE Let* Expr


FunctionCall ::=
	(IDENT | PIDENT) LPAREN (Expr (COMMA Expr)*)? RPAREN


Let ::=
	LET IDENT ASSIGN Expr


FunctionDecl ::=
	DEF IDENT LPAREN (IDENT (COMMA IDENT)*)? RPAREN Let* Expr


Import ::=
	IMPORT STRING AS IDENT

//Tokens

NULL ::= "null"
LBRACKET ::= "["
RBRACKET ::= "]"
COMMA ::= ","
COLON ::= ":"
LCURLY ::= "{"
RCURLY ::= "}"
TRUE ::= "true"
FALSE ::= "false"
OR ::= "or"
AND ::= "and"
DOT ::= "."
IF ::= "if"
ELSE ::= "else"
LPAREN ::= "("
RPAREN ::= ")"
LET ::= "let"
ASSIGN ::= "="
EQUALS ::= "=="
UNEQUALS ::= "!="
BIGOREQ ::= ">="
BIGGER ::= ">"
SMALLER ::= "<"
SMALLOREQ ::= "<="
PLUS ::= "+"
MINUS ::= "-"
STAR ::= "*"
SLASH ::= "/"
PIPE ::= "|"
FOR ::= "for"
DEF ::= "def"
IMPORT ::= "import"
AS ::= "as"
@larsga larsga added the enhancement New feature or request label Jul 3, 2024
@larsga
Copy link
Collaborator

larsga commented Jul 3, 2024

That sounds like it could be interesting. Is this something you're interested in contributing?

@mingodad
Copy link
Author

mingodad commented Jul 3, 2024

It's already here.

@larsga
Copy link
Collaborator

larsga commented Jul 3, 2024

I don't follow. Where is it?

@catull
Copy link

catull commented Jul 3, 2024

Perhaps @mingodad can explain what he intends or expects with the link above.

@mingodad
Copy link
Author

mingodad commented Jul 3, 2024

Sorry ! it seems that people here is not ready for new things !

@mingodad mingodad closed this as completed Jul 3, 2024
@larsga
Copy link
Collaborator

larsga commented Jul 3, 2024

Sorry. I didn't fully understand your meaning at first. Now I generated some railroad diagrams. Nice!

I need to think a little about how to use them, but it may indeed be that this could be useful.

@larsga larsga reopened this Jul 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants