-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLexicalParser.cpp
117 lines (91 loc) · 2.11 KB
/
LexicalParser.cpp
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
/*
* LexicalParser.cpp
*
* Created on: 07.04.2013
* Author: urandon
*/
#include "LexicalParser.h"
#include <cstdlib>
#include <ctype.h>
#include <cstdio>
#include <string.h>
LexicalParser::LexicalParser() { }
LexicalParser::~LexicalParser() { }
/* LexicalParere::buffer_t */
void LexicalParser::buffer_t::push(char ch) {
int newsize = size + 1;
if(newsize > allocated){
allocated *= 2;
data = (char *) realloc(data, allocated * sizeof(* data));
}
data[size] = ch;
size = newsize;
}
void LexicalParser::buffer_t::flush() {
size = 0;
if(allocated != DEFAULT_SIZE){
allocated = DEFAULT_SIZE;
free(data);
data = (char *) malloc(allocated * sizeof(char));
}
}
LexicalParser::buffer_t::buffer_t():size(0),allocated(DEFAULT_SIZE) {
data = (char *) malloc(DEFAULT_SIZE * sizeof(char));
}
LexicalParser::buffer_t::~buffer_t() {
free(data);
}
/* LexicalParser::lexem */
lexem::lexem(int mid, int mtype, int mline):
type(mtype),id(mid),line(mline) {} ;
lexem::lexem(const lexem & lex) {
type = lex.type;
id = lex.id;
line = lex.line;
}
lexem::~lexem() {}
/* LexicalParser::map */
LexicalParser::map_t::map_t():size(0) {
const int DEFAULT = 64;
allocated = DEFAULT;
strings = (char **) malloc(allocated * sizeof(*strings));
}
LexicalParser::map_t::~map_t(){
for(int i = 0; i < size; ++i){
free(strings[i]);
}
free(strings);
}
const char * LexicalParser::map_t::getString(int id){
if(id < 0 || id >= size){
throw UnknownLexemIdError();
} else {
return strings[id];
}
}
int LexicalParser::map_t::getId(const char *s){
for(int i = 0; i < size; ++i){
if(strcmp(strings[i],s)){
return i;
}
}
throw UnregistredLexemError(s);
}
int LexicalParser::map_t::push(const char *s, int length){
for(int i = 0; i < size; ++i){
if(strcmp(strings[i],s) == 0){
return i;
}
}
if(size >= allocated){ //resize it
allocated *= 2;
strings = (char **) realloc(strings, allocated * sizeof(* strings));
}
strings[size] = (char *) malloc( (length) * sizeof(char) );
strcpy(strings[size], s);
++size;
return size-1;
}
int LexicalParser::map_t::push(const char *s){
return push(s, strlen(s)+1);
}