From fc16fc104a038146c8ab6c2e9fad38e18663a09f Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Tue, 12 Oct 2010 18:10:47 +0000 Subject: It's getting close. I'm not 100% sure abouth this method yet... --- src/tools/parser.cpp | 57 +++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 50 insertions(+), 7 deletions(-) (limited to 'src/tools') 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 ) { } +/* Basic grammer example: + * + * input: expr '=' + * ; + * + * expr: expr '+' expr + * | '(' expr ')' + * | NUMBER + * ; + * + * The problem is, that we can't actually make something left hand recursive, + * so we break it into two exprs: + * + * expr': '(' expr ')' + * | NUMBER + * ; + * + * expr: expr' expr'' + * ; + * + * expr'': '+' expr + * | + * ; + * + * 5 + 5 + 5 = + */ + int main( int argc, char *argv[] ) { File fIn( argv[1], File::Read ); Parser p; + p.addNonTerminal("expr"); + p.addNonTerminal("expr'"); + p.addNonTerminal("expr''"); { Parser::NonTerminal nt; - int iSelf = p.addNonTerminal("expr"); nt.addProduction( Parser::Production( Parser::State( @@ -175,7 +204,7 @@ int main( int argc, char *argv[] ) ).append( Parser::State( Parser::State::typeNonTerminal, - iSelf + p.getNonTerminalId("expr") ) ).append( Parser::State( @@ -185,9 +214,11 @@ int main( int argc, char *argv[] ) ) ); nt.addProduction( - Parser::Production() + Parser::Production( + ) ); - p.addNonTerminal( "expr", nt ); + nt.setCanSkip(); + p.setNonTerminal("expr''", nt ); } { Parser::NonTerminal nt; @@ -197,14 +228,26 @@ int main( int argc, char *argv[] ) Parser::State::typeTerminalPush, tokNumber ) + ) + ); + p.setNonTerminal("expr'", nt ); + } + { + Parser::NonTerminal nt; + nt.addProduction( + Parser::Production( + Parser::State( + Parser::State::typeNonTerminal, + p.getNonTerminalId("expr'") + ) ).append( Parser::State( Parser::State::typeNonTerminal, - p.getNonTerminalId("expr") + p.getNonTerminalId("expr''") ) ) ); - p.addNonTerminal( "expr'", nt ); + p.setNonTerminal("expr", nt ); } { Parser::NonTerminal nt; @@ -212,7 +255,7 @@ int main( int argc, char *argv[] ) Parser::Production( Parser::State( Parser::State::typeNonTerminal, - p.getNonTerminalId("expr'") + p.getNonTerminalId("expr") ) ).append( Parser::State( -- cgit v1.2.3