dawrehxyz

TreeS2

Nov 18th, 2016
380
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.40 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <sstream>
  5. #include <regex>
  6. #include <algorithm>
  7.  
  8. using namespace std;
  9.  
  10. struct Token
  11. {
  12.     Token(int n, string c)
  13.     {
  14.         s = c;
  15.         nr = n;
  16.     }
  17.     string s;
  18.     int nr;
  19. };
  20.  
  21. struct Command
  22. {
  23.     Command(string p1, string p2)
  24.     {
  25.         s1 = p1;
  26.         s2 = p2;
  27.         next = NULL;
  28.         branch = NULL;
  29.     }
  30.     string s1;
  31.     string s2;
  32.     Command *next;
  33.     Command *branch;
  34. };
  35.  
  36. struct V2
  37. {
  38.     V2(double px, double py)
  39.     {
  40.         x = px;
  41.         y = py;
  42.     }
  43.     double x;
  44.     double y;
  45.     int rotate = 0;
  46. };
  47.  
  48. void lexicalanalysator();
  49. char caseupper(char c);
  50. bool hexletter(char c);
  51. bool parser();
  52. void calc(Command *ptr);
  53. void syntaxerror(int nr);
  54. bool c1(string token);
  55. bool c2(string token);
  56. bool h6(string token);
  57. bool D(string token);
  58.  
  59. vector<string> rows;
  60. vector<Token> tokens;
  61. vector<Command*> branchJumpBack;
  62. Command *commandTree = new Command("-1", "-1");
  63. Command *commandTreePtr = commandTree;
  64.  
  65. const double pi = 3.1415926535897;
  66. string outputColor = "#0000FF";
  67. V2 position = V2(0, 0);
  68. vector<string> outputVector;
  69.  
  70. int main()
  71. {
  72.     /*Command *tempCommand = new Command("-1", "-1");
  73.     for (int a = 0; a < 5; a++)
  74.     {
  75.     tempCommand = new Command(to_string(a), to_string(a));
  76.     current->next = tempCommand;
  77.     current = current->next;
  78.     }*/
  79.  
  80.     lexicalanalysator();
  81.     if (!parser()) return 0;
  82.     /*for (int a = 0; a < commands.size(); a++)
  83.     {
  84.     cout << commands.at(a).s1 << " " << commands.at(a).s2 << endl;
  85.     }*/
  86.     calc(commandTreePtr);
  87. }
  88.  
  89. void calc(Command *ptr)
  90. {
  91.     static bool stateUP = true;
  92.  
  93.     if (ptr == NULL)
  94.         return;
  95.     else if (ptr->s1 == "-1")
  96.         return;
  97.  
  98.     Command TI = *ptr;
  99.     double d = atoi(TI.s2.c_str());
  100.     if (TI.s1 == "DOWN") stateUP = false;
  101.     else if (TI.s1 == "UP") stateUP = true;
  102.     else if (TI.s1 == "COLOR") outputColor = TI.s2;
  103.     else if (TI.s1 == "LEFT") position.rotate += d;
  104.     else if (TI.s1 == "RIGHT") position.rotate -= d;
  105.     else if (TI.s1 == "FORW")
  106.     {
  107.         string otemp = "";
  108.         otemp += outputColor + " " + to_string(position.x) + " " + to_string(position.y);
  109.         position.x += d*cos(pi*position.rotate / 180.0);
  110.         position.y += d*sin(pi*position.rotate / 180.0);
  111.         otemp += " " + to_string(position.x) + " " + to_string(position.y);
  112.         if (!stateUP) cout << otemp << endl;
  113.     }
  114.     else if (TI.s1 == "BACK")
  115.     {
  116.         string otemp = "";
  117.         otemp += outputColor + " " + to_string(position.x) + " " + to_string(position.y);
  118.         position.x -= d*cos(pi*position.rotate / 180.0);
  119.         position.y -= d*sin(pi*position.rotate / 180.0);
  120.         otemp += " " + to_string(position.x) + " " + to_string(position.y);
  121.         if (!stateUP) cout << otemp << endl;
  122.     }
  123.  
  124.     if (ptr->s1 == "REP")
  125.     {
  126.         for (int a = 0; a < atoi((ptr->s2).c_str()); a++)
  127.         {
  128.             calc(ptr->branch);
  129.         }
  130.     }
  131.  
  132.     calc(ptr->next);
  133. }
  134.  
  135. bool parser()
  136. {
  137.     vector<string> citations;
  138.     vector<bool> repactivated;
  139.     string expected = "none";
  140.     bool singlerep = false;
  141.     bool branch = false;
  142.     bool done = false;
  143.     bool noCommand = false;
  144.     int repmult = 1;
  145.     for (int a = 0; a < tokens.size(); a++)
  146.     {
  147.         if (done)
  148.         {
  149.             done = false;
  150.             if (branch)
  151.             {
  152.                 branch = false;
  153.                 commandTree->branch = new Command("-1", "-1");
  154.                 commandTree = commandTree->branch;
  155.             }
  156.             else
  157.             {
  158.                 commandTree->next = new Command("-1", "-1");
  159.                 commandTree = commandTree->next;
  160.             }
  161.         }
  162.         Token token = tokens.at(a);
  163.         if (expected == "none")
  164.         {
  165.             if (c1(token.s))
  166.             {
  167.                 expected = ".";
  168.                 commandTree->s1 = token.s;
  169.             }
  170.             else if (c2(token.s))
  171.             {
  172.                 expected = "c2nr";
  173.                 commandTree->s1 = token.s;
  174.             }
  175.             else if (token.s == "COLOR")
  176.             {
  177.                 expected = "colorhex";
  178.                 commandTree->s1 = token.s;
  179.             }
  180.             else if (token.s == "REP")
  181.             {
  182.                 expected = "repnr";
  183.                 commandTree->s1 = token.s;
  184.             }
  185.             else if (token.s == "\"" && repactivated.size() && repactivated.back())
  186.             {
  187.                 expected = "none";
  188.                 singlerep = false;
  189.                 if (citations.back() == "\"")
  190.                 {
  191.                     if (noCommand)
  192.                     {
  193.                         syntaxerror(token.nr);
  194.                         return false;
  195.                     }
  196.                     citations.pop_back();
  197.                     repactivated.pop_back();
  198.                     commandTree = branchJumpBack.back();
  199.                     branchJumpBack.pop_back();
  200.                     done = true;
  201.                 }
  202.                 else
  203.                 {
  204.                     citations.back() = "\"";
  205.                 }
  206.             }
  207.             else
  208.             {
  209.                 syntaxerror(token.nr);
  210.                 return false;
  211.             }
  212.         }
  213.         else
  214.         {
  215.             if (expected == ".")
  216.             {
  217.                 done = true;
  218.                 noCommand = false;
  219.                 expected = "none";
  220.                 if (singlerep)
  221.                 {
  222.                     singlerep = false;
  223.                     citations.pop_back();
  224.                     repactivated.pop_back();
  225.                     commandTree = branchJumpBack.back();
  226.                     branchJumpBack.pop_back();
  227.                 }
  228.             }
  229.             else if (expected == "c2nr")
  230.             {
  231.                 expected = ".";
  232.                 if (D(token.s))
  233.                 {
  234.                     commandTree->s2 = token.s;
  235.                 }
  236.                 else
  237.                 {
  238.                     syntaxerror(token.nr);
  239.                     return false;
  240.                 }
  241.             }
  242.             else if (expected == "colorhex")
  243.             {
  244.                 expected = ".";
  245.                 if (h6(token.s))
  246.                 {
  247.                     commandTree->s2 = token.s;
  248.                 }
  249.                 else
  250.                 {
  251.                     syntaxerror(token.nr);
  252.                     return false;
  253.                 }
  254.             }
  255.             else if (expected == "repnr")
  256.             {
  257.                 expected = "none";
  258.                 if (D(token.s))
  259.                 {
  260.  
  261.                     repmult *= atoi(token.s.c_str());
  262.                     if (tokens.at(a + 1).s == "REP")
  263.                     {
  264.                         continue;
  265.                     }
  266.  
  267.                     branch = true;
  268.                     done = true;
  269.                     noCommand = true;
  270.  
  271.                     commandTree->s2 = to_string(repmult);
  272.                     branchJumpBack.push_back(commandTree);
  273.  
  274.                     repactivated.push_back(true);
  275.                     citations.push_back("");
  276.  
  277.                     singlerep = true;
  278.                     repmult = 1;
  279.                 }
  280.                 else
  281.                 {
  282.                     syntaxerror(token.nr);
  283.                     return false;
  284.                 }
  285.             }
  286.         }
  287.     }
  288.     if (repactivated.size())
  289.     {
  290.         syntaxerror(tokens.at(tokens.size() - 1).nr);
  291.         return false;
  292.     }
  293.     if (expected != "none")
  294.     {
  295.         syntaxerror(tokens.at(tokens.size() - 1).nr);
  296.         return false;
  297.     }
  298.  
  299.     return true;
  300. }
  301.  
  302. bool c1(string token)
  303. {
  304.     if (token == "UP" || token == "DOWN")
  305.         return true;
  306.     return false;
  307. }
  308.  
  309. bool c2(string token)
  310. {
  311.     if (token == "FORW" || token == "BACK" || token == "LEFT" || token == "RIGHT")
  312.         return true;
  313.     return false;
  314. }
  315.  
  316. bool h6(string token)
  317. {
  318.     if (token.length() == 7 && token[0] == '#' && hexletter(token[1]) && hexletter(token[2]) && hexletter(token[3]) && hexletter(token[4]) && hexletter(token[5]) && hexletter(token[6]))
  319.         return true;
  320.     return false;
  321. }
  322.  
  323. bool D(string token)
  324. {
  325.     if (!(token[0] > 48 && token[0] < 58))
  326.         return false;
  327.     for (int a = 0; a < token.length(); a++)
  328.     {
  329.         if (!(token[a] > 47 && token[a] < 58))
  330.             return false;
  331.     }
  332.     return true;
  333. }
  334.  
  335. bool hexletter(char c)
  336. {
  337.     if ((c > 47 && c < 58) || (c > 64 && c < 71))
  338.         return true;
  339.     return false;
  340. }
  341.  
  342. void lexicalanalysator()
  343. {
  344.     string row;
  345.     string word;
  346.  
  347.     ios_base::sync_with_stdio(false);
  348.     while (getline(cin, row)) rows.push_back(row);
  349.  
  350.     for (int a = 0; a < rows.size(); a++)
  351.     {
  352.         bool bc = false;
  353.         row = rows.at(a);
  354.         word = "";
  355.         for (int b = 0; b < row.length(); b++)
  356.         {
  357.             char c = row.at(b);
  358.  
  359.             if (c == '%')
  360.             {
  361.                 if (bc)
  362.                 {
  363.                     bc = false;
  364.                     tokens.push_back(Token(a + 1, word));
  365.                 }
  366.                 break;
  367.             }
  368.             else if (c == ' ' || c == '\t')
  369.             {
  370.                 if (bc)
  371.                 {
  372.                     bc = false;
  373.                     tokens.push_back(Token(a + 1, word));
  374.                 }
  375.                 word = "";
  376.                 continue;
  377.             }
  378.             else if (c == '.')
  379.             {
  380.                 if (word != "")
  381.                     tokens.push_back(Token(a + 1, word));
  382.                 tokens.push_back(Token(a + 1, "."));
  383.                 word = "";
  384.                 bc = false;
  385.                 continue;
  386.             }
  387.             else if (word == "\"")
  388.             {
  389.                 if (bc)
  390.                 {
  391.                     bc = false;
  392.                     tokens.push_back(Token(a + 1, word));
  393.                 }
  394.                 word = "";
  395.             }
  396.  
  397.             bc = true;
  398.             word += caseupper(row.at(b));
  399.  
  400.             if (b == row.length() - 1) tokens.push_back(Token(a + 1, word));
  401.         }
  402.     }
  403.  
  404.     //for (int a = 0; a < tokens.size(); a++)
  405.     //cout << tokens.at(a).nr << " " << tokens.at(a).s << endl;
  406. }
  407.  
  408. char caseupper(char c)
  409. {
  410.     if (c > 96 && c < 123)
  411.         return char(c - 32);
  412.     return c;
  413. }
  414.  
  415. void syntaxerror(int nr)
  416. {
  417.     cout << "syntaxfel pĆ„ rad " << nr << endl;
  418. }
Advertisement
Add Comment
Please, Sign In to add comment