Advertisement
dark-s0ul

knuth

May 9th, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.85 KB | None | 0 0
  1. #include "scanner.h"
  2. #include <iostream>
  3. #include "symbols.h"
  4. #include "cstream.h"
  5. #include <stack>
  6. #include <fstream>
  7. #include <iomanip>
  8.  
  9. using std::cout;
  10. using std::endl;
  11.  
  12. uint8_t charset::table[256] = {
  13.         6,
  14.         6, 6, 6, 6, 6, 6, 6, 6, 0, 0,
  15.         0, 0, 0, 6, 6, 6, 6, 6, 6, 6,
  16.         6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  17.         6, 0, 6, 6, 6, 6, 6, 6, 6, 3,
  18.         3, 6, 6, 3, 6, 6, 6, 1, 1, 1,
  19.         1, 1, 1, 1, 1, 1, 1, 6, 3, 6,
  20.         6, 6, 6, 6, 2, 2, 2, 2, 2, 2,
  21.         2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  22.         2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  23.         6, 6, 6, 6, 6, 6, 2, 2, 2, 2,
  24.         2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  25.         2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  26.         2, 2, 6, 6, 6, 6, 6, 6, 6, 6,
  27.         6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  28.         6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  29.         6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  30.         6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  31.         6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  32.         6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  33.         6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  34.         6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  35.         6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  36.         6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  37.         6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  38.         6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
  39.         6, 6, 6, 6, 6
  40. };
  41.  
  42. struct state {
  43.     int ip;
  44.     std::string buffer;
  45.     int code;
  46.     bool(*accept)(int);
  47.     int at, af;
  48. };
  49.  
  50. struct analyzer {
  51.     static std::vector<state> states;
  52.     static void run(tstream);
  53. };
  54.  
  55. static bool is_ident(const int code)  {
  56.     return code > 1000;
  57. }
  58.  
  59. static bool is_uint(const int code)  {
  60.     return (501 <= code) && (code <= 1000);
  61. }
  62.  
  63. static bool ret_true(const int) { return true; }
  64.  
  65. enum {
  66.     signal_program,
  67.     program,
  68.     block,
  69.     declarations,
  70.     procedure_declarations,
  71.     procedure,
  72.     parameters_list,
  73.     identifiers_list,
  74.     statements_list,
  75.     statement,
  76.     actual_arguments,
  77.     actual_arguments_list,
  78.     variable_identifier,
  79.     procedure_identifier,
  80.     identifier,
  81.     unsigned_integer
  82. };
  83.  
  84. std::vector<state> analyzer::states = {
  85.         {  0,         "<signal-program>",  -1,  nullptr, -1,  0 },
  86.         {  1,                "<program>", 401,  nullptr,  0,  0 },
  87.         {  2,                         "", -40,  nullptr, -1,  0 },
  88.         {  3,                         "", -18,  nullptr,  0, -1 },
  89.         {  4,                         "", ';',  nullptr, -1,  0 },
  90.         {  5,                         "",  -7,  nullptr, -1,  0 },
  91.         {  6,                         "", ';',  nullptr,  1,  0 },
  92.         {  7,                  "<block>", -11,  nullptr,  0, -1 },
  93.         {  8,                         "", 403,  nullptr, -1,  0 },
  94.         {  9,                         "", -24,  nullptr, -1, -1 },
  95.         { 10,                         "", 404,  nullptr,  1,  0 },
  96.         { 11,           "<declarations>", -12,  nullptr,  0,  0 },
  97.         { 12, "<procedure-declarations>", -14,  nullptr,  0,  0 },
  98.         { 13, "<procedure-declarations>", -12,  nullptr,  0,  0 },
  99.         { 14,              "<procedure>", 401,  nullptr,  0,  1 },
  100.         { 15,                         "", -39,  nullptr,  0,  0 },
  101.         { 16,                         "", -18,  nullptr, -1, -1 },
  102.         { 17,                         "", ';',  nullptr,  1,  0 },
  103.         { 18,        "<parameters-list>", '(',  nullptr,  0,  1 },
  104.         { 19,                         "", -21,  nullptr,  0,  0 },
  105.         { 20,                         "", ')',  nullptr,  1,  0 },
  106.         { 21,       "<identifiers-list>", -39,  nullptr,  0,  0 },
  107.         { 22,                         "", ',',  nullptr,  0,  1 },
  108.         { 23,                         "", -21,  nullptr,  0,  0 },
  109.         { 24,        "<statements-list>", -26,  nullptr, -1,  0 },
  110.         { 25,                         "", -24,  nullptr,  0,  0 },
  111.         { 26,              "<statement>", -30,  nullptr,  0, -1 },
  112.         { 27,                         "", -40,  nullptr, -1,  0 },
  113.         { 28,                         "", -32,  nullptr, -1, -1 },
  114.         { 29,                         "", ';',  nullptr,  1,  0 },
  115.         { 30,                         "", 402,  nullptr, -1,  1 },
  116.         { 31,                         "", ';',  nullptr,  1,  0 },
  117.         { 32,       "<actual-arguments>", '(',  nullptr, -1,  1 },
  118.         { 33,                         "", -42,  nullptr, -1,  0 },
  119.         { 34,                         "", -36,  nullptr,  0, -1 },
  120.         { 35,                         "", ')',  nullptr,  1,  0 },
  121.         { 36,  "<actual-arguments-list>", ',',  nullptr, -1,  1 },
  122.         { 37,                         "", -42,  nullptr, -1,  0 },
  123.         { 38,                         "", -36,  nullptr,  0,  0 },
  124.         { 39,    "<variable-identifier>", -41,  nullptr,  0,  0 },
  125.         { 40,   "<procedure-identifier>", -41,  nullptr,  0,  0 },
  126.         { 41,             "<identifier>",   0, is_ident,  1,  1 },
  127.         { 42,       "<unsigned-integer>",   0,  is_uint,  1,  1 },
  128. };
  129.  
  130. struct tree {
  131.     std::string name;
  132.     tree* root = nullptr;
  133.     std::vector<tree*> nodes;
  134.     int ip = 0;
  135.  
  136.     tree* find_root() {
  137.  
  138.     }
  139. };
  140.  
  141. void print(tree* root, int prefix = 0) {
  142.     printf("%*c%i -> %s\n", prefix, '\0', root->ip, root->name.c_str());
  143.  
  144.     for (auto node : root->nodes) {
  145.         print(node, prefix + 4);
  146.     }
  147. }
  148.  
  149. void analyzer::run(tstream tokens) {
  150.     std::stack<state> sp;
  151.  
  152.     int ip = 0;
  153.     int prefix = 0;
  154.  
  155.     do {
  156.         sp.push(states[ip]);
  157.  
  158.         printf("%*c%s\n", prefix, '\0', sp.top().buffer.c_str());
  159.  
  160.         if (sp.top().code < 0) {
  161.             ip = -sp.top().code;
  162.             prefix += 4;
  163.             continue;
  164.         }
  165.  
  166.         bool accept = ((sp.top().code > 0) && (sp.top().code == tokens.peek().type)) || ((sp.top().code == 0) && sp.top().accept(tokens.peek().type));
  167.  
  168.         if (!accept && (sp.top().af != 1)) {
  169.             printf("error: %i -> %s\n", sp.top().ip, tokens.peek().buffer.c_str());
  170.             break;
  171.         }
  172.  
  173.         printf("%*c%s\n", prefix += 4, '\0', (accept ? tokens.peek().buffer.c_str() : "empty"));
  174.  
  175.         if ((accept ? (tokens.advance(), sp.top().at) : sp.top().af) == 1) {
  176.             while ((accept ? sp.top().at : sp.top().af) != -1) {
  177.                 sp.pop();
  178.                 prefix -= 4;
  179.             }
  180.         }
  181.  
  182.         ip = sp.top().ip + 1;
  183.  
  184.         prefix -= 4;
  185.         sp.pop();
  186.     } while (!sp.empty());
  187. }
  188.  
  189. int main() {
  190.     symbols::keywords["procedure"] = 401;
  191.     symbols::keywords["return"] = 402;
  192.     symbols::keywords["begin"] = 403;
  193.     symbols::keywords["end"] = 404;
  194.  
  195.     auto tokens = scanner::scan("input.signal");
  196.  
  197.     printf("%-15s\t%-23s\t%s\n", "token", "position", "code");
  198.  
  199.     tokens.reset();
  200.  
  201.     while (tokens) {
  202.         const auto &tok = tokens.get();
  203.  
  204.         printf("%-15s\t(line: %2u, column: %2u)\t%-4i\n", tok.buffer.c_str(), tok.line, tok.column, tok.type);
  205.     }
  206.  
  207.     printf("\n");
  208.  
  209.     tokens.reset();
  210.     analyzer::run(tokens);
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement