From 0b62c3a7c7548cd447b4f18e0346cc9e74862ab3 Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Fri, 27 Nov 1998 21:32:45 +0000 Subject: Trivial 15-line bison excercise: Implemented expressions including user-defined numeric symbols. Whenever possible, use `expr' instead of `NUM' to get full express ion power :-) --- conf/cf-lex.l | 3 ++- conf/conf.h | 2 ++ conf/confbase.Y | 31 +++++++++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) (limited to 'conf') diff --git a/conf/cf-lex.l b/conf/cf-lex.l index f653bca..66f3038 100644 --- a/conf/cf-lex.l +++ b/conf/cf-lex.l @@ -101,7 +101,7 @@ WHITE [ \t] return SYM; } -[{}:;,] { +[={}:;,()+*/%-] { return yytext[0]; } @@ -177,6 +177,7 @@ cf_find_sym(byte *c, unsigned int h0) sym_hash[h] = s; s->class = SYM_VOID; s->def = NULL; + s->aux = 0; strcpy(s->name, c); return s; } diff --git a/conf/conf.h b/conf/conf.h index 0853ae8..6a0a328 100644 --- a/conf/conf.h +++ b/conf/conf.h @@ -21,12 +21,14 @@ extern int (*cf_read_hook)(byte *buf, unsigned int max); struct symbol { struct symbol *next; int class; + int aux; void *def; char name[1]; }; #define SYM_VOID 0 #define SYM_PROTO 1 +#define SYM_NUMBER 2 void cf_lex_init_tables(void); int cf_lex(void); diff --git a/conf/confbase.Y b/conf/confbase.Y index b20986a..db6d021 100644 --- a/conf/confbase.Y +++ b/conf/confbase.Y @@ -30,8 +30,17 @@ CF_DECLS %token SYM %token TEXT +%type expr + +%left '+' '-' +%left '*' '/' '%' + +CF_KEYWORDS(DEFINE) + CF_GRAMMAR +/* Basic config file structure */ + config: conf_entries END { return 0; } @@ -44,6 +53,28 @@ conf_entries: CF_ADDTO(conf, /* EMPTY */) +/* Expressions */ + +expr: + NUM + | expr '+' expr { $$ = $1 + $3; } + | expr '-' expr { $$ = $1 - $3; } + | expr '*' expr { $$ = $1 * $3; } + | expr '/' expr { if ($3) $$ = $1 / $3; else cf_error("Division by zero"); } + | expr '%' expr { if ($3) $$ = $1 % $3; else cf_error("Division by zero"); } + | '(' expr ')' { $$ = $2; } + | SYM { if ($1->class != SYM_NUMBER) cf_error("Number expected"); else $$ = $1->aux; } + ; + +CF_ADDTO(conf, definition) +definition: + DEFINE SYM '=' expr { + if ($2->class != SYM_VOID) cf_error("Symbol already defined"); + $2->class = SYM_NUMBER; + $2->aux = $4; + } + ; + CF_CODE CF_END -- cgit v1.2.3