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

Closed
mingodad opened this issue May 5, 2022 · 1 comment
Closed

Grammar railroad diagram #2

mingodad opened this issue May 5, 2022 · 1 comment

Comments

@mingodad
Copy link

mingodad commented May 5, 2022

Using a modified peg/leg from here https://github.com/mingodad/peg to convert the grammar/Grammar.peg in an EBNF understood by https://www.bottlecaps.de/rr/ui to generate a nice railroad diagram (https://en.wikipedia.org/wiki/Syntax_diagram) to help show/understand the syntax.

Command to generate before some minor manual fixes (basically comment out what is not accepted) peg -e syntax.peg .

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.

//To be viewd at https://www.bottlecaps.de/rr/ui

CompilationUnit ::=
	 _ TopLevelDeclaration+ _ _NOT_  .

_ ::=
	 ( WS | ML_COMMENT | SL_COMMENT )*

TopLevelDeclaration ::=
	 _
	| GlobalVariableDeclaration
	| FunctionDeclaration
	| ClassDeclaration
	| TraitDeclaration
	| TraitImplementation
	| CTypeDeclaration
	| CFlagDeclaration
	| ImportStatement

GlobalVariableDeclaration ::=
	 "A"

FunctionDeclaration ::=
	 "F"

ClassDeclaration ::=
	 "class" _ STypeIdent _ TraitConstraint? _ "{" _ ClassMember* _ "}" _ ";"?

TraitDeclaration ::=
	 "trait" SIdent ";"

TraitImplementation ::=
	 "impl" _ STypeIdent _ "for" _ STypeIdent _ "where"

CTypeDeclaration ::=
	 "ctype" _ CTypeIdent _ ";"

CFlagDeclaration ::=
	 "cflag" _ StringLiteral _ ";"

ImportStatement ::=
	 ( "import" | "include" ) _ StringLiteral _ ";"

ClassMember ::=
	 _

TraitConstraint ::=
	 ( "impl" _ STypeIdent _ WhereConstraint? )?

STypeIdent ::=
	 [_A-Z] [_a-zA-Z0-9]+

WhereConstraint ::=
	 ( "where" _ STypeIdent _ "impl" _ TraitList )?

TraitList ::=
	 STypeIdent

SIdent ::=
	 ( [_a-zA-Z] | GreekChar ) ( [_a-zA-Z0-9] | GreekChar )+

CTypeIdent ::=
	 [_a-zA-Z] [_a-zA-Z0-9]+ "*"*

StringLiteral ::=
	 SCharFrag+

CTBindDeclaration ::=
	 "tbind" _ ";"

CFBindDeclaration ::=
	 "fbind" _ ";"

Statement ::=
	 ";"
	| Expr
	| BlockStatement
	| IfStatement
	| ElseStatement
	| ForStatement
	| WhileStatement
	| ReturnStatement

Expr ::=
	 "expr"

BlockStatement ::=
	 "{" _ Statement* _ "}"

IfStatement ::=
	 "if"

ElseStatement ::=
	 "else"

ForStatement ::=
	 "for"

WhileStatement ::=
	 "while" _ "(" _ Expr _ "}" _

ReturnStatement ::=
	 _ ( "return" | "ret" ) _ Expr _ ";" _

GreekChar ::=
	 [αβγδεζηθικλμνξοπρσςτυφχψωΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟΠΡΣΤΥΦΧΨΩ]

SType ::=
	 STypeIdent STypePostfix+

STypePostfix ::=
	 "*"
	| "[]"
	| ( "<" SType ">" )

SCharFrag ::=
	 '"' SChar+ '"'

SChar ::=
	 [^"\\\r\n]
	| EscapeSequence

EscapeSequence ::=
	 ( "\\" ['"?abfnrtv\\] )
	| ( "\\x" [0-9A-F]+ )

WS ::=
	 [ \t\r\n]+

ML_COMMENT ::=
	 "/*" .*? "*/"

SL_COMMENT ::=
	 "//" [^\n]*


//Added tokens for railroad generation
_NOT_ ::= '!'
_AND_ ::= '&'
@apaz-cli
Copy link
Owner

apaz-cli commented May 6, 2022

Pretty cool. I'll keep that site in mind in the future. Also, it's pretty cool that you have a transpiler for grammars.

As you know (because you've created an issue there before), the work is currently being done on https://github.com/apaz-cli/pgen. The plan is that I'm going to have it generate the tokenizer and parser for Daisho.

Unfortunately, my tokenizer generator works from an NFA state machine description, not from a regex like EBNF is expecting. So, it may be hard to convert. Similarly, my grammar files are completely procedural, essentially a programming language of their own. So, no converting to EBNF there either.

With that said, I'll mess around with it. It may be cool for visualizing the grammar for the grammar of the tokenizer and parser files for pgen, which are not written in themselves, but (confusingly) in normal PEG.

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

2 participants