Skip to content

Weltraumschaf/jebnf

Repository files navigation

EBNF Parser and Image Generator for PHP/Java

This package contains classes for scanning and parsing EBNF grammar files and generate images with railroad diagrams for that grammar.

This repository contains a work in progress implementation in Java.

The original code I discovered here. But that PHP project seems to be discontinued. So I decided to refactor and port the code to PHP5. After that I decided to port it to Java for learning more Java.

Install

You can install the EBNF package library and the command line tool via Git and Maven:

Get the repository:

$ git clone git@github.com:Weltraumschaf/jebnf.git

Build and instal:

$ cd jebnf && mvn install

After successful installation you should be able to invoke the command line tool:

$ ./bin/ebnf -h

File Signatures

To receive the keys type:

$ gpg --keyserver hkp://pool.sks-keyservers.net --recv-keys BA265082

Usage

You can either use the shell script bin/ebnf for generating images or XML from a grammar file:

$ ./bin/jebnf -h
usage: jebnf
 -d         Enables debug output.
 -f <arg>   Output format: xml, jpg, gif.
 -h         This help.
 -i,--ide   Starts the GUI IDE.
 -o <arg>   Output file name.
 -s <arg>   EBNF syntax file to parse.
 -t         Prints textual representation of the syntax tree to stdout.

Or you can use the classes for embedding the functionality in your code:

[[TBD]]

Short introduction to EBNF

EBNF is a code that expresses the grammar of a computer language. An EBNF consists of terminal symbol and non-terminal production rules which are the restrictions governing how terminal symbols can be combined into a legal sequence. Wikipedia

Describing EBNF with EBNF as an example:

syntax     = [ title ] "{" { rule } "}" [ meta ] .
rule       = identifier ( "=" | ":" | ":==" ) expression ( "." | ";" ) .
expression = term { "|" term } .
term       = factor { factor } .
factor     = identifier
           | literal
           | range
           | comment
           | "[" expression "]"
           | "(" expression ")"
           | "{" expression "}" .
identifier = character { character } .
range      = character ".." character .
title      = literal .
meta       = literal .
literal    = "'" character { character } "'"
           | '"' character { character } '"' .
comment    = "(*" character { character } "*)" .
character  = "a" .. "z"
           | "A" .. "Z"
           | "0" .. "9" .

Table of symbols

Here is a list of symbols implemented in the package. There are a lot of variants of (E)BNFs out in the wild with some more or other symbols. This package implements only a reasonable subset.

definition
= or : or :==
termination
; or .
alternation
|
option
[ ... ]
repetition
{ ... }
grouping
( ... )
terminal string
" ... " or ' ... '
comment
(* ... *)

 ## Ideas for an IDE

The IDE has two panes:

  1. Text editor for the EBNF syntax with highlighting. Highlighted tokens are identifier and terminals. Matched highlighting on same identifiers or corresponding braces. The editor has line numbers and signals syntax errors.
  2. Graphic preview which renders the EBNF as railroad diagram.

The view mode may be switched between horizontal split, vertical split and tabbed view. In the tabbed view source and preview are tabs. The IDE provides export to XML, PNG, JPG, GIF, and PDF.

The IDE also provides a project view. It holds references to a project configuration file for each project and expands a tree view to files and directories added to the project.

Project file format (JSON):

./ebnf.project:

{
    "name":  "The Project Name",
    "files": ["/foo/bar/baz.ebnf", "..."],
    "directories": ["/foo/snafu", "..."]
}

TODO

  • Consioder Latex or SVG.
  • Use Graphics2D#translate(int x, int y) instead of absolute position by drawing shapes.
  • Fix NPE if no -o given (check other opts, too)

About

Java tool/lib to parse EBNF syntax and generate railroad diagrams.

Resources

License

Stars

Watchers

Forks

Packages

No packages published