%{ #include #include #include 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; Bu::String *sValue; 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 tokGlobal %token tokFunction %token tokSituation %token tokSetup %token tokEnter %token tokWhile %token tokFor %token tokEach %token tokIn %token tokIf %token tokThen %token tokElse %token tokNot %token tokCommand %token tokPlayer %token tokLtEq %token tokGtEq %token tokCmp %token tokPlusAssign %token tokMinusAssign %token tokTimesAssign %token tokDivideAssign %token tokSituationName %token tokIdent %token tokGoto %token tokString %token tokInt %token tokFloat %token tokBool %token tokNull %destructor { delete $$; printf("string deleted.\n"); } tokString tokSituationName %token eos 0 "end of stream" %right tokNot %right '=' tokPlusAssign tokMinusAssign tokTimesAssign tokDivideAssign %right tokLtEq tokGtEq tokCmp %left tokIn %left '(' ')' '[' ']' %left '*' '/' %left '-' '+' %% input: gameDecls globalDecl bodyDecl ; gameDecls: | gameDecls tokGame '.' tokIdent '=' literal ';' ; globalDecl: tokGlobal '{' globalExprList '}' ; globalExprList: | globalExprList cmpltGlobalExpr ; cmpltGlobalExpr: globalExpr ';' | commandDecl ; globalExpr: tokIdent '=' expr ; bodyDecl: | bodyDecl situation | bodyDecl function ; situation: tokSituation tokSituationName { printf("Read situtaion: %s\n", (*$2).getStr() ); } '{' situationMembers '}' ; situationMembers: | situationMembers situationModeFunc | situationMembers commandDecl ; situationModeFunc: situationMode '{' cmpltExprList '}' ; situationMode: tokSetup | tokEnter ; function: tokFunction tokIdent '(' ')' '{' '}' ; cmpltExprList: | cmpltExprList cmpltExpr ; cmpltExpr: expr ';' | tokGoto '(' expr ')' ';' | tokIf expr tokThen '{' cmpltExprList '}' ifnext ; ifnext: | tokElse '{' cmpltExprList '}' | tokElse tokIf '{' cmpltExprList '}' ifnext ; varRef: tokIdent | tokPlayer '.' tokIdent | tokGlobal '.' tokIdent | tokSituation '.' tokIdent ; literal: tokInt | tokFloat | tokString | tokBool | tokNull | tokSituationName ; expr: literal | tokIdent '(' listValues ')' | varRef | varRef '=' expr | varRef tokPlusAssign expr | varRef tokMinusAssign expr | varRef tokTimesAssign expr | varRef tokDivideAssign expr | expr '+' expr | expr '-' expr | expr '/' expr | expr '*' expr | expr tokIn expr | '(' expr ')' | expr '[' expr ']' | '[' ']' | '[' listValues ']' | '{' '}' | '{' dictValues '}' | tokNot expr ; listValues: expr | listValues ',' expr ; dictValues: expr ':' expr | dictValues ',' expr ':' expr ; commandDecl: tokCommand ':' tokString commandParamList '{' cmpltExprList '}' ; commandParamList: | commandParamList tokString | commandParamList tokIdent ; %% void yylex_init( yyscan_t * ); void yylex_destroy( yyscan_t ); int main() { yyscan_t scanner; yylex_init ( &scanner ); yyparse( scanner ); yylex_destroy ( scanner ); return 0; }