MastaC729

CMPSC 122 Homework 3 tokenlist.cpp

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