Consider a very simple programming language named Z+-. The Z+- programming language has the following features:
- Every program can declare integer variables, using case-sensitive alphabetic identifiers. This is done using the DEF statement:
DEF hello ;
All declared variables are automatically initialized to 0.
- At the end of the program, all the variables are printed to the console in alphabetical order (this is the only form of output). Again, there are no explicit statements to do this printing; it is done implicitly.
- The right-hand side of a simple assignment statement (i.e., =) is either a variable name or unsigned integer. For example, the following are valid:
A = 12 ;
A = B ;
- There are two assignment statements: +=, *=, and -=. These have their usual meaning:
A += 34 ;
A *= B ;
- Every statement is terminated by a semi-colon.
- There is a loop statement - FOR - whose body contains at least one simple statement (i.e., no nested loops), which are on presented on one line. The keyword FOR is followed by a number, which indicates the number of times to execute the loop. Following this number is a sequence of statements defining the loop's body, followed by the word ENDFOR.
- Z+- programs must have at least one space separating all lexical elements.
Here is an example Z+- program:
DEF A ;
DEF B ;
A = 1 ;
FOR 5 B += A ; A *= 2 ; ENDFOR
A += 1000 ;
This program's output is:
A=1032
B=31
An equivalent Java program would be:
public static void main(String [] args)
{
int A=0;
int B=0;
A = 1;
for (int i=0; i<5; i++) {
B += A;
A *= 2;
}
A += 1000;
System.out.println("A=" + A);
System.out.println("B=" + B);
}
Z+- did not acquire any faithful users; therefore, the designer developed Z+-+. Z+-+ has the following features:
- Unlike Z+-, the programmer does not have to use the DEF statement to create a variable. Instead, the programmer can simply use the variable. The first use of the variable creates it and initializes it to 0.
- Z+-+ for loops can be nested:
FOR 5 B += A ; A *= 2 ; FOR 10 A += B ; ENDFOR ENDFOR
- You may assume that the programs are syntactically correct and will not cause arithmetic errors.