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…











Operator conversion in a calculation for Natural to JAVA program conversion

condConvert converts all the operators
in a given calculation.


string condConvert(string inpCond)
{

}
string checkSeperatingOperator(string inpToken)
{

}
string convertOperator(string inpToken)
{

}



string checkSeperatingOperator(string inpToken);
string convertOperator(string inpToken);

string condConvert(string inpCond)
{

//inpCond = " A NE 'B'";
//inpCond = "#SITE-A NE AAA";
//inpCond = "NOT A = B";
//inpCond = "A";
//inpCond = "NOT DEVICE = BATCH";

string inpCondToken="AAA";
string seperatingOperator = "";
int tokenSeq =0;
int seperatingOperatorPOS =0;
int notPOS = 0;
string outputCond="";
string begInpCond = "";
string endInpCond = "";
string convertedSeperatingOperator= "";

cout << "inpCond=" << inpCond << endl;
inpCond = eraseNewLine(inpCond);

while (inpCondToken != "")
{
inpCondToken = get_ith_token_of_string(inpCond, tokenSeq);
cout << "inpCondToken=" << inpCondToken << endl;
if (seperatingOperator == "")
{
seperatingOperator = checkSeperatingOperator(inpCondToken);
seperatingOperatorPOS = inpCond.find(seperatingOperator, 0);
}

notPOS = inpCond.find("NOT" , 0);

if (notPOS == -1) notPOS = inpCond.find("not" , 0);

tokenSeq = tokenSeq + 1;
}

cout << "seperatingOperator=" << seperatingOperator << " seperatingOperatorPOS=" << seperatingOperatorPOS << endl;
cout << "notPOS=" << notPOS << endl;

if (notPOS > -1)
{
inpCond = inpCond.substr(notPOS + 3);
cout << "inpCond=" << inpCond << endl;
seperatingOperatorPOS = seperatingOperatorPOS - 4;
}
begInpCond = inpCond.substr(0, seperatingOperatorPOS);
cout << begInpCond << endl;
endInpCond = inpCond.substr(seperatingOperatorPOS + seperatingOperator.length());
cout << endInpCond << endl;
convertedSeperatingOperator = convertOperator(seperatingOperator);
cout << convertedSeperatingOperator << endl;
trim(begInpCond);
trim(endInpCond);
if (notPOS == -1)
outputCond = begInpCond + "." + convertedSeperatingOperator + "(" + endInpCond + ")";
else
outputCond = "!(" + begInpCond + "." + convertedSeperatingOperator + "(" + endInpCond + "))";
outputCond = convertCharInString(outputCond, '\'', '\"');
cout << "outputCond=" << outputCond << endl;


return(outputCond);
}
string checkSeperatingOperator(string inpToken)
{
string seperatingOperatorList[23] = {"%=",
"EQUAL TO", "EQUAL TO", "EQ", "<=", "<>",
"NE", "NOT =", "NOT EQUAL TO", "NOT EQUAL", "EQUAL",
"<", "LT", "LESS THAN", ">", "LE",
"LESS EQUAL", ">=", "GT", "GREATER THAN", "=",
"GE", "GREATER EQUAL"};
int operatorPos = -1;

for (int j =0; j < 23; j++)
{
operatorPos = inpToken.find(seperatingOperatorList[j], 0);
if (operatorPos > -1) return(seperatingOperatorList[j]);
}
return("");
}
string convertOperator(string inpToken)
{
string seperatingOperatorList[23] = {"%=",
"EQUAL TO", "EQUAL TO", "EQ", "<=", "<>",
"NE", "NOT =", "NOT EQUAL TO", "NOT EQUAL", "EQUAL",
"<", "LT", "LESS THAN", ">", "LE",
"LESS EQUAL", ">=", "GT", "GREATER THAN", "=",
"GE", "GREATER EQUAL"};
string convertedOperatorList[23] = {"eq",
"eq", "eq", "eq", "le", "ne",
"ne", "ne", "ne", "ne", "eq",
"lt", "lt", "lt", "gt", "le",
"le", "ge", "gt", "gt", "eq",
"ge", "ge"};
int operatorPos = -1;

for (int j =0; j < 23; j++)
{
operatorPos = inpToken.find(seperatingOperatorList[j], 0);
if (operatorPos > -1) return(convertedOperatorList[j]);
}
return("");
}

