A meta approach to implementing programming languages

Published on 2018-12-02.

28 May 2019: Added link to next article in Putting it together.

How does the computer know what to do with the following expression?

1 + 2 * 3

How does it know how to recognize the sequence of characters as an arithmetic expression? How does it know that 2*3 should be computed first? How does it translate it to instructions that execute on the CPU?

In this article I present a metalanguage that I've developed that can help answer those questions.

Metalanguages

Metalanguages are used to reason about languages. In metalanguages you can make statements about statements in a different language.

The metalanguage I've developed is called RLMeta. It is inspired by a metalanguage from the sixties called META II. I wanted to develop my own version of META II to understand it deeply. RLMeta is also inspired by OMeta (another META II derivative).

RLMeta is a programming language in which you write grammars. Grammars have rules that specify how to match objects from an input stream and specify what should happen when objects are matched. The RLMeta compiler translates grammars into programs that recognize the objects specified in the grammar and evaluates the semantic actions when the objects are matched.

Overview of RLMeta compiler.

Interpreting expressions

How can RLMeta be used to give meaning to arithmetic expressions of the kind presented in the introductory example? Here is a grammar:

calculator calculator.rlmeta C a l c u l a t o r { e x p r e s s i o n = | a d d i t i v e a d d i t i v e = | m u l t i t i v e : x ' + ' a d d i t i v e : y - > a d d ( x y ) | m u l t i t i v e m u l t i t i v e = | d i g i t : x ' * ' m u l t i t i v e : y - > m u l ( x y ) | d i g i t d i g i t = | ' 0 ' - ' 9 ' : x - > i n t ( x ) }

This grammar is called Calculator . It has four rules. The first rule says that an expression is an additive. The second rule says that an additive is either a multitive followed by the character '+' followed by another additive or just a multitive. The second case is only tried if the first does not match. The third rule says that a multitive is either a digit followed by the character '*' followed by another multitive or just a digit. The fourth rule says that a digit is a character in the range 0-9. The : followed by a name binds the result of a match to a variable. The expressions to the right of -> are semantic actions. They specify what should happen on a match. They can refer to variables. In this grammar they say that whenever an additive is matched, call the host language function add with the left and right side, and whenever a multitive is matched, call the host language function mul with the left and right side, and whenever a digit is matched, call the host language function int with the digit character. The host language function int converts a digit character to an integer and the add and mul functions perform addition and multiplication.

This grammar describes how to recognize an arithmetic expression in a sequence of characters. Precedence is encoded by the order of the rules. An additive is x1 + x2 + x3 + .. where the xes are multitives. Therefore multiplication is performed before addition. It gives meaning to the expression by calling host language functions when parts are matched.

When the calculator grammar is fed to the RLMeta compiler, a program is output that is an interpreter for arithmetic expressions.

Overview of calculator compilation.

More specifically, this program is a Python class that implements interpretation of arithmetic expressions. The class depends on a support library and also on the host language functions that were called from the grammar ( add , mul , and int ). Host language refers to the language that grammars are compiled to. In this case Python. The pieces must be assembled to form an executable program. Here is a template for the final Python file that implements a read-eval-print loop (REPL) for arithmetic expressions:

calculator compile.sh python file template f r o m o p e r a t o r i m p o r t a d d , m u l $ s u p p o r t _ p y $ c a l c u l a t o r _ p y i f _ _ n a m e _ _ = = " _ _ m a i n _ _ " : c a l c u l a t o r = C a l c u l a t o r ( ) w h i l e T r u e : l i n e = r a w _ i n p u t ( " > " ) r e s u l t = c a l c u l a t o r . r u n ( " e x p r e s s i o n " , l i n e ) p r i n t ( r e s u l t )

First the host language functions are imported ( int is always available). Then the support library and the compiled calculator grammar snippets are inserted. Finally the main method which is the REPL is implemented. Compiled grammars are used by instantiating them and calling their run method with the name of the rule and the input object. This template is rendered with a Bash script:

calculator compile.sh # ! / b i n / b a s h s e t - e c d " $ ( d i r n a m e " $ 0 " ) " s u p p o r t _ p y = $ ( p y t h o n . . / r l m e t a / r l m e t a . p y - - s u p p o r t ) c a l c u l a t o r _ p y = $ ( p y t h o n . . / r l m e t a / r l m e t a . p y < c a l c u l a t o r . r l m e t a ) c a t < < E O F < < p y t h o n f i l e t e m p l a t e > > E O F

First the set -e directive ensures that the script exits as soon as there is an error. Then the directory is changed to the one where the compile script lives. Then the output of two calls to the RLMeta compiler ( rlmeta.py ) are captured in two variables. The RLMeta compiler reads a grammar from stdin and writes a Python class to stdout. If the --support flag is given, it writes the support library to stdout instead. The command < file syntax redirects the contents of the file to the command's stdin. Finally the Python file template is rendered and written to stdout using a here document which has access to the previously captured variables.

Example usage on the command line:

$ p y t h o n < ( . / c a l c u l a t o r / c o m p i l e . s h ) > 1 + 2 * 3 7

The compile script writes a Python file to stdout. The <(command) syntax is process substitution and turns the output of the command into a temporary file which can then be run with Python.

In this example, the input stream to the calculator becomes a list of characters:

[ ' 1 ' , ' + ' , ' 2 ' , ' * ' , ' 3 ' ]

When the calculator matches the expression, the following host language functions will be called:

a d d ( i n t ( ' 1 ' ) , m u l ( i n t ( ' 2 ' ) , i n t ( ' 3 ' ) ) )

Compiling expressions

The previous example relied on host language functions to perform addition and multiplication. The meaning of an expression was defined in terms of the meaning of Python functions. To understand what an expression means, you need to understand how Python implements those functions. The next example compiles an expression down to a kind of assembly language that eliminates the need for Python.

For this compilation, two grammars are written: a parser and a code generator. The parser looks similar to the calculator grammar but instead of evaluating the expression, it creates an abstract syntax tree (AST) describing the expression:

expression parser.rlmeta P a r s e r { e x p r e s s i o n = | a d d i t i v e a d d i t i v e = | m u l t i t i v e : x ' + ' a d d i t i v e : y - > [ " a d d " x y ] | m u l t i t i v e m u l t i t i v e = | d i g i t : x ' * ' m u l t i t i v e : y - > [ " m u l " x y ] | d i g i t d i g i t = | ' 0 ' - ' 9 ' : x - > [ " d i g i t " x ] }

The bracket notation in the semantic actions creates lists. Nodes in the AST are represented as lists where the first item is a string denoting the type of node.

The code generator takes as input an AST from the parser and generates assembly language code for an imaginary stack machine:

expression codegenerator.rlmeta C o d e G e n e r a t o r { a s t = | [ " a d d " a s t : x a s t : y ] - > { x y " a d d " " \ n " } | [ " m u l " a s t : x a s t : y ] - > { x y " m u l " " \ n " } | [ " d i g i t " . : x ] - > { " p u s h " x " \ n " } }

This grammar has only one rule: ast . It says that an AST is either a list that starts with the string 'add', or a list that starts with the string 'mul', or a list that starts with the string 'digit'. The add and mul cases recursively match AST nodes as their left and right side whereas the digit matches anything ( . ) which is the digit stored in the AST node. The semantic actions in this grammar generate string output which is denoted by the curly braces. When a digit AST node is matched, the string 'push [digit]

' is generated. It instructs the stack machine to push the given digit to the stack. For add and mul, instructions for the operands are first output followed by an 'add

' or 'mul

' instruction. They instruct the stack machine to pop two numbers off the stack, add or multiply them, and push the result.

This grammar describes how to recognize and AST in a sequence of objects. It gives meaning to the AST by generating assembly language code when AST nodes are matched.

When the expression grammars are fed to the RLMeta compiler, programs are output whose combination is a compiler for arithmetic expressions.

Overview of expression compilation.

Here is a template for the final Python file that implements a REPL for arithmetic expression compilation:

expression compile.sh python file template i m p o r t s y s $ s u p p o r t _ p y $ p a r s e r _ p y $ c o d e g e n e r a t o r _ p y i f _ _ n a m e _ _ = = " _ _ m a i n _ _ " : p a r s e r = P a r s e r ( ) c o d e g e n e r a t o r = C o d e G e n e r a t o r ( ) w h i l e T r u e : l i n e = r a w _ i n p u t ( " > " ) a s t = p a r s e r . r u n ( " e x p r e s s i o n " , l i n e ) a s s e m b l y = c o d e g e n e r a t o r . r u n ( " a s t " , a s t ) s y s . s t d o u t . w r i t e ( a s s e m b l y )

First the sys module is imported because the main method needs it. Then the support library, compiled parser grammar, and compiled code generator grammar snippets are inserted. Finally the main method which is the REPL is implemented. The output of the parser is fed to the code generator and its output is finally written to stdout. This template is rendered with a Bash script:

expression compile.sh # ! / b i n / b a s h s e t - e c d " $ ( d i r n a m e " $ 0 " ) " s u p p o r t _ p y = $ ( p y t h o n . . / r l m e t a / r l m e t a . p y - - s u p p o r t ) p a r s e r _ p y = $ ( p y t h o n . . / r l m e t a / r l m e t a . p y < p a r s e r . r l m e t a ) c o d e g e n e r a t o r _ p y = $ ( p y t h o n . . / r l m e t a / r l m e t a . p y < c o d e g e n e r a t o r . r l m e t a ) c a t < < E O F < < p y t h o n f i l e t e m p l a t e > > E O F

First the set -e directive ensures that the script exits as soon as there is an error. Then the directory is changed to the one where the compile script lives. Then the output of three calls to the RLMeta compiler are captured in three variables. Finally the Python file template is rendered and written to stdout.

Example usage on the command line:

$ p y t h o n < ( . / e x p r e s s i o n / c o m p i l e . s h ) > 1 + 2 * 3 p u s h 1 p u s h 2 p u s h 3 m u l a d d > 1 * 2 + 3 p u s h 1 p u s h 2 m u l p u s h 3 a d d

In the first example, the input stream to the parser becomes a list of characters (same as for the calculator):

[ ' 1 ' , ' + ' , ' 2 ' , ' * ' , ' 3 ' ]

The input stream to the code generator becomes a list with a single object which is a list (the root AST node):

[ [ ' a d d ' , [ ' d i g i t ' , ' 1 ' ] , [ ' m u l ' , [ ' d i g i t ' , ' 2 ' ] , [ ' d i g i t ' , ' 3 ' ] ] ] ]

