Friday 29 June 2007

Notes on the use of REDUCTION in program conversion from Natural to JAVA

This is an explanation of reduction in complex statements.

There have to be two stacks. One holds the
instructions that are being processed. The other
holds the tokens in the statement.
The stack leaves are composed of two fields.

For ex:

MOVE A TO B

Requires

instruction stack:
MOVE, MOVE
Token stack:
B, MOVE-PARM2 -> TO, MOVE-TO -> A, MOVE-PARM1 -> MOVE, MOVE


This is simple as it is.
Let’s take a complex statement:

IF X = Y
MOVE A TO B
END-IF

instruction stack at first line:
IF, IF
Token stack:
X = Y, CALC -> IF, IF

instruction stack at second line:
MOVE, MOVE -> IF, IF
Token stack:
B, MOVE-PARM2 -> TO, MOVE-TO -> A, MOVE-PARM1 -> MOVE, MOVE
-> X = Y, CALC -> IF, IF

The problem here is, how are we going to convert this parsing information to
a logical meaning?

The solution is at the end of the move statement a REDUCTION should be
Made so that the token stack holds the pith of the MOVE statement, namely the
Natural statement converted to JAVA.

The solution follows:
instruction stack at the end of second line:
IF, IF
Token stack:
B = A; , MOVE-STATEMENT -> X = Y, CALC -> IF, IF

In this case, the stacks will be as below:
instruction stack at the end of third line:
nill
Token stack:
Nill

Output:
If (X==Y) {
B = A;
}


Things are not that neat though. There is a diference between a simple
MOVE statement

MOVE A TO B

And a MOVE statement in a complex statement

IF X = Y
MOVE A TO B
END-IF

The simple MOVE statement does not upush its reduction output
into the token stack but the the MOVE statement in the complex statement
does. So, just after the reduction the program checks if there are any
leaves in the instruction stack. If yes then it pushes its reduced and converted
output into the token stack.

One more note, as you may have noticed there is a need to write a pretty
Printer for the outputs.

PROBLEM: While this redduction approach feels very sensible,
it is difficult to print the source Natural lines and output JAVA lines
in sequence with this approach. The output lists the input
complex Natural statement with all its lines and then the output
complex JAVA statement as bunch…