Data Redefine problem in program conversion from Natural to JAVA

*S****DF 0000A 44 1#APPL-TYP-VAL
*S****DRR 0000 R1#APPL-TYP-VAL
*S****DFR 0000A 8 2#APPLID
*S****DFR 0000A 20 2#TYPE
*S****DFR 0000A 16 2#VALUE

The conversion process has to find a solution for how to
Convert the data redefines…

One open source solution from an established
IT conversion company is given below. Please
note that this solution requires a total approach
defining variable types as classes etc. This
means a large amount of substructure code
and home-keeping.


// 0000A 44 1#APPL-TYP-VAL
public final AValMem _applTypVal=newAValMem(44);

// 0000 R1#APPL-TYP-VAL
public static class DataRedefine_1 extends AValMem {

// 0000A 8 2#APPLID
public final AValMem _applid;

// 0000A 20 2#TYPE
public final AValMem _type;

// 0000A 16 2#VALUE
public final AValMem _value;

public DataRedefine_1() {
this(new AValMem(44),0);
}

public DataRedefine_1(MemoryBlock PARENT,int OFFSET) {
this(PARENT,OFFSET,true);
}

public DataRedefine_1(MemoryBlock PARENT,int OFFSET,boolean MUST_INITIALIZE) {
super(PARENT,OFFSET,44);
_applid=newAValMem(this,0,8);
_type=newAValMem(this,8,20);
_value=newAValMem(this,28,16);
}

}

public final DataRedefine_1 redefine_1=new DataRedefine_1(_applTypVal,0);

Variable Conversion in program conversion from Natural to JAVA

Natural programs have different types of variables,
namely
Local Variables,
Global Variables,
Database Table field names.

Local and global variable definitions have to be
converted and kept in new files. There has to be
a dedicated conversion program to convert variable
definitions.

Once variable definitions are converted, the output
files of this conversion process can be used to
convert the variable names in the natural program.
It is not enough to convert the instructions in the
Natural program. The variable names have to be
converted also…

An important problem in variable conversion is
DataRedefine statements in the original Natural
Programs’ variable definitions… I will write a
seperate note on this subject matter.

The programs listed below uses ready JAVA
Data structure definitions to convert Natural
programs’ var names and get their variable
lengths to be used for WRITE statements…


string convertLocalVarName(string inpToken);
string convertGlobVarName(string inpGlobalStr);


string convertVarNames(string inpStr)
{
… //convert array names
}
string convertVarNamesBody(string tokenVal)
{
… //convert var names
}
string convertLocalVarName(string inpToken)
{

}
string getLocVarLength(string lineStr, string inpToken)
{

}
string getLocRedefVarLength(string lineStr, string inpToken)
{

}
string convertGlobVarName(string inpToken)
{

}
string prepNameForTableVar(string inpStr)
{

}
string prepNameForTableVarAlternate(string inpStr)
{

}

string convertSimpleGlobalVarName(string inpToken)
{

}

--------------------------------------------------------------------------
string convertLocalVarName(string inpToken)
{
string inputFile="";
FILE *varFILE;
string progNameVar="";
string dumStr="";
string searchNameVar="";
int loc=0;
int locBeg=0;
int locEnd=0;
string varName="";
int locRedefine=0;
int locRedefineEnd=0;
string strRedefine="";
string strRedefinePrev="";
string progNameTag="";
string retVarName="";

int locLeftBracket=0;
int locRightBracket=0;
int countNest=0;
int locStaticClass=0;
string redef[10] = {"","","","","","","","","",""};
int locTablesView=0;
int locRedefineBeg=0;
string varLengthStr="";
string ThirdTokenofLineStr="";
int endInpToken=0;

string initialChar="";
int numPos=0;
int operatorPos=0;
string numbersStr = "0123456789";
string operatorStr="";

cout << "convertLocalVarName inpToken=" << inpToken << endl;
//if (inpToken.substr(0,1) != "#")
// return("");

initialChar = inpToken.substr(0,1);
numPos = numbersStr.find(initialChar,0);

if (numPos != -1) return("");

trim(inpToken);
operatorStr = toUpper(inpToken);
//cout << "inpToken=(" << operatorStr << ")" << endl;
operatorPos = operatorList_find(operatorStr, 0);
//cout << "operatorPos=" << operatorPos << endl;
if ((operatorPos > -1) (inpToken == ""))
return("");

string VAR_REF_DIR_NAME = getSetupParamVal("VAR_REF_DIR_NAME");
//Hrdb301.java --> Local_Hrdl301b.java
//Hrdp001.java --> Local_Hrdl001.java


progNameVar = progName;
progNameVar = StringToUpper(progNameVar);

if (progNameVar.substr(3,1) == "B")
{
progNameVar = "LOCAL_HRDL" + progNameVar.substr(4,3) + "B.JAVA";
}
else
{
progNameVar = "LOCAL_HRDL" + progNameVar.substr(4,3) + ".JAVA";
}

inputFile = VAR_REF_DIR_NAME + "\\" + progNameVar;
//cout << inputFile<
char line[200] = "";
string lineStr="";

if( (varFILE = fopen(inputFile.c_str(), "r" )) != NULL )
{

while( !feof( varFILE ))
{
if (fgets( line, 199, varFILE) == NULL) break;

//lineStr = StringToUpper(line);
lineStr = ConvertToString(line);
locLeftBracket = lineStr.find("{",0);
locRightBracket = lineStr.find("}",0);
if (locLeftBracket > -1) countNest = countNest + 1;
if (locRightBracket > -1) countNest = countNest - 1;
locRightBracket = lineStr.find("}",locRightBracket + 1);
if (locRightBracket > -1) {countNest = countNest - 1;}
//cout << "countNest =" << countNest << " " << lineStr;

if (countNest > 0)
{
for (int ii=countNest - 1; ii < 9; ii++) {redef[ii] = "";}
strRedefinePrev ="";
for (int jj=0; jj < 9; jj++)
{
if (redef[jj] != "") strRedefinePrev= strRedefinePrev + "." + redef[jj];
if (strRedefinePrev.substr(0,1) == ".") strRedefinePrev = strRedefinePrev.substr(1);
}
}
loc = lineStr.find(inpToken,0);

if (loc > -1)
{
//cout << "loc = " << loc << lineStr << endl;
endInpToken = lineStr.find(" ", loc + 1 );

if (endInpToken == -1) endInpToken = lineStr.find("=", loc + 1 );
if (endInpToken == -1) endInpToken = lineStr.find(";", loc + 1 );
if (endInpToken == -1) endInpToken = lineStr.find("\n", loc + 1 );
//cout << endInpToken << endl;
if ((endInpToken - loc) > 0)
//cout << "inpToken=" << lineStr.substr(loc, endInpToken - loc)<< endl;
//cout << inpToken.length()<< endl;
if ((endInpToken - loc) > inpToken.length()) loc = -1;
}
//loc = lineStr.find(inpToken,0);
//cout << line ;
if (loc > -1)
{
if (inpToken.substr(0,1) == "#")
{
varLengthStr = getLocVarLength(lineStr,inpToken);
cout << " varLengthStr=" << varLengthStr << endl;
VarLengthValueStr = varLengthStr;
fgets( line, 199, varFILE);
lineStr = ConvertToString(line);
locBeg = lineStr.find("_",0);
locEnd = lineStr.find("=",0);
if (locEnd == -1) locEnd = lineStr.find(";",0);
varName = lineStr.substr(locBeg, (locEnd - locBeg));
}
else
{
varLengthStr = getLocRedefVarLength(lineStr,inpToken);
ThirdTokenofLineStr = get_ith_token_of_string(lineStr, 3 );
locBeg = ThirdTokenofLineStr.find(inpToken, 0);

if (locBeg > -1) varName = inpToken;
else
{
varName = "";
varLengthStr = "";
}
}

if ((progName.substr(3,1) == "B") (progName.substr(3,1) == "b"))
progNameTag = "hrdl" + progName.substr(4,3) + "b";
else
progNameTag = "hrdl" + progName.substr(4,3);

//cout << "progNameTag=" << progNameTag<< endl;
//cout << "strRedefinePrev=" << strRedefinePrev<< endl;
if (varName!="")
{
if (strRedefinePrev == "")
retVarName = progNameTag + "." + varName;
else
retVarName = progNameTag + "." + strRedefinePrev + "." + varName;
}
trim(retVarName);
return(retVarName);
}

lineStr = ConvertToString(line);
locRedefine = lineStr.find("DataRedefine", 0);
if (locRedefine == -1) locRedefine = lineStr.find("TablesView", 0);
locTablesView = lineStr.find("TablesView", 0);

if (locRedefine > 0)
{
locStaticClass = lineStr.find("static class", 0);
if (locStaticClass == -1) locStaticClass = lineStr.find("public class", 0);

if (locTablesView == -1)
{
locRedefineEnd = lineStr.find(" ", locRedefine);
strRedefine = lineStr.substr(locRedefine + 4,(locRedefineEnd - locRedefine -4));
strRedefine = toLower(strRedefine);

}
else
{
locRedefineEnd = lineStr.find(" ", locTablesView);
locRedefineBeg = lineStr.find_last_of(" ", locRedefine - 1);
strRedefine = lineStr.substr(locRedefineBeg + 5,(locRedefineEnd - locRedefineBeg -4));
//strRedefine = toLower(strRedefine);
strRedefine = toLower(strRedefine.substr(0,1)) + strRedefine.substr(1);
trim(strRedefine);
};

if ((countNest > 0) && (locStaticClass > -1))
{
//cout << "countNest =" << countNest << "++++++++++++++++++++++++++++++++++++++++" << endl;
//cout << "lineStr" << lineStr << endl;
redef[countNest - 2] = strRedefine;
for (int ii=countNest - 1; ii < 9; ii++) {redef[ii] = "";}
strRedefinePrev ="";
for (int jj=0; jj < 9; jj++)
{
if (redef[jj] != "") strRedefinePrev= strRedefinePrev + "." + redef[jj];
if (strRedefinePrev.substr(0,1) == ".") strRedefinePrev = strRedefinePrev.substr(1);
//cout << jj << "=" << redef[jj] << endl;
}
//cout << "strRedefinePrev=" << strRedefinePrev << endl;
}

if (countNest == 1) strRedefinePrev ="";
}
}
fclose( varFILE );
}
return(varName);
}