The generated assembly language code is much closer to CPU instructions than the Python-based interpreter. A grammar could be written to convert these assembly instructions to assembly instructions of a real CPU. But I will not do that here. The point is that the meaning of an expression can be described by a series of transformations that eventually output machine instructions.

RLMeta implementation

So far I've just given informal descriptions of how RLMeta works. To fully understand how arithmetic expressions are evaluated and compiled, you need to understand how RLMeta is implemented.

The RLMeta compiler translates a grammar to a Python class. This translation is implemented in RLMeta itself. A grammar is, like an expression, translated in two steps: the parser translates grammar syntax to an AST and the code generator translates the AST to a Python class. The generated Python class depends on a support library.

RLMeta compiler internals illustrated.

The RLMeta compiler thus comprises three pieces: the parser, the code generator, and the support library.

Parser

This section defines the parser:

rlmeta parser.rlmeta P a r s e r { < < r u l e s > > }

Grammar

The top level syntactic element is a grammar. A grammar has a name followed by rules enclosed in curly braces. When this is matched, a Grammar AST node is created containing the name of the grammar and the rule AST nodes:

rlmeta parser.rlmeta rules g r a m m a r = | n a m e : x s p a c e ' { ' r u l e * : y s s p a c e ' } ' - > [ " G r a m m a r " x ~ y s ]

Throughout the parser, space is ignored. As a rule of thumb, it is inserted before matching a character sequence (and not before matching other rules).

The * operator after rule means match the preceding expression zero or more times. The result is a list.

The ~ operator in the semantic action means splice the list in-line into the enclosing list. The Grammar AST node thus has the name as element one, the first rule AST node as element two, the second rule AST node as element three, and so on.

Rule

A rule has a name followed by an equal sign followed by a choice. When this is matched, a Rule AST node is created containing the name and the choice AST node:

rlmeta parser.rlmeta rules r u l e = | n a m e : x s p a c e ' = ' c h o i c e : y - > [ " R u l e " x y ]

Choice

A choice has sequences separated by vertical bars. Optionally the first sequence can start with a vertical bar to allow all sequence lines to look the same. When this is matched, an Or AST node is created containing the sequence AST nodes:

rlmeta parser.rlmeta rules c h o i c e = | ( s p a c e ' | ' ) ? s e q u e n c e : x ( s p a c e ' | ' s e q u e n c e ) * : x s - > [ " O r " x ~ x s ]

The ? operator means match the preceding expression zero or one time.

The result of xs is a list of sequences since the expression inside parenthesis returns the last match (which is a sequence).

Sequence

A sequence has one or more expressions. When this is matched, a Scope AST node is created containing an And AST node containing the expression AST nodes:

rlmeta parser.rlmeta rules s e q u e n c e = | e x p r : x e x p r * : x s - > [ " S c o p e " [ " A n d " x ~ x s ] ]

Expression

An expression is one of the following sequences:

rlmeta parser.rlmeta rules e x p r = | e x p r 1 : x s p a c e ' : ' n a m e : y - > [ " B i n d " y x ] | e x p r 1

The first sequence is an expression of level 1 followed by a colon followed by a name. When this is matched, a Bind AST node is created containing the name and the expression AST node.

The second sequence is an expression of level 1.

Expression of level 1

An expression of level 1 is one of the following sequences:

rlmeta parser.rlmeta rules e x p r 1 = | e x p r 2 : x s p a c e ' * ' - > [ " S t a r " x ] | e x p r 2 : x s p a c e ' ? ' - > [ " O r " x [ " A n d " ] ] | s p a c e ' ! ' e x p r 2 : x - > [ " N o t " x ] | e x p r 2

The first sequence is an expression of level 2 followed by an asterisk. When this is matched, a Star AST node is created containing the expression AST node.

The second sequence is an expression of level 2 followed by a question mark. When this is matched, an Or AST node is created containing the expression AST node and an empty And AST node. There is no dedicated AST node for the ? operator, but it is equivalent to matching the expression or zero expressions and'ed.

The third sequence is an exclamation mark followed by an expression of level 2. When this is matched, a Not AST node is created containing the expression AST node.

The fourth sequence is an expression of level 2.

Expression of level 2

An expression of level 2 is one of the following sequences:

rlmeta parser.rlmeta rules e x p r 2 = | s p a c e ' - > ' h o s t E x p r : x - > [ " S e m a n t i c A c t i o n " x ] | n a m e : x ! ( s p a c e ' = ' ) - > [ " M a t c h R u l e " x ] | s p a c e c h a r : x ' - ' c h a r : y - > [ " M a t c h R a n g e " x y ] | s p a c e s t r i n g : x - > [ " M a t c h S t r i n g " x ] | s p a c e c h a r s e q : x - > [ " M a t c h C h a r s e q " x ] | s p a c e ' . ' - > [ " M a t c h A n y " ] | s p a c e ' ( ' c h o i c e : x s p a c e ' ) ' - > x | s p a c e ' [ ' e x p r * : x s s p a c e ' ] ' - > [ " M a t c h L i s t " [ " A n d " ~ x s ] ]

The first sequence is the characters '->' followed by a host expression. When this is matched, a SemanticAction AST node is created containing the expression AST node.

The second sequence is a name that is not followed by an equal sign (otherwise it would also match the start of a rule). When this is matched, a MatchRule AST node is created containing the name.

The third sequence is a character followed by a dash followed by another character. When this is matched, a MatchRange AST node is created containing the two characters.

The fourth sequence is a string. When this is matched, a MatchString AST node is created containing the string.

The fifth sequence is a character sequence. When this is matched, a MatchCharseq AST node is created containing the character sequence.

The sixth sequence is a dot. When this is matched, a MatchAny AST node is created.

The seventh sequence is an open parenthesis followed by a choice followed by a closing parenthesis. When this is matched, the choice AST node is returned.

The eighth sequence is an open bracket followed by expressions followed by a closing bracket. When this is matched, a MatchList AST node is created containing an And AST node containing the expressions.

Host expression

A host expression is one of the following sequences:

rlmeta parser.rlmeta rules h o s t E x p r = | s p a c e s t r i n g : x - > [ " S t r i n g " x ] | s p a c e ' [ ' h o s t E x p r L i s t I t e m * : x s s p a c e ' ] ' - > [ " L i s t " ~ x s ] | s p a c e ' { ' b u i l d E x p r * : x s s p a c e ' } ' - > [ " B u i l d e r " ~ x s ] | n a m e : x s p a c e ' ( ' h o s t E x p r * : y s s p a c e ' ) ' - > [ " F n C a l l " x ~ y s ] | n a m e : x - > [ " V a r L o o k u p " x ]

The first sequence is a string. When this is matched, a String AST node is created containing the string.

The second sequence is an open bracket followed by host expression list items followed by a closing bracket. When this is matched, a List AST node is created containing the list item AST nodes.

A list item is either a host expression preceded by the ~ operator, in which case a ListItemSplice AST node is created containing the expression, or a host expression:

rlmeta parser.rlmeta rules h o s t E x p r L i s t I t e m = | s p a c e ' ~ ' h o s t E x p r : x - > [ " L i s t I t e m S p l i c e " x ] | h o s t E x p r

The third sequence is an open curly brace followed by build expressions followed by a closing curly brace. When this is matched, a Builder AST node is created containing the expression AST nodes.

A build expression is either a greater than character, in which case an IndentBuilder AST node is created, or a less than character, in which case a DedentBuilder AST node is created, or a host expression.

rlmeta parser.rlmeta rules b u i l d E x p r = | s p a c e ' > ' - > [ " I n d e n t B u i l d e r " ] | s p a c e ' < ' - > [ " D e d e n t B u i l d e r " ] | h o s t E x p r

The fourth sequence is a name followed by an open parenthesis followed by host expressions followed by a closing parenthesis. When this is matched, a FnCall AST node is created containing the name and expression AST nodes.

The fifth sequence is a name. When this is matched, a VarLookup AST node is created containing the name.

Character related

Character related rules capture strings, character sequences, and single characters. Inside all of them a few escape codes are possible. When this is matched, the characters inside the delimiters are joined together to create the string:

rlmeta parser.rlmeta rules s t r i n g = ' " ' ( ! ' " ' i n n e r C h a r ) * : x s ' " ' - > j o i n ( x s ) c h a r s e q = ' \ ' ' ( ! ' \ ' ' i n n e r C h a r ) * : x s ' \ ' ' - > j o i n ( x s ) c h a r = ' \ ' ' ! ' \ ' ' i n n e r C h a r : x ' \ ' ' - > x i n n e r C h a r = ' \ \ ' e s c a p e | . e s c a p e = ' \ \ ' - > " \ \ " | ' \ ' ' - > " ' " | ' " ' - > " \ " " | ' n ' - > " \ n "

Name

A name has at least one alphabetic character followed by any number of alphanumeric characters. When this is matched, the individual characters are joined together to create a string:

rlmeta parser.rlmeta rules n a m e = s p a c e n a m e S t a r t : x n a m e C h a r * : x s - > j o i n ( [ x ~ x s ] ) n a m e S t a r t = ' a ' - ' z ' | ' A ' - ' Z ' n a m e C h a r = ' a ' - ' z ' | ' A ' - ' Z ' | ' 0 ' - ' 9 '

Space

A space is any number of space characters or newlines:

rlmeta parser.rlmeta rules s p a c e = ( ' ' | ' \ n ' ) *

Code generator

This section defines the code generator and the support library:

rlmeta codegenerator.rlmeta C o d e G e n e r a t o r { < < r u l e s > > }

rlmeta support.py < < c l a s s e s > >

Note on target language

The choice of Python as the target language for code generation is an implementation detail. A different target language could easily be used. Say for example that you would like to use RLMeta in a web browser. In that case JavaScript must be used as the target language. RLMeta could be ported to JavaScript by modifying the code generator and the support library.

Parsing algorithm

The parsing algorithm that RLMeta implements is based on parsing expression grammars (PEG), but is extended to match arbitrary objects, not just characters. Another way to describe the parsing algorithm is that it is a recursive descent parser with backtracking an memoization. Details of the algorithm is shown in the remainder of this section.

Structure of code generator

The code generator has two main rules: ast and astFnBody :

rlmeta codegenerator.rlmeta rules a s t = < < a s t > > | a s t F n B o d y : x - > { " ( l a m b d a : \ n " > x < " \ n ) " } a s t F n B o d y = < < a s t F n B o d y > >

Sometimes generated code for an AST node should be wrapped in a lambda. Those AST nodes are added to the astFnBody rule. The astFnBody rule is not strictly needed, but without it, many rules would have to wrap its output in a lambda.

