Commit 0b62c3a7 authored by Martin Mareš's avatar Martin Mareš

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 :-)
parent c74c0e3c
Core Core
~~~~ ~~~~
* right usage of DBG vs. debug * right usage of DBG vs. debug
* cleanup debugging calls!
- TOS not supported by kernel -> automatically drop routes with TOS<>0 - TOS not supported by kernel -> automatically drop routes with TOS<>0
- use -freg-struct-return ? - use -freg-struct-return ?
- fake multipath? - fake multipath?
- replace all NUM's by expr's
- config file: symbolic constants? - config file: symbolic constants?
- counters (according to SNMP MIB?) - counters (according to SNMP MIB?)
- generation of subnet mask ICMP's for v6? - generation of subnet mask ICMP's for v6?
...@@ -14,7 +16,6 @@ Core ...@@ -14,7 +16,6 @@ Core
- ifdef out some debugging code? - ifdef out some debugging code?
- better memory allocators - better memory allocators
- default preferences of protocols: prefer BGP over OSPF/RIP external routes? - default preferences of protocols: prefer BGP over OSPF/RIP external routes?
- all internal tables are in host order
- secondary addresses -> subinterfaces - secondary addresses -> subinterfaces
- check if all protocols set proper packet priorities and TTL's. - check if all protocols set proper packet priorities and TTL's.
...@@ -37,8 +38,6 @@ Core ...@@ -37,8 +38,6 @@ Core
- Check incoming packets and log errors!! - Check incoming packets and log errors!!
- implement snprintf etc. and clean up debugging and logging functions
RIP RIP
~~~ ~~~
...@@ -51,7 +50,6 @@ RIP ...@@ -51,7 +50,6 @@ RIP
OSPF OSPF
~~~~ ~~~~
- Dijkstra: use Fibonacci heaps?
- point-to-point interface with address: advertise as stub network - point-to-point interface with address: advertise as stub network
- static routes: stub networks? - static routes: stub networks?
- modes: PtP, PtP-unnumbered, Broadcast, NBMA, point-to-multipoint - modes: PtP, PtP-unnumbered, Broadcast, NBMA, point-to-multipoint
......
...@@ -6,6 +6,8 @@ ...@@ -6,6 +6,8 @@
router id 62.168.0.1 router id 62.168.0.1
define xyzzy = 120+10;
protocol rip MyRIP_test { protocol rip MyRIP_test {
preference 130 preference xyzzy;
} }
...@@ -101,7 +101,7 @@ WHITE [ \t] ...@@ -101,7 +101,7 @@ WHITE [ \t]
return SYM; return SYM;
} }
[{}:;,] { [={}:;,()+*/%-] {
return yytext[0]; return yytext[0];
} }
...@@ -177,6 +177,7 @@ cf_find_sym(byte *c, unsigned int h0) ...@@ -177,6 +177,7 @@ cf_find_sym(byte *c, unsigned int h0)
sym_hash[h] = s; sym_hash[h] = s;
s->class = SYM_VOID; s->class = SYM_VOID;
s->def = NULL; s->def = NULL;
s->aux = 0;
strcpy(s->name, c); strcpy(s->name, c);
return s; return s;
} }
......
...@@ -21,12 +21,14 @@ extern int (*cf_read_hook)(byte *buf, unsigned int max); ...@@ -21,12 +21,14 @@ extern int (*cf_read_hook)(byte *buf, unsigned int max);
struct symbol { struct symbol {
struct symbol *next; struct symbol *next;
int class; int class;
int aux;
void *def; void *def;
char name[1]; char name[1];
}; };
#define SYM_VOID 0 #define SYM_VOID 0
#define SYM_PROTO 1 #define SYM_PROTO 1
#define SYM_NUMBER 2
void cf_lex_init_tables(void); void cf_lex_init_tables(void);
int cf_lex(void); int cf_lex(void);
......
...@@ -30,8 +30,17 @@ CF_DECLS ...@@ -30,8 +30,17 @@ CF_DECLS
%token <s> SYM %token <s> SYM
%token <t> TEXT %token <t> TEXT
%type <i> expr
%left '+' '-'
%left '*' '/' '%'
CF_KEYWORDS(DEFINE)
CF_GRAMMAR CF_GRAMMAR
/* Basic config file structure */
config: conf_entries END { config: conf_entries END {
return 0; return 0;
} }
...@@ -44,6 +53,28 @@ conf_entries: ...@@ -44,6 +53,28 @@ conf_entries:
CF_ADDTO(conf, /* EMPTY */) 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_CODE
CF_END CF_END
...@@ -54,9 +54,10 @@ proto_name: ...@@ -54,9 +54,10 @@ proto_name:
proto_item: proto_item:
/* EMPTY */ /* EMPTY */
| PREFERENCE NUM { | PREFERENCE expr {
if ($2 < 0 || $2 > 255) cf_error("Invalid preference"); if ($2 < 0 || $2 > 255) cf_error("Invalid preference");
this_proto->preference = $2; this_proto->preference = $2;
die("pref=%d", $2);
} }
; ;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment