From: ian Date: Sat, 9 Sep 2006 17:43:19 +0000 (+0000) Subject: starting on flex/bison parser for hostside X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ijackson/git?a=commitdiff_plain;h=89485d5a713aa4bbfb5cb20c9dc07ff411a34662;p=trains.git starting on flex/bison parser for hostside --- diff --git a/cprogs.make b/cprogs.make index e6fd70c..19d8d04 100644 --- a/cprogs.make +++ b/cprogs.make @@ -8,6 +8,12 @@ CFLAGS= $(CPPFLAGS) -D_GNU_SOURCE \ CPPFLAGS= LINK= $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $+ $(LIBS) +LEX=flex +BISON=bison + %.o: %.c $(AUTOINCS) $(CC) $(CFLAGS) -MM $< >$*.d $(CC) $(CFLAGS) -o $@ -c $< + +%.c: %.y + $(BISON) -o $@ $< diff --git a/hostside/.cvsignore b/hostside/.cvsignore index 7ef68e1..62750d5 100644 --- a/hostside/.cvsignore +++ b/hostside/.cvsignore @@ -11,3 +11,5 @@ gui-plan-bot selectors.h errorcodes.h retransmit-table.h +record-l.c +record-y.[ch] diff --git a/hostside/Makefile b/hostside/Makefile index b66c194..a718672 100644 --- a/hostside/Makefile +++ b/hostside/Makefile @@ -1,6 +1,7 @@ # -AUTOINCS= auproto-pic.h layoutinfo.h selectors.h retransmit-table.h +AUTOINCS= auproto-pic.h layoutinfo.h selectors.h retransmit-table.h \ + errorcodes.h TARGETS= hostside-old gui-plan-bot realtime include ../common.make @@ -54,6 +55,7 @@ safety: safety.o utils.o trackloc.o ../layout/ours.layout-data.o hostside: hostside.o serialio.o client.o obc.o commands.o utils.o \ nmra.o encode.o retransmit.o output.o auproto-pic.o \ parseutils.o \ + record-l.o record-y.o record.o \ -loop $(LINK) diff --git a/hostside/record-l.l b/hostside/record-l.l new file mode 100644 index 0000000..4a61e8d --- /dev/null +++ b/hostside/record-l.l @@ -0,0 +1,41 @@ +/* -*- fundamental -*- */ + +%{ +#include "y.tab.h" +%} + +%option warn +%option batch +%option noyywrap + +%option bison-locations +%option bison-bridge + +%{ +#define STR yylval->str= xstrdup(yytext); return +%} + +%% + +max-trains { yylval->str= 0; return MAXTRAINS; } + +train { STR TRAIN; } +seg { STR SEG; } +is { STR IS; } +at { STR AT; } +has { STR HAS; } +step { STR STEP; } +end { STR END; } + +[A-Za-z0-9_]+ { STR IDENT; } + +[0-9]{0,5} { yylval->num= strtoul(yytext,0,10); return NUM; } +[0-9]{6} { badrecord("number too long"); } + +[-+:=~$/] { yylval->str= 0; return yytext[0]; } + +\#.*\n|\ { yylval->str= 0; return NL; } + +[ \t] { } + +. { badrecord("lexically invalid input"); } diff --git a/hostside/record-y.y b/hostside/record-y.y new file mode 100644 index 0000000..3f8f769 --- /dev/null +++ b/hostside/record-y.y @@ -0,0 +1,56 @@ +/* -*- fundamental -*- */ + +%{ +#include "realtime.h" +%} + +%union { + char *name; + Train *train; + Segment *seg; + int num; +} + +%token TRAIN SEG IS AT HAS STEP END IDENT +%token MAXTRAINS NL +%token NUM + +%type ident +%type train +%type seg +%type sign + +%defines + +%% + +file: end + | line NL file + +line: /* empty */ + | MAXTRAINS NUM + { record_max_trains($2); } + | TRAIN train AT sign SEG ':' NUM '+' '-' NUM + { record_train_at($2,$4,$5,$7,$10); } + | TRAIN train IS NUM NUM '+' NUM '+' NUM + { record_train_is($2,$4,$5,$7,$9); } + | TRAIN train STEP NUM '=' NUM NUM '/' NUM + { record_train_step($2,$4,$6,$7,$9); } + | SEG seg HAS sign train + { record_seg_has($2,$4,$5); } + | SEG seg HAS '$' + { record_seg_has($2,0,0); } + | SEG seg AT NUM + { record_seg_at($2,$4); } + +sign: /* empty */ { return 1; } + | '-' { return -1; } + +ident: IDENT | TRAIN | SEG | IS | AT | STEP | END + +seg: ident { $$= pname2seg($1); } +train: ident { $$= pname2train($1); } + +end: END NL + +%% diff --git a/hostside/record.c b/hostside/record.c index b025692..429cee2 100644 --- a/hostside/record.c +++ b/hostside/record.c @@ -3,9 +3,11 @@ * * File format: * + * max-trains num * train at [-]:+- - * seg has [-]|$ - * train is # ++ + * train is ++ * train step = / + * seg has [-]|$ + * seg at */