MastaC729

CMPSC 122 Homework 2 token.h

Feb 21st, 2015
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.49 KB | None | 0 0
  1. // Token Header file
  2. // This is for the beginning of an object-oriented string tokenizer
  3. // to be used for arithmetic expressions.    The tokenizer is an
  4. // object that returns a linked list of object tokens, which are these.
  5.  
  6. // Since lots of files want to know what a token is, there is the
  7. // danger of redeclaration, which these next couple lines will neutralize.
  8. // (There is an #endif at the bottom as well)
  9. #ifndef TOKEN
  10. #define TOKEN
  11.  
  12. // Provides some helpful functionality for understanding tokens
  13. #include <iostream>
  14. #include <string>
  15. #include <stdlib.h>
  16. #include <ctype.h>
  17. using namespace std;
  18.  
  19. // Here is a definition of the token itself:
  20. class Token {
  21.     //  All the data members are private to keep them protected.
  22.     private:
  23.     bool    isInt;      // to identify the token type later
  24.     int value;      // value for an integer token
  25.     string  text;       // character for an operator token
  26.  
  27.     //  All of the methods here are public (which is not always the case)
  28.     //  First, a couple to initialize a new token, either operator or integer
  29.     public:         // and some constructors to initialize
  30.     Token(char c)       // single character
  31.     {
  32.         text = "" ;  
  33.         text += c;
  34.         isInt = false;
  35.         value = 0;      // initialize unused value
  36.     }
  37.     Token(string s)     // full string
  38.     {          
  39.         text =  s;    
  40.         isInt = false;
  41.         value = 0;      // initialize unused value
  42.     }
  43.     Token(int i)        // integer value
  44.     {      
  45.         value = i;
  46.         isInt = true;
  47.         text = "";      // initialize unused value
  48.     }
  49.  
  50.     Token()         // default constructor
  51.     {
  52.         value = 0;
  53.         text = "";
  54.         isInt = false;
  55.     }
  56.     //  Here are several accessor methods used to describe
  57.     //  the token, making visible the hidden private members.
  58.     //  No changes are permitted to this object -- only queries.
  59.     //
  60.     //  Placing these within the class definition allows for
  61.     //  inline expansion at the function call point, to save
  62.     //  the need for call and return.  In-line is only suitable
  63.     //  for very short functions (like these and constructors).
  64.  
  65.     bool isNull() const
  66.     {
  67.         return !isInt && text=="";
  68.     }
  69.  
  70.     bool isInteger() const
  71.     {
  72.         return isInt;
  73.     }
  74.  
  75.     int integerValue() const
  76.     {
  77.         return value;
  78.     }
  79.    
  80.     string tokenText() const
  81.     {
  82.         return text;
  83.     }
  84.  
  85.     char tokenChar() const
  86.     {
  87.         return text[0];
  88.     }
  89.  
  90.     //   And a function that  will be postponed to an
  91.     //   implementation file.
  92.  
  93.     friend ostream& operator <<( ostream& stream, Token &t);
  94. };
  95.  
  96. // End of the conditional compilation
  97. #endif
Advertisement
Add Comment
Please, Sign In to add comment