previous                   ToC                next


3. Lexical analysis


First of all we have to specify the lexical elements of our source language by means of a lexical-analyzer generator.

JFlex transforms the specification of a scanner (regular definitions, actions to be executed when a matching occurs, ...) into a Java program that implements a minimum state Deterministic Finite Automaton accepting the specified lexemes.

The lexical specification of the mjava language is reported in the mjava.flex file, that uses  the interface to declare integer constants for the terminal tokens in the language.

From these files JFlex generates a file with one class that contains code for the scanner. The class will have a constructor taking a from which the input is read.

The %debug directive in mjava.flex also creates a main function in the generated class that expects the name of an input file on the command line and then runs the scanner on this input file, by printing information about each returned token.

The information includes: line number (if line counting is enabled), column (if column counting is enabled), the matched text, the executed action (with line number in the specification), and the integer value of the token declared in


You can generate a scanner for mjava and run it on an input program such as SampleProgram.mjava by means of the Windows batch file mjava.bat or the Linux shell script mjava.spt :

· put the mjava.flex,, SampleProgram.mjava and mjava.bat / mjava.spt files in a same directory and “cd”  into it

· run mjava.bat / mjava.spt

· you will get something like SampleProgramTokens.txt printed to your Java console.


previous                   ToC                next