55 lines
1.5 KiB
C++
55 lines
1.5 KiB
C++
#include "{{name}}.h"
|
|
|
|
#define TABLE {{name}}___Table___{{name}}
|
|
#define REDUCE_COUNT {{name}}___Num_Reduces___{{name}}
|
|
|
|
const std::uint64_t TABLE[{{num_states}}][{{num_symbols}}] = {
|
|
{{#states}}
|
|
{ {{#actions}}({{action}} | ({{data}}) << 2),{{/actions}}
|
|
{{#gotos}}(({{.}}) << 2),{{/gotos}} },
|
|
{{/states}}
|
|
};
|
|
|
|
const unsigned char REDUCE_COUNT[{{num_rules}}] = { {{#rules}}{{rhs_length}},{{/rules}} };
|
|
|
|
|
|
bool {{name}}<bool>::parse() {
|
|
std::stack<std::uint64_t> stateStack;
|
|
using Sym = {{name}}_Symbol;
|
|
|
|
stateStack.push(0);
|
|
Sym tok = lex();
|
|
|
|
while (true) {
|
|
std::uint64_t act = TABLE[stateStack.top()][static_cast<std::uint64_t>(tok)];
|
|
|
|
switch (act & 0x3) {
|
|
case ERROR:
|
|
return false;
|
|
case SHIFT:
|
|
stateStack.push(act >> 2);
|
|
tok = lex();
|
|
break;
|
|
case REDUCE:
|
|
{
|
|
std::uint64_t tmp = act >> 2;
|
|
Sym symbol = static_cast<Sym>(tmp >> 31);
|
|
std::uint32_t rule = tmp & ((1ull << 31) - 1);
|
|
|
|
for (unsigned char i = 0; i < REDUCE_COUNT[rule]; i++) {
|
|
stateStack.pop();
|
|
}
|
|
|
|
stateStack.push(TABLE[stateStack.top()][static_cast<std::uint64_t>(symbol)] >> 2);
|
|
}
|
|
break;
|
|
case ACCEPT:
|
|
assert(stateStack.size() == 2);
|
|
return true;
|
|
default:
|
|
//IMPOSSIBLE
|
|
break;
|
|
}
|
|
}
|
|
}
|