[Assorted-commits] SF.net SVN: assorted: [846] sandbox/trunk/src
Brought to you by:
yangzhang
From: <yan...@us...> - 2008-06-03 01:50:44
|
Revision: 846 http://assorted.svn.sourceforge.net/assorted/?rev=846&view=rev Author: yangzhang Date: 2008-06-02 18:50:46 -0700 (Mon, 02 Jun 2008) Log Message: ----------- going through flex/bison tutorial Added Paths: ----------- sandbox/trunk/src/flex/ sandbox/trunk/src/flex/ex1/ sandbox/trunk/src/flex/ex1/Makefile sandbox/trunk/src/flex/ex1/ex1.l sandbox/trunk/src/flex/ex2/ sandbox/trunk/src/flex/ex2/ex2.l sandbox/trunk/src/flex/ex3/ sandbox/trunk/src/flex/ex3/ex3.l sandbox/trunk/src/flex/ex4/ sandbox/trunk/src/flex/ex4/Makefile sandbox/trunk/src/flex/ex4/ex4.l sandbox/trunk/src/flex/ex4/ex4.y sandbox/trunk/src/flex/ex5/ sandbox/trunk/src/flex/ex5/Makefile sandbox/trunk/src/flex/ex5/ex5.l sandbox/trunk/src/flex/ex5/ex5.y Added: sandbox/trunk/src/flex/ex1/Makefile =================================================================== --- sandbox/trunk/src/flex/ex1/Makefile (rev 0) +++ sandbox/trunk/src/flex/ex1/Makefile 2008-06-03 01:50:46 UTC (rev 846) @@ -0,0 +1,8 @@ +all: ex1 + +%: %.yy.c + gcc -Wall -o $@ $< -ll + # libl contains a `main` function. + +%.yy.c: %.l + flex -o $@ $< Added: sandbox/trunk/src/flex/ex1/ex1.l =================================================================== --- sandbox/trunk/src/flex/ex1/ex1.l (rev 0) +++ sandbox/trunk/src/flex/ex1/ex1.l 2008-06-03 01:50:46 UTC (rev 846) @@ -0,0 +1,8 @@ +%{ +#include <stdio.h> +%} + +%% +stop printf("Stop command received\n"); +start printf("Start command received\n"); +%% Added: sandbox/trunk/src/flex/ex2/ex2.l =================================================================== --- sandbox/trunk/src/flex/ex2/ex2.l (rev 0) +++ sandbox/trunk/src/flex/ex2/ex2.l 2008-06-03 01:50:46 UTC (rev 846) @@ -0,0 +1,8 @@ +%{ +#include <stdio.h> +%} + +%% +[0123456789]+ printf("NUMBER\n"); +[a-zA-Z][a-zA-Z0-9]* printf("WORD\n"); +%% Added: sandbox/trunk/src/flex/ex3/ex3.l =================================================================== --- sandbox/trunk/src/flex/ex3/ex3.l (rev 0) +++ sandbox/trunk/src/flex/ex3/ex3.l 2008-06-03 01:50:46 UTC (rev 846) @@ -0,0 +1,14 @@ +%{ +#include <stdio.h> +%} + +%% +[a-zA-Z][a-zA-Z0-9]* printf("WORD "); +[a-zA-Z0-9\/.-]+ printf("FILENAME "); +\" printf("QUOTE "); +\{ printf("OBRACE "); +\} printf("EBRACE "); +; printf("SEMICOLON "); +\n printf("\n"); +[ \t]+ /* ignore whitespace */; +%% Added: sandbox/trunk/src/flex/ex4/Makefile =================================================================== --- sandbox/trunk/src/flex/ex4/Makefile (rev 0) +++ sandbox/trunk/src/flex/ex4/Makefile 2008-06-03 01:50:46 UTC (rev 846) @@ -0,0 +1,13 @@ +all: ex4 + +%: %.tab.c %.yy.c + gcc -Wall -o $@ $^ + +%.yy.c: %.l %.tab.h + flex -o $@ $< + +%.tab.c: %.y + bison -d $< + +%.tab.h: %.y + bison -d $< Added: sandbox/trunk/src/flex/ex4/ex4.l =================================================================== --- sandbox/trunk/src/flex/ex4/ex4.l (rev 0) +++ sandbox/trunk/src/flex/ex4/ex4.l 2008-06-03 01:50:46 UTC (rev 846) @@ -0,0 +1,14 @@ +%{ +#include <stdio.h> +#include "ex4.tab.h" +%} +%option nounput +%% +[0-9]+ return NUMBER; +heat return TOKHEAT; +on|off return STATE; +target return TOKTARGET; +temperature return TOKTEMPERATURE; +\n /* ignore end of line */; +[ \t]+ /* ignore whitespace */; +%% Added: sandbox/trunk/src/flex/ex4/ex4.y =================================================================== --- sandbox/trunk/src/flex/ex4/ex4.y (rev 0) +++ sandbox/trunk/src/flex/ex4/ex4.y 2008-06-03 01:50:46 UTC (rev 846) @@ -0,0 +1,53 @@ +%{ +#include <stdio.h> +#include <string.h> + +int yylex(void); + +void yyerror(const char *str) +{ + fprintf(stderr,"error: %s\n",str); +} + +int yywrap() +{ + return 1; +} + +%} + +%token NUMBER TOKHEAT STATE TOKTARGET TOKTEMPERATURE + +%% + +commands: /* empty */ + | commands command + ; + +command: + heat_switch + | + target_set + ; + +heat_switch: + TOKHEAT STATE + { + printf("\tHeat turned on or off\n"); + } + ; + +target_set: + TOKTARGET TOKTEMPERATURE NUMBER + { + printf("\tTemperature set\n"); + } + ; + +%% + +int main() +{ + yyparse(); + return 0; +} Added: sandbox/trunk/src/flex/ex5/Makefile =================================================================== --- sandbox/trunk/src/flex/ex5/Makefile (rev 0) +++ sandbox/trunk/src/flex/ex5/Makefile 2008-06-03 01:50:46 UTC (rev 846) @@ -0,0 +1,13 @@ +all: ex5 + +%: %.tab.c %.yy.c + gcc -Wall -o $@ $^ + +%.yy.c: %.l %.tab.h + flex -o $@ $< + +%.tab.c: %.y + bison -d $< + +%.tab.h: %.y + bison -d $< Added: sandbox/trunk/src/flex/ex5/ex5.l =================================================================== --- sandbox/trunk/src/flex/ex5/ex5.l (rev 0) +++ sandbox/trunk/src/flex/ex5/ex5.l 2008-06-03 01:50:46 UTC (rev 846) @@ -0,0 +1,14 @@ +%{ +#include <stdio.h> +#include "ex5.tab.h" +%} +%option nounput +%% +[0-9]+ yylval=atoi(yytext); return NUMBER; +heat return TOKHEAT; +on|off yylval=!strcmp(yytext,"on"); return STATE; +target return TOKTARGET; +temperature return TOKTEMPERATURE; +\n /* ignore end of line */; +[ \t]+ /* ignore whitespace */; +%% Added: sandbox/trunk/src/flex/ex5/ex5.y =================================================================== --- sandbox/trunk/src/flex/ex5/ex5.y (rev 0) +++ sandbox/trunk/src/flex/ex5/ex5.y 2008-06-03 01:50:46 UTC (rev 846) @@ -0,0 +1,53 @@ +%{ +#include <stdio.h> +#include <string.h> + +int yylex(void); + +void yyerror(const char *str) +{ + fprintf(stderr,"error: %s\n",str); +} + +int yywrap() +{ + return 1; +} + +%} + +%token NUMBER TOKHEAT STATE TOKTARGET TOKTEMPERATURE + +%% + +commands: /* empty */ + | commands command + ; + +command: + heat_switch + | + target_set + ; + +heat_switch: + TOKHEAT STATE + { + printf("\tHeat turned %s\n", $2 ? "on" : "off"); + } + ; + +target_set: + TOKTARGET TOKTEMPERATURE NUMBER + { + printf("\tTemperature set to %d\n", $3); + } + ; + +%% + +int main() +{ + yyparse(); + return 0; +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |