diff --git a/src/grammar/expenses.l b/src/grammar/expenses.l new file mode 100644 index 0000000..c3aa604 --- /dev/null +++ b/src/grammar/expenses.l @@ -0,0 +1,30 @@ +/* expenses.l: Expenses lexical analysis file +*/ + +%{ +#include +#include +#include "../../src/grammar/types.h" +#include "expenses.tab.h" +%} + +DIGIT [0-9] + +%% + +{DIGIT}+ yylval.res=atoi(yytext); return TOK_INT; +{DIGIT}+"."{DIGIT}* yylval.val=atof(yytext); return TOK_DOUBLE; +"-"|out|spesa yylval.cmd=BOT_CMD_OUT; return TOK_CMD_OUT; +"+"|in|ricavo yylval.cmd=BOT_CMD_IN; return TOK_CMD_IN; +del|storno yylval.cmd=BOT_CMD_DELETE; return TOK_CMD_DEL; +report yylval.cmd=BOT_CMD_REPORT; return TOK_CMD_REPORT; +daily yylval.tsp=BOT_TS_DAILY; return TOK_TS_DAILY; +weekly yylval.tsp=BOT_TS_WEEKLY; return TOK_TS_WEEKLY; +monthly yylval.tsp=BOT_TS_MONTHLY; return TOK_TS_MONTHLY; +yearly yylval.tsp=BOT_TS_YEARLY; return TOK_TS_YEARLY; +[a-zA-Z0-9_]+ yylval.str=g_string_new(yytext); return TOK_USER; /* user can't have special characters */ +\"[^\n]+\" yylval.str=g_string_new(yytext); return TOK_STR; /* anything excepts '\n'; needs to be at the bottom of flex rules here */ +[ \t]+ /* ignore whitespace */; +\n /* ignore end of line */; + +%% diff --git a/src/grammar/expenses.y b/src/grammar/expenses.y new file mode 100644 index 0000000..b1e0769 --- /dev/null +++ b/src/grammar/expenses.y @@ -0,0 +1,72 @@ +%{ +#include +#include /* For GString */ +#include "../../src/grammar/types.h" + +/* suppress warning about missing declarations */ +int yylex(); +int yyparse(); + +void yyerror(const char *str) +{ + fprintf(stderr,"error: %s\n",str); +} + +int yywrap() +{ + return 1; +} + +%} + +%define parse.lac full +%define parse.error verbose + +%union { + int res; /* return value of C procedure */ + double val; /* For returning numbers. */ + BotCommand cmd; + BotReportTimespan tsp; + GString *str; +} + +%token TOK_INT +%token TOK_DOUBLE +%token TOK_CMD_IN TOK_CMD_OUT TOK_CMD_DEL TOK_CMD_REPORT +%token TOK_TS_DAILY TOK_TS_WEEKLY TOK_TS_MONTHLY TOK_TS_YEARLY +%token TOK_USER /* string for user */ +%token TOK_STR /* string for notes */ +%type update; +%type value; +%type ts; +%type msg; + +%% +input: /* empty */ + | input line +; + +line: '\n' + | msg + | error { yyerrok; } +; + +msg: update value TOK_USER TOK_STR { printf("update: %d %f %s %s\n", $1, $2, $3->str, $4->str); $$ = 0;} + | TOK_CMD_DEL TOK_INT { printf("delete: %d\n", $2); $$ = 0; } + | TOK_CMD_REPORT ts { printf("report: %d\n", $2); $$ = 0; } + +value: TOK_INT { $$ = (double)$1; } + | TOK_DOUBLE + ; + +update: TOK_CMD_IN + | TOK_CMD_OUT +; + +ts: TOK_TS_DAILY + | TOK_TS_WEEKLY + | TOK_TS_MONTHLY + | TOK_TS_YEARLY +; +%% + diff --git a/src/grammar/types.h b/src/grammar/types.h new file mode 100644 index 0000000..716dd38 --- /dev/null +++ b/src/grammar/types.h @@ -0,0 +1,17 @@ +/* additional types for expenses + */ +#pragma once + +typedef enum BotCommand { + BOT_CMD_IN, + BOT_CMD_OUT, + BOT_CMD_DELETE, + BOT_CMD_REPORT +} BotCommand; + +typedef enum BotReportTimespan { + BOT_TS_DAILY, + BOT_TS_WEEKLY, + BOT_TS_MONTHLY, + BOT_TS_YEARLY +} BotReportTimespan;