The greater than and less than characters in the string output expression cause an indent and a dedent like this:

( l a m b d a : x )

Grammar

When a Grammar AST node is matched, a Python class inheriting _Grammar is generated:

rlmeta codegenerator.rlmeta rules ast | [ " G r a m m a r " . : x a s t * : y s ] - > { " c l a s s " x " ( _ G r a m m a r ) : \ n " > y s < }

The name of the class is the same as the name of the grammar and the child AST nodes are assumed to generate methods in the class.

The _Grammar class is defined in the support library:

rlmeta support.py classes c l a s s _ G r a m m a r ( o b j e c t ) : < < _ G r a m m a r > >

Names of support classes start with underscore to not collide with generated grammar names (which can not contain underscores).

Rule

When a Rule AST node is matched, a Python method with a name prefixed with _rule_ is generated:

rlmeta codegenerator.rlmeta rules ast | [ " R u l e " . : x a s t : y ] - > { " \ n d e f _ r u l e _ " x " ( s e l f ) : \ n " > " r e t u r n " y " ( ) \ n " < }

The method name ends with the same name as the rule. The child AST node is assumed to generate a matcher. A matcher is a function that tries to match objects from the input stream and return a semantic action if it succeeds or raise an exception if it fails. That function is called from the generated method and the semantic action is returned.

Or

When an Or AST node is matched, a matcher that calls the built-in _or method is generated:

rlmeta codegenerator.rlmeta rules astFnBody | [ " O r " a s t I t e m s : x ] - > { " s e l f . _ o r ( [ " x " ] ) " }

Rules to generate a list of items:

rlmeta codegenerator.rlmeta rules a s t I t e m s = a s t I t e m * : x s - > { " \ n " > x s < } a s t I t e m = a s t : x - > { x " , \ n " }

The generated string is wrapped in a lambda because the code is added to the astFnBody rule and will thus look like this:

( l a m b d a : s e l f . _ o r ( [ m a t c h e r 1 , m a t c h e r 2 , . . . ] ) )

The _or method expects a list of matchers which are tried in sequence:

rlmeta support.py classes _Grammar d e f _ o r ( s e l f , m a t c h e r s ) : o r i g i n a l _ s t r e a m = s e l f . _ s t r e a m f o r m a t c h e r i n m a t c h e r s : t r y : r e t u r n m a t c h e r ( ) e x c e p t _ M a t c h E r r o r : s e l f . _ s t r e a m = o r i g i n a l _ s t r e a m o r i g i n a l _ s t r e a m . f a i l ( " n o c h o i c e m a t c h e d " )

The result of the first succeeding matcher is returned. The input stream is stored in _stream . Streams are immutable, so resetting the stream upon failure is just a matter of saving and restoring _stream . _MatchError is the name of the exception that is raised when a match fails. Streams have a fail method that generates that exception and adds context to it that is useful for error reporting.

Scope

When a Scope AST node is matched, a matcher that creates a new scope is generated:

rlmeta codegenerator.rlmeta rules astFnBody | [ " S c o p e " a s t : x ] - > { " ( l a m b d a _ v a r s : \ n " > x < " ( ) \ n ) ( _ V a r s ( ) ) " }

A scope is a set of variables that do not interfere with variables in other scopes. The name _vars is used to refer to variables in the current scope. The child AST node is assumed to generate a matcher which is called to return a semantic action. The generated string will thus look like this:

( l a m b d a : ( l a m b d a _ v a r s : m a t c h e r ( ) ) ( _ V a r s ( ) ) )

The _Vars class is a subclass of a Python dictionary:

rlmeta support.py classes c l a s s _ V a r s ( d i c t ) : < < _ V a r s > >

And

When an And AST node is matched, a matcher that calls the built-in _and method is generated:

rlmeta codegenerator.rlmeta rules astFnBody | [ " A n d " a s t I t e m s : x ] - > { " s e l f . _ a n d ( [ " x " ] ) " }

The _and method expects a list of matchers which are called in sequence:

rlmeta support.py classes _Grammar d e f _ a n d ( s e l f , m a t c h e r s ) : r e s u l t = N o n e f o r m a t c h e r i n m a t c h e r s : r e s u l t = m a t c h e r ( ) r e t u r n r e s u l t

The result of the last matcher is returned.

Bind

When a Bind AST node is matched, a matcher that binds the name to a value in the current scope is generated:

rlmeta codegenerator.rlmeta rules astFnBody | [ " B i n d " . : x a s t : y ] - > { " _ v a r s . b i n d ( " r e p r ( x ) " , " y " ( ) ) " }

The child AST node is assumed to generate a matcher which is called to make the value a semantic action.

The bind method stores and returns a value with the given name:

rlmeta support.py classes _Vars d e f b i n d ( s e l f , n a m e , v a l u e ) : s e l f [ n a m e ] = v a l u e r e t u r n v a l u e

Star

When a Star AST node is matched, a matcher that calls the built-in _star method is generated:

rlmeta codegenerator.rlmeta rules astFnBody | [ " S t a r " a s t : x ] - > { " s e l f . _ s t a r ( " x " ) " }

The _star method expects a matcher and calls it for as long as it succeeds:

rlmeta support.py classes _Grammar d e f _ s t a r ( s e l f , m a t c h e r ) : r e s u l t = [ ] w h i l e T r u e : o r i g i n a l _ s t r e a m = s e l f . _ s t r e a m t r y : r e s u l t . a p p e n d ( m a t c h e r ( ) ) e x c e p t _ M a t c h E r r o r : s e l f . _ s t r e a m = o r i g i n a l _ s t r e a m r e t u r n _ S e m a n t i c A c t i o n ( l a m b d a : [ x . e v a l ( ) f o r x i n r e s u l t ] )

The return value is a semantic action. When evaluated, it returns a list of all match results evaluated.

A semantic action is a wrapper for a function:

rlmeta support.py classes c l a s s _ S e m a n t i c A c t i o n ( o b j e c t ) : d e f _ _ i n i t _ _ ( s e l f , f n ) : s e l f . f n = f n d e f e v a l ( s e l f ) : r e t u r n s e l f . f n ( )

Its eval method calls the function. The reason for wrapping semantic actions in functions is to prevent them from being evaluated before a parse is complete. If a parse fails, no semantic actions are evaluated.

Not

When a Not AST node is matched, a matcher that calls the built-in _not method is generated:

rlmeta codegenerator.rlmeta rules astFnBody | [ " N o t " a s t : x ] - > { " s e l f . _ n o t ( " x " ) " }

The _not method expects a matcher and succeeds if that matcher fails:

rlmeta support.py classes _Grammar d e f _ n o t ( s e l f , m a t c h e r ) : o r i g i n a l _ s t r e a m = s e l f . _ s t r e a m t r y : m a t c h e r ( ) e x c e p t _ M a t c h E r r o r : r e t u r n _ S e m a n t i c A c t i o n ( l a m b d a : N o n e ) e l s e : o r i g i n a l _ s t r e a m . f a i l ( " m a t c h f o u n d " ) f i n a l l y : s e l f . _ s t r e a m = o r i g i n a l _ s t r e a m

It never consumes any input. The original stream is always reset.

Semantic action

When a SemanticAction AST node is matched, a matcher that creates a _SemanticAction is generated:

rlmeta codegenerator.rlmeta rules astFnBody | [ " S e m a n t i c A c t i o n " a s t : x ] - > { " _ S e m a n t i c A c t i o n ( l a m b d a : " x " ) " }

The child AST node is assumed to generate a Python expression that will be returned when the semantic action is evaluated.

Match rule

When a MatchRule AST node is matched, a matcher that calls the built-in _match_rule is generated:

rlmeta codegenerator.rlmeta rules astFnBody | [ " M a t c h R u l e " . : x ] - > { " s e l f . _ m a t c h _ r u l e ( " r e p r ( x ) " ) " }

The _match_rule method expects the name of the rule to call:

rlmeta support.py classes _Grammar d e f _ m a t c h _ r u l e ( s e l f , r u l e _ n a m e ) : k e y = ( r u l e _ n a m e , s e l f . _ s t r e a m . p o s i t i o n ( ) ) i f k e y i n s e l f . _ m e m o : r e s u l t , _ , s e l f . _ s t r e a m = s e l f . _ m e m o [ k e y ] e l s e : s t a r t = s e l f . _ s t r e a m r e s u l t = g e t a t t r ( s e l f , " _ r u l e _ { } " . f o r m a t ( r u l e _ n a m e ) ) ( ) e n d = s e l f . _ s t r e a m s e l f . _ m e m o [ k e y ] = ( r e s u l t , s t a r t , e n d ) r e t u r n r e s u l t

If the given rule has been matched at the current position before, the memoized result is returned and the input stream is changed to where the previous match ended. If there has been no previous match, the rule is matched by calling the method. The result of the match is stored in the memoization table for later retrieval.

Match range

When a MatchRange AST node is matched, a matcher that calls the built-in _match_range is generated:

rlmeta codegenerator.rlmeta rules astFnBody | [ " M a t c h R a n g e " . : x . : y ] - > { " s e l f . _ m a t c h _ r a n g e ( " r e p r ( x ) " , " r e p r ( y ) " ) " }

The _match_range method expects two objects defining a range to match:

rlmeta support.py classes _Grammar d e f _ m a t c h _ r a n g e ( s e l f , s t a r t , e n d ) : o r i g i n a l _ s t r e a m = s e l f . _ s t r e a m n e x t _ o b j e x t , s e l f . _ s t r e a m = s e l f . _ s t r e a m . n e x t ( ) i f n e x t _ o b j e x t > = s t a r t a n d n e x t _ o b j e x t < = e n d : r e t u r n _ S e m a n t i c A c t i o n ( l a m b d a : n e x t _ o b j e x t ) e l s e : o r i g i n a l _ s t r e a m . f a i l ( " e x p e c t e d r a n g e { ! r } - { ! r } b u t f o u n d { ! r } " . f o r m a t ( s t a r t , e n d , n e x t _ o b j e x t ) )

If the next object from the input stream is in that range, it succeeds, otherwise it fails.

Match string

When a MatchString AST node is matched, a matcher that calls the built-in _match_string is generated:

rlmeta codegenerator.rlmeta rules astFnBody | [ " M a t c h S t r i n g " . : x ] - > { " s e l f . _ m a t c h _ s t r i n g ( " r e p r ( x ) " ) " }

The _match_string method expects the string to match:

