Question: Task 1: Build your simple calculator parser In the lectures, you learned about basic algorithms to manually build lexical analyzer and parser for a compiler.
Task 1: Build your simple calculator parser
In the lectures, you learned about basic algorithms to manually build lexical analyzer and parser for a compiler. As we briefly discussed, there is also various tools to help you build new compilers by automatically generating the code for (parts of) the new compiler. One approach to build a compiler for your new language is to build a new frontend for GNU Compiler Collection (gcc). In this exercise, you learn how to use two basic tools provided by gcc for creating your own lexical analyzers and parsers.
First, carefully read the first three sections of the GCC Frontend HOWTO to learn the basics of building lexers and parsers. The first complete example in this tutorial is to build a simple calculator capable of "+-*/" operations. Your task is very simple. Extend the example in the tutorial by including parentheses in your grammar. For example, your new parser should able to resolve that (12+4)*5 will be 80.
Your executable should be named calc. The parser as given in the example will accept input from stdin, prints the parsed value, and waits for next input (you perhaps need to manually terminate the program using ctrl-c). In order to test your program you can either use it in the interactive mode or pipe your input to it. The latter is how we will test your program. For example:
echo "(12+4)*5" | ./calc
The contents of your project should be
calc.l: the input file to flex
calc.y: the input file to bison
makefile: including at least the following targets:
calc (default target): build the executable file calc
lex.yy.o: compiles lex.yy.c which is the result of running flex on your input
calc.tab.o: compiles calc.tab.c which is the result of running bison on your input
clean: delete all but your input files and makefile
Hint: I suggest that you start by running the tutorial example. But before that, make sure you have at least read the document once. Refer back to the document and manual pages to understand the details better and implement the requested feature.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
