Skip navigation
The Australian National University

COMP3610 Principles of Programming Languages

Flex and Bison

Manuals

Examples & Exercises

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 lexer.
    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 expressions like -3-4 and -3*4 are parsed as (-3)-4 and (-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 parsed as (-3)-4 and -(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.

Updated:  14 August 2013 / Responsible Officer:   JavaScript must be enabled to display this email address. / Page Contact:   JavaScript must be enabled to display this email address. / Powered by: Snorkel 1.4