dawrehxyz

TreeS2Commented

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