-
Notifications
You must be signed in to change notification settings - Fork 1
/
lex.h
65 lines (50 loc) · 1.14 KB
/
lex.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
/*
* lex.h
*
* CS280
* Fall 2020
*/
#ifndef LEX_H_
#define LEX_H_
#include <string>
#include <iostream>
using std::string;
using std::istream;
using std::ostream;
enum Token {
// keywords
PRINT, BEGIN, END, IF, THEN,
// an identifier
IDENT,
// an integer and string constant
ICONST, RCONST, SCONST,
// the operators, parens, semicolon
PLUS, MINUS, MULT, DIV, EQ, LPAREN, RPAREN, SCOMA, COMA,
// any error returns this token
ERR,
// when completed (EOF), return this token
DONE
};
class LexItem {
Token token;
string lexeme;
int lnum;
public:
LexItem() {
token = ERR;
lnum = -1;
}
LexItem(Token token, string lexeme, int line) {
this->token = token;
this->lexeme = lexeme;
this->lnum = line;
}
bool operator==(const Token token) const { return this->token == token; }
bool operator!=(const Token token) const { return this->token != token; }
Token GetToken() const { return token; }
string GetLexeme() const { return lexeme; }
int GetLinenum() const { return lnum; }
};
extern ostream& operator<<(ostream& out, const LexItem& tok);
extern LexItem getNextToken(istream& in, int& linenum);
#endif /* LEX_H_ */