rlmeta support.py classes _Grammar d e f _ m a t c h _ s t r i n g ( s e l f , s t r i n g ) : o r i g i n a l _ s t r e a m = s e l f . _ s t r e a m n e x t _ o b j e c t , s e l f . _ s t r e a m = s e l f . _ s t r e a m . n e x t ( ) i f n e x t _ o b j e c t = = s t r i n g : r e t u r n _ S e m a n t i c A c t i o n ( l a m b d a : s t r i n g ) e l s e : o r i g i n a l _ s t r e a m . f a i l ( " e x p e c t e d { ! r } b u t f o u n d { ! r } " . f o r m a t ( s t r i n g , n e x t _ o b j e c t ) )

If the next object from the input stream is that string, it succeeds, otherwise it fails.

Match character sequence

When a MatchCharseq AST node is matched, a matcher that calls the built-in _match_charseq is generated:

rlmeta codegenerator.rlmeta rules astFnBody | [ " M a t c h C h a r s e q " . : x ] - > { " s e l f . _ m a t c h _ c h a r s e q ( " r e p r ( x ) " ) " }

The _match_charseq method expects a string with characters to match:

rlmeta support.py classes _Grammar d e f _ m a t c h _ c h a r s e q ( s e l f , c h a r s e q ) : f o r c h a r i n c h a r s e q : o r i g i n a l _ s t r e a m = s e l f . _ s t r e a m n e x t _ o b j e c t , s e l f . _ s t r e a m = s e l f . _ s t r e a m . n e x t ( ) i f n e x t _ o b j e c t ! = c h a r : o r i g i n a l _ s t r e a m . f a i l ( " e x p e c t e d { ! r } b u t f o u n d { ! r } " . f o r m a t ( c h a r , n e x t _ o b j e c t ) ) r e t u r n _ S e m a n t i c A c t i o n ( l a m b d a : c h a r s e q )

If the next objects from the input stream are those characters, it succeeds, otherwise it fails.

Match any

When a MatchAny AST node is matched, the built-in _match_any is generated:

rlmeta codegenerator.rlmeta rules ast | [ " M a t c h A n y " ] - > { " s e l f . _ m a t c h _ a n y " }

The _match_any method expects no arguments and always matches the next object from the input stream:

rlmeta support.py classes _Grammar d e f _ m a t c h _ a n y ( s e l f ) : n e x t _ o b j e c t , s e l f . _ s t r e a m = s e l f . _ s t r e a m . n e x t ( ) r e t u r n _ S e m a n t i c A c t i o n ( l a m b d a : n e x t _ o b j e c t )

It only fails if there are no more objects.

Match list

When a MatchList AST node is matched, a matcher that calls the built-in _match_list is generated:

rlmeta codegenerator.rlmeta rules astFnBody | [ " M a t c h L i s t " a s t : x ] - > { " s e l f . _ m a t c h _ l i s t ( " x " ) " }

The _match_list method expects a matcher that should match the contents of the list:

rlmeta support.py classes _Grammar d e f _ m a t c h _ l i s t ( s e l f , m a t c h e r ) : o r i g i n a l _ s t r e a m = s e l f . _ s t r e a m n e x t _ o b j e c t , n e x t _ s t r e a m = s e l f . _ s t r e a m . n e x t ( ) i f i s i n s t a n c e ( n e x t _ o b j e c t , l i s t ) : s e l f . _ s t r e a m = s e l f . _ s t r e a m . n e s t e d ( n e x t _ o b j e c t ) m a t c h e r ( ) i f s e l f . _ s t r e a m . i s _ a t _ e n d ( ) : s e l f . _ s t r e a m = n e x t _ s t r e a m r e t u r n _ S e m a n t i c A c t i o n ( l a m b d a : n e x t _ o b j e c t ) o r i g i n a l _ s t r e a m . f a i l ( " l i s t m a t c h f a i l e d " )

If the next object is a list, a new stream is created with the nested call that contains all child objects. It is set to be the input stream, and the matcher is then called. The matcher must match all child objects, or the match fails.

String

When a String AST node is matched, a Python string is generated:

rlmeta codegenerator.rlmeta rules ast | [ " S t r i n g " . : x ] - > { r e p r ( x ) }

List

When a List AST node is matched, a Python list is generated:

rlmeta codegenerator.rlmeta rules ast | [ " L i s t " a s t L i s t : x ] - > { x }

rlmeta codegenerator.rlmeta rules a s t L i s t = a s t L i s t I t e m * : x s - > { " ( " x s " [ ] ) " } a s t L i s t I t e m = | [ " L i s t I t e m S p l i c e " a s t : x ] - > { x " + " } | a s t : x - > { " [ " x " ] + " }

The Python list is generated by concatenating sub-lists. If an item should be spliced, it is assumed to be a list already and is not wrapped in brackets.

Builder

When a Builder AST node is matched, a _Builder is generated:

rlmeta codegenerator.rlmeta rules ast | [ " B u i l d e r " a s t I t e m s : x ] - > { " _ B u i l d e r . c r e a t e ( [ " x " ] ) " }

The child AST nodes are assumed to generate Python expressions.

When an IndentBuilder AST node is matched, an _IndentBuilder is generated:

rlmeta codegenerator.rlmeta rules ast | [ " I n d e n t B u i l d e r " ] - > { " _ I n d e n t B u i l d e r ( ) " }

When a DedentBuilder AST node is matched, a _DedentBuilder is generated:

rlmeta codegenerator.rlmeta rules ast | [ " D e d e n t B u i l d e r " ] - > { " _ D e d e n t B u i l d e r ( ) " }

All builders inherit from _Builder :

rlmeta support.py classes c l a s s _ B u i l d e r ( o b j e c t ) : < < _ B u i l d e r > >

A builder can build a string with the build_string method:

rlmeta support.py classes _Builder d e f b u i l d _ s t r i n g ( s e l f ) : o u t p u t = _ O u t p u t ( ) s e l f . w r i t e ( o u t p u t ) r e t u r n o u t p u t . v a l u e

All builders must implement the write method that is passed an instance of _Output . The _Output class has functionality to build a string with appropriate indentation:

rlmeta support.py classes c l a s s _ O u t p u t ( o b j e c t ) : d e f _ _ i n i t _ _ ( s e l f ) : s e l f . v a l u e = " " s e l f . i n d e n t a t i o n = 0 d e f w r i t e ( s e l f , v a l u e ) : f o r c h i n v a l u e : i f s e l f . v a l u e a n d c h ! = " \ n " a n d s e l f . v a l u e [ - 1 ] = = " \ n " : s e l f . v a l u e + = " " * s e l f . i n d e n t a t i o n s e l f . v a l u e + = c h

The create method of a builder creates an instance of a builder depending on the type of object passed in:

rlmeta support.py classes _Builder @ c l a s s m e t h o d d e f c r e a t e ( s e l f , i t e m ) : i f i s i n s t a n c e ( i t e m , _ B u i l d e r ) : r e t u r n i t e m e l i f i s i n s t a n c e ( i t e m , l i s t ) : r e t u r n _ L i s t B u i l d e r ( [ _ B u i l d e r . c r e a t e ( x ) f o r x i n i t e m ] ) e l s e : r e t u r n _ A t o m B u i l d e r ( i t e m )

A _ListBuilder calls the write method on all child builders:

rlmeta support.py classes c l a s s _ L i s t B u i l d e r ( _ B u i l d e r ) : d e f _ _ i n i t _ _ ( s e l f , b u i l d e r s ) : s e l f . b u i l d e r s = b u i l d e r s d e f w r i t e ( s e l f , o u t p u t ) : f o r b u i l d e r i n s e l f . b u i l d e r s : b u i l d e r . w r i t e ( o u t p u t )

An _AtomBuilder converts its object to a string and writes it to the output:

rlmeta support.py classes c l a s s _ A t o m B u i l d e r ( _ B u i l d e r ) : d e f _ _ i n i t _ _ ( s e l f , a t o m ) : s e l f . a t o m = a t o m d e f w r i t e ( s e l f , o u t p u t ) : o u t p u t . w r i t e ( s t r ( s e l f . a t o m ) )

An _IndentBuilder changes the indentation of the output:

rlmeta support.py classes c l a s s _ I n d e n t B u i l d e r ( _ B u i l d e r ) : d e f w r i t e ( s e l f , o u t p u t ) : o u t p u t . i n d e n t a t i o n + = 1

A _DedentBuilder changes the indentation of the output:

rlmeta support.py classes c l a s s _ D e d e n t B u i l d e r ( _ B u i l d e r ) : d e f w r i t e ( s e l f , o u t p u t ) : o u t p u t . i n d e n t a t i o n - = 1

Function call

When a FnCall AST node is matched, a Python function call is generated:

rlmeta codegenerator.rlmeta rules ast | [ " F n C a l l " . : x a s t I t e m s : y ] - > { x " ( " y " ) " }

The child AST nodes are assumed to generate Python expressions.

Variable lookup

When a VarLookup AST node is matched, a Python expression that looks up the variable and evaluates it is generated:

rlmeta codegenerator.rlmeta rules ast | [ " V a r L o o k u p " . : x ] - > { " _ v a r s . l o o k u p ( " r e p r ( x ) " ) . e v a l ( ) " }

The lookup method returns the stored value:

rlmeta support.py classes _Vars d e f l o o k u p ( s e l f , n a m e ) : r e t u r n s e l f [ n a m e ]

Every time a variable is referenced, the eval method is called. Consider this grammar:

A G r a m m a r { f o o = b a r : x - > { x x } b a r = . - > a _ s i d e _ e f f e c t ( ) }

If evaluating x has a side effect, it will be executed twice. The eval method of _SemanticAction could be modified so that the function is only called once if needed.

Final support methods

Grammars in Python have a single entry point, run , which expects the name of the rule to match and the input object:

rlmeta support.py classes _Grammar d e f r u n ( s e l f , r u l e _ n a m e , i n p u t _ o b j e c t ) : s e l f . _ m e m o = _ M e m o ( ) s e l f . _ s t r e a m = _ S t r e a m . f r o m _ o b j e c t ( s e l f . _ m e m o , i n p u t _ o b j e c t ) r e s u l t = s e l f . _ m a t c h _ r u l e ( r u l e _ n a m e ) . e v a l ( ) i f i s i n s t a n c e ( r e s u l t , _ B u i l d e r ) : r e t u r n r e s u l t . b u i l d _ s t r i n g ( ) e l s e : r e t u r n r e s u l t

It initializes the memoization table and the input stream, matches the rule, and returns the evaluated result. If the result is a builder, the string that the builder builds is returned.

The memoization table is a subclass of a Python dictionary:

