-
Notifications
You must be signed in to change notification settings - Fork 0
/
scanner.h
177 lines (166 loc) · 3.42 KB
/
scanner.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
* Project: IFJ16, a programming language interpreter
* FIT VUT Brno
* Authors: xzaryb00 - Zarybnický Jakub
* xtamas01 - Tamaškovič Marek
* xvasko12 - Vaško Martin
* xvasko14 - Vaško Michal
* xzales12 - Záleský Jiří
*/
#ifndef IFJ_SCANNER_H
#define IFJ_SCANNER_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <stdbool.h>
#include <ctype.h>
#include "error.h"
#include "ir.h"
#define ALLOC_BLOCK 2
//int alloc_length = ALLOC_BLOCK;
typedef enum {
RESERVED,
SYMBOL,
ID_SIMPLE,
ID_COMPOUND,
LIT_INTEGER,
LIT_STRING,
LIT_DOUBLE,
} TokenType;
typedef enum {
RES_BOOLEAN,
RES_BREAK,
RES_CLASS,
RES_CONTINUE,
RES_DO,
RES_DOUBLE,
RES_ELSE,
RES_FALSE,
RES_FOR,
RES_IF,
RES_INT,
RES_RETURN,
RES_STRING,
RES_STATIC,
RES_TRUE,
RES_VOID,
RES_WHILE,
} ReservedWord;
typedef enum {
SYM_BRACE_OPEN, //{
SYM_BRACE_CLOSE, //}
SYM_BRACKET_OPEN, //[
SYM_BRACKET_CLOSE,//]
SYM_PAREN_OPEN, //(
SYM_PAREN_CLOSE, //)
SYM_PLUS, //+
SYM_MINUS, //-
SYM_UNARY_PLUS, //++
SYM_UNARY_MINUS, //--
SYM_STAR, //*
SYM_SLASH, ///
SYM_SEMI, //;
SYM_COMMA, //,
SYM_GREATER, //>
SYM_GREATER_EQUAL,//>=
SYM_LESS, //<
SYM_LESS_EQUAL, //<=
SYM_EQUALS, //==
SYM_NOT_EQUALS, //!=
SYM_ASSIGN, //=
SYM_NOT, //!
SYM_AND, //&&
SYM_OR, //||
} SymbolType;
typedef enum{
NEUTRAL_STATE,
Start_state,
RESERVED_WORD,
END_OF_FILE,
AUT_SYMBOL,
AUT_IDEN,
AUT_IDEN2,
AUT_IDEN3,
AUT_NUM,
AUT_FLOAT1 ,
AUT_FLOAT2,
AUT_EX1,
AUT_EX2,
AUT_EX3,
AUT_STRING,
AUT_ESC,
AUT_ESC_ZERO,
AUT_ESCN,
AUT_ESC_ZERO2,
AUT_ESCN2,
AUT_DIV,
AUT_DIV2,
AUT_CMTL,
AUT_CMTB,
AUT_CMTB_END,
AUT_EQUALS,
AUT_LESS,
AUT_GREAT,
AUT_NOT_EQUALS,
AUT_UNARY_PL,
AUT_UNARY_MN,
AUT_AND,
AUT_OR,
AUT_BIN,
AUT_BIN2,
AUT_HEX,
AUT_HEX1,
AUT_HEX2,
AUT_HEX3,
AUT_OCT,
NUMBER,
FLOAT,
ERROR_IDEN,
ERROR_R,
ERROR_NUMBER,
ERROR_ASCII,
ERROR_ESC,
ERROR_ESC_ZERO,
ERROR_ESCN,
ERROR_ESC_ZERO2,
ERROR_ESCN2,
ERROR_CMTB,
ERROR_NOT_EQUALS,
ERROR_AND,
ERROR_OR,
ERROR_BIN,
ERROR_HEX,
IDEN,
IDEN_COMPOUND,
STRING,
EXT_BASE,
EXT_HEX,
EXT_OCT,
}AUTSTATES;
typedef struct s_Token {
//For error reporting
int lineNum;
int lineChar;
char *original; //The token as it appeared in the input stream
//For parsing
TokenType type;
struct s_Token *next; //For parsing only, not for lexing
union {
ReservedWord reserved;
SymbolType symbol;
char *id; //both for ID_SIMPLE & ID_COMPOUND
int intVal;
double doubleVal;
bool boolVal;
char *stringVal;
} val;
} Token;
Token *getNextToken(FILE *stream);
void string_end(char **string, char c, int *stringLength, int *stringAlloc);
Token *createToken(TokenType, void *, char *);
Token *chainTokens(Token *, ...);
void freeToken(Token *t);
void printToken(Token *t);
AUTSTATES Get_Token(FILE *input, char **orig, char **string, ReservedWord *reser, SymbolType *symbol, int *lineNumToken, int *lineColToken);
#endif /* IFJ_SCANNER_H */