Language: Modula-
Make sure following is installed:
- GCC/G++
- flex (2.6.4)
- bison (3.0.4)
- make
- jre8 / openjkd-8
Building will only work on LINUX system.
On Windows, you'll need to use WSL to build it.
Tested on Ubuntu 18.04 and Windows 10 (WSL Ubuntu 18.04)
To build
chmod +x javaa
make clean
make
compiler [options] filename
Options:
-r Run the code on jvm after code generation.
-c Output comment into .jasm file durning code generation.
-o Output node operator type comment into .jasm file durning code generation.
-n No jasm code generation.
-l Showing each line when lex is parsing tokens.
-t Showing lex token information.
-p Showing yacc parsing trace.
-v Showing all trace information (same as -t -p).
- Implement all
String
andFloat
function - Implement local and global Array declaration, load and store.
- Implement
Read
(using java.util.Scanner) - Implement
Continue
andExit
inside loop statements. - Implement
Repeat
Until
loop - Implement
For
loop - Implement
Loop
loop
- Implement String escaping
- Implement negative float and double
- Fixing slash(/) in multiline comment /* */
comma ,
colon :
period .
semicolon ;
parentheses ( )
square brackets [ ]
brackets { }
arithmetic + - * /
remainder %
relational < <= >= > = <>
logical && || ~
assignment :=
array
boolean
begin
break
char
case
const
continue
do
else
end
exit
false
for
fn
if
in
integer
loop
module
print
println
procedure
repeat
return
real
string
record
then
true
type
use
util
var
while
An identifier is a string of letters and digits beginning with a letter. Case of letters is relevant, i.e. ident, Ident, and IDENT are not the same identifier. Note that keywords are not identifiers.
A sequence of one or more digits.
Either true or false.
A sequence of one or more digits containing a decimal point, optinally preceded by a sign (+ or ), and optionally following by an exponent letter and exponent.
A string constant is a sequence of zero or more ASCII characters appearing between double-quote (”) delimiters. A double-quote appearing with a string must be written after a ”. For example, ”aa” ”bb” denotes the string constant aa”bb.
Comments can be denoted in several ways:
- Modula-style is text surrounded by “(” and “)” delimiters, which may span more than one line;
- C++-style comments are a text following a “//” delimiter running up to the end of the line.
The predefined data types are string
, integer
, boolean
, and real
.
There are two types of constants and variables in a program:
- global constants and variables: declared inside the program
- local constants and variables: declared inside functions
A constant declaration has the form:
const identifier = constant exp; <...; identifier = constant exp; >
The type of the declared constant must be inferred based on the constant expression on the right-hand side. Note that constants cannot be reassigned or this code would cause an error.
For example,
const s = "Hey There";
i = -25;
f = 3.14;
b = true;
A variable declaration has the form:
var identifier<, ... , identifier>: type; <...; identifier<, ... , identifier>: type;>
where type is one of the predefined data types. For example,
var s : string;
i : integer;
d : real;
b : boolean;
Arrays declaration has the form:
identifier<, ... , identifier>: array [num ... num ] of type;
...;
identifier<, ... , identifier>: array [num ... num ] of type;
For example,
a: array [1, 10] of integer; // an array of 10 integer elements
b: array [0, 5] of boolean; // an array of 6 boolean elements
f: array [1, 100] of real; // an array of 100 float-point elements
The two program units are the program and functions.
A program has the form:
module identifier
<zero or more variable and constant declarations>
<zero or more function declarations>
begin
<zero or more statements>
end identifier.
where the item in the < > pair is optional.
Procedure declaration has the following form:
procedure identifier <( formal arguments )> <: type >
<zero or more constant and anvariable declarations>
begin
<one or more statements>
end identifier;
where : type is optional and type can be one of the predefined types. The formal arguments are declared in the following form:
identifier : type <, identifier : type, ... , identifier : type>
Parentheses are not required when no arguments are declared. No procedures may be declared inside a procedure. For example,
module example
// constants and variables
const a = 5;
var c : integer;
// procedure declaration
procedure add(a:integer, b:integer) : integer
begin
return a+b;
end add;
// statements
begin
c = add(a, 10);
print c;
end example.
There are several distinct types of statements in Modula.
The simple statement has the form:
identifier := expression;
or
identifier[integer expression] := expression;
or
print expression; or println expression;
or
read identifier;
or
return; or return expression;
Arithmetic expressions are written in infix notation, using the following operators with the precedence:
(1) - (unary)
(2) * /
(3) + -
(4) < <= = => > <>
(5) ~
(6) &&
(7) ||
Associativity is the left. Valid components of an expression include literal constants, variable names, function invocations, and array reference of the form
A [ integer expression ]
A function invocation has the following form:
identifier ( <comma-separated expressions> )
The conditional statement may appear in two forms:
if (boolean expr) then
<zero or more statements>
else
<zero or more statements>
end;
or
if (boolean expr) then
<zero or more statements>
end;
The loop statement has the form:
while (boolean expr) do
<zero or more statements>
end ;
The semantics of the constructs are the same as the corresponding Pascal and C constructs, with the following exceptions and notes:
- The parameter passing mechanism for procedures in call-by-value.
- Scope rules are similar to C.
- The identifier after the end of program or procedure declaration must be the same identifiers as the name given at the beginning of the declaration.
- Types of the left-hand-side identifier and the right-hand-side expression of every assignment must be matched.
- The types of formal parameters must match the types of the actual parameters.