Grammar as Train Track

Programming Language

declarative / BNF / EBNF

Form

Analogy

Attribution — Origin / Source

Collected by Matthias Hauswirth — Own practice, based on 'railroad diagrams'

Conceptual Advantage

Pushing a train engine through the track makes visible the process of interpretation (generation or parsing) of the grammar (that is the track).

Mapping

PLNM
grammar rule a train track with a start and an end
parsing or generating pushing a train engine through the train track
terminal symbol (in a grammar rule) tunnel representing a terminal symbol, generating or recognizing the text with which it is labeled
non-terminal symbol (in a grammar rule) tunnel representing a non-terminal symbol, invoking the rule mentioned on its label
sequence track segment going from one tunnel to the next
selection two switches to split and join the track, requiring the train engine to decide which track to take
repetition two switches allowing the train engine to go back and repeat a part of the grammar multiple times

Draws Attention To

The process of parsing or generating driven by a grammar.

Use When

Introducing formal grammars (BNF / EBNF).

Cost

Space to build train track (non-trivial grammars will need multiple large tables or significant floor space).

Details

The idea is described under the name “Language Trains”, one of three different “programming languages” that strive to eliminate abstraction, in the following paper:

Check this research paper

Matthias Hauswirth, Andrea Adamoli, and Mohammad Azadmanesh
The Program is the System
Koli Calling '17

This notional machine was used to explain grammars to high school students and the general public in the 10-year anniversary exhibition of the Faculty of Informatics of USI.

It was further used, in a virtual form, in a course on programming language concepts for high school informatics teachers in the Swiss state of Ticino:

Usage Note

Non-terminal symbols appearing in a grammar rule mean that one has to take an additional train engine to start processing the rule of that non-terminal (i.e., one needs multiple train engines, and it can be confusing to understand which prior engine to go back to when one finishes the track of a rule).

Comments or Feedback?

Do you have feedback on this notional machine? Did you find a mistake, or do you have a request for improvement? You can create an Issue on GitHub, where the description is hosted. This way we can see your feedback and address it.

For this, you need a GitHub account. Then follow this link to see the source file of this page. In there, click the ... left of the highlighted line, then pick "Reference in a new issue".

Create an Issue on GitHub