The following just mean you can copy the example code into your program or use it for whatever purpose without crediting me (though I would really like it if you did):
The parts of the example code written by myself are released into the public domain or, at your option, under the Do What The Fuck You Want To Public License (WTFPL), which can be found in the file COPYING. There are some special GPL license exceptions for the included source files from the Bison and Flex distributions.
The idea and method of this example is based on code from http://ioctl.org/jan/bison/
However if you write a program with one of the frameworks above, then your users need that parser framework installed to compile your program. But Flex and Bison require no compile-time dependencies, because they generate fully autonomous source code. (And Flex and Bison are installed almost everywhere.) So far I have not found any modern parser generator which outputs independent code.
It is even possible to compile the generated source with Visual C++ on Windows (worked with 8.0 aka 2005). Flex and Bison need not be installed on the windows machine. The source package includes a VC++ solution and two project files.
By defining the macro yyFlexLexer => ExampleFlexLexer in scanner.h, the default name of the scanner class is changed. Furthermore to extend yylex()'s parameter list, the class example::Scanner is derived from the ExampleFlexLexer class. It is mainly a forwarding class. By defining the macro YY_DECL, the yylex() function generated by Flex is renamed to example::Scanner::lex().
Another change to the default Flex code is that the token type is changed from int
to the enum example::Parser::token defined by parser.
In the example calculator the Bison code constructs a calculation node tree. The tree's nodes are derived from CalcNode and are evaluated to output the parsed expression's result.
The example::Driver object can be accessed by the Bison actions. Therefore it will contain a reference to the data classes filled by the parser's rules. In the example it contains a reference to the CalcContext. Thus a refernce to a CalcContext must be given to the constructor of example::Driver. This CalcContext object will be filled with the parsed data.
To initiate parsing the example::Driver class contains the three functions example::Driver::parse_stream(), example::Driver::parse_file() and example::Driver::parse_string().
Besides these simple arithmetic operators, the program also supports variables. These can be assigned a value and used in subsequent expressions.
It can be started interactively and will process expressions entered on the console. The expression's parse tree is printed and then evaluated. Here some examples:
$ ./exprtest Reading expressions from stdin input: 4 * 1.5 + 3 * (2 ^ 4 - 4) tree: + add * multiply 4 1.5 * multiply 3 - subtract ^ power 2 4 4 evaluated: 42 input: v = (2 ^ 4 - 4) Setting variable v = 12 input: 3.5 * a input:1.6: Unknown variable "a" input: 3.5 * v tree: * multiply 3.5 12 evaluated: 42 input: 5 + * 6 input:1.4: syntax error, unexpected '*' input: 5 + (4 * 4 input:1.10-9: syntax error, unexpected end of file, expecting ')'
The exprtest can also be used to process text files containing expressions. Within the file each line is parsed as an expression. Multiple expressions can be put into one line by terminating them with a semicolon ';
'. The exprtest outputs a parse tree for each parsed non-assignment line.
v = (2 ^ 4 - 4); e = 2.71828 4 * 1.5 + 3 * v 6 * (2 * 2) ^ 2 / 2
The above example file (included as exprtest.txt
) can be processed by calling ./exprtest exprtest.txt
. The program outputs the following evaluation:
Setting variable v = 12 Setting variable e = 2.71828 Expressions: [0]: tree: + add * multiply 4 1.5 * multiply 3 12 evaluated: 42 [1]: tree: / divide * multiply 6 ^ power * multiply 2 2 2 2 evaluated: 48