This week we will hand out the second phase of the class project, which deals with parsing using the Bison tool. We will also complete an exercise that will help you get acquainted with Bison.
Outline for today's lab:
In this exercise, we will write a Bison specification for a parser for a simple calculator language. This calculator language is the same used in the first lab. For now, this language will contain integer numbers, the operators plus, minus, multiply, and divide, and parentheses for grouping. Additionally, the symbol "=" is in the language to terminate an expression. These symbols and their corresponding token names are shown in the table below.
|Symbol in Language||Token Name|
|integer number (e.g., "0", "12", "1719")||NUMBER XXXX [where XXXX is the number itself]|
The calculator language itself is very simple. There is only one type of phrase in the language: "Expression=", where "Expression" is defined in the same way as for the class project, except for the fact that there are no variables in the calculator language, only numbers. For example, all of the following are valid in the calculator language.
Note, however, that scanner only scans for valid tokens in the calculator language, not valid expressions. The parsing phase is where sequences of tokens will be checked to ensure that they adhere to the specified language grammar.
Task 1: Create a Bison specification to evaluate expressions in the calculator language. Print out an error message and exit if any unrecognized character is encountered in the input or if the input does not follow the calculator grammar. Use Bison to compile your specification into an executable parser that reads text from standard-in and prints the results to the screen.
Use the following commands to compile your calculator project:
bison -v -d --file-prefix=y calc.y
gcc -o calc y.tab.c lex.yy.c -lfl
Task 2: Enhance your Bison specification so that input text can be optionally read from an input file, if one is specified on the command line when invoking the parser.
Task 3: Enhance your Bison specification so that if the input expression is wrong (it does not follow the calculator grammar), it prints out where the error is and what token the parser was expecting.
Task 4: Change Bison specification so that in addition to evaluate expressions, the parser will also count the following.
+, -, *, /
Task 5 (optional): For a challenge, you may want to try extending the calculator language to allow for decimal numbers in addition to regular integers. Thus, the following numbers should be recognized by your parser.
For an even greater challenge, extend the calculator language to allow for scientific notation in the numbers. After the number, there can be an optional "e-phrase" consisting of either "e" or "E", followed by an optional "+" or "-", followed by one or more digits. For example, the following numbers in scientific notation would be recognized by your parser.