COMP3610 Principles of Programming Languages
Flex and Bison
Examples & Exercises
- The following three list grammars demonstrate the effect of left and right recursion.
- The following Flex specification demonstrates simple recognition (of a collection of verbs).
The following examples of Bison and Flex specifications are all various calculators. They form the basis of some of the tutorial exercises.
- The first example is a calculator for arithmetic expressions in reverse-polish notation. It is a simple Bison specification with a hand-coded lexer.
- Here is another Bison specification based on the first, but there is a problem with the grammar.
- The second example is a calculator for arithmetic expressions in
standard infix notation. There is a separate Flex specification for
The demonstration of
%precto specify context-dependent precedence is not a very clear one. As it stands, unary negation is given higher precedence than all other operators, so expressions like
-3*4are parsed as
(-3)*4respectively. This is as we would wish. Without the
%prec UMINUS, unary negation is given the same precedence as subtraction. so the expressions are parsed as
-(3*4)respectively. Unfortunately, this change to the parse is not reflected by a change to the value of the expressions.
- The third example allows single-letter variables to be declared.
- The fourth example allows typical identifiers to be declared. There is a separate implementation of a simple symbol table.