scanner.h
上传用户:kalinte
上传日期:2013-04-07
资源大小:127k
文件大小:1k
源码类别:

DNA

开发平台:

C++ Builder

  1. /****************************************************/
  2. /* File: scanner.h                                  */
  3. /* The scanner implementation for the C- compiler   */
  4. /* Xiang Cui (sean)                                 */
  5. /* 230030782                                        */
  6. /****************************************************/
  7. #include <iostream>
  8. #include <iomanip.h>
  9. #include <string>
  10. #include <map>
  11. #include <fstream>
  12. #include "stdlib.h"
  13. #include "stdio.h"
  14. #include "string.h"
  15. #include "ctype.h"
  16. typedef enum
  17. /* book-keeping tokens */
  18. {ERROR,ENDFILE,
  19.  /* keywords */
  20.  ELSE,IF,INT,RETURN,VOID,WHILE,
  21.  /* multicharacter tokens */
  22.  ID,NUM,
  23.  /* special symbols */
  24.  PLUS,MINUS,TIMES,DIV,LT,LTEQ,GT,GTEQ,EQ,NEQ,ASSIGN,SEMI,
  25.  COMMA,LPAREN,RPAREN,LSQR,RSQR,LCRLY,RCRLY,LCMNT,RCMNT
  26. } TokenType;
  27. /* states in scanner DFA */
  28. typedef enum
  29. { START,INNUM,INID,DONE,INLT,INGT,INEQ,INNEQ,INSLASH,INLCMNT,INRCMNT,ERR}
  30. StateType;
  31. class Myscanner
  32. {
  33. public:
  34.     Myscanner(const char *);
  35.     TokenType scan(); /*scan procedure*/
  36.     void printToken(TokenType token, string tokenString);/*print out tokens*/
  37.     TokenType reservedLookup (string str);/*look up the Keywords table*/
  38.     string tokens;
  39.     int lineno;
  40.     ifstream sf;//source file
  41. private:
  42.     bool save;
  43.     char c;
  44.     StateType state;
  45.     TokenType currentToken;
  46. };