From 4d5d24f1da11a1279da0b5ea9351c4372a74bc43 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Sun, 11 Dec 2011 10:01:52 -0700 Subject: Basic parser coming together. --- src/parser.l | 43 ++++++++++++++++++++++++++++++++ src/parser.y | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 124 insertions(+) create mode 100644 src/parser.l create mode 100644 src/parser.y (limited to 'src') diff --git a/src/parser.l b/src/parser.l new file mode 100644 index 0000000..d5ff735 --- /dev/null +++ b/src/parser.l @@ -0,0 +1,43 @@ +%{ +#include +#include "parser.tab.h" +%} + +%option noyywrap + +%x sitname +%x comment +%x tdqstr tsqstr +%% + +[-{}<>=+/*,();] { return yytext[0]; } + +game { return tokGame; } +function { return tokFunction; } +situation { return tokSituation; } +while { return tokWhile; } +for { return tokFor; } +each { return tokEach; } +in { return tokIn; } +if { return tokIf; } +then { return tokThen; } +else { return tokElse; } +command { return tokCommand; } +goto { return tokGoto; } + +"<<" { BEGIN( sitname ); } +[- a-zA-Z0-9]+ { printf("situation: %s\n", yytext ); return tokSituationName; } +">>" { BEGIN( INITIAL ); } +. REJECT; + +"//"[^\n]* {} + +"/\*" { BEGIN( comment ); } +"\*/" { BEGIN( INITIAL ); } +. {} + +[a-zA-Z_][a-zA-Z0-9_]* { return tokIdent; } + +[ \t\n\r]+ {} + +%% diff --git a/src/parser.y b/src/parser.y new file mode 100644 index 0000000..b3da848 --- /dev/null +++ b/src/parser.y @@ -0,0 +1,81 @@ +%{ +#include +#include + +int yylex(); +void yyerror( const char *error ) { printf("%s\n", error ); } +%} + +%union { + int64_t iValue; + double dValue; + char *sValue; +} + +%token tokGame +%token tokFunction +%token tokSituation +%token tokWhile +%token tokFor +%token tokEach +%token tokIn +%token tokIf +%token tokThen +%token tokElse +%token tokCommand +%token tokSituationName +%token tokIdent +%token tokGoto +%token tokString +%token tokInt +%token tokFloat +%token tokBool +%token tokNull + +%token eos 0 "end of stream" + +%left '(' ')' '[' ']' +%left '*' '/' +%left '-' '+' + +%% +input: + | input situation + | input function + ; + +situation: tokSituation tokSituationName '{' cmpltExprList '}' + ; + +function: tokFunction tokIdent '(' ')' '{' '}' + ; + +cmpltExprList: + | cmpltExprList cmpltExpr + ; + +cmpltExpr: expr ';' + ; + +expr: tokInt + | tokFloat + | tokString + | tokBool + | tokNull + | tokSituationName + | expr '+' expr + | expr '-' expr + | expr '/' expr + | expr '*' expr + | '(' expr ')' + | expr '[' expr ']' + | tokGoto '(' expr ')' + ; +%% + +int main() +{ + yyparse(); + return 0; +} + -- cgit v1.2.3