00001 #include "xrb_arithmeticparser.h"
00002
00003 #include <cstdio>
00004 #include <iomanip>
00005 #include <iostream>
00006
00007 #define DEBUG_SPEW_1(x) if (m_debug_spew_level >= 1) std::cerr << x
00008 #define DEBUG_SPEW_2(x) if (m_debug_spew_level >= 2) std::cerr << x
00009
00010
00011 #line 38 "xrb_arithmeticparser.trison"
00012
00013 #include "xrb_arithmeticscanner.hpp"
00014
00015 #include "xrb_math.hpp"
00016
00017 namespace Xrb
00018 {
00019
00020 #line 21 "xrb_arithmeticparser.cpp"
00021
00022 ArithmeticParser::ArithmeticParser ()
00023
00024 {
00025
00026 #line 47 "xrb_arithmeticparser.trison"
00027
00028 m_scanner = NULL;
00029
00030 #line 31 "xrb_arithmeticparser.cpp"
00031 m_debug_spew_level = 0;
00032 DEBUG_SPEW_2("### number of state transitions = " << ms_state_transition_count << std::endl);
00033 m_reduction_token = 0.0f;
00034 }
00035
00036 ArithmeticParser::~ArithmeticParser ()
00037 {
00038
00039 #line 51 "xrb_arithmeticparser.trison"
00040
00041 ASSERT1(m_scanner == NULL);
00042
00043 #line 44 "xrb_arithmeticparser.cpp"
00044 }
00045
00046 void ArithmeticParser::CheckStateConsistency ()
00047 {
00048 unsigned int counter = 1;
00049 for (unsigned int i = 0; i < ms_state_count; ++i)
00050 {
00051 if (ms_state[i].m_lookahead_transition_offset > 0)
00052 {
00053 assert(counter == ms_state[i].m_lookahead_transition_offset);
00054 assert(ms_state[i].m_lookahead_transition_count > 0);
00055 }
00056 else
00057 assert(ms_state[i].m_lookahead_transition_count == 0);
00058
00059 counter += ms_state[i].m_lookahead_transition_count;
00060
00061 if (ms_state[i].m_default_action_offset > 0)
00062 ++counter;
00063
00064 if (ms_state[i].m_non_terminal_transition_offset > 0)
00065 {
00066 assert(counter == ms_state[i].m_non_terminal_transition_offset);
00067 assert(ms_state[i].m_non_terminal_transition_offset > 0);
00068 }
00069 else
00070 assert(ms_state[i].m_non_terminal_transition_offset == 0);
00071
00072 counter += ms_state[i].m_non_terminal_transition_count;
00073 }
00074 assert(counter == ms_state_transition_count);
00075 }
00076
00077 ArithmeticParser::ParserReturnCode ArithmeticParser::Parse ()
00078 {
00079
00080
00081 ParserReturnCode return_code = PrivateParse();
00082
00083
00084
00085 return return_code;
00086 }
00087
00088 bool ArithmeticParser::DoesStateAcceptErrorToken (ArithmeticParser::StateNumber state_number) const
00089 {
00090 assert(state_number < ms_state_count);
00091 State const &state = ms_state[state_number];
00092
00093 for (unsigned int transition = state.m_lookahead_transition_offset,
00094 transition_end = state.m_lookahead_transition_offset +
00095 state.m_lookahead_transition_count;
00096 transition < transition_end;
00097 ++transition)
00098 {
00099 if (ms_state_transition[transition].m_token_type == Token::ERROR_)
00100 return true;
00101 }
00102
00103 return false;
00104 }
00105
00106 ArithmeticParser::ParserReturnCode ArithmeticParser::PrivateParse ()
00107 {
00108 m_state_stack.clear();
00109 m_token_stack.clear();
00110
00111 m_lookahead_token_type = Token::INVALID_;
00112 m_lookahead_token = 0.0f;
00113 m_is_new_lookahead_token_required = true;
00114
00115 m_saved_lookahead_token_type = Token::INVALID_;
00116 m_get_new_lookahead_token_type_from_saved = false;
00117 m_previous_transition_accepted_error_token = false;
00118
00119 m_is_returning_with_non_terminal = false;
00120 m_returning_with_this_non_terminal = Token::INVALID_;
00121
00122
00123 PushState(0);
00124
00125 while (true)
00126 {
00127 StateNumber current_state_number = m_state_stack.back();
00128 assert(current_state_number < ms_state_count);
00129 State const ¤t_state = ms_state[current_state_number];
00130
00131 unsigned int state_transition_number;
00132 unsigned int state_transition_count;
00133 unsigned int default_action_state_transition_number;
00134 Token::Type state_transition_token_type = Token::INVALID_;
00135
00136
00137
00138 if (m_is_returning_with_non_terminal)
00139 {
00140 m_is_returning_with_non_terminal = false;
00141 state_transition_number = current_state.m_non_terminal_transition_offset;
00142 state_transition_count = current_state.m_non_terminal_transition_count;
00143 default_action_state_transition_number = 0;
00144 state_transition_token_type = m_returning_with_this_non_terminal;
00145 }
00146
00147 else
00148 {
00149 state_transition_number = current_state.m_lookahead_transition_offset;
00150 state_transition_count = current_state.m_lookahead_transition_count;
00151 default_action_state_transition_number = current_state.m_default_action_offset;
00152
00153
00154 if (state_transition_count != 0)
00155 {
00156 state_transition_token_type = LookaheadTokenType();
00157 DEBUG_SPEW_1("*** lookahead token type: " << state_transition_token_type << std::endl);
00158 }
00159 }
00160
00161 unsigned int i;
00162 for (i = 0;
00163 i < state_transition_count;
00164 ++i, ++state_transition_number)
00165 {
00166 StateTransition const &state_transition =
00167 ms_state_transition[state_transition_number];
00168
00169 if (state_transition.m_token_type == state_transition_token_type)
00170 {
00171 if (state_transition.m_token_type == Token::ERROR_)
00172 m_previous_transition_accepted_error_token = true;
00173 else
00174 m_previous_transition_accepted_error_token = false;
00175
00176 PrintStateTransition(state_transition_number);
00177 if (ProcessAction(state_transition.m_action) == ARC_ACCEPT_AND_RETURN)
00178 return PRC_SUCCESS;
00179 else
00180 break;
00181 }
00182 }
00183
00184
00185 if (i == state_transition_count)
00186 {
00187
00188 if (default_action_state_transition_number != 0)
00189 {
00190 PrintStateTransition(default_action_state_transition_number);
00191 Action const &default_action =
00192 ms_state_transition[default_action_state_transition_number].m_action;
00193 if (ProcessAction(default_action) == ARC_ACCEPT_AND_RETURN)
00194 return PRC_SUCCESS;
00195 }
00196
00197 else
00198 {
00199 assert(!m_is_new_lookahead_token_required);
00200
00201 DEBUG_SPEW_1("!!! error recovery: begin" << std::endl);
00202
00203
00204
00205 if (DoesStateAcceptErrorToken(current_state_number))
00206 {
00207
00208
00209
00210 if (m_previous_transition_accepted_error_token)
00211 {
00212 ThrowAwayToken(m_lookahead_token);
00213 m_is_new_lookahead_token_required = true;
00214 }
00215
00216
00217
00218 else
00219 {
00220 m_saved_lookahead_token_type = m_lookahead_token_type;
00221 m_get_new_lookahead_token_type_from_saved = true;
00222 m_lookahead_token_type = Token::ERROR_;
00223 }
00224 }
00225
00226 else
00227 {
00228
00229 m_saved_lookahead_token_type = m_lookahead_token_type;
00230 m_get_new_lookahead_token_type_from_saved = true;
00231 m_lookahead_token_type = Token::ERROR_;
00232
00233
00234
00235 assert(m_state_stack.size() > 0);
00236 do
00237 {
00238 DEBUG_SPEW_1("!!! error recovery: popping state " << current_state_number << std::endl);
00239 m_state_stack.pop_back();
00240
00241 if (m_state_stack.size() == 0)
00242 {
00243 ThrowAwayTokenStack();
00244 DEBUG_SPEW_1("!!! error recovery: unhandled error -- quitting" << std::endl);
00245 return PRC_UNHANDLED_PARSE_ERROR;
00246 }
00247
00248 assert(m_token_stack.size() > 0);
00249 ThrowAwayToken(m_token_stack.back());
00250 m_token_stack.pop_back();
00251 current_state_number = m_state_stack.back();
00252 }
00253 while (!DoesStateAcceptErrorToken(current_state_number));
00254 }
00255
00256 DEBUG_SPEW_1("!!! error recovery: found state which accepts %error token" << std::endl);
00257 PrintStateStack();
00258 }
00259 }
00260 }
00261
00262
00263 return PRC_UNHANDLED_PARSE_ERROR;
00264 }
00265
00266 ArithmeticParser::ActionReturnCode ArithmeticParser::ProcessAction (ArithmeticParser::Action const &action)
00267 {
00268 if (action.m_transition_action == TA_SHIFT_AND_PUSH_STATE)
00269 {
00270 ShiftLookaheadToken();
00271 PushState(action.m_data);
00272 }
00273 else if (action.m_transition_action == TA_PUSH_STATE)
00274 {
00275 PushState(action.m_data);
00276 }
00277 else if (action.m_transition_action == TA_REDUCE_USING_RULE)
00278 {
00279 unsigned int reduction_rule_number = action.m_data;
00280 assert(reduction_rule_number < ms_reduction_rule_count);
00281 ReductionRule const &reduction_rule = ms_reduction_rule[reduction_rule_number];
00282 ReduceUsingRule(reduction_rule, false);
00283 }
00284 else if (action.m_transition_action == TA_REDUCE_AND_ACCEPT_USING_RULE)
00285 {
00286 unsigned int reduction_rule_number = action.m_data;
00287 assert(reduction_rule_number < ms_reduction_rule_count);
00288 ReductionRule const &reduction_rule = ms_reduction_rule[reduction_rule_number];
00289 ReduceUsingRule(reduction_rule, true);
00290 DEBUG_SPEW_1("*** accept" << std::endl);
00291
00292 return ARC_ACCEPT_AND_RETURN;
00293 }
00294 else if (action.m_transition_action == TA_THROW_AWAY_LOOKAHEAD_TOKEN)
00295 {
00296 assert(!m_is_new_lookahead_token_required);
00297 ThrowAwayToken(m_lookahead_token);
00298 m_is_new_lookahead_token_required = true;
00299 }
00300
00301 return ARC_CONTINUE_PARSING;
00302 }
00303
00304 void ArithmeticParser::ShiftLookaheadToken ()
00305 {
00306 assert(m_lookahead_token_type != Token::DEFAULT_);
00307 assert(m_lookahead_token_type != Token::INVALID_);
00308 DEBUG_SPEW_1("*** shifting lookahead token -- type " << m_lookahead_token_type << std::endl);
00309 m_token_stack.push_back(m_lookahead_token);
00310 m_is_new_lookahead_token_required = true;
00311 }
00312
00313 void ArithmeticParser::PushState (StateNumber const state_number)
00314 {
00315 assert(state_number < ms_state_count);
00316
00317 DEBUG_SPEW_1("*** going to state " << state_number << std::endl);
00318 m_state_stack.push_back(state_number);
00319 PrintStateStack();
00320 }
00321
00322 void ArithmeticParser::ReduceUsingRule (ReductionRule const &reduction_rule, bool and_accept)
00323 {
00324 if (and_accept)
00325 {
00326 assert(reduction_rule.m_number_of_tokens_to_reduce_by == m_state_stack.size() - 1);
00327 assert(reduction_rule.m_number_of_tokens_to_reduce_by == m_token_stack.size());
00328 }
00329 else
00330 {
00331 assert(reduction_rule.m_number_of_tokens_to_reduce_by < m_state_stack.size());
00332 assert(reduction_rule.m_number_of_tokens_to_reduce_by <= m_token_stack.size());
00333 }
00334
00335 DEBUG_SPEW_1("*** reducing: " << reduction_rule.m_description << std::endl);
00336
00337 m_is_returning_with_non_terminal = true;
00338 m_returning_with_this_non_terminal = reduction_rule.m_non_terminal_to_reduce_to;
00339 m_reduction_rule_token_count = reduction_rule.m_number_of_tokens_to_reduce_by;
00340
00341
00342 if (reduction_rule.m_handler != NULL)
00343 m_reduction_token = (this->*(reduction_rule.m_handler))();
00344
00345 PopStates(reduction_rule.m_number_of_tokens_to_reduce_by, false);
00346
00347
00348 if (!and_accept)
00349 {
00350
00351 m_token_stack.push_back(m_reduction_token);
00352 PrintStateStack();
00353 }
00354 }
00355
00356 void ArithmeticParser::PopStates (unsigned int number_of_states_to_pop, bool print_state_stack)
00357 {
00358 assert(number_of_states_to_pop < m_state_stack.size());
00359 assert(number_of_states_to_pop <= m_token_stack.size());
00360
00361 while (number_of_states_to_pop-- > 0)
00362 {
00363 m_state_stack.pop_back();
00364 m_token_stack.pop_back();
00365 }
00366
00367 if (print_state_stack)
00368 PrintStateStack();
00369 }
00370
00371 void ArithmeticParser::PrintStateStack () const
00372 {
00373 DEBUG_SPEW_2("*** state stack: ");
00374 for (StateStack::const_iterator it = m_state_stack.begin(),
00375 it_end = m_state_stack.end();
00376 it != it_end;
00377 ++it)
00378 {
00379 DEBUG_SPEW_2(*it << " ");
00380 }
00381 DEBUG_SPEW_2(std::endl);
00382 }
00383
00384 void ArithmeticParser::PrintStateTransition (unsigned int const state_transition_number) const
00385 {
00386 assert(state_transition_number < ms_state_transition_count);
00387 DEBUG_SPEW_2("&&& exercising state transition " << std::setw(4) << std::right << state_transition_number << std::endl);
00388 }
00389
00390 void ArithmeticParser::ScanANewLookaheadToken ()
00391 {
00392 assert(!m_is_new_lookahead_token_required);
00393 m_lookahead_token = 0.0f;
00394 m_lookahead_token_type = Scan();
00395 DEBUG_SPEW_1("*** scanned a new lookahead token -- type " << m_lookahead_token_type << std::endl);
00396 }
00397
00398 void ArithmeticParser::ThrowAwayToken (Float token)
00399 {
00400
00401 }
00402
00403 void ArithmeticParser::ThrowAwayTokenStack ()
00404 {
00405 while (!m_token_stack.empty())
00406 {
00407 ThrowAwayToken(m_token_stack.back());
00408 m_token_stack.pop_back();
00409 }
00410 }
00411
00412 std::ostream &operator << (std::ostream &stream, ArithmeticParser::Token::Type token_type)
00413 {
00414 static std::string const s_token_type_string[] =
00415 {
00416 "BAD_TOKEN",
00417 "NUMERIC",
00418 "END_",
00419
00420 "exp",
00421 "START_",
00422
00423 "%error",
00424 "DEFAULT_",
00425 "INVALID_"
00426 };
00427 static unsigned int const s_token_type_string_count =
00428 sizeof(s_token_type_string) /
00429 sizeof(std::string);
00430
00431 unsigned token_type_value = static_cast<unsigned int>(token_type);
00432 if (token_type_value < 0x20)
00433 stream << token_type_value;
00434 else if (token_type_value < 0x7F)
00435 stream << "'" << static_cast<char>(token_type) << "'";
00436 else if (token_type_value < 0x100)
00437 stream << token_type_value;
00438 else if (token_type_value < 0x100 + s_token_type_string_count)
00439 stream << s_token_type_string[token_type_value - 0x100];
00440 else
00441 stream << token_type_value;
00442
00443 return stream;
00444 }
00445
00446
00447
00448
00449
00450
00451 Float ArithmeticParser::ReductionRuleHandler0000 ()
00452 {
00453 assert(static_cast<unsigned int>(0) < m_reduction_rule_token_count);
00454 return m_token_stack[m_token_stack.size() - m_reduction_rule_token_count];
00455
00456 return 0.0f;
00457 }
00458
00459
00460 Float ArithmeticParser::ReductionRuleHandler0001 ()
00461 {
00462 assert(static_cast<unsigned int>(0) < m_reduction_rule_token_count);
00463 Float left = static_cast< Float >(m_token_stack[m_token_stack.size() - m_reduction_rule_token_count + 0]);
00464 assert(static_cast<unsigned int>(2) < m_reduction_rule_token_count);
00465 Float right = static_cast< Float >(m_token_stack[m_token_stack.size() - m_reduction_rule_token_count + 2]);
00466
00467 #line 99 "xrb_arithmeticparser.trison"
00468 return left + right;
00469 #line 470 "xrb_arithmeticparser.cpp"
00470 return 0.0f;
00471 }
00472
00473
00474 Float ArithmeticParser::ReductionRuleHandler0002 ()
00475 {
00476 assert(static_cast<unsigned int>(0) < m_reduction_rule_token_count);
00477 Float left = static_cast< Float >(m_token_stack[m_token_stack.size() - m_reduction_rule_token_count + 0]);
00478 assert(static_cast<unsigned int>(2) < m_reduction_rule_token_count);
00479 Float right = static_cast< Float >(m_token_stack[m_token_stack.size() - m_reduction_rule_token_count + 2]);
00480
00481 #line 101 "xrb_arithmeticparser.trison"
00482 return left - right;
00483 #line 484 "xrb_arithmeticparser.cpp"
00484 return 0.0f;
00485 }
00486
00487
00488 Float ArithmeticParser::ReductionRuleHandler0003 ()
00489 {
00490 assert(static_cast<unsigned int>(0) < m_reduction_rule_token_count);
00491 Float left = static_cast< Float >(m_token_stack[m_token_stack.size() - m_reduction_rule_token_count + 0]);
00492 assert(static_cast<unsigned int>(2) < m_reduction_rule_token_count);
00493 Float right = static_cast< Float >(m_token_stack[m_token_stack.size() - m_reduction_rule_token_count + 2]);
00494
00495 #line 103 "xrb_arithmeticparser.trison"
00496 return left * right;
00497 #line 498 "xrb_arithmeticparser.cpp"
00498 return 0.0f;
00499 }
00500
00501
00502 Float ArithmeticParser::ReductionRuleHandler0004 ()
00503 {
00504 assert(static_cast<unsigned int>(0) < m_reduction_rule_token_count);
00505 Float left = static_cast< Float >(m_token_stack[m_token_stack.size() - m_reduction_rule_token_count + 0]);
00506 assert(static_cast<unsigned int>(2) < m_reduction_rule_token_count);
00507 Float right = static_cast< Float >(m_token_stack[m_token_stack.size() - m_reduction_rule_token_count + 2]);
00508
00509 #line 105 "xrb_arithmeticparser.trison"
00510 return (right == 0.0f) ? Math::Nan() : left / right;
00511 #line 512 "xrb_arithmeticparser.cpp"
00512 return 0.0f;
00513 }
00514
00515
00516 Float ArithmeticParser::ReductionRuleHandler0005 ()
00517 {
00518 assert(static_cast<unsigned int>(1) < m_reduction_rule_token_count);
00519 Float exp = static_cast< Float >(m_token_stack[m_token_stack.size() - m_reduction_rule_token_count + 1]);
00520
00521 #line 107 "xrb_arithmeticparser.trison"
00522 return exp;
00523 #line 524 "xrb_arithmeticparser.cpp"
00524 return 0.0f;
00525 }
00526
00527
00528 Float ArithmeticParser::ReductionRuleHandler0006 ()
00529 {
00530 assert(static_cast<unsigned int>(1) < m_reduction_rule_token_count);
00531 Float exp = static_cast< Float >(m_token_stack[m_token_stack.size() - m_reduction_rule_token_count + 1]);
00532
00533 #line 109 "xrb_arithmeticparser.trison"
00534 return -exp;
00535 #line 536 "xrb_arithmeticparser.cpp"
00536 return 0.0f;
00537 }
00538
00539
00540 Float ArithmeticParser::ReductionRuleHandler0007 ()
00541 {
00542 assert(static_cast<unsigned int>(0) < m_reduction_rule_token_count);
00543 Float left = static_cast< Float >(m_token_stack[m_token_stack.size() - m_reduction_rule_token_count + 0]);
00544 assert(static_cast<unsigned int>(2) < m_reduction_rule_token_count);
00545 Float right = static_cast< Float >(m_token_stack[m_token_stack.size() - m_reduction_rule_token_count + 2]);
00546
00547 #line 111 "xrb_arithmeticparser.trison"
00548 return Math::Pow(left, right);
00549 #line 550 "xrb_arithmeticparser.cpp"
00550 return 0.0f;
00551 }
00552
00553
00554 Float ArithmeticParser::ReductionRuleHandler0008 ()
00555 {
00556 assert(static_cast<unsigned int>(1) < m_reduction_rule_token_count);
00557 Float exp = static_cast< Float >(m_token_stack[m_token_stack.size() - m_reduction_rule_token_count + 1]);
00558
00559 #line 113 "xrb_arithmeticparser.trison"
00560 return exp;
00561 #line 562 "xrb_arithmeticparser.cpp"
00562 return 0.0f;
00563 }
00564
00565
00566 Float ArithmeticParser::ReductionRuleHandler0009 ()
00567 {
00568 assert(static_cast<unsigned int>(0) < m_reduction_rule_token_count);
00569 Float numeric = static_cast< Float >(m_token_stack[m_token_stack.size() - m_reduction_rule_token_count + 0]);
00570
00571 #line 115 "xrb_arithmeticparser.trison"
00572 return numeric;
00573 #line 574 "xrb_arithmeticparser.cpp"
00574 return 0.0f;
00575 }
00576
00577
00578
00579
00580
00581
00582
00583 ArithmeticParser::ReductionRule const ArithmeticParser::ms_reduction_rule[] =
00584 {
00585 { Token::START_, 2, &ArithmeticParser::ReductionRuleHandler0000, "rule 0: %start <- exp END_ "},
00586 { Token::exp__, 3, &ArithmeticParser::ReductionRuleHandler0001, "rule 1: exp <- exp '+' exp %left %prec ADDITION"},
00587 { Token::exp__, 3, &ArithmeticParser::ReductionRuleHandler0002, "rule 2: exp <- exp '-' exp %left %prec ADDITION"},
00588 { Token::exp__, 3, &ArithmeticParser::ReductionRuleHandler0003, "rule 3: exp <- exp '*' exp %left %prec MULTIPLICATION"},
00589 { Token::exp__, 3, &ArithmeticParser::ReductionRuleHandler0004, "rule 4: exp <- exp '/' exp %left %prec MULTIPLICATION"},
00590 { Token::exp__, 2, &ArithmeticParser::ReductionRuleHandler0005, "rule 5: exp <- '+' exp %prec UNARY"},
00591 { Token::exp__, 2, &ArithmeticParser::ReductionRuleHandler0006, "rule 6: exp <- '-' exp %prec UNARY"},
00592 { Token::exp__, 3, &ArithmeticParser::ReductionRuleHandler0007, "rule 7: exp <- exp '^' exp %left %prec EXPONENTIATION"},
00593 { Token::exp__, 3, &ArithmeticParser::ReductionRuleHandler0008, "rule 8: exp <- '(' exp ')' "},
00594 { Token::exp__, 1, &ArithmeticParser::ReductionRuleHandler0009, "rule 9: exp <- NUMERIC "},
00595
00596
00597 { Token::ERROR_, 1, NULL, "* -> error"}
00598 };
00599
00600 unsigned int const ArithmeticParser::ms_reduction_rule_count =
00601 sizeof(ArithmeticParser::ms_reduction_rule) /
00602 sizeof(ArithmeticParser::ReductionRule);
00603
00604
00605
00606
00607
00608 ArithmeticParser::State const ArithmeticParser::ms_state[] =
00609 {
00610 { 1, 4, 0, 5, 1},
00611 { 0, 0, 6, 0, 0},
00612 { 7, 4, 0, 11, 1},
00613 { 12, 4, 0, 16, 1},
00614 { 17, 4, 0, 21, 1},
00615 { 22, 6, 0, 0, 0},
00616 { 28, 1, 29, 0, 0},
00617 { 30, 1, 31, 0, 0},
00618 { 32, 6, 0, 0, 0},
00619 { 0, 0, 38, 0, 0},
00620 { 39, 4, 0, 43, 1},
00621 { 44, 4, 0, 48, 1},
00622 { 49, 4, 0, 53, 1},
00623 { 54, 4, 0, 58, 1},
00624 { 59, 4, 0, 63, 1},
00625 { 0, 0, 64, 0, 0},
00626 { 65, 3, 68, 0, 0},
00627 { 69, 3, 72, 0, 0},
00628 { 73, 1, 74, 0, 0},
00629 { 75, 1, 76, 0, 0},
00630 { 0, 0, 77, 0, 0}
00631
00632 };
00633
00634 unsigned int const ArithmeticParser::ms_state_count =
00635 sizeof(ArithmeticParser::ms_state) /
00636 sizeof(ArithmeticParser::State);
00637
00638
00639
00640
00641
00642 ArithmeticParser::StateTransition const ArithmeticParser::ms_state_transition[] =
00643 {
00644
00645 { Token::INVALID_, { TA_COUNT, 0}},
00646
00647
00648
00649
00650
00651 { Token::NUMERIC, { TA_SHIFT_AND_PUSH_STATE, 1}},
00652 { static_cast<Token::Type>('+'), { TA_SHIFT_AND_PUSH_STATE, 2}},
00653 { static_cast<Token::Type>('-'), { TA_SHIFT_AND_PUSH_STATE, 3}},
00654 { static_cast<Token::Type>('('), { TA_SHIFT_AND_PUSH_STATE, 4}},
00655
00656 { Token::exp__, { TA_PUSH_STATE, 5}},
00657
00658
00659
00660
00661
00662 { Token::DEFAULT_, { TA_REDUCE_USING_RULE, 9}},
00663
00664
00665
00666
00667
00668 { Token::NUMERIC, { TA_SHIFT_AND_PUSH_STATE, 1}},
00669 { static_cast<Token::Type>('+'), { TA_SHIFT_AND_PUSH_STATE, 2}},
00670 { static_cast<Token::Type>('-'), { TA_SHIFT_AND_PUSH_STATE, 3}},
00671 { static_cast<Token::Type>('('), { TA_SHIFT_AND_PUSH_STATE, 4}},
00672
00673 { Token::exp__, { TA_PUSH_STATE, 6}},
00674
00675
00676
00677
00678
00679 { Token::NUMERIC, { TA_SHIFT_AND_PUSH_STATE, 1}},
00680 { static_cast<Token::Type>('+'), { TA_SHIFT_AND_PUSH_STATE, 2}},
00681 { static_cast<Token::Type>('-'), { TA_SHIFT_AND_PUSH_STATE, 3}},
00682 { static_cast<Token::Type>('('), { TA_SHIFT_AND_PUSH_STATE, 4}},
00683
00684 { Token::exp__, { TA_PUSH_STATE, 7}},
00685
00686
00687
00688
00689
00690 { Token::NUMERIC, { TA_SHIFT_AND_PUSH_STATE, 1}},
00691 { static_cast<Token::Type>('+'), { TA_SHIFT_AND_PUSH_STATE, 2}},
00692 { static_cast<Token::Type>('-'), { TA_SHIFT_AND_PUSH_STATE, 3}},
00693 { static_cast<Token::Type>('('), { TA_SHIFT_AND_PUSH_STATE, 4}},
00694
00695 { Token::exp__, { TA_PUSH_STATE, 8}},
00696
00697
00698
00699
00700
00701 { Token::END_, { TA_SHIFT_AND_PUSH_STATE, 9}},
00702 { static_cast<Token::Type>('+'), { TA_SHIFT_AND_PUSH_STATE, 10}},
00703 { static_cast<Token::Type>('-'), { TA_SHIFT_AND_PUSH_STATE, 11}},
00704 { static_cast<Token::Type>('*'), { TA_SHIFT_AND_PUSH_STATE, 12}},
00705 { static_cast<Token::Type>('/'), { TA_SHIFT_AND_PUSH_STATE, 13}},
00706 { static_cast<Token::Type>('^'), { TA_SHIFT_AND_PUSH_STATE, 14}},
00707
00708
00709
00710
00711
00712 { static_cast<Token::Type>('^'), { TA_SHIFT_AND_PUSH_STATE, 14}},
00713
00714 { Token::DEFAULT_, { TA_REDUCE_USING_RULE, 5}},
00715
00716
00717
00718
00719
00720 { static_cast<Token::Type>('^'), { TA_SHIFT_AND_PUSH_STATE, 14}},
00721
00722 { Token::DEFAULT_, { TA_REDUCE_USING_RULE, 6}},
00723
00724
00725
00726
00727
00728 { static_cast<Token::Type>('+'), { TA_SHIFT_AND_PUSH_STATE, 10}},
00729 { static_cast<Token::Type>('-'), { TA_SHIFT_AND_PUSH_STATE, 11}},
00730 { static_cast<Token::Type>('*'), { TA_SHIFT_AND_PUSH_STATE, 12}},
00731 { static_cast<Token::Type>('/'), { TA_SHIFT_AND_PUSH_STATE, 13}},
00732 { static_cast<Token::Type>('^'), { TA_SHIFT_AND_PUSH_STATE, 14}},
00733 { static_cast<Token::Type>(')'), { TA_SHIFT_AND_PUSH_STATE, 15}},
00734
00735
00736
00737
00738
00739 { Token::DEFAULT_, {TA_REDUCE_AND_ACCEPT_USING_RULE, 0}},
00740
00741
00742
00743
00744
00745 { Token::NUMERIC, { TA_SHIFT_AND_PUSH_STATE, 1}},
00746 { static_cast<Token::Type>('+'), { TA_SHIFT_AND_PUSH_STATE, 2}},
00747 { static_cast<Token::Type>('-'), { TA_SHIFT_AND_PUSH_STATE, 3}},
00748 { static_cast<Token::Type>('('), { TA_SHIFT_AND_PUSH_STATE, 4}},
00749
00750 { Token::exp__, { TA_PUSH_STATE, 16}},
00751
00752
00753
00754
00755
00756 { Token::NUMERIC, { TA_SHIFT_AND_PUSH_STATE, 1}},
00757 { static_cast<Token::Type>('+'), { TA_SHIFT_AND_PUSH_STATE, 2}},
00758 { static_cast<Token::Type>('-'), { TA_SHIFT_AND_PUSH_STATE, 3}},
00759 { static_cast<Token::Type>('('), { TA_SHIFT_AND_PUSH_STATE, 4}},
00760
00761 { Token::exp__, { TA_PUSH_STATE, 17}},
00762
00763
00764
00765
00766
00767 { Token::NUMERIC, { TA_SHIFT_AND_PUSH_STATE, 1}},
00768 { static_cast<Token::Type>('+'), { TA_SHIFT_AND_PUSH_STATE, 2}},
00769 { static_cast<Token::Type>('-'), { TA_SHIFT_AND_PUSH_STATE, 3}},
00770 { static_cast<Token::Type>('('), { TA_SHIFT_AND_PUSH_STATE, 4}},
00771
00772 { Token::exp__, { TA_PUSH_STATE, 18}},
00773
00774
00775
00776
00777
00778 { Token::NUMERIC, { TA_SHIFT_AND_PUSH_STATE, 1}},
00779 { static_cast<Token::Type>('+'), { TA_SHIFT_AND_PUSH_STATE, 2}},
00780 { static_cast<Token::Type>('-'), { TA_SHIFT_AND_PUSH_STATE, 3}},
00781 { static_cast<Token::Type>('('), { TA_SHIFT_AND_PUSH_STATE, 4}},
00782
00783 { Token::exp__, { TA_PUSH_STATE, 19}},
00784
00785
00786
00787
00788
00789 { Token::NUMERIC, { TA_SHIFT_AND_PUSH_STATE, 1}},
00790 { static_cast<Token::Type>('+'), { TA_SHIFT_AND_PUSH_STATE, 2}},
00791 { static_cast<Token::Type>('-'), { TA_SHIFT_AND_PUSH_STATE, 3}},
00792 { static_cast<Token::Type>('('), { TA_SHIFT_AND_PUSH_STATE, 4}},
00793
00794 { Token::exp__, { TA_PUSH_STATE, 20}},
00795
00796
00797
00798
00799
00800 { Token::DEFAULT_, { TA_REDUCE_USING_RULE, 8}},
00801
00802
00803
00804
00805
00806 { static_cast<Token::Type>('*'), { TA_SHIFT_AND_PUSH_STATE, 12}},
00807 { static_cast<Token::Type>('/'), { TA_SHIFT_AND_PUSH_STATE, 13}},
00808 { static_cast<Token::Type>('^'), { TA_SHIFT_AND_PUSH_STATE, 14}},
00809
00810 { Token::DEFAULT_, { TA_REDUCE_USING_RULE, 1}},
00811
00812
00813
00814
00815
00816 { static_cast<Token::Type>('*'), { TA_SHIFT_AND_PUSH_STATE, 12}},
00817 { static_cast<Token::Type>('/'), { TA_SHIFT_AND_PUSH_STATE, 13}},
00818 { static_cast<Token::Type>('^'), { TA_SHIFT_AND_PUSH_STATE, 14}},
00819
00820 { Token::DEFAULT_, { TA_REDUCE_USING_RULE, 2}},
00821
00822
00823
00824
00825
00826 { static_cast<Token::Type>('^'), { TA_SHIFT_AND_PUSH_STATE, 14}},
00827
00828 { Token::DEFAULT_, { TA_REDUCE_USING_RULE, 3}},
00829
00830
00831
00832
00833
00834 { static_cast<Token::Type>('^'), { TA_SHIFT_AND_PUSH_STATE, 14}},
00835
00836 { Token::DEFAULT_, { TA_REDUCE_USING_RULE, 4}},
00837
00838
00839
00840
00841
00842 { Token::DEFAULT_, { TA_REDUCE_USING_RULE, 7}}
00843
00844 };
00845
00846 unsigned int const ArithmeticParser::ms_state_transition_count =
00847 sizeof(ArithmeticParser::ms_state_transition) /
00848 sizeof(ArithmeticParser::StateTransition);
00849
00850
00851 #line 55 "xrb_arithmeticparser.trison"
00852
00853 Float ArithmeticParser::Parse (std::string const &input_string)
00854 {
00855 ASSERT1(m_scanner == NULL);
00856
00857 Float retval;
00858
00859 m_scanner = new ArithmeticScanner(input_string);
00860 if (Parse() == PRC_SUCCESS)
00861 retval = GetAcceptedToken();
00862 else
00863 retval = Math::Nan();
00864 DeleteAndNullify(m_scanner);
00865
00866 return retval;
00867 }
00868
00869 ArithmeticParser::Token::Type ArithmeticParser::Scan ()
00870 {
00871 ASSERT1(m_scanner != NULL);
00872 return m_scanner->Scan(&m_lookahead_token);
00873 }
00874
00875 }
00876
00877 #line 878 "xrb_arithmeticparser.cpp"
00878