H1: lex/yacc
Assignment: Calculator
In this homework you will create a simple calculator (see calc.ebnf). This must be done with lex and
yacc. Skeleton files are provided here. You may
notice that Makefile is set up for using C++, which means you can use STL
to make your life easier (hopefully).
Description:
The specific goal of this assignment is to familiarize yourself with lex
and yacc. As a result of this assignment you should know and be able to
explain the principles of lex/yacc, how they interface and the following
items: conflicts in yacc, yyin, yytext, yyleng, yylex(), yywrap(),
yymore(), yyless(), yyparse(), yyerror(), yychar, yydebug. Another
question: what does -ly argument to cc command do?
The 'calc' program usage is: calc
[filename]. Without a filename argument it reads from stdin.
It should gracefully exit after reaching the end of
file. The command 'quit' exits the program.
The calculator needs to understand two types of statements: expression
evaluation (e.g. 2*2), and setting variables (e.g. set pi
3.1415926535). When an expression is evaluated, the result is
printed using
printf("%g\n",expr); Here's a sample calculator
session:
> calc
2*2
4
set a 5*5
a
25
sqrt(a)
5
quit
>
Your calculator should understand integers and standard C floating
point constants, e.g. 2.718281828459045, 6.022e23, etc. A valid floating
point constant consists of a non-empty sequence of digits optionally
containing a radix character, then an optional exponent part. An exponent
part consists of e or E, followed by an optional sign, followed by one or
more decimal digits. You do not need to implement high precision
operations. Regular 'double' precision operations will do.
Do not do anything special with regard to overflow.
Turnin:
Copy all your source files, including a Makefile, to your turnin directory
by the specified time. Do not include binaries or object files. The sample
Makefile has a target 'turnin', so that when you are done, all you need to
do is 'make turnin' (unless your project is different from the
assumptions in the Makefile). There must not be any conflicts in your
yacc specification.
Useful links:
docs.sun.com:
Programming Utilities Guide (Besides good documentation on lex, yacc
and other utilities, there's a description of a simple desk caclulator
which might be somewhat useful for this assignment.)
vin@nd.edu
Last modified: Wed Jan 16 16:50:31 EST 2002