Game Programming Language
Phase 1: Expression parser
Overview:
Extend the simple expression parser in class
(expr directory) to include:
doubles, multiplication, and division.
This is intended to be an easy program to introduce you to
the tools flex and bison. The majority of your time and effort
will be devoted to learning the syntax of flex and bison. I
encourage you to make sure you understand all the components of expr.l
and expr.y. If you understand these now, it will make the main
project easier.
This project uses two components from the main project: class Error,
and gpl_assert (they are in the src/p1 directory (see below)). If you learn how to use these now, it will make
the main project easier.
Requirements:
I am very picky when it comes to students following
directions. In software development careers, following directions
is crucial. Thus I expect students to follow my requirements
exactly. I will grade your program using another program (a bash script), so
your program must work exactly as specified for you to get full
credit.
Use * for multiplication, and / for
division.
A double can have any of the following forms:
In order to get full credit, bison must not generate any conflicts when processing your expr.
Hints:
The only thing you have to do is
to modify expr.l and expr.y. If you find yourself needing to
change something else, you are looking in the wrong place.
If you get any "conflicts" messages reported by bison, you probably have not
handled the precedence of * and / correctly. Look for "%left" in
expr.y to see how to set the precedence of * and /.
The goal of this assignment is for you to understand the flex/bison syntax. Make sure you understand it now.
Take a little time to understand the Makefile. You will use a very similar Makefile for the entire semester.
Here is an
introduction to regular
expressions. Mastering Regular Expressions is a great reference for regular expressions. This book is sometimes available on-line in the library's Safari Bookshelf.
Getting Started:
The
first thing you need to do is to download the files. You can download files in Linux, Cygwin,
and OSX using the sftp program. In the example below, replace USERNAME
with your ecst username (go to http://accounts.ecst.csuchico.edu if you don't have an account or forgot your password).
NOTE: Sometimes when downloading files using a web
browser extra characters are added to the file (I assume it is a Microsoft feature). I
recommend you use sftp and DO NOT use a web browser to save the files on your computer.
$ sftp USERNAME@jaguar.ecst.csuchico.edu
Connecting to jaguar.ecst.csuchico.edu...
USERNAME@jaguar.ecst.csuchico.edu's password:
sftp> cd /user/faculty/tyson/515/src/p1
sftp> ls
Makefile
README.txt
error.cpp
error.h
expr.l
expr.y
gpl_assert.cpp gpl_assert.h
grammar
p1_src.tar
parser.cpp parser.h
sftp> get p1_src.tar
Fetching /var/www/tyson/classes/515.s11/src/p1/p1_src.tar to p1_src.tar
/var/www/tyson/classes/515.s11/src/p1/p1_src. 100% 40KB 40.0KB/s 00:00
sftp> quit
Now you can un-tar the tar file (unpack the zip-like file that contains all the other files):
$
tar -xvf
p1_src.tar
error.cpp
error.h
expr.l
expr.y
gpl_assert.cpp
gpl_assert.h
grammar
Makefile
parser.cpp
parser.h
README.txt
$
Compile and run this code to make sure it downloaded correctly:
$ make make
(lots of un-important warnings printed here)
$ make
bison -vyd expr.y
g++ -Wall -pedantic -g -c y.tab.c
y.tab.c: In function ‘int yyparse()’:
y.tab.c:1425: warning: deprecated conversion from string constant to ‘char*’
y.tab.c:1568: warning: deprecated conversion from string constant to ‘char*’
flex expr.l
g++ -Wall -pedantic -g -c lex.yy.c
lex.yy.c:1178: warning: ‘void yyunput(int, char*)’ defined but not used
g++ -Wall -pedantic -g -c -o parser.o parser.cpp
g++ -Wall -pedantic -g -c -o error.o error.cpp
g++ -Wall -pedantic -g -c -o gpl_assert.o gpl_assert.cpp
g++ -g -o parser y.tab.o lex.yy.o parser.o error.o gpl_assert.o -L/usr/X11R6/lib -lX11 -lglut -lGL -lGLU -lm -lfl
$
You can ignore the three warnings shown above (they are caused by
code generated by bison and flex) but you should address all other
warnings.
Now run the program.
$ parser
parser.cpp::main() reading standard input.
parser.cpp::main() about to call yyparse()
type expression followed by control-D:
2 + 3
expression = 5
parser.cpp::main() after call to yyparse()
no errors found by yyparse()
end of parser.cpp::main()
$
In the above example the user typed "2 + 3" <return> <control-D>
Now extend the program as described above by editing expr.l and expr.y
Turning in and Testing:
You will turn in all the assignments the same way. See docs/turnin.html for a description of how to turn in assignments.
You will test all the assignments (except p2) the same way. Thus it is a VERY GOOD IDEA to learn HOW THE TESTING SCRIPT WORKS on this easy assignment instead of having to learn later when you are doing a more difficult assignment.
See docs/testing.html for a description of how to test your program.
Sample tests can be found in test_directory. Use sftp to download these tests.