Syntax Diagrams for APEXlang

Introduction

The APEXlang API Reference documentation went live a few days ago. You can also download the APEXlang EBNF grammar. In this post, Kris Rice explains the value of having an EBNF.

Since the APEXlang grammar is available as EBNF, it can be used to generate syntax diagrams. These diagrams (also known as railroad diagrams) make the language easier to explore and provide a visual complement to the API Reference.

In this blog post, I explain how to produce the syntax diagrams for APEXlang.

What Is EBNF

Wikipedia describes the Extended Backus-Naur Form. However, it’s important to notice that there are several variants of EBNF.

APEXlang EBNF

The APEXlang EBNF grammar uses a variant of the BNF in the SQL:2023 standard, with some extensions explained at the top of the apexlang.ebnf file.

1) Syntax conventions in apexlang.ebnf
(* Syntax conventions
   <nl> is significant: component declarations, groups, properties, and closing delimiters are line-oriented.
   [ X ] means optional X. { X } means zero or more X. A | B means either A or B.
*)

Square brackets [] indicate optionality, while curly brackets {} enclose a repeated group as defined in ISO/IEC 14977. However, the apexlang.ebnf file does not fully use this ISO EBNF standard. apexlang.ebnf uses ::= while the ISO standard uses a simple = for symbol definitions. This variant resembles a Wirth EBNF with comments.

Here’s the definition of the app rule in the apexlang.ebnf file.

2) app rule in apexlang.ebnf
<app> ::= "app" [ <required-ws> <component-id> ] <ws> "(" <line-end> { <app-body-line> } <indent> ")" <line-end>

Syntax Diagram

This is the syntax diagram, or railroad diagram, for the app rule.

I produced this syntax diagram with the Railroad Diagram Generator (RR) by Gunter Rademacher. However, this tool requires a W3C EBNF as input. In other words, the original apexlang.ebnf produces runtime errors.

W3C EBNF

Fortunately, it is straightforward to convert the APEXlang EBNF grammar to W3C EBNF. For the current APEXlang grammar, the conversion can be performed entirely through automated text transformations without manual edits.

Here is a list of the most important replacement actions:

ActionExample FromExample To
Replace multiline comments(* ... *)/* ... */
Remove leading < and trailing > in symbol names<app>app
Use expression and ? to express optionality[ ... ]( ... )?
Use expression and * for repetitions{ ... }( ... )*

The rule app in W3C EBNF looks as follows after applying these changes:

3) app rule in apexlang.w3c.ebnf
app ::= "app" ( required-ws component-id )? ws "(" line-end ( app-body-line )* indent ")" line-end

See Convert.java for the complete conversion program.

Syntax Diagrams on GitHub

You cannot produce the complete syntax diagrams in the online version of the Railroad Diagram Generator because the APEXlang grammar is huge, and you will get a timeout when trying.

So, you need to run the tool locally. I’ve created a GitHub workflow that downloads apexlang.ebnf, converts it to a W3C EBNF, and generates an HTML file containing all syntax diagrams. Finally, the workflow publishes the result:

The advantage of syntax diagrams is that they show all usages of a symbol, allowing you to navigate quickly to the relevant definition.

Outlook

The next step is to build a parser for APEXlang and use this as the basis for linting. This would enable APEXlang source code to be automatically validated, helping both developers and AI agents to produce code that conforms to defined quality standards.

Looking further ahead, we are considering providing full support for APEXlang within the dbLinter tool suite. Currently, dbLinter analyses only SQL and PL/SQL code blocks embedded in APEXlang files. Full APEXlang support is a natural evolution. Stay tuned!

3 Comments

  1. I still remember when Prof. Niklaus Wirth defined Modula-2 using EBNF; that marked a significant breakthrough and an attempt to standardise language specifications. Nowadays, there are many representation formats for EBNF—for verifiers, compiler generators, parser tools, railroad diagrams, and so on—as well as an ISO standard for EBNF: https://www.cl.cam.ac.uk/~mgk25/iso-14977.pdf.
    Perhaps a good approach would be to provide EBNF converters as Python libraries for various use cases.
    In any case, Oracle has taken a promising path with EBNF for APEXlang—much like they did for PGQL (https://pgql-lang.org/).

    • Perhaps a good approach would be to provide EBNF converters as Python libraries for various use cases.

      To do this, we need a complete list of EBNFs with full specifications.
      I tried to find the Wirth EBNF specification, but was unsuccessful.
      In my opinion, a parser is required for each EBNF to produce reliable transpiler results.
      The approach I took in this case is somewhat hacky, but it works for the current version of apexlang.ebnf.

      Please let me know once you have found (provided?) such a library. ;-)

  2. Here is Prof. Niklaus Wirth’s Modula-2 language definition incl. the EBNF of Modula-2:
    https://www.research-collection.ethz.ch/bitstreams/289cc859-94e5-4758-8786-ac05437780a2/download
    and about the syntax notation: https://grokipedia.com/page/Wirth_syntax_notation
    A good paper about EBNF: https://www.cs.cmu.edu/~pattis/misc/ebnf2.pdf .

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.