CS 3040 Sample Programs and Problems

Finite State Machines

Sample finite state machines. You should try them yourself before looking at solutions:

  1. Strings of a's and b's with each b right after an a. The empty string is not included.
  2. Non-empty strings of either a's or b's, but not both.
  3. aibcj or aicbk for non-negative i, j, k.
It's a good idea to write down positive and negative examples before trying to draw the machines.

Python

Haskell

  • myDrop.hs: a recursive re-implementation of Haskell's drop function. patternDrop.hs computes the same result but using patterns in the argument lists. These are discussed in the week 3 notes.
  • A Haskell implementation of a finite state machine

  • card.hs: a short example illustrating defining a data type and writing a function processing that type.
  • Top-down, bottom-up parsers
  • nats.hs: Natural numbers
  • C++

  • simple-expr-recognizer.cpp: a recursive-descent parser implementing an expression recognizer. This serves as an introduction to recursive descent parsing.
  • simple-expr.cpp: expression parser with evaluation.
  • expr.cpp: recursive-descent parser for expressions over +, -, *, /, (, and )

    ANTLR

    All ANTLR examples