MastaC729

CMPSC 122 Homework 2 tokenlist.cpp

Feb 21st, 2015
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.94 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.     Token *t;
  33.  
  34.     ListElement element;        // New element of token list
  35.     ListElement *newElement;
  36.    
  37.     newElement = &element;
  38.  
  39.     head = NULL;
  40.     tail = NULL;
  41.  
  42.     newElement->token = NULL;
  43.     newElement->next = NULL;
  44.  
  45.     // Initiation of List
  46.     for (int i = 0; i < exprlength && initiationBreak < 2; i++) {
  47.         switch (expr[i]) {
  48.         case '(':
  49.         case ')':
  50.             newTokenType = 3;       // A 3 indicates parenthesis of some sort
  51.         case '+':
  52.         case '-':
  53.         case '/':
  54.         case '*':
  55.         case '%':
  56.             newTokenType = 1;       // A 1 indicates an operator
  57.             break;
  58.         case ' ':
  59.             newTokenType = 4;       // A 4 indicates a space
  60.             continue;
  61.             break;
  62.         default:                    // A 2 (digit) or a 0 (unknown token type)
  63.             if (isdigit(expr[i])) {
  64.                 newTokenType = 2;   // A 2 indicates a digit
  65.             }
  66.         }
  67.         if (newTokenType != tokenType) {    // If the previous token is different
  68.             pos2 = i;                       // (operator -> integer)
  69.             switch (tokenType) {
  70.             case 3:                         // A parenthesis
  71.             case 1:                         // An operator
  72.                 if (initiationBreak == 0) {
  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.                     newElement = new ListElement();             // Create a new instance of newElement
  79.                     newElement->token = Token(expr[i]);     // Saves the operator (or parenthesis) token
  80.                     tail = newElement;                          // Sets the tail of the list
  81.                     head->next = tail;                  // Link the head and tail
  82.                     initiationBreak = 2;                        // Proceed to extension of list
  83.                 }
  84.                 break;
  85.             case 2:                         // A digit
  86.                 for (int j = pos; j < pos2; j++) {
  87.                         number = number * 10;
  88.                         number = number + atoi(&expr[j]);
  89.                     }
  90.                 if (initiationBreak == 0) {
  91.                     newElement->token = Token(number);          // Saves the integer token
  92.                     head = newElement;                          // Sets the head of the list
  93.                     initiationBreak = 1;                        // Proceeed to tail creation
  94.                     number = 0;                                 // Resets the value of number
  95.                 }
  96.                 else {
  97.                     newElement = new ListElement();             // Create a new instance of newElement
  98.                     newElement->token = Token(number);          // Saves the integer token
  99.                     tail = newElement;                          // Sets the tail of the list
  100.                     head->next = tail;                  // Link the head and tail
  101.                     initiationBreak = 2;                        // Proceed to extension of list
  102.                     number = 0;                                 // Resets the value of number
  103.                 }
  104.                 break;
  105.             }
  106.             pos = pos2;                    
  107.             newTokenType = tokenType;       // Change in type of token
  108.         }
  109.     }
  110.  
  111.     // Propagation and Termination of List
  112.     for (int i = pos; i < exprlength; i++) {
  113.         switch (expr[i]) {
  114.         case '(':
  115.         case ')':
  116.             newTokenType = 3;       // A 3 indicates parenthesis of some sort
  117.         case '+':
  118.         case '-':
  119.         case '/':
  120.         case '*':
  121.         case '%':
  122.             newTokenType = 1;       // A 1 indicates an operator
  123.             break;
  124.         case ' ':
  125.             newTokenType = 4;       // A 4 indicates a space
  126.             continue;
  127.             break;
  128.         default:
  129.             if (isdigit(expr[i])) {
  130.                 newTokenType = 2;   // A 2 indicates a digit
  131.             }
  132.         }
  133.         if (newTokenType != tokenType) {    // If the previous token is different
  134.             pos2 = i;                       // (operator -> integer)
  135.             switch (tokenType) {
  136.             case 3:                         // A parenthesis
  137.             case 1:                         // An operator
  138.                 newElement = new ListElement();             // Create a new instance of newElement
  139.                 newElement->token = Token(expr[i]);         // Saves the operator (or parenthesis) token
  140.                 newElement->next = NULL;                    // Sets the end next pointer to NULL
  141.                 tail->next = newElement;                    // The tail now points to newElement (will become the second to last element in the list)
  142.                 tail = newElement;                          // Tail becomes newElement
  143.                 break;
  144.             case 2:                         // A digit
  145.                 if (isdigit(expr[i])) {
  146.                     for (int j = pos; j < pos2; j++) {
  147.                         number = number * 10;
  148.                         number = number + atoi(&expr[j]);
  149.                     }
  150.                 }
  151.                 newElement = new ListElement();             // Create a new instance of newElement
  152.                 newElement->token = Token(number);          // Saves the integer token
  153.                 newElement->next = NULL;                    // Sets the end next pointer to NULL
  154.                 tail->next = newElement;                    // The tail now points to newElement (will become the second to last element in the list)
  155.                 tail = newElement;                          // Tail becomes newElement
  156.                 number = 0;
  157.                 break;
  158.             }
  159.             pos = pos2;
  160.             newTokenType = tokenType;
  161.         }
  162.         else if (newTokenType == 1 && tokenType == 1) { // Accounts for two consecutive operators
  163.             newElement = new ListElement();             // Create a new instance of newElement
  164.             newElement->token = Token(expr[i]);         // Saves the operator (or parenthesis) token
  165.             newElement->next = NULL;                    // Sets the end next pointer to NULL
  166.             tail->next = newElement;                    // The tail now points to newElement (will become the second to last element in the list)
  167.             tail = newElement;                          // Tail becomes newElement
  168.         }
  169.     }
  170.    
  171.     cout << "NIGGA JEFF" << endl;
  172.     cout << "niggs" << endl;
  173. }
  174.  
  175. //  output operation
  176. //  Display all of the tokens in the list
  177. ostream& operator<<( ostream &stream, TokenList &t )
  178. {
  179.     for (ListIterator iter = t.begin(); iter != t.end(); iter.advance())
  180.     {
  181.     stream << iter.token() << " ";
  182.     }
  183.     return stream;
  184. }
  185.  
  186. //  Creates an iterator to refer to the beginning of the list
  187. ListIterator TokenList::begin()
  188. {
  189.     return ListIterator( this, head );
  190. }
  191.  
  192. //  Creates an iterator to refer after the end of the list
  193. //  This list is simple:  it ends with NULL
  194. ListIterator TokenList::end()
  195. {
  196.     return ListIterator( this, NULL );
  197. }
  198.  
  199. //  Add a new element to the back of the list
  200. //  Parameter:
  201. //       t  (input Token)   the new item to add
  202. void TokenList::push_back(Token t)
  203. {
  204.  
  205.  
  206.  
  207. }
  208.  
  209. //  Add a new element to the front of the list
  210. //  Parameter:
  211. //       t  (input Token)   the new item to add
  212. void TokenList::push_front(Token t)
  213. {
  214.  
  215.  
  216.  
  217.  
  218. }
  219.  
  220. //  Remove and return the element at the front of the list
  221. Token TokenList::pop_front()
  222. {
  223.  
  224.  
  225.     return NULL;            // Temporary, ya dangus
  226.  
  227. }
Advertisement
Add Comment
Please, Sign In to add comment