-
Notifications
You must be signed in to change notification settings - Fork 166
/
gparser.cc
173 lines (141 loc) · 4.27 KB
/
gparser.cc
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
/*Copyright 2014 Francisco Alvaro
This file is part of SESHAT.
SESHAT is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
SESHAT is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with SESHAT. If not, see <http://www.gnu.org/licenses/>.
*/
#include "gparser.h"
#include <cstdlib>
#include <cstring>
#define SIZE 1024
gParser::gParser(Grammar *gram, FILE *fd, char *path) {
g = gram;
int n = strlen(path);
if( n > 0 ) {
pre = new char[n+1];
strcpy(pre, path);
}
else {
pre = new char[1];
pre[0] = 0;
}
parse( fd );
}
gParser::~gParser() {
delete[] pre;
}
bool gParser::isFillChar(char c) {
switch(c) {
case ' ':
case '\t':
case '\n':
case '\r':
return true;
default:
return false;
}
}
int gParser::split(char *str,char ***res){
char tokensaux[2*SIZE];
int n=0, i=0, j=0;
while( isFillChar(str[i]) ) i++;
while( str[i] ) {
if( str[i] == '\"' ) {
i++;
while( str[i] && str[i] != '\"' ) {
tokensaux[j] = str[i];
i++; j++;
}
i++;
}
else {
while( str[i] && !isFillChar(str[i]) ) {
tokensaux[j] = str[i];
i++; j++;
}
}
tokensaux[j++] = 0;
n++;
while( str[i] && isFillChar(str[i]) ) i++;
}
char **toks=new char*[n];
for(i=0, j=0; i<n; i++) {
int tlen = strlen(&tokensaux[j])+1;
toks[i] = new char[tlen];
strcpy(toks[i], &tokensaux[j]);
j += tlen;
}
*res = toks;
return n;
}
bool gParser::nextLine(FILE *fd, char *lin) {
do{
if( fgets(lin, SIZE, fd) == NULL )
return false;
}while( lin[0]=='#' || strlen(lin)<=1 );
return true;
}
void gParser::solvePath(char *in, char *out) {
strcpy(out, pre); //Copy prefix
strcat(out, in); //Add the remainding path
out[strlen(out)-1] = 0; //Remove the final '\n'
}
void gParser::parse(FILE *fd) {
char linea[SIZE], tok1[SIZE], tok2[SIZE], aux[SIZE];
//Read nonterminal symbols
while( nextLine(fd, linea) && strcmp(linea, "START\n") ) {
sscanf(linea, "%s", tok1);
g->addNoTerminal(tok1);
}
//Read start symbol(s) of the grammar
while( nextLine(fd, linea) && strcmp(linea, "PTERM\n") ) {
sscanf(linea, "%s", tok1);
g->addInitSym(tok1);
}
//Read terminal productions
while( nextLine(fd, linea) && strcmp(linea, "PBIN\n") ) {
float pr;
sscanf(linea, "%f %s %s %s", &pr, tok1, tok2, aux);
g->addTerminal(pr, tok1, tok2, aux);
}
//Read binary productions
while( nextLine(fd, linea) ) {
char **tokens;
int ntoks = split(linea, &tokens);
if( ntoks != 7 ) {
fprintf(stderr, "Error: Grammar not valid (PBIN)\n");
exit(-1);
}
if( !strcmp(tokens[1], "H") )
g->addRuleH(atof(tokens[0]), tokens[2], tokens[3], tokens[4], tokens[5], tokens[6]);
else if( !strcmp(tokens[1], "V") )
g->addRuleV(atof(tokens[0]), tokens[2], tokens[3], tokens[4], tokens[5], tokens[6]);
else if( !strcmp(tokens[1], "Ve") )
g->addRuleVe(atof(tokens[0]), tokens[2], tokens[3], tokens[4], tokens[5], tokens[6]);
else if( !strcmp(tokens[1], "Sup") )
g->addRuleSup(atof(tokens[0]), tokens[2], tokens[3], tokens[4], tokens[5], tokens[6]);
else if( !strcmp(tokens[1], "Sub") )
g->addRuleSub(atof(tokens[0]), tokens[2], tokens[3], tokens[4], tokens[5], tokens[6]);
else if( !strcmp(tokens[1], "SSE") )
g->addRuleSSE(atof(tokens[0]), tokens[2], tokens[3], tokens[4], tokens[5], tokens[6]);
else if( !strcmp(tokens[1], "Ins") )
g->addRuleIns(atof(tokens[0]), tokens[2], tokens[3], tokens[4], tokens[5], tokens[6]);
else if( !strcmp(tokens[1], "Mrt") )
g->addRuleMrt(atof(tokens[0]), tokens[2], tokens[3], tokens[4], tokens[5], tokens[6]);
else {
fprintf(stderr, "Error: Binary rule type '%s' nor valid\n", tokens[1]);
exit(-1);
}
//Free memory
for(int j=0; j<ntoks; j++)
delete[] tokens[j];
delete[] tokens;
}
}