Thursday 28 June 2007

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);
}