rlmeta support.py classes c l a s s _ M e m o ( d i c t ) : d e f _ _ i n i t _ _ ( s e l f ) : d i c t . _ _ i n i t _ _ ( s e l f ) s e l f . _ l a t e s t _ s t r e a m = _ O b j e c t S t r e a m ( s e l f , [ ] , p o s i t i o n = - 1 ) s e l f . _ l a t e s t _ m e s s a g e = " " d e f d e s c r i b e ( s e l f ) : i t e m s = [ ] f o r ( r u l e _ n a m e , _ ) , ( _ , s t a r t , e n d ) i n s e l f . i t e m s ( ) : i f e n d > s t a r t : i t e m s . a p p e n d ( ( r u l e _ n a m e , s t a r t , e n d ) ) i t e m s . s o r t ( k e y = l a m b d a i t e m : ( i t e m [ 2 ] . p o s i t i o n ( ) , i t e m [ 1 ] . p o s i t i o n ( ) ) ) m e s s a g e = [ ] f o r i t e m i n i t e m s : m e s s a g e . a p p e n d ( " m a t c h e d { : < 2 0 } { } - > { } \ n " . f o r m a t ( * i t e m ) ) m e s s a g e . a p p e n d ( " \ n " ) m e s s a g e . a p p e n d ( " E R R O R : { } : { } \ n " . f o r m a t ( s e l f . _ l a t e s t _ s t r e a m , s e l f . _ l a t e s t _ m e s s a g e ) ) r e t u r n " " . j o i n ( m e s s a g e ) d e f f a i l ( s e l f , s t r e a m , m e s s a g e ) : i f s t r e a m . p o s i t i o n ( ) > = s e l f . _ l a t e s t _ s t r e a m . p o s i t i o n ( ) : s e l f . _ l a t e s t _ s t r e a m = s t r e a m s e l f . _ l a t e s t _ m e s s a g e = m e s s a g e r a i s e _ M a t c h E r r o r ( s e l f )

Its describe method returns a string that describes what has been matched so far and what the latest error message was. It is used for error reporting. It keeps track of match failures via the fail method which is always called to raise a _MatchError .

A _MatchError is a Python exception that also has a reference to a memoization table so that the describe method can be exposed:

rlmeta support.py classes c l a s s _ M a t c h E r r o r ( E x c e p t i o n ) : d e f _ _ i n i t _ _ ( s e l f , m e m o ) : E x c e p t i o n . _ _ i n i t _ _ ( s e l f ) s e l f . _ m e m o = m e m o d e f d e s c r i b e ( s e l f ) : r e t u r n s e l f . _ m e m o . d e s c r i b e ( )

A _Stream is an immutable object that has a list of objects:

rlmeta support.py classes c l a s s _ S t r e a m ( o b j e c t ) : @ c l a s s m e t h o d d e f f r o m _ o b j e c t ( c l s , m e m o , i n p u t _ o b j e c t ) : i f i s i n s t a n c e ( i n p u t _ o b j e c t , b a s e s t r i n g ) : r e t u r n _ C h a r S t r e a m ( m e m o , l i s t ( i n p u t _ o b j e c t ) ) e l s e : r e t u r n _ O b j e c t S t r e a m ( m e m o , [ i n p u t _ o b j e c t ] ) d e f _ _ i n i t _ _ ( s e l f , m e m o , o b j e c t s ) : s e l f . _ m e m o = m e m o s e l f . _ o b j e c t s = o b j e c t s d e f f a i l ( s e l f , m e s s a g e ) : s e l f . _ m e m o . f a i l ( s e l f , m e s s a g e ) d e f n e x t ( s e l f ) : i f s e l f . i s _ a t _ e n d ( ) : s e l f . f a i l ( " n o t e o f " ) n e x t _ o b j e c t = s e l f . _ o b j e c t s [ 0 ] r e t u r n ( n e x t _ o b j e c t , s e l f . _ a d v a n c e ( n e x t _ o b j e c t , s e l f . _ o b j e c t s [ 1 : ] ) , ) d e f i s _ a t _ e n d ( s e l f ) : r e t u r n l e n ( s e l f . _ o b j e c t s ) = = 0

Its next method returns a tuple with the next object and the next stream. There are two kinds of streams: character streams and object streams. They are subclasses of _Stream and implement the position and _advance methods. The appropriate stream is chosen in the from_object method.

A character steam stores the position as a line + column:

rlmeta support.py classes c l a s s _ C h a r S t r e a m ( _ S t r e a m ) : d e f _ _ i n i t _ _ ( s e l f , m e m o , o b j e c t s , l i n e = 1 , c o l u m n = 1 ) : _ S t r e a m . _ _ i n i t _ _ ( s e l f , m e m o , o b j e c t s ) s e l f . _ l i n e = l i n e s e l f . _ c o l u m n = c o l u m n d e f p o s i t i o n ( s e l f ) : r e t u r n ( s e l f . _ l i n e , s e l f . _ c o l u m n ) d e f _ a d v a n c e ( s e l f , n e x t _ o b j e c t , o b j e c t s ) : i f n e x t _ o b j e c t = = " \ n " : r e t u r n _ C h a r S t r e a m ( s e l f . _ m e m o , o b j e c t s , s e l f . _ l i n e + 1 , 1 ) e l s e : r e t u r n _ C h a r S t r e a m ( s e l f . _ m e m o , o b j e c t s , s e l f . _ l i n e , s e l f . _ c o l u m n + 1 ) d e f _ _ s t r _ _ ( s e l f ) : r e t u r n " L { : 0 3 d } : C { : 0 3 d } " . f o r m a t ( s e l f . _ l i n e , s e l f . _ c o l u m n )

An object stream stores the position as a tuple of indices:

rlmeta support.py classes c l a s s _ O b j e c t S t r e a m ( _ S t r e a m ) : d e f _ _ i n i t _ _ ( s e l f , m e m o , o b j e c t s , p a r e n t = ( ) , p o s i t i o n = 0 ) : _ S t r e a m . _ _ i n i t _ _ ( s e l f , m e m o , o b j e c t s ) s e l f . _ p a r e n t = p a r e n t s e l f . _ p o s i t i o n = p o s i t i o n d e f p o s i t i o n ( s e l f ) : r e t u r n s e l f . _ p a r e n t + ( s e l f . _ p o s i t i o n , ) d e f n e s t e d ( s e l f , i n p u t _ o b j e c t ) : r e t u r n _ O b j e c t S t r e a m ( s e l f . _ m e m o , i n p u t _ o b j e c t , s e l f . _ p a r e n t + ( s e l f . _ p o s i t i o n , ) ) d e f _ a d v a n c e ( s e l f , n e x t _ o b j e c t , o b j e c t s ) : r e t u r n _ O b j e c t S t r e a m ( s e l f . _ m e m o , o b j e c t s , s e l f . _ p a r e n t , s e l f . _ p o s i t i o n + 1 ) d e f _ _ s t r _ _ ( s e l f ) : r e t u r n " [ { } ] " . f o r m a t ( " , " . j o i n ( s t r ( x ) f o r x i n s e l f . p o s i t i o n ( ) ) )

The nested method will only be called on object streams since character streams do not nest.

Putting it together

Here is a template for the final Python file that implements the RLMeta compiler:

rlmeta compile.sh python file template i m p o r t s y s S U P P O R T = $ s u p p o r t _ p y _ s t r i n g $ s u p p o r t _ p y $ p a r s e r _ p y $ c o d e g e n e r a t o r _ p y j o i n = " " . j o i n d e f c o m p i l e _ g r a m m a r ( g r a m m a r ) : p a r s e r = P a r s e r ( ) c o d e _ g e n e r a t o r = C o d e G e n e r a t o r ( ) r e t u r n c o d e _ g e n e r a t o r . r u n ( " a s t " , p a r s e r . r u n ( " g r a m m a r " , g r a m m a r ) ) i f _ _ n a m e _ _ = = " _ _ m a i n _ _ " : i f " - - s u p p o r t " i n s y s . a r g v : s y s . s t d o u t . w r i t e ( S U P P O R T ) e l s e : t r y : s y s . s t d o u t . w r i t e ( c o m p i l e _ g r a m m a r ( s y s . s t d i n . r e a d ( ) ) ) e x c e p t _ M a t c h E r r o r a s e : s y s . s t d e r r . w r i t e ( e . d e s c r i b e ( ) ) s y s . e x i t ( 1 )

First the sys module is imported because the main method needs it. Then the support library snippet is stored in a variable so that it can be output when the --support flag is given. Then the support library, compiled parser grammar, and compiled code generator grammar snippets are inserted. Then the host language function join is defined. Then a function to compile a grammar is defined. Finally the main method that reads a grammar from stdin and writes a Python class to stdout is implemented. If an error occurs, it is written to stderr using the exception's describe method. This template is rendered with a Bash script:

rlmeta compile.sh # ! / b i n / b a s h s e t - e r l m e t a _ c o m p i l e r = " $ ( p w d ) / $ 1 " c d " $ ( d i r n a m e " $ 0 " ) " t o _ p y t h o n _ s t r i n g ( ) { p y t h o n - c ' i m p o r t s y s ; s y s . s t d o u t . w r i t e ( r e p r ( s y s . s t d i n . r e a d ( ) ) ) ' } s u p p o r t _ p y = $ ( c a t s u p p o r t . p y ) s u p p o r t _ p y _ s t r i n g = $ ( t o _ p y t h o n _ s t r i n g < s u p p o r t . p y ) p a r s e r _ p y = $ ( p y t h o n " $ r l m e t a _ c o m p i l e r " < p a r s e r . r l m e t a ) c o d e g e n e r a t o r _ p y = $ ( p y t h o n " $ r l m e t a _ c o m p i l e r " < c o d e g e n e r a t o r . r l m e t a ) c a t < < E O F < < p y t h o n f i l e t e m p l a t e > > E O F

First the set -e directive ensures that the script exits as soon as there is an error. Then a variable containing the path to the RLMeta compiler is defined. The path must be given to the compile script as the first argument. Then the directory is changed to the one where the compile script lives. Then a function is defined that turns its stdin into a Python string with correct quoting. Then the support library is captured in a variable. Then the output of passing the support library to to_python_string is captured in a variable. Then the output of two calls to the RLMeta compiler are captured in two variables. Finally the Python file template is rendered and written to stdout.

If the compile script is run with the current RLMeta compiler ( rlmeta/rlmeta.py ), a new Python file is output that is exactly the same as the rlmeta/rlmeta.py file. It can be seen by diffing the two files:

