Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/Chrriis/RRDiagram
Generate railroad diagrams from code or BNF, generate BNF from code
https://github.com/Chrriis/RRDiagram
Last synced: 5 days ago
JSON representation
Generate railroad diagrams from code or BNF, generate BNF from code
- Host: GitHub
- URL: https://github.com/Chrriis/RRDiagram
- Owner: Chrriis
- License: apache-2.0
- Created: 2013-05-05T14:46:42.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2024-01-26T18:58:53.000Z (10 months ago)
- Last Synced: 2024-08-01T12:29:35.120Z (3 months ago)
- Language: Java
- Size: 130 KB
- Stars: 145
- Watchers: 6
- Forks: 30
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
- awesome-starred - Chrriis/RRDiagram - Generate railroad diagrams from code or BNF, generate BNF from code (others)
README
RRDiagram
=========Generate railroad diagrams from code or BNF. Generate BNF from code.
RR Diagram is a Java library that generates railroad diagrams (also called syntax diagrams) from code or from BNF notation. The output format is a very compact SVG image which can be integrated to web pages and where rules can contain links.
RR Diagram can also be used to generate BNF notation from a model.
The generated output was inspired from this online-only version .
Binaries can be found on the [SourceForge page](https://sourceforge.net/projects/rrdiagram/).
Example
=======This is the kind of diagrams that can get generated:
![H2 Select](http://rrdiagram.sourceforge.net/H2Select.svg)The above is generated using the right conversion options on this BNF:
H2_SELECT =
'SELECT' [ 'TOP' term ] [ 'DISTINCT' | 'ALL' ] selectExpression {',' selectExpression} \
'FROM' tableExpression {',' tableExpression} [ 'WHERE' expression ] \
[ 'GROUP BY' expression {',' expression} ] [ 'HAVING' expression ] \
[ ( 'UNION' [ 'ALL' ] | 'MINUS' | 'EXCEPT' | 'INTERSECT' ) select ] [ 'ORDER BY' order {',' order} ] \
[ 'LIMIT' expression [ 'OFFSET' expression ] [ 'SAMPLE_SIZE' rowCountInt ] ] \
[ 'FOR UPDATE' ];Usage
=====The diagram model represents the actual constructs visible on the diagram.
To convert a diagram model to SVG:
```Java
RRDiagram rrDiagram = new RRDiagram(rrElement);
RRDiagramToSVG rrDiagramToSVG = new RRDiagramToSVG();
String svg = rrDiagramToSVG.convert(rrDiagram);
```The grammar model represents a BNF-like grammar.
It can be converted to a diagram model:
```Java
Grammar grammar = new Grammar(rules);
GrammarToRRDiagram grammarToRRDiagram = new GrammarToRRDiagram();
for(Rule rule: grammar.getRules()) {
RRDiagram rrDiagram = grammarToRRDiagram.convert(rule);
// Do something with diagram, like get the SVG.
}
```The grammar model can be created from code, or can read BNF syntax:
```Java
BNFToGrammar bnfToGrammar = new BNFToGrammar();
Grammar grammar = bnfToGrammar.convert(reader);
// Do something with grammar, like get the diagram for SVG output.
```The grammar model can also be saved to BNF syntax:
```Java
GrammarToBNF grammarToBNF = new GrammarToBNF();
// Set options on the grammarToBNF object
String bnf = grammarToBNF.convert(grammar);
```BNF Syntax
==========The supported BNF subset when reading is the following:
- definition
=
:=
::=
- concatenation
,
<whitespace>
- termination
;
- alternation
|
- option
[ ... ]
?
- repetition
{ ... } => 0..N
expression* => 0..N
expression+ => 1..N
<digits> * expression => <digits>...<digits>
<digits> * [expression] => <0>...<digits>
<digits> * expression? => <0>...<digits>
- grouping
( ... )
- literal
" ... " or ' ... '
- special characters
(? ... ?)
- comments
(* ... *)When getting the BNF syntax from the grammar model, it is possible to tweak the kind of BNF to get by changing some options on the converter.
License
=======This library is provided under the ASL 2.0.