[Assorted-commits] SF.net SVN: assorted: [845] sandbox/trunk/src
Brought to you by:
yangzhang
From: <yan...@us...> - 2008-06-02 20:37:48
|
Revision: 845 http://assorted.svn.sourceforge.net/assorted/?rev=845&view=rev Author: yangzhang Date: 2008-06-02 13:37:15 -0700 (Mon, 02 Jun 2008) Log Message: ----------- playing with bison Added Paths: ----------- sandbox/trunk/src/bison/ sandbox/trunk/src/bison/calc/ sandbox/trunk/src/bison/calc/Makefile sandbox/trunk/src/bison/calc/calc.lex sandbox/trunk/src/bison/calc/calc.y sandbox/trunk/src/bison/calc/heading.h sandbox/trunk/src/bison/calc/main.cc sandbox/trunk/src/bison/calc/url Added: sandbox/trunk/src/bison/calc/Makefile =================================================================== --- sandbox/trunk/src/bison/calc/Makefile (rev 0) +++ sandbox/trunk/src/bison/calc/Makefile 2008-06-02 20:37:15 UTC (rev 845) @@ -0,0 +1,34 @@ +# Makefile + +OBJS = bison.o lex.o main.o + +CC = g++ +CFLAGS = -g -Wall -ansi -pedantic + +calc: $(OBJS) + $(CC) $(CFLAGS) $(OBJS) -o calc -lfl + +lex.o: lex.c + $(CC) $(CFLAGS) -c lex.c -o lex.o + +lex.c: calc.lex + flex calc.lex + cp lex.yy.c lex.c + +bison.o: bison.c + $(CC) $(CFLAGS) -c bison.c -o bison.o + +bison.c: calc.y + bison -d -v calc.y + cp calc.tab.c bison.c + cmp -s calc.tab.h tok.h || cp calc.tab.h tok.h + +main.o: main.cc + $(CC) $(CFLAGS) -c main.cc -o main.o + +lex.o yac.o main.o : heading.h +lex.o main.o : tok.h + +clean: + rm -f *.o *~ lex.c lex.yy.c bison.c tok.h calc.tab.c calc.tab.h calc.output calc + Added: sandbox/trunk/src/bison/calc/calc.lex =================================================================== --- sandbox/trunk/src/bison/calc/calc.lex (rev 0) +++ sandbox/trunk/src/bison/calc/calc.lex 2008-06-02 20:37:15 UTC (rev 845) @@ -0,0 +1,24 @@ +/* Mini Calculator */ +/* calc.lex */ + +%{ +#include "heading.h" +#include "tok.h" +int yyerror(char *s); +int yylineno = 1; +%} + +digit [0-9] +int_const {digit}+ + +%% + +{int_const} { yylval.int_val = atoi(yytext); return INTEGER_LITERAL; } +"+" { yylval.op_val = new std::string(yytext); return PLUS; } +"*" { yylval.op_val = new std::string(yytext); return MULT; } + +[ \t]* {} +[\n] { yylineno++; } + +. { std::cerr << "SCANNER "; yyerror(""); exit(1); } + Added: sandbox/trunk/src/bison/calc/calc.y =================================================================== --- sandbox/trunk/src/bison/calc/calc.y (rev 0) +++ sandbox/trunk/src/bison/calc/calc.y 2008-06-02 20:37:15 UTC (rev 845) @@ -0,0 +1,50 @@ +/* Mini Calculator */ +/* calc.y */ + +%{ +#include "heading.h" +int yyerror(char *s); +int yylex(void); +%} + +%union{ + int int_val; + string* op_val; +} + +%start input + +%token <int_val> INTEGER_LITERAL +%type <int_val> exp +%left PLUS +%left MULT + +%% + +input: /* empty */ + | exp { cout << "Result: " << $1 << endl; } + ; + +exp: INTEGER_LITERAL { $$ = $1; } + | exp PLUS exp { $$ = $1 + $3; } + | exp MULT exp { $$ = $1 * $3; } + ; + +%% + +int yyerror(string s) +{ + extern int yylineno; // defined and maintained in lex.c + extern char *yytext; // defined and maintained in lex.c + + cerr << "ERROR: " << s << " at symbol \"" << yytext; + cerr << "\" on line " << yylineno << endl; + exit(1); +} + +int yyerror(char *s) +{ + return yyerror(string(s)); +} + + Added: sandbox/trunk/src/bison/calc/heading.h =================================================================== --- sandbox/trunk/src/bison/calc/heading.h (rev 0) +++ sandbox/trunk/src/bison/calc/heading.h 2008-06-02 20:37:15 UTC (rev 845) @@ -0,0 +1,10 @@ +/* heading.h */ + +#define YY_NO_UNPUT + +using namespace std; + +#include <iostream> +#include <stdio.h> +#include <string> + Added: sandbox/trunk/src/bison/calc/main.cc =================================================================== --- sandbox/trunk/src/bison/calc/main.cc (rev 0) +++ sandbox/trunk/src/bison/calc/main.cc 2008-06-02 20:37:15 UTC (rev 845) @@ -0,0 +1,20 @@ +/* main.cc */ + +#include "heading.h" + +// prototype of bison-generated parser function +int yyparse(); + +int main(int argc, char **argv) +{ + if ((argc > 1) && (freopen(argv[1], "r", stdin) == NULL)) + { + cerr << argv[0] << ": File " << argv[1] << " cannot be opened.\n"; + exit( 1 ); + } + + yyparse(); + + return 0; +} + Added: sandbox/trunk/src/bison/calc/url =================================================================== --- sandbox/trunk/src/bison/calc/url (rev 0) +++ sandbox/trunk/src/bison/calc/url 2008-06-02 20:37:15 UTC (rev 845) @@ -0,0 +1 @@ +http://www.cs.ucr.edu/~lgao/teaching/bison.html This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |