WL Programming Language Specification
The WL programming language (pronounced Wool) contains
a strict subset of the features of Java and C. A WL source file contains
exactly one procedure definition.
Lexical Syntax
A procedure definition is a sequence of tokens
optionally separated by white space consisting
of spaces, newlines, or comments. Every valid token
is one of the following:
- ID: a string consisting of a letter (in the range a-z or A-Z) followed by
zero or more letters and digits (in the range 0-9), but not equal to
"wain", "int", "if", "else", "while", "println" or "return"
- NUM: a string
consisting of a single digit (in the range 0-9) or
two or more digits the first of which is not 0
- LPAREN: the string "("
- RPAREN: the string ")"
- LBRACE: the string "{"
- RBRACE: the string "}"
- RETURN: the string "return" (in lower case)
- IF: the string "if"
- ELSE: the string "else"
- WHILE: the string "while"
- PRINTLN: the string "println"
- BECOMES: the string "="
- WAIN: the string "wain"
- INT: the string "int"
- EQ: the string "=="
- NE: the string "!="
- LT: the string "<"
- GT: the string ">"
- LE: the string "<="
- GE: the string ">="
- PLUS: the string "+"
- MINUS: the string "-"
- STAR: the string "*"
- SLASH: the string "/"
- PCT: the string "%"
- COMMA: the string ","
- SEMI: the string ";"
White space consists of any sequence of the following:
- SPACE: (ascii 32)
- TAB: (ascii 9)
- NEWLINE: (ascii 10)
- COMMENT: the string "//" followed by all the characters up to and including the next
NEWLINE
Any pair of consecutive tokens may be separated by white space. Pairs of consecutive
tokens that both come from one of the following sets must be separated by
white space:
- {ID, NUM, RETURN, IF, ELSE, WHILE, PRINTLN, WAIN, INT}
- {EQ, NE, LT, LE, GT, GE, BECOMES}
Tokens that contain letters are case-sensitive; for example, int
is an INT token, while Int is not.
Context-free Syntax
A context-free grammar for a valid WL program is:
Context-sensitive Syntax
Any ID in a sequence derived from dcl is said to be declared.
Any ID in a sequence of tokens derived from statement or
factor is said to be used.
Any particular string x that is an ID may be declared at most once.
A string x which is an ID may be used any number of places,
but only if the same string x is declared. String comparisions
are case sensitive; for example, "FOO" and "foo" are distinct.
Semantics
The meaning of a syntactically correct WL program is defined by
transforming it to an equivalent Java program; the semantics of
the Java program are identical to those of the WL program.
A syntactically correct WL program may alternatively be transformed
into a C++ program with the same meaning.
Meaning in Java
Any WL program that obeys the syntax rules above is a valid Java program
fragment. The meaning of the program fragment is identical to that of
the Java program formed by inserting it at the indicated location in
the following Java program shell:
class WL {
public static void main(String[] args) {
println(wain(Integer.parseInt(args[0]),
Integer.parseInt(args[1])));
}
static void println(int x) { System.out.println(x); }
static
// === Insert WL Program Here ===
}
The resulting program takes two integer command line parameters,
passes them as arguments to the WL program, and prints the
result of the WL program to standard output.
Equivalent Meaning in C++
Any WL program that obeys the syntax rules above is a also a valid C++
program fragment. The meaning of the program fragment is identical to
that of the C++ program formed by inserting it at the indicated location
in the following C++ program shell:
int wain(int, int);
void println(int);
// === Insert WL Program Here ===
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char **argv){
int r = wain(atoi(argv[1]),atoi(argv[2]));
printf("%d\n",r);
}
void println(int x){
printf("%d\n",x);
}