$ d i f f < ( . / r l m e t a / c o m p i l e . s h r l m e t a / r l m e t a . p y ) r l m e t a / r l m e t a . p y & & e c h o E Q U A L E Q U A L

The rlmeta.py file never needs to be changed manually. The next version can always be produced using the previous version. Sometimes the next version must be produced in steps and intermediate compilers must be created. That is why the path to the compiler must be given to the compile script. (More on modifying RLMeta in the next article.) If you want to see what the rlmeta.py file looks like, it is here.

Bootstrapping

In the previous section, a version of the RLMeta compiler was needed to compile the RLMeta compiler:

p a r s e r _ p y = $ ( p y t h o n " $ r l m e t a _ c o m p i l e r " < p a r s e r . r l m e t a ) c o d e g e n e r a t o r _ p y = $ ( p y t h o n " $ r l m e t a _ c o m p i l e r " < c o d e g e n e r a t o r . r l m e t a )

But how can the RLMeta compiler be run before it exists? How was the first version of rlmeta.py created? This is a bootstrapping problem. In this case I solved it by translating the parser and the code generator to Python code manually according the rules specified in the grammars. I did manually what the compiler would do.

I started translating the parser that looks like this:

P a r s e r { . . . }

This is matched by the grammar rule in the parser and turned into a Grammar AST node:

g r a m m a r = | n a m e : x s p a c e ' { ' r u l e * : y s s p a c e ' } ' - > [ " G r a m m a r " x ~ y s ]

The Grammar AST node is then turned into a Python class definition by the code generator:

| [ " G r a m m a r " . : x a s t * : y s ] - > { " c l a s s " x " ( _ G r a m m a r ) : \ n " > y s < }

The parser is thus turned into the following Python code:

c l a s s P a r s e r ( _ G r a m m a r ) : . . .

I then went on to translate the rules in the parser. The first rule is grammar :

g r a m m a r = | n a m e : x s p a c e ' { ' r u l e * : y s s p a c e ' } ' - > [ " G r a m m a r " x ~ y s ]

It is matched by the rule rule in the parser and turned into a Rule AST node:

r u l e = | n a m e : x s p a c e ' = ' c h o i c e : y - > [ " R u l e " x y ]

The Rule AST node is then turned into a Python method definition by the code generator:

| [ " R u l e " . : x a s t : y ] - > { " \ n d e f _ r u l e _ " x " ( s e l f ) : \ n " > " r e t u r n " y " ( ) \ n " < }

The grammar rule is thus turned into the following Python code:

d e f _ r u l e _ g r a m m a r ( s e l f ) : r e t u r n . . . ( )

The body of the grammar rule is matched by the choice rule in the parser and turned into an Or AST node:

c h o i c e = | ( s p a c e ' | ' ) ? s e q u e n c e : x ( s p a c e ' | ' s e q u e n c e ) * : x s - > [ " O r " x ~ x s ]

The Or AST node is then turned into a Python lambda expression by the code generator:

| [ " O r " a s t I t e m s : x ] - > { " s e l f . _ o r ( [ " x " ] ) " }

The body of the grammar rule is thus turned into the following Python code:

( l a m b d a : s e l f . _ o r ( [ . . . ] ) )

I continued this process until all dots had been expanded. Then I did the same for all remaining rules. Finally I repeated the process for the code generator. Once I had the manually translated versions of parser.py and codegenerator.py , I could temporarily replace

p a r s e r _ p y = $ ( p y t h o n " $ r l m e t a _ c o m p i l e r " < p a r s e r . r l m e t a ) c o d e g e n e r a t o r _ p y = $ ( p y t h o n " $ r l m e t a _ c o m p i l e r " < c o d e g e n e r a t o r . r l m e t a )

with

p a r s e r _ p y = $ ( c a t p a r s e r . p y ) c o d e g e n e r a t o r _ p y = $ ( c a t c o d e g e n e r a t o r . p y )

to create the initial version of rlmeta.py .

With the initial version of rlmeta.py I could run the compile script to generate the second version of rlmeta.py . The second version did not match the first version exactly. The complete diff can be seen in the commit Get rid of bootstrapped compiler. Mostly I had used a different character for strings and forgotten some commas that were not strictly necessary. Once the second version replaced the first, the compile script reproduced rlmeta.py exactly. At this point the manually translated versions could be discarded. The RLMeta compiler was bootstrapped. This was a tremendously rewarding experience.

Did the first version work on the first run? No. In the translation process I noticed incorrect behavior in the grammars that I had to fix. And some manual translations were done incorrectly. But all fixes were relatively minor. Before the translation I had also carefully debugged the grammars in my head to decrease the risk of them having bugs.

To avoid making mistakes in the manual translation process, I created snippets for the Vim text editor for each AST node. So when I encountered a Rule AST node, I could type "rule", hit tab, and the following snippet would be inserted placing the cursor where the rule name should be inserted:

d e f _ r u l e _ $ { 1 : n a m e } ( s e l f ) : r e t u r n $ { 2 : m a t c h e r } ( )

I could then type the name ("grammar" for example), hit tab, write the name of the next AST node, and hit tab. If the next AST node was Or for example, the following snipped would be inserted, placing the cursor inside the list:

( l a m b d a : s e l f . _ o r ( [ $ { 1 : m a t c h e r s } ] ) )

The Vim snippets saved me a lot of typing and made manual translation feasible. Getting all commas and parenthesis right would have been difficult otherwise.

Implementing programming languages

When I read Structure and Interpretation of Computer Programs I remember thinking that it described what programming was all about. The following quote from chapter 4 I think summarizes it:

However, as we confront increasingly complex problems, we will find that Lisp, or indeed any fixed programming language, is not sufficient for our needs. We must constantly turn to new languages in order to express our ideas more effectively. Establishing new languages is a powerful strategy for controlling complexity in engineering design; we can often enhance our ability to deal with a complex problem by adopting a new language that enables us to describe (and hence to think about) the problem in a different way, using primitives, means of combination, and means of abstraction that are particularly well suited to the problem at hand.

In this article I set out to explain how a metalanguage could be used to give meaning to arithmetic expressions. But RLMeta is more powerful than that. It can be used to implement itself and also many other programming languages because matching and transforming is at the heart of programming language implementation. Its small implementation, just over 400 lines of code, also makes it feasible to understand and modify:

5 1 p a r s e r . r l m e t a 3 3 c o d e g e n e r a t o r . r l m e t a 2 7 8 s u p p o r t . p y 4 5 c o m p i l e . s h 4 0 7 t o t a l

I hope this article inspires you to experiment with implementing programming languages so that you can solve complex problems elegantly.

Resources

I was helped by the following resources when implementing RLMeta:

Code listings for RLMeta

P a r s e r { g r a m m a r = | n a m e : x s p a c e ' { ' r u l e * : y s s p a c e ' } ' - > [ " G r a m m a r " x ~ y s ] r u l e = | n a m e : x s p a c e ' = ' c h o i c e : y - > [ " R u l e " x y ] c h o i c e = | ( s p a c e ' | ' ) ? s e q u e n c e : x ( s p a c e ' | ' s e q u e n c e ) * : x s - > [ " O r " x ~ x s ] s e q u e n c e = | e x p r : x e x p r * : x s - > [ " S c o p e " [ " A n d " x ~ x s ] ] e x p r = | e x p r 1 : x s p a c e ' : ' n a m e : y - > [ " B i n d " y x ] | e x p r 1 e x p r 1 = | e x p r 2 : x s p a c e ' * ' - > [ " S t a r " x ] | e x p r 2 : x s p a c e ' ? ' - > [ " O r " x [ " A n d " ] ] | s p a c e ' ! ' e x p r 2 : x - > [ " N o t " x ] | e x p r 2 e x p r 2 = | s p a c e ' - > ' h o s t E x p r : x - > [ " S e m a n t i c A c t i o n " x ] | n a m e : x ! ( s p a c e ' = ' ) - > [ " M a t c h R u l e " x ] | s p a c e c h a r : x ' - ' c h a r : y - > [ " M a t c h R a n g e " x y ] | s p a c e s t r i n g : x - > [ " M a t c h S t r i n g " x ] | s p a c e c h a r s e q : x - > [ " M a t c h C h a r s e q " x ] | s p a c e ' . ' - > [ " M a t c h A n y " ] | s p a c e ' ( ' c h o i c e : x s p a c e ' ) ' - > x | s p a c e ' [ ' e x p r * : x s s p a c e ' ] ' - > [ " M a t c h L i s t " [ " A n d " ~ x s ] ] h o s t E x p r = | s p a c e s t r i n g : x - > [ " S t r i n g " x ] | s p a c e ' [ ' h o s t E x p r L i s t I t e m * : x s s p a c e ' ] ' - > [ " L i s t " ~ x s ] | s p a c e ' { ' b u i l d E x p r * : x s s p a c e ' } ' - > [ " B u i l d e r " ~ x s ] | n a m e : x s p a c e ' ( ' h o s t E x p r * : y s s p a c e ' ) ' - > [ " F n C a l l " x ~ y s ] | n a m e : x - > [ " V a r L o o k u p " x ] h o s t E x p r L i s t I t e m = | s p a c e ' ~ ' h o s t E x p r : x - > [ " L i s t I t e m S p l i c e " x ] | h o s t E x p r b u i l d E x p r = | s p a c e ' > ' - > [ " I n d e n t B u i l d e r " ] | s p a c e ' < ' - > [ " D e d e n t B u i l d e r " ] | h o s t E x p r s t r i n g = ' " ' ( ! ' " ' i n n e r C h a r ) * : x s ' " ' - > j o i n ( x s ) c h a r s e q = ' \ ' ' ( ! ' \ ' ' i n n e r C h a r ) * : x s ' \ ' ' - > j o i n ( x s ) c h a r = ' \ ' ' ! ' \ ' ' i n n e r C h a r : x ' \ ' ' - > x i n n e r C h a r = ' \ \ ' e s c a p e | . e s c a p e = ' \ \ ' - > " \ \ " | ' \ ' ' - > " ' " | ' " ' - > " \ " " | ' n ' - > " \ n " n a m e = s p a c e n a m e S t a r t : x n a m e C h a r * : x s - > j o i n ( [ x ~ x s ] ) n a m e S t a r t = ' a ' - ' z ' | ' A ' - ' Z ' n a m e C h a r = ' a ' - ' z ' | ' A ' - ' Z ' | ' 0 ' - ' 9 ' s p a c e = ( ' ' | ' \ n ' ) * }

