kartikkukreja

Tokenization

Apr 24th, 2013
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.19 KB | None | 0 0
  1. /*  Author : Kartik Kukreja
  2.     Roll No: 2K10/CO/049
  3.     Description :   Program to tokenize a given program. It reads in the tokens from "tokens.txt"
  4.             and a program from console and prints out its tokenization to console.
  5.  
  6.             Each line of "tokens.txt" contains a lexeme and its token class. Each token in
  7.             the input program is delimited by spaces.
  8.  */
  9.  
  10. #include <cstdio>
  11. #include <map>
  12. #include <string>
  13. #include <algorithm>
  14. #include <iostream>
  15. #include <fstream>
  16. #include <sstream>
  17. using namespace std;
  18.  
  19. map < string, string > classOf;
  20.  
  21. int main()  {
  22.     string lexeme, tokenClass;
  23.     char program[10000];
  24.  
  25.     // read in the underlying tokens
  26.     ifstream fin("tokens.txt");
  27.     while(!fin.eof())   {
  28.         fin >> lexeme >> tokenClass;
  29.         classOf[lexeme] = tokenClass;
  30.     }
  31.     fin.close();
  32.  
  33.     // read in a program and tokenize it
  34.     printf("Enter a valid program :\n");
  35.     scanf("%[^\n]", program);
  36.     stringstream ss(program);
  37.     printf("\nTokenization of the given program :\n");
  38.     while(!ss.eof())    {
  39.         ss >> lexeme;
  40.         if(classOf.find(lexeme) == classOf.end())   {
  41.             cout << "Invalid token : " << lexeme << "\n";
  42.             return -1;
  43.         }
  44.         cout << "< " << lexeme << ", " << classOf[lexeme] << " >\n";
  45.     }
  46.     return 0;
  47. }
Advertisement
Add Comment
Please, Sign In to add comment