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 RLA language is reported in the rla.flex file, that uses  the interface sym.java to declare integer constants for the terminal tokens in the language.

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

The %debug directive in rla.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 sym.java.

 

You can generate a scanner for RLA and run it on an input program such as Sample.rla by means of the Windows batch file rla.bat :

· put the rla.flex, sym.java, Sample.rla and rla.bat files in a same directory and “cd”  into it

· run rla.bat

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

 

previous                     ToC                 next