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

Open
mingodad opened this issue Dec 14, 2021 · 0 comments
Open

Grammar railroad diagram #146

mingodad opened this issue Dec 14, 2021 · 0 comments
Labels
enhancement New feature or request

Comments

@mingodad
Copy link
Contributor

With something like this mingodad@d8e43ed and using https://www.bottlecaps.de/convert/ to convert the naked output to be used here https://www.bottlecaps.de/rr/ui to get a railroad diagram for the grammars written to PeppaPEG.

Example:

../cli parse -n -G peppa.peg -e grammar peppa.peg 

grammar =
	 ( start_of_input rule+  end_of_input ) ;
start_of_input =
	&. ;
end_of_input =
	!. ;
rule =
	 ( decorators name /*~*/ "=" expression ";" ) ;
decorators =
	decorator*  ;
decorator =
	 ( "@" /*~*/  ( "squashed" / "scoped" / "spaced" / "lifted" / "tight" / "nonterminal" ) ) ;
/*@squashed*/ 
name =
	reference ;
/*@lifted*/ 
expression =
	left_recursion ;
/*@nonterminal*/ 
left_recursion =
	 ( choice  ( "|" /*~*/ reference choice )?  ) ;
/*@nonterminal*/ 
choice =
	 ( sequence  ( ! ( "//" / "/*" ) "/" sequence )*  ) ;
/*@nonterminal*/ 
sequence =
	repeat+  ;
/*@nonterminal*/ 
repeat =
	 ( primary  ( onceormore / zeroormore / zerooronce / repeatexact / repeatminmax / repeatmin / repeatmax )?  ) ;
onceormore =
	"+" ;
zeroormore =
	"*" ;
zerooronce =
	"?" ;
repeatexact =
	 ( "{" number "}" ) ;
repeatminmax =
	 ( "{" number "," number "}" ) ;
repeatmin =
	 ( "{" number "," "}" ) ;
repeatmax =
	 ( "{" "," number "}" ) ;
/*@lifted*/ 
primary =
	 ( literal / insensitive / range /  ( reference !"=" ) / back_reference / positive / negative /  ( "(" choice ")" ) / dot / cut ) ;
/*@squashed*/ /*@tight*/ 
literal =
	 ( "\"" /*~*/ chars "\"" ) ;
chars =
	char*  ;
/*@squashed*/ /*@tight*/ 
char =
	 ( [\x20-\x21] / [\x23-\x5b] / [\x5d-\U0010ffff] /  ( "\\"  ( "\"" / "/" / "\\" / "b" / "f" / "n" / "r" / "t" / "v" /  ( "x" /*~*/ two_hexdigits ) /  ( "u" /*~*/ four_hexdigits ) /  ( "U" /*~*/ eight_hexdigits ) ) ) ) ;
/*@squashed*/ /*@tight*/ 
range_category =
	 ( [a-z] / [A-Z] / [0-9] / "_" / " " )+  ;
range =
	 ( "["  (  ( char "-" char  ( ".." number )?  ) /  ( "\\p{" range_category "}" ) ) "]" ) ;
/*@tight*/ 
insensitive =
	 ( "i"  ( literal / back_reference ) ) ;
/*@squashed*/ /*@tight*/ 
reference =
	 (  ( [a-z] / [A-Z] / "_" )  ( [a-z] / [A-Z] / [0-9] / "_" )*  ) ;
/*@tight*/ 
back_reference =
	 ( "\\" /*~*/ number ) ;
positive =
	 ( "&" /*~*/ primary ) ;
negative =
	 ( "!" /*~*/ primary ) ;
dot =
	"." ;
cut =
	"~" ;
hexdigit =
	 ( [0-9] / [a-f] / [A-F] ) ;
two_hexdigits =
	hexdigit{2}  ;
four_hexdigits =
	hexdigit{4}  ;
eight_hexdigits =
	hexdigit{8}  ;
/*@squashed*/ /*@tight*/ 
number =
	 ( "0" /  ( [1-9] [0-9]*  ) ) ;
/*@lifted*/ /*@spaced*/ 
comment =
	 (  (  ( "#" / "//" )  ( !"\n". )*  "\n"?  ) /  ( "/*"  ( !"*/". )*  "*/" ) ) ;
/*@lifted*/ /*@spaced*/ 
whitespace =
	 ( " " / "\t" / "\r" / "\n" ) ;

Then coping and pasting the output shown above on https://www.bottlecaps.de/convert/ on the Input grammar: textarea and then clicking the button Convert then the button View Diagram we get a nice railroad diagram (https://en.wikipedia.org/wiki/Syntax_diagram) for the grammar.

@soasme soasme added the enhancement New feature or request label Dec 14, 2021
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

2 participants