University of Ottawa
SEG-2106 : Software construction
Gregor v. Bochmann
Winter 2008
, 2009, 2010, 2011, 2012, 2013

Lab-5 Part 2 : Lex

Originally prepared by Nicolas Gorse

Having fun with LEX

Description

Lex (or Flex) is a tool for generating lexical analyzers (scanners): programs that recognize lexical patterns in text. Flex reads the given input files, or its standard input if no file names are given, for a description of a scanner to be generated. The description is in the form of pairs of regular expressions and C code, called rules. Flex generates as output a C source file, lex.yy.c, which defines a routine yylex(). This file is compiled and linked with the -lfl library to produce an executable. When the executable is run, it analyzes its input for occurrences of the regular expressions. Whenever it finds one, it executes the corresponding C code.

Documentation

Edition, compilation, etc.

Examples

Your tasks

  1. Download, compile and experiment the examples of parsers given above. Try eventually to modify them in order to get familiar with their edition, compilation etc.
  2. For several regular expressions seen in Part 1, use Lex to construct a parser that will implement the regular expression. This means that your parser will recognize words characterized by its corresponding regular expression. For instance, the parser implementing the regular expression " aa (a | b)* " will only recognize words starting with aa.
  3. Construct a lexical analyser for recognizing e-mail addresses. Notes:
  1. Another suggestion (optional): Use Lex to program a parser implementing the following grammar:
      SENTENCE    --> NOUN_PHRASE  VERB  NOUN_PHRASE 
      VERB        --> hit
      NOUN_PHRASE --> ARTICLE   NOUN
      NOUN_PHRASE --> PROPER_NAME
      ARTICLE     --> a  |  the
      NOUN        --> ball  |  bat
      PROPER_NAME --> peter


Instructions and help for doing this lab