Thursday 28 June 2007

Actual Conversion in Converting Natural Programs to JAVA

Add statement without giving key word is chosen
to demonstrate the conversion process… The parts
related to parsing and other keyword options are
omitted.

In the conversion process, the tokens are popped
from the token stack and placed in param vars
according to their spatial positions. The JAVA
statement is assembled from these variables.
The JAVA statement is written into the output
file.

The instruction stack and the token stack are
cleaned.


void beginADDstatement()
{

}

void continueADDstatement(std::string tokenVal)
{
if (tokenVal == "END")
{
endADDstatementWithoutGiving();
return;
}

if (object.last(head)->meaning == "ADD")
{

else
//if parm2
if (object.last(head)->meaning == "ADD-TO")
{
head=object.push(head, tokenVal, "ADD-PARM2");
cout << "ADDprocessss tokenVal=" << nexttokenval=" << nextTokenVal << endl; if (objTOKEN.is_characteristic(nextTokenVal) nextTokenVal == " statement_text = "\n" statement_text =" object.last(head)-">element + statement_text;
PARM2_text = object.last(head)->element;
PARM2_text = convertVarNames(PARM2_text);

if (object.pop(head) == NULL) head = NULL;
else statement_text = object.last(head)->element + " " + statement_text;
if (object.pop(head) == NULL) head = NULL;
else
{
statement_text = object.last(head)->element + " " + statement_text;
PARM1_text = object.last(head)->element;
PARM1_text = convertVarNames(PARM1_text);
}
if (object.pop(head) == NULL) head = NULL;
else statement_text = object.last(head)->element + " " + statement_text;
if (object.pop(head) == NULL) head = NULL;

JAVA_statement_text = PARM2_text + ".add(" + PARM1_text + ");\n";

if (opened_output_file == true)
{
fputs( JAVA_statement_text.c_str(), outFile);
}

if (instrObject.pop(instrHead) == NULL) instrHead = NULL;

if (instrHead == NULL) object.stack_delete();
instrObject.stack_display(instrHead);
object.stack_display(head);
}

Calculation Parser for a Parser/Converter from Natural to JAVA

Calculation Parser
calcParse processes each token that is passed to it.
For
MOVE A + 1 TO B
Calc variable is A + 1
A + 1 is divided to
A
+
1
And these values are passed to calcParse
each time.

calcParse evaluates the value of the token first.
(It parses the calculation to parantshesis,
Operators and variable names…)
Then it pushes the token into a calcStack.

At the end the calling program pulls the evaluated calculation
Variable value from the calcStack.

int operatorList_find(string calcStatement, int i);
std::string CH;

void calcPARSE(string calcStatement)
{

string alphabet = "ABCÇDEFGĞHIİJKLMNOÖPQRSŞTUÜVWXYZ0123456789#*'-/=_%<>()";


calcStatement = StringToUpper(calcStatement);

for (int i=0; i < calcStatement.length(); i++)
{
CH = calcStatement.substr(i,1);

if (CH == " ") continue;
//delete all spaces in the input string

// first character***********************************************
if ((i==0) && (CH == "("))
{
//write OPERATOR to token stack
tokenSTACK = tokenSTACK + CH;
calcHead=calcObject.push(calcHead, "(", "LPRNTHSS");
continue;
}

//first character is right paranthesis
if ((i==0) && (CH == ")"))
{…

operatorLNGTH = operatorList_find(calcStatement, i);

//first char is alphanum
if ((i == 0) && ((CH != "(") && (operatorLNGTH == -1)))
{
//if CH is A-Z and BEG ---beg char
alphabetPOS = alphabet.find(CH,0);

if (alphabetPOS > -1)
{
charSTACK = charSTACK + CH;
}

if (i == calcStatement.length() - 1)
{
calcHead=calcObject.push(calcHead, charSTACK, "PARM");
}
continue;
}

// OPERATOR **************************************

operatorLNGTH = operatorList_find(calcStatement, i);

if (operatorLNGTH > -1)
{
i = i + operatorLNGTH - 1;
operatorLNGTH = -1;

tokenSTACK = tokenSTACK + charSTACK + CH;

if (calcObject.last(calcHead) != NULL)
{
if ( calcObject.last(calcHead)->element == ")" )
{
}
else //write previous PARM to token stack
calcHead=calcObject.push(calcHead, charSTACK, "PARM");
} //write previous PARM to token stack
else if ((i > 0) && charSTACK.length() > 0) calcHead=calcObject.push(calcHead, charSTACK, "PARM");

charSTACK = "";

//write operator to token stack
calcHead=calcObject.push(calcHead, CH, "OPERATOR");
charSTACK = "";
continue;
}

// NOT **************************************
if ( calcStatement.substr(i,3) == "NOT")
{…

//if CH is A-Z and BEG continue char*************************
alphanumPOS = alphanum.find(CH,0);

if (alphanumPOS > -1)
{
charSTACK = charSTACK + CH;

if (i == (calcStatement.length() - 1))
{ //write previous PARM to token stack
calcHead=calcObject.push(calcHead, charSTACK, "PARM");
}
continue;
}

// ( **************************************
if (CH == "(")
{…

// ) **************************************
if (CH == ")")
{…

// EOS **************************************

if (i == calcStatement.length() - 1)
{
calcHead=calcObject.push(calcHead, charSTACK, "PARM");
charSTACK = "";
continue;
}
}
//cout << "calcStatement=[" << calcStatement << "]" << endl;
//calcObject.stack_display(calcHead);
}

operatorList_find(string calcStatement, int operator_pos)
function checks whether the operator an the calcstatement
position i is a legitimate operator and converts it
to a formatted version. It handles single and double character
tokens which may appear at seperate lines…


int operatorList_find(string calcStatement, int i)
{
int operatorPos = -1;
string restCalcStatement = calcStatement.substr(i);
//cout << "i=" << i << " restCalcStatement=" << restCalcStatement << endl;
string operatorList[29] = {" * ", " / ", " + ", " - ", " %= ",
"EQUAL TO", "EQUAL TO", " EQ ", " <= ", " <> ",
" NE ", "NOT =", "NOT EQUAL TO", "NOT EQUAL", "EQUAL",
" < ", " LT ", "LESS THAN", " > ", " LE ",
"LESS EQUAL", " >= ", " GT ", "GREATER THAN", " = ",
" GE ", "GREATER EQUAL", " OR ", " AND "};
string operatorListBegChar[29] = {"*", "/", "+", "-", "%=",
"EQUAL TO", "EQUAL TO", "EQ", "<=", "<>",
"NE", "NOT =", "NOT EQUAL TO", "NOT EQUAL", "EQUAL",
"<", "LT", "LESS THAN", ">", "LE",
"LESS EQUAL", ">=", "GT", "GREATER THAN", "=",
"GE", "GREATER EQUAL", "OR", "AND"};
string operatorListBegDoubleChar[29] = {"* ", "/ ", "+ ", "- ", "%= ",
"EQUAL TO ", "EQUAL TO ", "EQ ", "<= ", "<> ",
"NE ", "NOT = ", "NOT EQUAL TO ", "NOT EQUAL ", "EQUAL ",
"< ", "LT ", "LESS THAN ", "> ", "LE ",
"LESS EQUAL ", ">= ", "GT ", "GREATER THAN ", "= ",
"GE ", "GREATER EQUAL ", "OR ", "AND "};
//pre-compiler conversions
for (int j =0; j < 29; j++)
{
if (i==0)
{
if (calcStatement.length() < 3)
operatorPos = restCalcStatement.find(operatorListBegChar[j], 0);
else
operatorPos = restCalcStatement.find(operatorListBegDoubleChar[j], 0);
}
else operatorPos = restCalcStatement.find(operatorList[j], 0);

if ( operatorPos == 0)
{
//cout << "J=" << j << endl;
CH = operatorList[j];
return(operatorList[j].length());
}
}
return(-1);
}

Wednesday 27 June 2007

Statement continuation for a Parser/converter from Natural to JAVA

Statement continuation includes parsing and
conversion processes. This outline excludes
statements related to conversion and simplifies
the program generally.

// ADD A + 1 TO B GIVING C
// ADD A + 1 TO B END

void continueADDstatement(std::string tokenVal)
{
//if END
if (tokenVal == "END")
{
endADDstatementWithoutGiving();
return;
}
//if move
if (object.last(head)->meaning == "ADD")
{
if (tokenVal == "TO")
{

//push CONDITION from the calcSTACK to the tokenSTACK
… object.push(head,statement_text,"CALC");

head=object.push(head, tokenVal, "ADD-TO");
}
else
{
//call CONDITION processing with (tokenVal)Take in(A + 1)
calcPARSE(tokenVal);
}
}
else
//if parm2 (B)
if (object.last(head)->meaning == "ADD-TO")
{
head=object.push(head, tokenVal, "ADD-PARM2");

if (objTOKEN.is_characteristic(nextTokenVal) nextTokenVal == "END-IF") //if End of statement
{
endADDstatementWithoutGiving();
}
}
else
//if giving
if (object.last(head)->meaning == "ADD-PARM2")
{
head=object.push(head, tokenVal, "ADD-GIVING");
}
else
//if parm3
if (object.last(head)->meaning == "ADD-GIVING")
{
head=object.push(head, tokenVal, "ADD-PARM3");

// CONVERT


JAVA_statement_text = "" + PARM3_text + " = " + PARM2_text + " + " + PARM1_text + ";" + "\n";

fputs( JAVA_statement_text.c_str(), outFile);
}

if (instrObject.pop(instrHead) == NULL) instrHead = NULL;

if (instrHead == NULL) object.stack_delete();
}
}
void endADDstatementWithoutGiving()
{

// pop the TOKEN stack and create the output code line for MOVE
// CONVERTTTTTTTTTTTTTTTTTTTTttttttttttttttt

JAVA_statement_text = PARM2_text + ".add(" + PARM1_text + ");\n";

//if (head == NULL)
if (opened_output_file == true)
{
fputs( JAVA_statement_text.c_str(), outFile);
}

if (instrObject.pop(instrHead) == NULL) instrHead = NULL;

if (instrHead == NULL) object.stack_delete();
instrObject.stack_display(instrHead);
object.stack_display(head);
}

statement initiation for a parser/converter from Natural to JAVA

Statement initiation is basicly putting a leaf into the
instruction stack. The last leaf in the instruction stack
indicates the instruction that is related to the leafs in
the token stack. If reduction is used, instruction stack
becomes very useful.

void beginADDstatement()
{
string statement_text="";

head=object.push(head, "ADD", "ADD");
instrHead=instrObject.push(instrHead, "ADD", "ADD");
}

Tuesday 26 June 2007

Implementation of a LOOK AHEAD function with C++ for a PARSER/CONVERTER

//20070318ARS Break this line into its tokens
tokenSeq = 0;

while (tokenVal != "")
{
tokenVal = objTOKEN.get_ith_token (code_line, tokenSeq);
nextTokenVal = objTOKEN.get_ith_token (code_line, tokenSeq + 1);

if (nextTokenVal == "" && tokenVal != "" && !feof( stream ))
{
filePos = ftell( stream );

if (fgets( line_lookFW, 199, stream) == NULL) cout << "could not read in LookFW" << endl;

nextTokenVal = objTOKEN.get_ith_token (line_lookFW, 0);
cout << "line_lookFW nextTokenVal=" << nextTokenVal << endl;

while (nextTokenVal == "" && !feof( stream ))
{
if (fgets( line_lookFW, 199, stream) == NULL) cout << "could not read in LookFW" << endl;
nextTokenVal = objTOKEN.get_ith_token (line_lookFW, 0);
cout << "1line_lookFW nextTokenVal=" << nextTokenVal << endl;
}
result = fseek( stream, filePos, SEEK_SET);
if (result) printf( "line_lookFW Fseek failedddddddd" );
if (nextTokenVal == "") cout << "EOFFFFFFFFFFffff" << endl;
}

if (tokenVal == "") //tokens on the line are finished
{
cout << "tokens on the line are finished" << endl;
}
else //tokens on the line are being processed.............




//20070318ARS Break this line into its tokens
tokenSeq = 0;

while (tokenVal != "")
{
tokenVal = objTOKEN.get_ith_token (code_line, tokenSeq);
nextTokenVal = objTOKEN.get_ith_token (code_line, tokenSeq + 1);

if (nextTokenVal == "" && tokenVal != "" && !feof( stream ))
{
filePos = ftell( stream );

if (fgets( line_lookFW, 199, stream) == NULL) cout << "could not read in LookFW" << endl;

nextTokenVal = objTOKEN.get_ith_token (line_lookFW, 0);
cout << "line_lookFW nextTokenVal=" << nextTokenVal << endl;

while (nextTokenVal == "" && !feof( stream ))
{
if (fgets( line_lookFW, 199, stream) == NULL) cout << "could not read in LookFW" << endl;
nextTokenVal = objTOKEN.get_ith_token (line_lookFW, 0);
cout << "1line_lookFW nextTokenVal=" << nextTokenVal << endl;
}
result = fseek( stream, filePos, SEEK_SET);
if (result) printf( "line_lookFW Fseek failedddddddd" );
if (nextTokenVal == "") cout << "EOFFFFFFFFFFffff" << endl;
}

if (tokenVal == "") //tokens on the line are finished
{
cout << "tokens on the line are finished" << endl;
}
else //tokens on the line are being processed.............

Main loop and process line function

Main loop
While not EOF
Check end of member file
Readline
Processline
end




Process line loop
Extract statement
Parse statement
Convert statement




Extract statement
Read line
Read tokens of the line
if the new token is characteristic break
else continue to read tokens
if EOL continue to READ line


MAIN LOOP
//20070316ARS open inputFile
if( (stream = fopen(inputFile.c_str(), "r" )) != NULL )
{
//20070316ARS read inputFile
while( !feof( stream ))
{
if (fgets( line, 199, stream) == NULL) break;
//20070316ARS process input line
StringToUpper(line);
process_line(line);

if (EndOfInput == true) break;
}

//20070316ARS close files
fclose( stream );
fclose( outFile );
}

PROCESS LINE
void process_line (std::string code_line)
{
if program header process_header_line(code_line);
else
write the input line as it is to the output file
tokenSeq = 0;

while (tokenVal != "")
{
get_ith_token(code_line, tokenSeq);
get nextTokenVal

if (tokenVal == "")
tokens on the line are finished
else
{
if (tokenVal == "*E") Signal Natural program end;

if (is_characteristic(tokenVal))
call statement initialization routine
else
token is not characteristic
a regular statement is continued

end regular statement cont
end token is not characteristic
end tokens on the line are being processed
tokenSeq = tokenSeq + 1;
end of get next token
end of IF header ELSE program code line
end of process line function





void process_line (std::string code_line)
{


//20070316ARS check whether it is program header
if program header process_header_line(code_line);
//20070316ARS normal program line not header
else
{
//20070316ARS write the input line as it is to the output file
//20070318ARS Break this line into its tokens
tokenSeq = 0;

while (tokenVal != "")
{
tokenVal = objTOKEN.get_ith_token (code_line, tokenSeq);
get nextTokenVal

}

if (tokenVal == "")
tokens on the line are finished
else //tokens on the line are being processed........
{
if (tokenVal == "*E") EndOfInput=true;
//token is the first characteristic token
if (objTOKEN.is_characteristic(tokenVal))
{
if (tokenVal == "MOVE") beginMOVEstatement();

}
else //token is not characteristic
{
if instruction stack Head is NULL
not a regular statement
else
// a regular statement is continued
if MOVE
{
continueMOVEstatement(tokenVal);
tokenSeq = tokenSeq + 1;
continue;
}
… similar statements for other commands such as IF, SUBTRACT etc…
} //end regular statement cont
} // end token is not characteristic
} // end tokens on the line are being processed
tokenSeq = tokenSeq + 1;
} //end of get next token
} //end of IF header ELSE program code line
} //end of process line function

Thursday 7 June 2007

BASIC DEFINITIONS FOR A PARSER - PROGRAM CONVERTER

BASIC DEFINITIONS FOR A PARSER - PROGRAM CONVERTER
for NATURAL to JAVAJ2EE program conversion
by Ali Riza SARAL


Line is a string of characters that and with ‘\n’. A line may be comprised of program code and comments… NATURAL language requires that a comment line’s 7th char is ‘*’. So it begins with ‘*’ instead of a natural command or its parameter etc…

A statement is a group of tokens that comprise a characteristic token that specifies the Statement. Other tokens may be keywords that indicate the parameters that follow them. This is a multi-line statement.

A statement may continue for more than one line. Its tokens may be places on more than one line.
A keyword may not be divided on more than one line.

A parameter may be a variable name, a constant or a calculation formula. A calculation formula may be divided on more than one line.

A single line may include more than one statement. This is a multi-statement line.



GENERAL REQUIREMENTS FOR A PARSER - PROGRAM CONVERTER


GENERAL REQUIREMENTS FOR A PARSER - PROGRAM CONVERTER
for NATURAL to JAVAJ2EE program conversion
by Ali Riza SARAL

Input may be a single SYSTRANS file which includes all the NATURAL batch, online programs, maps and data structure definitions. In other cases input may be a single folder where the programs to be converted reside as members of that directory.

The conversion program is supposed to convert the input file(s) and produce output programs in JAVA (or in other language). The output programs may reside in an output folder as members of that directory.

All of the above requires directory and member handling both for reading and writing. This requires that you take the names of these directories etc. from a decent setup file. This means, there must be an XML file where the program accesses through functions that use XML utilities. The same XML file and utilities may be used to write the setup part of the program which may give the user to choose specific directories for input, etc. as he/she wishes.

A small warning: The conversion process may not be a straight forward single run process. There may not be a single monolithic conversion program either. For example, a special conversion program may be run to convert local variables, global variables, DDM data structures first… Only after that, the program conversion process may be used because tprogram conversion includes the conversion of the names of the variables in the programs. The new names of the variables will be taken from the tables generated by the data structure conversion processes.

Also, program conversion process may be comprised of more than one runs according to how you implement parsing and conversion.