From 00ecd458ced768b3b8752cdd421a22244b7adc01 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Sun, 17 Oct 2010 05:50:57 +0000 Subject: Hey! The parser parses now! It's actually a little stupid, I didn't implement lookahead or precedence, but I should be able to do that easily with the next version. I'm treating this more as a proof of concept than a real working model. Although it can handle +, -, (), and = :) --- src/parser.cpp | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) (limited to 'src/parser.cpp') diff --git a/src/parser.cpp b/src/parser.cpp index e4e8ae5..3ce73b6 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -22,16 +22,25 @@ void Bu::Parser::popLexer() delete sLexer.peekPop(); } +Lexer::Token *Bu::Parser::popToken() +{ + return sToken.peekPop(); +} + +void Bu::Parser::pushToken( Lexer::Token *pTok ) +{ + sToken.push( pTok ); +} + void Bu::Parser::parse() { int iCurNt = iRootNonTerminal; Lexer::Token *ptCur = sLexer.peek()->nextToken(); - sio << "Token: " << sLexer.peek()->tokenToString( *ptCur ) << sio.nl; + sio << "Token(a): " << sLexer.peek()->tokenToString( *ptCur ) << sio.nl; selectProduction( iCurNt, ptCur ); while( !sState.isEmpty() ) { - sio << "Currently: " << *sState.peek() << sio.nl; switch( (*sState.peek()).eType ) { case State::typeTerminal: @@ -42,7 +51,7 @@ void Bu::Parser::parse() advanceState(); delete ptCur; ptCur = sLexer.peek()->nextToken(); - sio << "Token: " << sLexer.peek()->tokenToString( *ptCur ) << sio.nl; + sio << "Token(b): " << sLexer.peek()->tokenToString( *ptCur ) << sio.nl; } else { @@ -59,7 +68,7 @@ void Bu::Parser::parse() sToken.push( ptCur ); ptCur = sLexer.peek()->nextToken(); - sio << "Token: " << sLexer.peek()->tokenToString( *ptCur ) << sio.nl; + sio << "Token(c): " << sLexer.peek()->tokenToString( *ptCur ) << sio.nl; } else { @@ -72,7 +81,7 @@ void Bu::Parser::parse() << (*sState.peek()).iIndex << sio.nl; { int iNt = (*sState.peek()).iIndex; - advanceState(); + sio << "Current state: " << *sState.peek() << sio.nl; if( !selectProduction( iNt, ptCur ) ) { throw Bu::ExceptionBase("Error parsing code."); @@ -132,7 +141,11 @@ bool Bu::Parser::selectProduction( int iNt, Lexer::Token *ptCur ) } } if( nt.bCanSkip ) + { + sio << "Nothing matches, skipping non-terminal." << sio.nl; + advanceState(); return true; + } sio << "-->(Found nothing)" << sio.nl; return false; } @@ -148,6 +161,7 @@ void Bu::Parser::advanceState() sio.decIndent(); sState.pop(); sio << "State advanced, End of production." << sio.nl; + advanceState(); return; } sio << "State advanced, now: " << *(sState.peek()) << sio.nl; -- cgit v1.2.3