Introduction

Almost any Perl programmer knows what Parsing is about. One of the strengths of Perl is its excellence for text analysis. Additionally to its embedded regular expression capacities, modules like Parse::RecDescent [1] and Parse::Yapp [2] make easier the task of text understanding and text transformation.

Parse::Eyapp (Extended yapp) is a collection of modules that extends Francois Desarmenien Parse::Yapp 1.05: Any yapp program runs without changes with eyapp. Additionally Parse::Eyapp provides new functionalities like named attributes, EBNF-like expressions, modifiable default actions, abstract syntax tree building and translation schemes. It also provides a language for tree transformations.

This article introduces the basics of translator construction with Parse::Eyapp through an example that compiles infix expressions into Parrot Intermediate Representation (PIR)[3]. Parrot is a virtual machine (VM), similar to the Java VM and the .NET VM. However, unlike these two which are designed for statically-typed languages like Java or C#, Parrot is designed for use with dynamically typed languages such as Perl, Python, Ruby, or PHP.

The input to the program will be a (semicolon separated) list of infix expressions, like in this example located in file input1.inf:

$ cat input1.inf 
b = 5;
a = b+2;
a = 2*(a+b)*(2-4/2);
print a;
d = (a = a+1)*4-b;
c = a*b+d;
print c;
print d

and the output is the PIR resulting from the translation:

 1  .sub 'main' :main
 2     .local num a, b, c, d
 3     b = 5
 4     a = b + 2
 5     a = 0 # expression at line 3 
 6     print "a = "     # above was
 7     print a    # reduced to zero
 8     print "\n" # at compile time
 9     a = a + 1
10     $N5 = a * 4
11     d = $N5 - b
12     $N7 = a * b
13     c = $N7 + d
14     print "c = "
15     print c
16     print "\n"
17     print "d = "
18     print d
19     print "\n"
20  .end

You can download the code for this example from http://nereida.deioc.ull.es/~pl/eyapsimple/source.tgz. To use it, unpack the tarball:

tar xvzf source.tgz
Change to the directory:
cd src
and compile the grammar with eyapp:
eyapp Infix.eyp
Compile also the set of tree transformations using treereg:
treereg -m main I2PIR.trg
After these two compilations we have two new modules:
nereida:/tmp/src> ls -ltr |tail -2
-rw-rw----  1 pl users   Infix.pm
-rw-rw----  1 pl users   I2PIR.pm
Module Infix.pm contains the parser for the grammar described in Infix.eyp. Module I2PIR.pm contains the collection of tree transformations described in I2PIR.trg. Now we can run the script infix2pir.pl which makes use of these two modules:
$ ./infix2pir.pl input1.inf > input1.pir
We can now make use of the parrot interpreter to execute the code:
$ /Users/casianorodriguezleon/src/parrot/parrot-1.9.0/parrot input1.pir 
a = 0
c = 4
d = -1

A note about the image links you can find at the top and bottom of each page:

Procesadores de Lenguajes 2010-01-31