MastaC729

CMPSC 122 Homework 3 tokenlist.cpp

Feb 21st, 2015
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.36 KB | None | 0 0
  1. // Token List Implementation file
  2. //
  3. // This tokenizer will scan a character string representing
  4. // an expression, and will return a list of tokens.
  5. // --- only integers and operators are supported (no names)
  6.  
  7. // The standard C library has some useful functions for us
  8. #include <string.h>
  9. #include <ctype.h>
  10.  
  11. // And to get the definition of a token:
  12. #include "tokenlist.h"
  13.  
  14.  
  15.  
  16. // TokenList constructor
  17. // converts a character string into a list of tokens
  18. // Parameter:
  19. //  expr    (input char pointer)    // string to examine
  20. // Pre-condition:  str may not be a null pointer
  21. //     and is assumed to actually point at a valid expression.
  22. TokenList::TokenList( const char expr[])
  23. {
  24.     int pos = -1;                           // Iterator for loop
  25.     int pos2;                               // Start of the next token
  26.     int initiationBreak = 0;                // Breaks out of the initiation of the list: 1 means go to tail, 2 means break out of initiation
  27.     int tokenType = 0;
  28.     int newTokenType = 0;
  29.     int number = 0;                             // Number token, used for calculating multidigit integers
  30.     int exprlength = strlen(expr);
  31.  
  32.     ListElement element;        // New element of token list
  33.     ListElement *newElement;
  34.    
  35.     newElement = &element;
  36.  
  37.     head = NULL;
  38.     tail = NULL;
  39.  
  40.     newElement->next = NULL;
  41.  
  42.     // Initiation of List
  43.     for (int i = 0; i < exprlength && initiationBreak < 2; i++) {
  44.         switch (expr[i]) {
  45.         case '(':
  46.         case ')':
  47.             newTokenType = 3;       // A 3 indicates parenthesis of some sort
  48.         case '+':
  49.         case '-':
  50.         case '/':
  51.         case '*':
  52.         case '%':
  53.             newTokenType = 1;       // A 1 indicates an operator
  54.             break;
  55.         case ' ':
  56.             newTokenType = 4;       // A 4 indicates a space
  57.             continue;
  58.             break;
  59.         default:                    // A 2 (digit) or a 0 (unknown token type)
  60.             if (isdigit(expr[i])) {
  61.                 newTokenType = 2;   // A 2 indicates a digit
  62.             }
  63.         }
  64.         if (newTokenType != tokenType) {    // If the previous token is different
  65.             cout << "get inside me" << tokenType << newTokenType << endl;
  66.             pos2 = i;                       // (operator -> integer)
  67.             switch (tokenType) {
  68.             case 3:                         // A parenthesis
  69.             case 1:                         // An operator
  70.                 cout << "get inside me" << tokenType << newTokenType << endl;
  71.                 if (initiationBreak == 0) {
  72.                     cout << "get inside me" << tokenType << newTokenType << endl;
  73.                     head = newElement;                          // Sets the head of the list
  74.                     newElement->token = Token(expr[i]);                 // Saves the operator (or parenthesis) token
  75.                     initiationBreak = 1;                        // Proceed to tail creation
  76.                 }
  77.                 else {
  78.                     cout << "get inside me" << tokenType << newTokenType << endl;
  79.                     newElement = new ListElement();             // Create a new instance of newElement
  80.                     newElement->token = Token(expr[i]);     // Saves the operator (or parenthesis) token
  81.                     tail = newElement;                          // Sets the tail of the list
  82.                     head->next = tail;                  // Link the head and tail
  83.                     initiationBreak = 2;                        // Proceed to extension of list
  84.                 }
  85.                 break;
  86.             case 2:                         // A digit
  87.                 cout << "get inside me" << tokenType << newTokenType << endl;
  88.                 for (int j = pos; j < pos2; j++) {
  89.                         number = number * 10;
  90.                         number = number + atoi(&expr[j]);
  91.                     }
  92.                 if (initiationBreak == 0) {
  93.                     cout << "get inside me" << tokenType << newTokenType << endl;
  94.                     newElement->token = Token(number);          // Saves the integer token
  95.                     head = newElement;                          // Sets the head of the list
  96.                     initiationBreak = 1;                        // Proceeed to tail creation
  97.                     number = 0;                                 // Resets the value of number
  98.                 }
  99.                 else {
  100.                     cout << "get inside me" << tokenType << newTokenType << endl;
  101.                     newElement = new ListElement();             // Create a new instance of newElement
  102.                     newElement->token = Token(number);          // Saves the integer token
  103.                     tail = newElement;                          // Sets the tail of the list
  104.                     head->next = tail;                  // Link the head and tail
  105.                     initiationBreak = 2;                        // Proceed to extension of list
  106.                     number = 0;                                 // Resets the value of number
  107.                 }
  108.                 break;
  109.             }
  110.             pos = pos2;                    
  111.             tokenType = newTokenType;       // Change in type of token
  112.         }
  113.     }
  114.  
  115.     // Propagation and Termination of List
  116.     for (int i = pos; i < exprlength; i++) {
  117.         switch (expr[i]) {
  118.         case '(':
  119.         case ')':
  120.             newTokenType = 3;       // A 3 indicates parenthesis of some sort
  121.         case '+':
  122.         case '-':
  123.         case '/':
  124.         case '*':
  125.         case '%':
  126.             newTokenType = 1;       // A 1 indicates an operator
  127.             break;
  128.         case ' ':
  129.             newTokenType = 4;       // A 4 indicates a space
  130.             continue;
  131.             break;
  132.         default:
  133.             if (isdigit(expr[i])) {
  134.                 newTokenType = 2;   // A 2 indicates a digit
  135.             }
  136.         }
  137.         if (newTokenType != tokenType) {    // If the previous token is different
  138.             pos2 = i;                       // (operator -> integer)
  139.             switch (tokenType) {
  140.             case 3:                         // A parenthesis
  141.             case 1:                         // An operator
  142.                 newElement = new ListElement();             // Create a new instance of newElement
  143.                 newElement->token = Token(expr[i]);         // Saves the operator (or parenthesis) token
  144.                 newElement->next = NULL;                    // Sets the end next pointer to NULL
  145.                 tail->next = newElement;                    // The tail now points to newElement (will become the second to last element in the list)
  146.                 tail = newElement;                          // Tail becomes newElement
  147.                 break;
  148.             case 2:                         // A digit
  149.                 if (isdigit(expr[i])) {
  150.                     for (int j = pos; j < pos2; j++) {
  151.                         number = number * 10;
  152.                         number = number + atoi(&expr[j]);
  153.                     }
  154.                 }
  155.                 newElement = new ListElement();             // Create a new instance of newElement
  156.                 newElement->token = Token(number);          // Saves the integer token
  157.                 newElement->next = NULL;                    // Sets the end next pointer to NULL
  158.                 tail->next = newElement;                    // The tail now points to newElement (will become the second to last element in the list)
  159.                 tail = newElement;                          // Tail becomes newElement
  160.                 number = 0;
  161.                 break;
  162.             }
  163.             pos = pos2;
  164.             tokenType = newTokenType;
  165.         }
  166.         else if (newTokenType == 1 && tokenType == 1) { // Accounts for two consecutive operators
  167.             newElement = new ListElement();             // Create a new instance of newElement
  168.             newElement->token = Token(expr[i]);         // Saves the operator (or parenthesis) token
  169.             newElement->next = NULL;                    // Sets the end next pointer to NULL
  170.             tail->next = newElement;                    // The tail now points to newElement (will become the second to last element in the list)
  171.             tail = newElement;                          // Tail becomes newElement
  172.         }
  173.     }
  174.    
  175.     cout << "NIGGA JEFF" << endl;
  176.     cout << "niggs" << endl;
  177. }
  178.  
  179. //  output operation
  180. //  Display all of the tokens in the list
  181. ostream& operator<<( ostream &stream, TokenList &t )
  182. {
  183.     for (ListIterator iter = t.begin(); iter != t.end(); iter.advance())
  184.     {
  185.     stream << iter.token() << " ";
  186.     }
  187.     return stream;
  188. }
  189.  
  190. //  Creates an iterator to refer to the beginning of the list
  191. ListIterator TokenList::begin()
  192. {
  193.     return ListIterator( this, head );
  194. }
  195.  
  196. //  Creates an iterator to refer after the end of the list
  197. //  This list is simple:  it ends with NULL
  198. ListIterator TokenList::end()
  199. {
  200.     return ListIterator( this, NULL );
  201. }
  202.  
  203. //  Add a new element to the back of the list
  204. //  Parameter:
  205. //       t  (input Token)   the new item to add
  206. void TokenList::push_back(Token t)
  207. {
  208.  
  209.  
  210.  
  211. }
  212.  
  213. //  Add a new element to the front of the list
  214. //  Parameter:
  215. //       t  (input Token)   the new item to add
  216. void TokenList::push_front(Token t)
  217. {
  218.  
  219.  
  220.  
  221.  
  222. }
  223.  
  224. //  Remove and return the element at the front of the list
  225. Token TokenList::pop_front()
  226. {
  227.  
  228.  
  229.     return NULL;            // Temporary, ya dangus
  230.  
  231. }
Advertisement
Add Comment
Please, Sign In to add comment