This practical is designed to introduce you to code generation.
To create an infix-to-postfix filter using flex and byacc.
dc is a calculator program available as part of the unix utilities. Unfortunately, it uses reverse-polish notation (postfix). bc is a front-end for dc that reads infix expressions from the user, translates them into postfix, and hands them on to dc, which performs the arithmetic. You should read the man information for bc and dc and try running them both. In particular, try "bc -c" to see the output it generates. You are going to create a simple version of bc, using flex and byacc.
Copy the starting files from "$CS5031/p*/ex3". The files include byacc and flex programs, a makefile and some (trivial) test data. This is the flex and byacc calculator used in the lectures, slightly changed to be usable in a pipeline. You can compile and run it against the test data by:
make testbut you will only see:
result is 7 syntax erroruntil you have modified the grammar descriptions and actions in the byacc file as outlined below.
You should modify the grammar rules to allow more than one calculation i.e. change "input" to be the equivalent of:
input = (expression ';')+As you don't calculate directly in your bc but output to dc instead, you should get rid of the %type declaration and replace all the existing actions in the byacc file. To convert infix to postfix, your new actions must:
For example, (infix) input of:
1 * 2 + 3 / 4 ; 1 * ( 2 + 3 ) / 4 ;should generate (postfix) output of:
1 2 * 3 4 / + p 1 2 3 + * 4 / p q
You can run your version of bc by itself initially, but you should be able to use it as a calculator by typing: bc | dc
If you have time to spare, you could implement more of the facilities of the
real dc and bc programs, such as floating point, functions or variables.