Principles of Programming Languages
Flex & 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
This page is controlled and maintained by
- The first example is a calculator for arithmetic expressions in
reverse-polish notation. It is a simple Bison specification with a
- 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
%prec to specify
context-dependent precedence is not a very clear one. As it stands,
unary negation is given higher precedence than all other operators, so
-3*4 are parsed as
(-3)*4 respectively. This is as
we would wish. Without the
%prec UMINUS, unary negation
is given the same precedence as subtraction. so the expressions are
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.
Last modified: Tue Feb 22 14:46:43 EST 2005