eXample Programming Language

Version 0.3.0 (2012)
Project state
inactive
Download Check Download page
GitHub https://github.com/phorward/xpl
License Public domain / WTFPL

About

XPL compiles out of the box after UniCC is installed

XPL, eXample Programming Language, is the name a simple scripting language compiler that is implementend in the course of the UniCC User's Manual as a real-world example of implementing a compiler for a simple, stack-based virtual machine.

XPL provides

It can also be perfectly used as an implementation base for any other language.

Grammar

XPLs grammar draft in the UniCC grammar definition language is the following:

//Precedence and associativity
#left               "=";
#left               "==" "!=" "<=" ">=" '>' '<';
#left               '+' '-';
#left               '*' '/';

//Regular expressions
@string             '"' !'"'* '"';

@identifier         'A-Za-z_' 'A-Za-z0-9_'*;

//Lexemes
#lexeme             real
                    ;

real               ->      real_integer '.' real_fraction
                    |       real_integer '.'?
                    |       '.' real_fraction
                    ;

real_integer        ->      real_integer '0-9'
                    |       '0-9'
                    ;
                   
real_fraction       ->      real_fraction '0-9'
                    |       '0-9'
                    ;
                   
//Whitespace grammar construct
#whitespaces        whitespace
                    ;

whitespace          ->      ' \r\n\t'+
                    |       "//" !'\n'* '\n'
                    ;

//Goal symbol
program$            ->      statement*
                    ;

statement           ->      "if" '(' expression ')' statement
                    |       "if" '(' expression ')' statement
                                "else" statement
                    |       "while" '(' expression ')' statement
                    |       '{' statement* '}'
                    |       expression ';'
                    |       ';'
                    ;

expression          ->      variable "=" expression
                    |       expression "==" expression
                    |       expression "!=" expression
                    |       expression '>' expression
                    |       expression '<' expression
                    |       expression "<=" expression
                    |       expression ">=" expression
                    |       expression '+' expression
                    |       expression '-' expression
                    |       expression '*' expression
                    |       expression '/' expression
                    |       '-' expression              #precedence '*'
                    |       '(' expression ')'
                    |       real
                    |       @string
                    |       variable
                    |       function '(' parameter_list? ')'
                    ;

parameter_list      ->      parameter_list ',' expression
                    |       expression
                    ;

variable
function            ->      @identifier
                    ;

Example

A simple program example written in XPL is the 99bottles.prg, a version of the famous 99-bottles-of-beer program.

if( ( bottles = prompt( "Enter number of bottles [default=99]" ) ) == "" )
    bottles = 99;

if( integer( bottles ) <= 0 )
{
    print( "Sorry, but the input '" + bottles + "' is invalid." );
    exit( 1 );
}

while( bottles > 0 )
{
    if( bottles > 1 )
        print( bottles + " bottles of beer on the wall, " +
                bottles + " bottles of beer." );
    else
        print( "One bottle of beer on the wall, one bottle of beer." );

    print( "Take one down, pass it around." );
        
    if( ( bottles = bottles - 1 ) == 0 )
        print( "No more bottles of beer on the wall." );
    else if( bottles == 1 )
        print( "One more bottle of beer on the wall." );
    else
        print( bottles + " more bottles of beer on the wall." );
}