test0::dActionEntry test0::FindAction (const int* const actionList, int count, dToken token) const { int i0 = 0; int i1 = count - 1; while ((i1 - i0) >= 4) { int i = (i1 + i0 + 1)>>1; dActionEntry action (actionList[i]); if (token <= dToken(action.m_token)) { i1 = i; } else { i0 = i; } } for (int i = i0; i <= i1; i ++) { dActionEntry action (actionList[i]); if (token == dToken(action.m_token)) { return action;; } } return dActionEntry(unsigned (-1)); }
const test6::dActionEntry* test6::GetNextAction (dList<dStackPair>& stack, dToken token, xxxx& scanner) const { static short actionsCount[] = { 2, 1, 1, 1, 1}; static short actionsStart[] = { 0, 2, 3, 4, 5}; static dActionEntry actionTable[] = { dActionEntry (254, 0, 1, 0, 0, 1), dActionEntry (256, 0, 1, 1, 0, 3), dActionEntry (256, 0, 1, 2, 0, 4), dActionEntry (254, 0, 2, 0, 0, 0), dActionEntry (256, 0, 0, 4, 0, 0), dActionEntry (254, 0, 1, 0, 3, 2)}; bool errorMode = false; const dStackPair& stackTop = stack.GetLast()->GetInfo(); int state = stackTop.m_state; int start = actionsStart[state]; int count = actionsCount[state]; const dActionEntry* const table = &actionTable[start]; const dActionEntry* action = FindAction (table, count, token); while (!action && (stack.GetCount() > 1)) { errorMode = true; // we found a syntax error in go into error recovering mode, and find the token mark by a ". error" rule stack.Remove (stack.GetLast()); const dStackPair& stackTop = stack.GetLast()->GetInfo(); int state = stackTop.m_state; int start = actionsStart[state]; int count = actionsCount[state]; const dActionEntry* const table = &actionTable[start]; action = FindAction (table, count, ERROR_TOKEN); if (action && !action->m_errorRule) { action = NULL; } } if (errorMode && action) { dStackPair& stackTop = stack.GetLast()->GetInfo(); stackTop.m_token = ERROR_TOKEN; int state = action->m_nextState; int start = actionsStart[state]; int count = actionsCount[state]; const dActionEntry* const table = &actionTable[start]; // find the next viable token to continues parsing while (!FindAction (table, count, token)) { token = dToken (scanner.NextToken()); if (token == -1) { // reached end of the file, can not recover from this error; return NULL; } } action = FindAction (table, count, token); dStackPair& entry = stack.Append()->GetInfo(); entry.m_state = state; entry.m_scannerLine = stackTop.m_scannerLine; entry.m_scannerIndex = stackTop.m_scannerIndex; entry.m_value = dUserVariable (ERROR_TOKEN, "error", entry.m_scannerLine, entry.m_scannerIndex); entry.m_token = token; } return action; }
bool test4::Parce(lextest1& scanner) { dList<dStackPair> stack; static short actionsCount[] = {2, 1, 2, 1, 1, 2, 2, 2, 1, 2}; static short actionsStart[] = {0, 2, 0, 3, 4, 5, 7, 0, 9, 5}; static dActionEntry actionTable[] = { dActionEntry (259, 0, 1, 0, 0), dActionEntry (256, 0, 2, 0, 0), dActionEntry (0, 1, 0, 1, 3), dActionEntry (0, 2, 0, 0, 0), dActionEntry (257, 0, 5, 0, 0), dActionEntry (259, 0, 6, 0, 0), dActionEntry (256, 0, 7, 0, 0), dActionEntry (0, 1, 0, 1, 3), dActionEntry (258, 1, 0, 1, 3), dActionEntry (257, 0, 9, 0, 0), }; static short gotoCount[] = {1, 0, 1, 0, 0, 1, 0, 1, 0, 1}; static short gotoStart[] = {0, 1, 1, 2, 2, 2, 3, 3, 4, 4}; static dGotoEntry gotoTable[] = { dGotoEntry (261, 3), dGotoEntry (261, 4), dGotoEntry (261, 4), dGotoEntry (261, 8), dGotoEntry (261, 8)}; const int lastToken = 261; stack.Append (); dToken token = dToken (scanner.NextToken()); for (;;) { const dStackPair& stackTop = stack.GetLast()->GetInfo(); int start = actionsStart[stackTop.m_state]; int count = actionsCount[stackTop.m_state]; const dActionEntry* const action (FindAction (&actionTable[start], count, token)); _ASSERTE (action); switch (action->m_stateType) { case dSHIFT: { dStackPair& entry = stack.Append()->GetInfo(); entry.m_token = dToken (action->m_token); entry.m_state = action->m_nextState; entry.m_value = dStackPair::dUserVariable (entry.m_token, scanner.GetTokenString()); token = dToken (scanner.NextToken()); if (token == -1) { token = dToken (0); } break; } case dREDUCE: { dStackPair parameter[MAX_USER_PARAM]; int reduceCount = action->m_ruleSymbols; _ASSERTE (reduceCount < sizeof (parameter) / sizeof (parameter[0])); for (int i = 0; i < reduceCount; i ++) { parameter[reduceCount - i - 1] = stack.GetLast()->GetInfo(); stack.Remove (stack.GetLast()); } const dStackPair& stackTop = stack.GetLast()->GetInfo(); int start = gotoStart[stackTop.m_state]; int count = gotoCount[stackTop.m_state]; const dGotoEntry* const gotoEntry = FindGoto (&gotoTable[start], count, dToken (action->m_nextState + lastToken)); dStackPair& entry = stack.Append()->GetInfo(); entry.m_state = gotoEntry->m_nextState; entry.m_token = dToken (gotoEntry->m_token); switch (action->m_ruleIndex) { //do user semantic Actions case 3:// rule stmt : a {} break; default:; } break; } case dACCEPT: // 2 = accept { // program parced successfully, exit with successful code return true; } default: { _ASSERTE (0); // syntact error parciing program //if (!ErrorHandler ("error")) { //} break; } } } return false; }