Write a program that simulates a small programming language (SPL) with subprograms. The language has the following EBNF:
<program> ::= begin <statement> | <statement_sequence> end;
<statement_sequence> ::= <statement>; <statement_sequence>;
<statement> ::= <subprogram_invocation> | <other_statement>
<subprogram_invocation> ::= invoke <subprogram_identifier>
<other_statement> ::= increment <variable> | decrement <variable> |
output <variable> | stop_sub
<subprogram_identifier> ::= A | B | C
<variable> ::= X | Y | Z
Assumptions:
Every program automatically consists of environments for the main program and subprogram A, B, and C (even if not used). Each environment contains space for variables X, Y, and Z which are initialized to zero.
class Environment_Type
{
int X, Y, Z = 0;
}
Environment_Type Main, A, B, C = new Environment_Type ();
Sample Program
begin
increment X;
output X;
invoke A;
output X;
invoke B;
increment X;
increment X;
output X;
stop_sub;
output X;
stop_sub;
end;
It is erroneous to end a program when executing a subprogram.
Miscellaneous
program reads a text file
I have started and my next task is to check the length of the string and print the last character. This is the code i have so far
// create a fileScan Scanner for a file, Simple.txt
Scanner fileScan = new Scanner(new File("simple.txt"));
String inputString = "";
String commandString;
inputString = fileScan.next();
System.out.println (inputString);
if (inputString.equals ("begin"))
{
System.out.println ("Parsing begin");
// check whether the fileScan has the next item (i.e., next line)
while (fileScan.hasNext()) // check if there is any data to read
{
inputString = fileScan.next();
System.out.println (inputString);
if (inputString.equals ("output"))
{
System.out.print ("Parsing output ");
commandString = fileScan.next ();
System.out.println ("The Command is: " + commandString);
}
else
if (inputString.equals ("invoke"))
{
System.out.print ("Parsing invoke ");
commandString = fileScan.next ();
System.out.println ("The Command is: " + commandString);
}
else
if (inputString.equals ("increment"))
{
System.out.print ("Parsing increment ");
commandString = fileScan.next ();
System.out.println ("The Command is: " + commandString);
}
else
if (inputString.equals ("stop_sub;"))
{
System.out.print ("Parsing stop_sub ");
commandString = fileScan.next ();
System.out.println ("The Command is: " + commandString);
}
else
if (inputString.equals ("decrement"))
{
System.out.print ("Parsing decrement ");
commandString = fileScan.next ();
System.out.println ("The Command is: " + commandString);
}
}
}
else//if information is not found in file then the program is false
System.out.println ("Bad program");
}
}
this is the output
begin
Parsing begin
increment
Parsing increment The Command is: X;
output
Parsing output The Command is: X;
invoke
Parsing invoke The Command is: A;
output
Parsing output The Command is: X;
invoke
Parsing invoke The Command is: B;
increment
Parsing increment The Command is: X;
increment
Parsing increment The Command is: X;
output
Parsing output The Command is: X;
stop_sub;
Parsing stop_sub The Command is: output(this is where the problem is, I have started and my next task is to check the length of the string and print the last character. because the line should read:parsing stop_sub; and on the nextline output...and so on)
X;
stop_sub;
Parsing stop_sub The Command is: end;
Can anyone help please