From 2abb355a18934d72006e2958cf79fae1b18868ca Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Fri, 16 Dec 2011 00:25:48 -0700 Subject: It's coming along. I'm still debating how the whole thing should work... --- src/parser.l | 51 ++++++++++++++++++++++++++++++++++----------------- src/parser.y | 49 +++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 79 insertions(+), 21 deletions(-) diff --git a/src/parser.l b/src/parser.l index 59a0358..cbf359c 100644 --- a/src/parser.l +++ b/src/parser.l @@ -2,20 +2,28 @@ #include #include #include "parser.tab.h" + +//#define YY_DECL int yylex( YYSTYPE *yylval, struct YYLTYPE *llocp, yyscan_t yyscanner ) + +//YY_DECL; + +# define YY_USER_ACTION yylloc->last_column += yyleng; %} -%option noyywrap +%option reentrant noyywrap nounput batch +%option bison-bridge bison-locations %x sitname %x comment %x dqstr tdqstr tsqstr %% -[-{}<>=+/*,();:] { return yytext[0]; } +[-{}<>=+/*,();:.] { return yytext[0]; } game { return tokGame; } function { return tokFunction; } situation { return tokSituation; } +player { return tokPlayer; } while { return tokWhile; } for { return tokFor; } each { return tokEach; } @@ -27,12 +35,12 @@ command { return tokCommand; } goto { return tokGoto; } not { return tokNot; } -true { yylval.bValue = true; return tokBool; } -false { yylval.bValue = false; return tokBool; } +true { yylval->bValue = true; return tokBool; } +false { yylval->bValue = false; return tokBool; } null { return tokNull; } "<<" { BEGIN( sitname ); } -[- a-zA-Z0-9]+ { yylval.sValue = new Bu::String( yytext ); return tokSituationName; } +[- a-zA-Z0-9]+ { yylval->sValue = new Bu::String( yytext ); return tokSituationName; } ">>" { BEGIN( INITIAL ); } . REJECT; @@ -42,32 +50,41 @@ null { return tokNull; } "\*/" { BEGIN( INITIAL ); } . {} -[a-zA-Z_][a-zA-Z0-9_]* { yylval.sValue = new Bu::String( yytext ); return tokIdent; } +[a-zA-Z_][a-zA-Z0-9_]* { yylval->sValue = new Bu::String( yytext ); return tokIdent; } [1-9][0-9]* { - yylval.iValue = strtoll( yytext, NULL, 10 ); + yylval->iValue = strtoll( yytext, NULL, 10 ); return tokInt; } ([1-9][0-9]*)?\.[0-9]* { - yylval.dValue = strtod( yytext, NULL ); + yylval->dValue = strtod( yytext, NULL ); return tokFloat; } -\"\"\" { BEGIN( tdqstr ); yylval.sValue = new Bu::String(); } -[^"]+ { (*yylval.sValue) += yytext; } -\" { (*yylval.sValue) += yytext; } +\"\"\" { BEGIN( tdqstr ); yylval->sValue = new Bu::String(); } +[^"]+ { (*yylval->sValue) += yytext; } +\" { (*yylval->sValue) += yytext; } \"\"\" { BEGIN( INITIAL ); return tokString; } -\'\'\' { BEGIN( tsqstr ); yylval.sValue = new Bu::String(); } -[^']+ { (*yylval.sValue) += yytext; } -\' { (*yylval.sValue) += yytext; } +\'\'\' { BEGIN( tsqstr ); yylval->sValue = new Bu::String(); } +[^']+ { (*yylval->sValue) += yytext; } +\' { (*yylval->sValue) += yytext; } \'\'\' { BEGIN( INITIAL ); return tokString; } -\" { BEGIN( dqstr ); yylval.sValue = new Bu::String(); } -[^"]+ { (*yylval.sValue) += yytext; } +\" { BEGIN( dqstr ); yylval->sValue = new Bu::String(); } +[^"]+ { (*yylval->sValue) += yytext; } \" { BEGIN( INITIAL ); return tokString; } -[ \t\n\r]+ {} +[ \t]+ { + yylloc->first_line = yylloc->last_line; + yylloc->first_column = yylloc->last_column+1; +} + +\n+ { + yylloc->last_column = yylloc->first_column = 0; + yylloc->last_line+= yyleng; + yylloc->first_line = yylloc->last_line; +} %% diff --git a/src/parser.y b/src/parser.y index 7242df0..e2f8b6d 100644 --- a/src/parser.y +++ b/src/parser.y @@ -3,10 +3,19 @@ #include #include -int yylex(); -void yyerror( const char *error ) { printf("%s\n", error ); } +typedef void *yyscan_t; + %} + +%locations +%define api.pure +%debug +%error-verbose + +%parse-param { yyscan_t yyscanner } +%lex-param { yysacn_t yyscanner } + %union { int64_t iValue; double dValue; @@ -14,6 +23,17 @@ void yyerror( const char *error ) { printf("%s\n", error ); } bool bValue; } +%{ +int yylex( YYSTYPE *yylval, struct YYLTYPE *llocp, yyscan_t yyscanner ); +void yyerror( YYLTYPE *llocp, yyscan_t yyscanner, const char *error ) +{ + printf("%d:%d-%d:%d: %s\n", + llocp->first_line, llocp->first_column, + llocp->last_line, llocp->last_column, error + ); +} +%} + %token tokGame %token tokFunction %token tokSituation @@ -26,6 +46,7 @@ void yyerror( const char *error ) { printf("%s\n", error ); } %token tokElse %token tokNot %token tokCommand +%token tokPlayer %token tokSituationName %token tokIdent %token tokGoto @@ -39,6 +60,8 @@ void yyerror( const char *error ) { printf("%s\n", error ); } %token eos 0 "end of stream" +%right '=' +%left tokIn %left '(' ')' '[' ']' %left '*' '/' %left '-' '+' @@ -78,24 +101,35 @@ cmpltExprList: cmpltExpr: expr ';' | tokGoto '(' expr ')' ';' + | tokIf expr tokThen '{' cmpltExprList '}' ifnext ; +ifnext: + | tokElse '{' cmpltExprList '}' + | tokElse tokIf '{' cmpltExprList '}' ifnext + ; + expr: tokInt | tokFloat | tokString | tokBool | tokNull + | tokIdent | tokSituationName + | tokSituation '.' tokIdent + | tokPlayer '.' tokIdent | expr '+' expr | expr '-' expr | expr '/' expr | expr '*' expr + | expr tokIn expr | '(' expr ')' | expr '[' expr ']' | '[' ']' | '[' listValues ']' | '{' '}' | '{' dictValues '}' +/* | tokNot expr */ ; listValues: expr @@ -106,7 +140,7 @@ dictValues: expr ':' expr | dictValues ',' expr ':' expr ; -commandDecl: tokCommand ':' tokString commandParamList '{' '}' +commandDecl: tokCommand ':' tokString commandParamList '{' cmpltExprList '}' ; commandParamList: @@ -115,9 +149,16 @@ commandParamList: ; %% +void yylex_init( yyscan_t * ); +void yylex_destroy( yyscan_t ); + int main() { - yyparse(); + yyscan_t scanner; + + yylex_init ( &scanner ); + yyparse( scanner ); + yylex_destroy ( scanner ); return 0; } -- cgit v1.2.3