1. Article structure

linked from (parent) similar files in same level important links in this

Overview Java Text XML conversion

→ This page →

ZBNF syntax description

2. History

This is a personality presentation.

I have firstly used microprocessors from 1978, in may master thesis. That was programmed in assembly with the Z80 processor. The microprocessor technology was very newly. Firstly I have written some programs to show and change RAM content and such nones.

Later, from 1982 till 1985, Basic language becomes familiar and I have written a system with Basic, also possible to edit on the fly storing a meta machine code. It may be seen as similar to a byte code as in Java.

In that time I want to write a complete and complex parser using the idea of graphical syntax diagrams https://en.wikipedia.org/wiki/Syntax_diagram. But unfortunately I have only assembly language.

I the 1990th using C/ language I have tried to write this parser, but on the first hand I have not the necessary time, and second hand the C/ language and my development environment and knowledge was not enough powerful to do so.

Only when I used Java, the idea of a parser working on the principles of syntax graphs was successfully. I have written the first version of the parser on only one week end. Java which its capabilities for container and the before written class org.vishia.util.StringPartScan which has had its precursor in some C++ classes has enabled this work.

The result was org.vishia.zbnf.ZbnfParser This was in the year 2007, ~20 years after the first ideas.

3. Approach

ZBNF is firstly a syntax description which can be used outside of the parser itself. It is similar or an enhancement of EBNF from Prof. Niklaus Wirth. The EBNF idea bases on the BNF which is the "''Backus Naur Format''" to has described the syntax of programming language in the 50th and 60th (https://en.wikipedia.org/wiki/Backus–Naur_form) especially for Algol.

The ZBNF has the semantic aspect in the formal syntax definition. The 'Z' is a reverse 'S' for ''Semantic'. With that it is possible to immediately store the parsed data in Java instances without additional effort. That is done by the org.vishia.zbnf.ZbnfParser.

The `org,vishia.zbnf.ZbnfParser works with the textual syntax script. It reads the script, stores it as Java data for fast using.

The ZBNF syntax script can also be used to generate proper data storage classes, see Example storage class for data, how to get

4. Working principle

The parser itself tests the source code similar as the Syntax Diagram (see https://en.wikipedia.org/wiki/Syntax_diagram or https://en.wikipedia.org/wiki/Wirth_syntax_notation (both articles are similar).

It tests all possibilities, go back to the previous fork point if nothing matches and hence search the correct path. This is not the fastest way to parse, but it works proper with all necessities of parsing approaches. The speed to parse is not the first question in the century of fast processors. It was a important question in the 1980th and 1990th if processor power was lesser. (This was the idea of parsing by me also in the 1980th, a simple way to parse, imagined by the syntax diagrams).

5. Usages

This ZbnfParser.html was used firstly for parsing C language header and C files to translate to Java code: ../../../Java2C/index.html. Secondly I have used reflection for C/++ programming. To create the code of the reflection file, the parser was used with the same syntax as for Java2C translation. See ../../../Inspc/html/Reflect.html[].

Later all texts which I have parsed uses this parser.

The parser was written independently of other known parser principles. It works with a textual given syntax and does not need deep depending packages. Only some Java files all contained in the 1.4 MByte vishiaBase.jar package are necessary. Though the parser is fast enough for all practical approaches. The textual given syntax is firstly translated in internal pre prepared data, then it is used.

The parser is used also in some professional projects.

6. Example for a ZBNF syntax file, how to get

A bill of material should be used as example. This may be given in any special format, in this case in a simple list maybe from older tools.

Bill of material
================

order-number: 134.23.14
date:febr-16-2008

amount  code     description     value
---------------------------------------------
  21    1234567  Resistor        3.9 kOhm/5%
  12    1234537  Resistor        1.8 kOhm/2%
   5    1234237  Resistor        2.7 kOhm/2%
   7    1234557  Resistor         10 kOhm/5%
   1    1234127  Resistor        120 Ohm/1%
   2    1234897  Resistor        630 Ohm/1%
  34    1235771  capacitor        47 nF
  12    1235781  capacitor       100 nF
   5    1235791  capacitor       4.7 uF
---------------------------------------------

The file format is not very well for parsing. Unified delimiters between rows are missing. It seems to be spaces are delimiters. But in the last column spaces are parts of the text. The description should be only 16 characters, so the specific rule (for the example).

With this information the following ZBNF syntax file is created manually:

<?ZBNF-www.vishia.org version="1.0" encoding="iso-8859-1" ?>

$setLinemode.

BillOfMaterial::=
  <*|order-number?>          ##skip over all characters until string "order-number"

  order-number : <#?order/@part1>\.<#?order/@part2>\.<#?order/@part3> \n  ##its a three-part number. NOTE: . must be written \.
  date : <date> \n           ##date have its own syntax.

  <*|---?><*\n?>\n           ##skip over all until ---- and than until newline,than accept newline.
  { <position> \n }
  <*|---?><*\n?>             ##skip over all until ---- and than until newline.
  [{ \n}] \e                 ##skip over all newline, than end of file is expected.
.

##NOTE: Notes to the syntax of input text:
##The fields amount and code are red as number, whitespaces before and behind were skipped.
##But the description is not terminated by chars, but it is a maximum of chars.
##The description is stored with white spaces on end.
##The value is also a block without any terminating chars, else a line end with possible carrige return.
##
position::= <#?amount> <#?code> <16*?description>  <*\r\n?value> .

date::= <*\r\n?date>.

As you see …​ TODO

7. Example storage class for data, how to get

xxx

8. Example for parsing with ZBNF syntax and storage class

The example is contained in