C o d e G e n e r a t o r { a s t = | [ " G r a m m a r " . : x a s t * : y s ] - > { " c l a s s " x " ( _ G r a m m a r ) : \ n " > y s < } | [ " R u l e " . : x a s t : y ] - > { " \ n d e f _ r u l e _ " x " ( s e l f ) : \ n " > " r e t u r n " y " ( ) \ n " < } | [ " M a t c h A n y " ] - > { " s e l f . _ m a t c h _ a n y " } | [ " S t r i n g " . : x ] - > { r e p r ( x ) } | [ " L i s t " a s t L i s t : x ] - > { x } | [ " B u i l d e r " a s t I t e m s : x ] - > { " _ B u i l d e r . c r e a t e ( [ " x " ] ) " } | [ " I n d e n t B u i l d e r " ] - > { " _ I n d e n t B u i l d e r ( ) " } | [ " D e d e n t B u i l d e r " ] - > { " _ D e d e n t B u i l d e r ( ) " } | [ " F n C a l l " . : x a s t I t e m s : y ] - > { x " ( " y " ) " } | [ " V a r L o o k u p " . : x ] - > { " _ v a r s . l o o k u p ( " r e p r ( x ) " ) . e v a l ( ) " } | a s t F n B o d y : x - > { " ( l a m b d a : \ n " > x < " \ n ) " } a s t F n B o d y = | [ " O r " a s t I t e m s : x ] - > { " s e l f . _ o r ( [ " x " ] ) " } | [ " S c o p e " a s t : x ] - > { " ( l a m b d a _ v a r s : \ n " > x < " ( ) \ n ) ( _ V a r s ( ) ) " } | [ " A n d " a s t I t e m s : x ] - > { " s e l f . _ a n d ( [ " x " ] ) " } | [ " B i n d " . : x a s t : y ] - > { " _ v a r s . b i n d ( " r e p r ( x ) " , " y " ( ) ) " } | [ " S t a r " a s t : x ] - > { " s e l f . _ s t a r ( " x " ) " } | [ " N o t " a s t : x ] - > { " s e l f . _ n o t ( " x " ) " } | [ " S e m a n t i c A c t i o n " a s t : x ] - > { " _ S e m a n t i c A c t i o n ( l a m b d a : " x " ) " } | [ " M a t c h R u l e " . : x ] - > { " s e l f . _ m a t c h _ r u l e ( " r e p r ( x ) " ) " } | [ " M a t c h R a n g e " . : x . : y ] - > { " s e l f . _ m a t c h _ r a n g e ( " r e p r ( x ) " , " r e p r ( y ) " ) " } | [ " M a t c h S t r i n g " . : x ] - > { " s e l f . _ m a t c h _ s t r i n g ( " r e p r ( x ) " ) " } | [ " M a t c h C h a r s e q " . : x ] - > { " s e l f . _ m a t c h _ c h a r s e q ( " r e p r ( x ) " ) " } | [ " M a t c h L i s t " a s t : x ] - > { " s e l f . _ m a t c h _ l i s t ( " x " ) " } a s t I t e m s = a s t I t e m * : x s - > { " \ n " > x s < } a s t I t e m = a s t : x - > { x " , \ n " } a s t L i s t = a s t L i s t I t e m * : x s - > { " ( " x s " [ ] ) " } a s t L i s t I t e m = | [ " L i s t I t e m S p l i c e " a s t : x ] - > { x " + " } | a s t : x - > { " [ " x " ] + " } }

