• Stars
    star
    101
  • Rank 338,166 (Top 7 %)
  • Language ANTLR
  • License
    Other
  • Created over 10 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Railroad Diagrams for ANTLR 4 grammar rules.

RRD for ANTLR4

Railroad Diagrams for ANTLR 4 grammar rules.

I won't be adding new features to this tool. If you're looking for a more fully featured tool (including LaTeX output, to name just one thing), and a more modular setup version of this tool, have a look at flashpixx' RRD-ANTLR4: https://github.com/flashpixx/RRD-ANTLR4

Introduction

This tool parses ANTLR 4 grammars and creates an html page containing all parser- and lexer rules from this grammar with accompanying railroad diagrams as well as creating separate railroad PNG images of all grammar rules.

Install

Clone this repository:

git clone https://github.com/bkiers/rrd-antlr4.git

Then build it:

cd rrd-antlr4
mvn clean package

The JAR file containing all dependencies can be found in the target folder.

To give it a test by parsing the JSON grammar found in the official ANTLR 4 repository, run the following command from the terminal:

cd target
curl -O https://raw.githubusercontent.com/antlr/grammars-v4/master/json/JSON.g4
java -jar rrd-antlr4-0.1.2.jar JSON.g4

When the command above finishes, one html and pdf file and some png images will have been generated in the folder ./output/JSON. The html file does not make use of the png files but uses SVG to display the railroad diagram and will look like this:

json diagrams

Using programmatically

To use this library in your own code, either stick the JAR file (rrd-antlr4-0.1.2.jar) in your classpath, or add the following dependency to your POM (after having done a mvn clean package!):

<dependency>
    <groupId>nl.big-o</groupId>
    <artifactId>rrd-antlr4</artifactId>
    <version>0.1.2</version>
</dependency>

and parse a grammar file as follows:

String grammarFileName = "/path/to/Json.g4";
DiagramGenerator generator = new DiagramGenerator(grammarFileName);

// Print all parsed rules
System.out.println("all parsed rules: " + generator.getRules().keySet());

// The name of the rule to create a railroad diagram of
String ruleName = "jsonObject";

// Get the SVG of the rule
String svg = generator.getSVG(ruleName);
System.out.println("the svg looks like this: " + svg);

// Create the PNG railroad diagram
boolean success = generator.createDiagram(ruleName);
System.out.println("successfully created diagram: " + success);

// Create an html file containing all rules
success = generator.createHtml("/path/to/file.html");
System.out.println("successfully created the html file: " + success);

Credits

  • railroad-diagrams to create the SVG diagrams of ANTLR 4's rules
  • ANTLR 4 grammar (and runtime) to parse ANTLR 4 grammars
  • Batik to convert SVG to PNG

License

The MIT License (MIT)

Copyright (c) 2014 Bart Kiers

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

More Repositories

1

tiny-language-antlr4

Tiny Language with ANTLR 4
Java
201
star
2

retrofit-oauth

A Retrofit + Google OAuth demo.
Java
200
star
3

Liqp

An ANTLR based 'Liquid Template' parser and rendering engine.
Java
165
star
4

sqlite-parser

An ANTLR4 grammar for SQLite statements.
PLpgSQL
124
star
5

Mu

A small demonstration how to use ANTLR 4's visitor feature.
Java
113
star
6

python3-parser

An ANTLR4 grammar for Python 3
Python
40
star
7

GrahamScan

A Java implementation of the Graham Scan algorithm to find the convex hull of a set of points.
Java
37
star
8

pcre-parser

An ANTLR 4 grammar for PCRE
Java
30
star
9

CompGeom

A computational geometry library using arbitrary-precision arithmetic where possible, written in Java.
Java
26
star
10

RotatingCalipers

A Java implementation of the Rotating Calipers algorithm to find the minimum bounding rectangle of a set of points.
Java
24
star
11

PCREParser

An ANTLR 3 grammar for PCRE
Java
19
star
12

Curta

A small, customizable expression evaluator.
Java
16
star
13

ecmascript-parser

An ANTLR4 grammar for ECMAScript 5.1
Java
15
star
14

lua-parser

A Lua 5.3 parser and AST walker using ANTLR 3
GAP
14
star
15

atlas

Atlas, an offline reverse Geo-coding library written in Java.
Java
13
star
16

antlr4-csv-demo

ANTLR4-CSV-Demo
Java
10
star
17

PGN-parser

A Portable Game Notation (PGN) ANTLR 4 grammar and parser.
ANTLR
7
star
18

antlr-tree-rewriter-java

Java
4
star
19

antlr4android

An Android app with an ANTLR4 expression evaluator.
Java
4
star
20

ICalParser

An iCalendar (RFC 5545) parser backed up by an ANTLR v4 grammar.
Java
3
star
21

iri-parser

An ANTLR 4 grammar and parser for IRI (Internationalized Resource Identifiers).
ANTLR
3
star
22

uri-parser

An ANTLR4 grammar for URIs
ANTLR
2
star
23

antlr-tree-rewriter-cs

C#
1
star
24

ah-bonus-mailer

Albert Heijn bonus mailer
Python
1
star
25

xi

TypeScript
1
star
26

ada2012-parser

Ada
1
star
27

BigO.SPARQLParser

A C# SPARQL parser with rewrite possibilities
C#
1
star