This note is a follow-on to abc.

I show how to build a transpiler for the ABC language in about 20 lines of code.

I use:

  • the ftranspile() function (from transpiler.js)
  • abc.glue - a specification of the transpiler in the Glue SCN1.

Ftranspile() and glue are auxiliary libraries built on top of Ohm-JS.

The ABC Language

This is the most-trivial language that I could imagine, that also shows how to get started using Ohm-JS.

The ABC language is discussed in the ABC note (see below).

Test Sample Program

b = 2
c = 3
a = b + c

Grammar

The file abc.ohm contains the grammar

ABCgrok {
TopLevel = Assignment+

  Assignment =   Variable "=" Expression -- complex
               | Variable "=" number -- simple
  Expression = Variable "+" Variable

    Variable = "a" .. "z"
    number = dig+
    dig = "0" .. "9"
}

I have added an explicit digit rule (dig) instead of using the Ohm-JS library.

Glue (aka action (aka semantics))

TopLevel [@assignments] = [[${console.log (assignments)}]]
Assignment_complex [v keq e] = [[var ${v} = ${e};\n]]
Assignment_simple [v keq n] = [[var ${v} = ${n};\n]]
Expression [v1 kplus v2] = [[${v1} + ${v2}]]
Variable [c] = [[${c}]]
number [digits] = [[${digits}]]
dig [c] = [[${c}]]

Each rule in the glue specification corresponds to a rule in the grammar.

The left-hand-side parameters name each of the sub-matches in the grammar.

Parameters that correspond to iteration nodes in the grammar (+, *, ?) are prefixed with an at symbol @.

The right-hand-side of each specification rule consists of JS template strings enclosed in double-brackets [[]].

Glue also supports raw preamble code enclosed in double-braces ``, but this feature is not use in this example.

Future

Ftranspile() should be a command that runs from the command line.

The syntax for glue was chosen arbitrarily for experimentation. I intend to upgrade the syntax (using glue 0.1 as a preprocessor) but haven’t gotten around to it yet. Having glue run from the command line would allow creating glue pipelines with newer syntax (decommissioning 0.1 is unnecessary, using pipelines).

[Aside: ftranspile() is a first step in creating a command-line version of glue.]

Glue rules should return JS objects instead of returning only strings.

To Run

To run this version, use the bash script

./run.bash

You should see the result

var b = 2;
var c = 3;
var a = b + c;

Github

The details of this solution can be perused in abc-glue

Ohm-JS

ohm-js

Glue

glue tool

FTranspile

[yet to be documented]

ABC

abc

See Also

Table of Contents
Blog
Videos
References

  1. SCN means Solution Centric Notation.