c l a s s _ G r a m m a r ( o b j e c t ) : d e f _ o r ( s e l f , m a t c h e r s ) : o r i g i n a l _ s t r e a m = s e l f . _ s t r e a m f o r m a t c h e r i n m a t c h e r s : t r y : r e t u r n m a t c h e r ( ) e x c e p t _ M a t c h E r r o r : s e l f . _ s t r e a m = o r i g i n a l _ s t r e a m o r i g i n a l _ s t r e a m . f a i l ( " n o c h o i c e m a t c h e d " ) d e f _ a n d ( s e l f , m a t c h e r s ) : r e s u l t = N o n e f o r m a t c h e r i n m a t c h e r s : r e s u l t = m a t c h e r ( ) r e t u r n r e s u l t d e f _ s t a r ( s e l f , m a t c h e r ) : r e s u l t = [ ] w h i l e T r u e : o r i g i n a l _ s t r e a m = s e l f . _ s t r e a m t r y : r e s u l t . a p p e n d ( m a t c h e r ( ) ) e x c e p t _ M a t c h E r r o r : s e l f . _ s t r e a m = o r i g i n a l _ s t r e a m r e t u r n _ S e m a n t i c A c t i o n ( l a m b d a : [ x . e v a l ( ) f o r x i n r e s u l t ] ) d e f _ n o t ( s e l f , m a t c h e r ) : o r i g i n a l _ s t r e a m = s e l f . _ s t r e a m t r y : m a t c h e r ( ) e x c e p t _ M a t c h E r r o r : r e t u r n _ S e m a n t i c A c t i o n ( l a m b d a : N o n e ) e l s e : o r i g i n a l _ s t r e a m . f a i l ( " m a t c h f o u n d " ) f i n a l l y : s e l f . _ s t r e a m = o r i g i n a l _ s t r e a m d e f _ m a t c h _ r u l e ( s e l f , r u l e _ n a m e ) : k e y = ( r u l e _ n a m e , s e l f . _ s t r e a m . p o s i t i o n ( ) ) i f k e y i n s e l f . _ m e m o : r e s u l t , _ , s e l f . _ s t r e a m = s e l f . _ m e m o [ k e y ] e l s e : s t a r t = s e l f . _ s t r e a m r e s u l t = g e t a t t r ( s e l f , " _ r u l e _ { } " . f o r m a t ( r u l e _ n a m e ) ) ( ) e n d = s e l f . _ s t r e a m s e l f . _ m e m o [ k e y ] = ( r e s u l t , s t a r t , e n d ) r e t u r n r e s u l t d e f _ m a t c h _ r a n g e ( s e l f , s t a r t , e n d ) : o r i g i n a l _ s t r e a m = s e l f . _ s t r e a m n e x t _ o b j e x t , s e l f . _ s t r e a m = s e l f . _ s t r e a m . n e x t ( ) i f n e x t _ o b j e x t > = s t a r t a n d n e x t _ o b j e x t < = e n d : r e t u r n _ S e m a n t i c A c t i o n ( l a m b d a : n e x t _ o b j e x t ) e l s e : o r i g i n a l _ s t r e a m . f a i l ( " e x p e c t e d r a n g e { ! r } - { ! r } b u t f o u n d { ! r } " . f o r m a t ( s t a r t , e n d , n e x t _ o b j e x t ) ) d e f _ m a t c h _ s t r i n g ( s e l f , s t r i n g ) : o r i g i n a l _ s t r e a m = s e l f . _ s t r e a m n e x t _ o b j e c t , s e l f . _ s t r e a m = s e l f . _ s t r e a m . n e x t ( ) i f n e x t _ o b j e c t = = s t r i n g : r e t u r n _ S e m a n t i c A c t i o n ( l a m b d a : s t r i n g ) e l s e : o r i g i n a l _ s t r e a m . f a i l ( " e x p e c t e d { ! r } b u t f o u n d { ! r } " . f o r m a t ( s t r i n g , n e x t _ o b j e c t ) ) d e f _ m a t c h _ c h a r s e q ( s e l f , c h a r s e q ) : f o r c h a r i n c h a r s e q : o r i g i n a l _ s t r e a m = s e l f . _ s t r e a m n e x t _ o b j e c t , s e l f . _ s t r e a m = s e l f . _ s t r e a m . n e x t ( ) i f n e x t _ o b j e c t ! = c h a r : o r i g i n a l _ s t r e a m . f a i l ( " e x p e c t e d { ! r } b u t f o u n d { ! r } " . f o r m a t ( c h a r , n e x t _ o b j e c t ) ) r e t u r n _ S e m a n t i c A c t i o n ( l a m b d a : c h a r s e q ) d e f _ m a t c h _ a n y ( s e l f ) : n e x t _ o b j e c t , s e l f . _ s t r e a m = s e l f . _ s t r e a m . n e x t ( ) r e t u r n _ S e m a n t i c A c t i o n ( l a m b d a : n e x t _ o b j e c t ) d e f _ m a t c h _ l i s t ( s e l f , m a t c h e r ) : o r i g i n a l _ s t r e a m = s e l f . _ s t r e a m n e x t _ o b j e c t , n e x t _ s t r e a m = s e l f . _ s t r e a m . n e x t ( ) i f i s i n s t a n c e ( n e x t _ o b j e c t , l i s t ) : s e l f . _ s t r e a m = s e l f . _ s t r e a m . n e s t e d ( n e x t _ o b j e c t ) m a t c h e r ( ) i f s e l f . _ s t r e a m . i s _ a t _ e n d ( ) : s e l f . _ s t r e a m = n e x t _ s t r e a m r e t u r n _ S e m a n t i c A c t i o n ( l a m b d a : n e x t _ o b j e c t ) o r i g i n a l _ s t r e a m . f a i l ( " l i s t m a t c h f a i l e d " ) d e f r u n ( s e l f , r u l e _ n a m e , i n p u t _ o b j e c t ) : s e l f . _ m e m o = _ M e m o ( ) s e l f . _ s t r e a m = _ S t r e a m . f r o m _ o b j e c t ( s e l f . _ m e m o , i n p u t _ o b j e c t ) r e s u l t = s e l f . _ m a t c h _ r u l e ( r u l e _ n a m e ) . e v a l ( ) i f i s i n s t a n c e ( r e s u l t , _ B u i l d e r ) : r e t u r n r e s u l t . b u i l d _ s t r i n g ( ) e l s e : r e t u r n r e s u l t c l a s s _ V a r s ( d i c t ) : d e f b i n d ( s e l f , n a m e , v a l u e ) : s e l f [ n a m e ] = v a l u e r e t u r n v a l u e d e f l o o k u p ( s e l f , n a m e ) : r e t u r n s e l f [ n a m e ] c l a s s _ S e m a n t i c A c t i o n ( o b j e c t ) : d e f _ _ i n i t _ _ ( s e l f , f n ) : s e l f . f n = f n d e f e v a l ( s e l f ) : r e t u r n s e l f . f n ( ) c l a s s _ B u i l d e r ( o b j e c t ) : d e f b u i l d _ s t r i n g ( s e l f ) : o u t p u t = _ O u t p u t ( ) s e l f . w r i t e ( o u t p u t ) r e t u r n o u t p u t . v a l u e @ c l a s s m e t h o d d e f c r e a t e ( s e l f , i t e m ) : i f i s i n s t a n c e ( i t e m , _ B u i l d e r ) : r e t u r n i t e m e l i f i s i n s t a n c e ( i t e m , l i s t ) : r e t u r n _ L i s t B u i l d e r ( [ _ B u i l d e r . c r e a t e ( x ) f o r x i n i t e m ] ) e l s e : r e t u r n _ A t o m B u i l d e r ( i t e m ) c l a s s _ O u t p u t ( o b j e c t ) : d e f _ _ i n i t _ _ ( s e l f ) : s e l f . v a l u e = " " s e l f . i n d e n t a t i o n = 0 d e f w r i t e ( s e l f , v a l u e ) : f o r c h i n v a l u e : i f s e l f . v a l u e a n d c h ! = " \ n " a n d s e l f . v a l u e [ - 1 ] = = " \ n " : s e l f . v a l u e + = " " * s e l f . i n d e n t a t i o n s e l f . v a l u e + = c h c l a s s _ L i s t B u i l d e r ( _ B u i l d e r ) : d e f _ _ i n i t _ _ ( s e l f , b u i l d e r s ) : s e l f . b u i l d e r s = b u i l d e r s d e f w r i t e ( s e l f , o u t p u t ) : f o r b u i l d e r i n s e l f . b u i l d e r s : b u i l d e r . w r i t e ( o u t p u t ) c l a s s _ A t o m B u i l d e r ( _ B u i l d e r ) : d e f _ _ i n i t _ _ ( s e l f , a t o m ) : s e l f . a t o m = a t o m d e f w r i t e ( s e l f , o u t p u t ) : o u t p u t . w r i t e ( s t r ( s e l f . a t o m ) ) c l a s s _ I n d e n t B u i l d e r ( _ B u i l d e r ) : d e f w r i t e ( s e l f , o u t p u t ) : o u t p u t . i n d e n t a t i o n + = 1 c l a s s _ D e d e n t B u i l d e r ( _ B u i l d e r ) : d e f w r i t e ( s e l f , o u t p u t ) : o u t p u t . i n d e n t a t i o n - = 1 c l a s s _ M e m o ( d i c t ) : d e f _ _ i n i t _ _ ( s e l f ) : d i c t . _ _ i n i t _ _ ( s e l f ) s e l f . _ l a t e s t _ s t r e a m = _ O b j e c t S t r e a m ( s e l f , [ ] , p o s i t i o n = - 1 ) s e l f . _ l a t e s t _ m e s s a g e = " " d e f d e s c r i b e ( s e l f ) : i t e m s = [ ] f o r ( r u l e _ n a m e , _ ) , ( _ , s t a r t , e n d ) i n s e l f . i t e m s ( ) : i f e n d > s t a r t : i t e m s . a p p e n d ( ( r u l e _ n a m e , s t a r t , e n d ) ) i t e m s . s o r t ( k e y = l a m b d a i t e m : ( i t e m [ 2 ] . p o s i t i o n ( ) , i t e m [ 1 ] . p o s i t i o n ( ) ) ) m e s s a g e = [ ] f o r i t e m i n i t e m s : m e s s a g e . a p p e n d ( " m a t c h e d { : < 2 0 } { } - > { } \ n " . f o r m a t ( * i t e m ) ) m e s s a g e . a p p e n d ( " \ n " ) m e s s a g e . a p p e n d ( " E R R O R : { } : { } \ n " . f o r m a t ( s e l f . _ l a t e s t _ s t r e a m , s e l f . _ l a t e s t _ m e s s a g e ) ) r e t u r n " " . j o i n ( m e s s a g e ) d e f f a i l ( s e l f , s t r e a m , m e s s a g e ) : i f s t r e a m . p o s i t i o n ( ) > = s e l f . _ l a t e s t _ s t r e a m . p o s i t i o n ( ) : s e l f . _ l a t e s t _ s t r e a m = s t r e a m s e l f . _ l a t e s t _ m e s s a g e = m e s s a g e r a i s e _ M a t c h E r r o r ( s e l f ) c l a s s _ M a t c h E r r o r ( E x c e p t i o n ) : d e f _ _ i n i t _ _ ( s e l f , m e m o ) : E x c e p t i o n . _ _ i n i t _ _ ( s e l f ) s e l f . _ m e m o = m e m o d e f d e s c r i b e ( s e l f ) : r e t u r n s e l f . _ m e m o . d e s c r i b e ( ) c l a s s _ S t r e a m ( o b j e c t ) : @ c l a s s m e t h o d d e f f r o m _ o b j e c t ( c l s , m e m o , i n p u t _ o b j e c t ) : i f i s i n s t a n c e ( i n p u t _ o b j e c t , b a s e s t r i n g ) : r e t u r n _ C h a r S t r e a m ( m e m o , l i s t ( i n p u t _ o b j e c t ) ) e l s e : r e t u r n _ O b j e c t S t r e a m ( m e m o , [ i n p u t _ o b j e c t ] ) d e f _ _ i n i t _ _ ( s e l f , m e m o , o b j e c t s ) : s e l f . _ m e m o = m e m o s e l f . _ o b j e c t s = o b j e c t s d e f f a i l ( s e l f , m e s s a g e ) : s e l f . _ m e m o . f a i l ( s e l f , m e s s a g e ) d e f n e x t ( s e l f ) : i f s e l f . i s _ a t _ e n d ( ) : s e l f . f a i l ( " n o t e o f " ) n e x t _ o b j e c t = s e l f . _ o b j e c t s [ 0 ] r e t u r n ( n e x t _ o b j e c t , s e l f . _ a d v a n c e ( n e x t _ o b j e c t , s e l f . _ o b j e c t s [ 1 : ] ) , ) d e f i s _ a t _ e n d ( s e l f ) : r e t u r n l e n ( s e l f . _ o b j e c t s ) = = 0 c l a s s _ C h a r S t r e a m ( _ S t r e a m ) : d e f _ _ i n i t _ _ ( s e l f , m e m o , o b j e c t s , l i n e = 1 , c o l u m n = 1 ) : _ S t r e a m . _ _ i n i t _ _ ( s e l f , m e m o , o b j e c t s ) s e l f . _ l i n e = l i n e s e l f . _ c o l u m n = c o l u m n d e f p o s i t i o n ( s e l f ) : r e t u r n ( s e l f . _ l i n e , s e l f . _ c o l u m n ) d e f _ a d v a n c e ( s e l f , n e x t _ o b j e c t , o b j e c t s ) : i f n e x t _ o b j e c t = = " \ n " : r e t u r n _ C h a r S t r e a m ( s e l f . _ m e m o , o b j e c t s , s e l f . _ l i n e + 1 , 1 ) e l s e : r e t u r n _ C h a r S t r e a m ( s e l f . _ m e m o , o b j e c t s , s e l f . _ l i n e , s e l f . _ c o l u m n + 1 ) d e f _ _ s t r _ _ ( s e l f ) : r e t u r n " L { : 0 3 d } : C { : 0 3 d } " . f o r m a t ( s e l f . _ l i n e , s e l f . _ c o l u m n ) c l a s s _ O b j e c t S t r e a m ( _ S t r e a m ) : d e f _ _ i n i t _ _ ( s e l f , m e m o , o b j e c t s , p a r e n t = ( ) , p o s i t i o n = 0 ) : _ S t r e a m . _ _ i n i t _ _ ( s e l f , m e m o , o b j e c t s ) s e l f . _ p a r e n t = p a r e n t s e l f . _ p o s i t i o n = p o s i t i o n d e f p o s i t i o n ( s e l f ) : r e t u r n s e l f . _ p a r e n t + ( s e l f . _ p o s i t i o n , ) d e f n e s t e d ( s e l f , i n p u t _ o b j e c t ) : r e t u r n _ O b j e c t S t r e a m ( s e l f . _ m e m o , i n p u t _ o b j e c t , s e l f . _ p a r e n t + ( s e l f . _ p o s i t i o n , ) ) d e f _ a d v a n c e ( s e l f , n e x t _ o b j e c t , o b j e c t s ) : r e t u r n _ O b j e c t S t r e a m ( s e l f . _ m e m o , o b j e c t s , s e l f . _ p a r e n t , s e l f . _ p o s i t i o n + 1 ) d e f _ _ s t r _ _ ( s e l f ) : r e t u r n " [ { } ] " . f o r m a t ( " , " . j o i n ( s t r ( x ) f o r x i n s e l f . p o s i t i o n ( ) ) )

# ! / b i n / b a s h s e t - e r l m e t a _ c o m p i l e r = " $ ( p w d ) / $ 1 " c d " $ ( d i r n a m e " $ 0 " ) " t o _ p y t h o n _ s t r i n g ( ) { p y t h o n - c ' i m p o r t s y s ; s y s . s t d o u t . w r i t e ( r e p r ( s y s . s t d i n . r e a d ( ) ) ) ' } s u p p o r t _ p y = $ ( c a t s u p p o r t . p y ) s u p p o r t _ p y _ s t r i n g = $ ( t o _ p y t h o n _ s t r i n g < s u p p o r t . p y ) p a r s e r _ p y = $ ( p y t h o n " $ r l m e t a _ c o m p i l e r " < p a r s e r . r l m e t a ) c o d e g e n e r a t o r _ p y = $ ( p y t h o n " $ r l m e t a _ c o m p i l e r " < c o d e g e n e r a t o r . r l m e t a ) c a t < < E O F i m p o r t s y s S U P P O R T = $ s u p p o r t _ p y _ s t r i n g $ s u p p o r t _ p y $ p a r s e r _ p y $ c o d e g e n e r a t o r _ p y j o i n = " " . j o i n d e f c o m p i l e _ g r a m m a r ( g r a m m a r ) : p a r s e r = P a r s e r ( ) c o d e _ g e n e r a t o r = C o d e G e n e r a t o r ( ) r e t u r n c o d e _ g e n e r a t o r . r u n ( " a s t " , p a r s e r . r u n ( " g r a m m a r " , g r a m m a r ) ) i f _ _ n a m e _ _ = = " _ _ m a i n _ _ " : i f " - - s u p p o r t " i n s y s . a r g v : s y s . s t d o u t . w r i t e ( S U P P O R T ) e l s e : t r y : s y s . s t d o u t . w r i t e ( c o m p i l e _ g r a m m a r ( s y s . s t d i n . r e a d ( ) ) ) e x c e p t _ M a t c h E r r o r a s e : s y s . s t d e r r . w r i t e ( e . d e s c r i b e ( ) ) s y s . e x i t ( 1 ) E O F