-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtokentype.h
executable file
·103 lines (85 loc) · 2.96 KB
/
tokentype.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/* tokentype.h
Alistair Campbell
Fall 2014
CS 310 Compilers
A "mapping" between token types and their names
In the future, this file, or one like it, will get
Generated automatically.
Also contains the definition of a Token for purposes of
our lexical analysis phase.
Fall 2019: Changed so that T_values start with 1
*/
#ifndef SCANNER
#define SCANNER
struct ParseTree;
typedef ParseTree * YYSTYPE;
#define YYSTYPE_IS_DECLARED
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include "y.tab.h"
using namespace std;
/* Constants for all 51 types of tokens in Decaf */
/* enum TokenType { */
/* /\* The 22 keywords *\/ */
/* T_Void=1, T_Int, T_Double, T_Bool, */
/* T_String, T_Class, T_Interface, T_Null, */
/* T_This, T_Extends, T_Implements, T_For, */
/* T_While, T_If, T_Else, T_Return, */
/* T_Break, T_New, T_NewArray, T_Print, */
/* T_ReadInteger, T_ReadLine, */
/* /\* 2 Identifiers *\/ */
/* T_Identifier, T_TypeIdentifier, */
/* /\* The 4 kinds of constants (literals) *\/ */
/* T_IntConstant, T_BoolConstant, T_DoubleConstant, T_StringConstant, */
/* /\* The 24 other tokens */
/* + - * / % < <= > >= = == != && || ! ; , . [ ] ( ) { } *\/ */
/* T_Plus, T_Minus, T_Times, T_Div, */
/* T_Mod, T_Less, T_LessEqual, T_Greater, */
/* T_GreaterEqual, T_Assign, T_Equal, T_NotEqual, */
/* T_And, T_Or, T_Not, T_Semicolon, */
/* T_Comma, T_Dot, T_LBracket, T_RBracket, */
/* T_LParen, T_RParen, T_LBrace, T_RBrace */
/* }; */
/* And their associated names. Which we will use solely to verify our
scanner is working. */
static const char *TokenNames[] = {
"UNUSED",
/* The 22 keywords */
"T_Void", "T_Int", "T_Double", "T_Bool",
"T_String", "T_Class", "T_Interface", "T_Null",
"T_This", "T_Extends", "T_Implements", "T_For",
"T_While", "T_If", "T_Else", "T_Return",
"T_Break", "T_New", "T_NewArray", "T_Print",
"T_ReadInteger", "T_ReadLine",
/* 2 Identifiers */
"T_Identifier", "T_TypeIdentifier",
/* The 4 kinds of constants (literals) */
"T_IntConstant", "T_BoolConstant", "T_DoubleConstant", "T_StringConstant",
/* The 24 other tokens
+ - * / % < <= > >= = == != && || ! ; , . [ ] ( ) { } */
"T_Plus", "T_Minus", "T_Times", "T_Div",
"T_Mod", "T_Less", "T_LessEqual", "T_Greater",
"T_GreaterEqual", "T_Assign", "T_Equal", "T_NotEqual",
"T_And", "T_Or", "T_Not", "T_Semicolon",
"T_Comma", "T_Dot", "T_LBracket", "T_RBracket",
"T_LParen", "T_RParen", "T_LBrace", "T_RBrace"
};
/* The struct for tokens for the Decaf lexer assignment */
struct Token {
int type;
string text;
int line;
Token() {} // leave uninitialized
Token(int type, string text, int line) : type(type), text(text), line(line) {}
string toString() {
// convert line to a C string
char lineStr[200];
sprintf(lineStr,"%d",line);
return string(TokenNames[type]) + '(' + text + ',' + lineStr + ')';
}
void print() {
cout << toString() << endl;
}
};
#endif