aboutsummaryrefslogtreecommitdiff
path: root/src/tools
diff options
context:
space:
mode:
Diffstat (limited to 'src/tools')
-rw-r--r--src/tools/parser.cpp57
1 files changed, 50 insertions, 7 deletions
diff --git a/src/tools/parser.cpp b/src/tools/parser.cpp
index e4fc95f..76d4a72 100644
--- a/src/tools/parser.cpp
+++ b/src/tools/parser.cpp
@@ -157,15 +157,44 @@ void redPrint( Bu::Parser &p )
157{ 157{
158} 158}
159 159
160/* Basic grammer example:
161 *
162 * input: expr '='
163 * ;
164 *
165 * expr: expr '+' expr
166 * | '(' expr ')'
167 * | NUMBER
168 * ;
169 *
170 * The problem is, that we can't actually make something left hand recursive,
171 * so we break it into two exprs:
172 *
173 * expr': '(' expr ')'
174 * | NUMBER
175 * ;
176 *
177 * expr: expr' expr''
178 * ;
179 *
180 * expr'': '+' expr
181 * |
182 * ;
183 *
184 * 5 + 5 + 5 =
185 */
186
160int main( int argc, char *argv[] ) 187int main( int argc, char *argv[] )
161{ 188{
162 File fIn( argv[1], File::Read ); 189 File fIn( argv[1], File::Read );
163 190
164 Parser p; 191 Parser p;
165 192
193 p.addNonTerminal("expr");
194 p.addNonTerminal("expr'");
195 p.addNonTerminal("expr''");
166 { 196 {
167 Parser::NonTerminal nt; 197 Parser::NonTerminal nt;
168 int iSelf = p.addNonTerminal("expr");
169 nt.addProduction( 198 nt.addProduction(
170 Parser::Production( 199 Parser::Production(
171 Parser::State( 200 Parser::State(
@@ -175,7 +204,7 @@ int main( int argc, char *argv[] )
175 ).append( 204 ).append(
176 Parser::State( 205 Parser::State(
177 Parser::State::typeNonTerminal, 206 Parser::State::typeNonTerminal,
178 iSelf 207 p.getNonTerminalId("expr")
179 ) 208 )
180 ).append( 209 ).append(
181 Parser::State( 210 Parser::State(
@@ -185,9 +214,11 @@ int main( int argc, char *argv[] )
185 ) 214 )
186 ); 215 );
187 nt.addProduction( 216 nt.addProduction(
188 Parser::Production() 217 Parser::Production(
218 )
189 ); 219 );
190 p.addNonTerminal( "expr", nt ); 220 nt.setCanSkip();
221 p.setNonTerminal("expr''", nt );
191 } 222 }
192 { 223 {
193 Parser::NonTerminal nt; 224 Parser::NonTerminal nt;
@@ -197,14 +228,26 @@ int main( int argc, char *argv[] )
197 Parser::State::typeTerminalPush, 228 Parser::State::typeTerminalPush,
198 tokNumber 229 tokNumber
199 ) 230 )
231 )
232 );
233 p.setNonTerminal("expr'", nt );
234 }
235 {
236 Parser::NonTerminal nt;
237 nt.addProduction(
238 Parser::Production(
239 Parser::State(
240 Parser::State::typeNonTerminal,
241 p.getNonTerminalId("expr'")
242 )
200 ).append( 243 ).append(
201 Parser::State( 244 Parser::State(
202 Parser::State::typeNonTerminal, 245 Parser::State::typeNonTerminal,
203 p.getNonTerminalId("expr") 246 p.getNonTerminalId("expr''")
204 ) 247 )
205 ) 248 )
206 ); 249 );
207 p.addNonTerminal( "expr'", nt ); 250 p.setNonTerminal("expr", nt );
208 } 251 }
209 { 252 {
210 Parser::NonTerminal nt; 253 Parser::NonTerminal nt;
@@ -212,7 +255,7 @@ int main( int argc, char *argv[] )
212 Parser::Production( 255 Parser::Production(
213 Parser::State( 256 Parser::State(
214 Parser::State::typeNonTerminal, 257 Parser::State::typeNonTerminal,
215 p.getNonTerminalId("expr'") 258 p.getNonTerminalId("expr")
216 ) 259 )
217 ).append( 260 ).append(
218 Parser::State( 261 Parser::State(