forked from pgrandinetti/compilers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer.h
105 lines (94 loc) · 1.72 KB
/
lexer.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
101
102
103
104
105
enum TokenType {
// Basic Token Types in the Grammar
Comma,
Lpar, // (
Rpar, // )
Lbrack, // [
Rbrack, // ]
Lcurly, // {
Rcurly, // }
Plus,
Minus,
Equal,
EqEq, // ==
//Exclam, // !
NotEq, // !=
Lesser, // <
Greater, // >
LesserEq,
GreaterEq,
Star, // *
Div, // /
FloatDiv, // /.
Percent, // %
Or,
And, // &
Int,
Float,
Pow,
Dot,
QuotedStr, // "
Bool,
Null,
Var,
ReadIn,
WriteOut,
If,
While,
Break,
Continue,
Else,
Endline,
WS, // white space(s)
UNK,
/* -----=====----- */
// Grammar Constructs
// used by the parser, not the lexer
Program,
Line,
Assign,
Input,
Output,
IfLine,
LoopLine,
Expr,
Term,
Pred,
BaseExpr,
Operator,
CondOp,
IfCond,
IfBody,
OptElse,
LoopBody,
Obj,
Str,
Num,
Frac,
List,
ListElem,
ListExpr
};
// Generate readable string instead of int
const char* type2char (enum TokenType t);
struct Token{
char* lexeme;
enum TokenType type;
};
struct TokenList {
struct Token* token;
struct TokenList* next;
};
void print_Token(struct Token* p);
void print_TokenList(struct TokenList* p);
void free_Token(struct Token* tok);
void free_TokenList(struct TokenList* tok);
struct Token* new_Token(char* lexeme, enum TokenType tok);
struct TokenList* new_TokenList(struct Token* tok);
/*
* Create a TokenList from the characters stream
* (typically a file with source code).
* Return NULL if the characters sequence is not valid in the Grammar.
*/
struct TokenList* build_TokenList(const char* fp);
struct TokenList* strip_WS(struct TokenList* list);