Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "scanner.h"
- #include <iostream>
- #include "symbols.h"
- #include "cstream.h"
- #include <stack>
- #include <fstream>
- #include <iomanip>
- using std::cout;
- using std::endl;
- uint8_t charset::table[256] = {
- 6,
- 6, 6, 6, 6, 6, 6, 6, 6, 0, 0,
- 0, 0, 0, 6, 6, 6, 6, 6, 6, 6,
- 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
- 6, 0, 6, 6, 6, 6, 6, 6, 6, 3,
- 3, 6, 6, 3, 6, 6, 6, 1, 1, 1,
- 1, 1, 1, 1, 1, 1, 1, 6, 3, 6,
- 6, 6, 6, 6, 2, 2, 2, 2, 2, 2,
- 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
- 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
- 6, 6, 6, 6, 6, 6, 2, 2, 2, 2,
- 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
- 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
- 2, 2, 6, 6, 6, 6, 6, 6, 6, 6,
- 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
- 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
- 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
- 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
- 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
- 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
- 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
- 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
- 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
- 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
- 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
- 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
- 6, 6, 6, 6, 6
- };
- struct state {
- int ip;
- std::string buffer;
- int code;
- bool(*accept)(int);
- int at, af;
- };
- struct analyzer {
- static std::vector<state> states;
- static void run(tstream);
- };
- static bool is_ident(const int code) {
- return code > 1000;
- }
- static bool is_uint(const int code) {
- return (501 <= code) && (code <= 1000);
- }
- static bool ret_true(const int) { return true; }
- enum {
- signal_program,
- program,
- block,
- declarations,
- procedure_declarations,
- procedure,
- parameters_list,
- identifiers_list,
- statements_list,
- statement,
- actual_arguments,
- actual_arguments_list,
- variable_identifier,
- procedure_identifier,
- identifier,
- unsigned_integer
- };
- std::vector<state> analyzer::states = {
- { 0, "<signal-program>", -1, nullptr, -1, 0 },
- { 1, "<program>", 401, nullptr, 0, 0 },
- { 2, "", -40, nullptr, -1, 0 },
- { 3, "", -18, nullptr, 0, -1 },
- { 4, "", ';', nullptr, -1, 0 },
- { 5, "", -7, nullptr, -1, 0 },
- { 6, "", ';', nullptr, 1, 0 },
- { 7, "<block>", -11, nullptr, 0, -1 },
- { 8, "", 403, nullptr, -1, 0 },
- { 9, "", -24, nullptr, -1, -1 },
- { 10, "", 404, nullptr, 1, 0 },
- { 11, "<declarations>", -12, nullptr, 0, 0 },
- { 12, "<procedure-declarations>", -14, nullptr, 0, 0 },
- { 13, "<procedure-declarations>", -12, nullptr, 0, 0 },
- { 14, "<procedure>", 401, nullptr, 0, 1 },
- { 15, "", -39, nullptr, 0, 0 },
- { 16, "", -18, nullptr, -1, -1 },
- { 17, "", ';', nullptr, 1, 0 },
- { 18, "<parameters-list>", '(', nullptr, 0, 1 },
- { 19, "", -21, nullptr, 0, 0 },
- { 20, "", ')', nullptr, 1, 0 },
- { 21, "<identifiers-list>", -39, nullptr, 0, 0 },
- { 22, "", ',', nullptr, 0, 1 },
- { 23, "", -21, nullptr, 0, 0 },
- { 24, "<statements-list>", -26, nullptr, -1, 0 },
- { 25, "", -24, nullptr, 0, 0 },
- { 26, "<statement>", -30, nullptr, 0, -1 },
- { 27, "", -40, nullptr, -1, 0 },
- { 28, "", -32, nullptr, -1, -1 },
- { 29, "", ';', nullptr, 1, 0 },
- { 30, "", 402, nullptr, -1, 1 },
- { 31, "", ';', nullptr, 1, 0 },
- { 32, "<actual-arguments>", '(', nullptr, -1, 1 },
- { 33, "", -42, nullptr, -1, 0 },
- { 34, "", -36, nullptr, 0, -1 },
- { 35, "", ')', nullptr, 1, 0 },
- { 36, "<actual-arguments-list>", ',', nullptr, -1, 1 },
- { 37, "", -42, nullptr, -1, 0 },
- { 38, "", -36, nullptr, 0, 0 },
- { 39, "<variable-identifier>", -41, nullptr, 0, 0 },
- { 40, "<procedure-identifier>", -41, nullptr, 0, 0 },
- { 41, "<identifier>", 0, is_ident, 1, 1 },
- { 42, "<unsigned-integer>", 0, is_uint, 1, 1 },
- };
- struct tree {
- std::string name;
- tree* root = nullptr;
- std::vector<tree*> nodes;
- int ip = 0;
- tree* find_root() {
- }
- };
- void print(tree* root, int prefix = 0) {
- printf("%*c%i -> %s\n", prefix, '\0', root->ip, root->name.c_str());
- for (auto node : root->nodes) {
- print(node, prefix + 4);
- }
- }
- void analyzer::run(tstream tokens) {
- std::stack<state> sp;
- int ip = 0;
- int prefix = 0;
- do {
- sp.push(states[ip]);
- printf("%*c%s\n", prefix, '\0', sp.top().buffer.c_str());
- if (sp.top().code < 0) {
- ip = -sp.top().code;
- prefix += 4;
- continue;
- }
- bool accept = ((sp.top().code > 0) && (sp.top().code == tokens.peek().type)) || ((sp.top().code == 0) && sp.top().accept(tokens.peek().type));
- if (!accept && (sp.top().af != 1)) {
- printf("error: %i -> %s\n", sp.top().ip, tokens.peek().buffer.c_str());
- break;
- }
- printf("%*c%s\n", prefix += 4, '\0', (accept ? tokens.peek().buffer.c_str() : "empty"));
- if ((accept ? (tokens.advance(), sp.top().at) : sp.top().af) == 1) {
- while ((accept ? sp.top().at : sp.top().af) != -1) {
- sp.pop();
- prefix -= 4;
- }
- }
- ip = sp.top().ip + 1;
- prefix -= 4;
- sp.pop();
- } while (!sp.empty());
- }
- int main() {
- symbols::keywords["procedure"] = 401;
- symbols::keywords["return"] = 402;
- symbols::keywords["begin"] = 403;
- symbols::keywords["end"] = 404;
- auto tokens = scanner::scan("input.signal");
- printf("%-15s\t%-23s\t%s\n", "token", "position", "code");
- tokens.reset();
- while (tokens) {
- const auto &tok = tokens.get();
- printf("%-15s\t(line: %2u, column: %2u)\t%-4i\n", tok.buffer.c_str(), tok.line, tok.column, tok.type);
- }
- printf("\n");
- tokens.reset();
- analyzer::run(tokens);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement