[pic]
Contents
• Overview • Format of a JLex Specification o Regular Expression Rules ▪ Test Yourself #1 o JLex Directives ▪ Test Yourself #2 o Comments • yyline and yytext • A Small Example • Quick Reference Guide
Overview
JLex is a scanner generator that produces Java code. Here's a picture illustrating how to create and run a program using JLex:
+-----------+
JLex specification ---> | JLex.Main | ---> Java source code
(xxx.jlex) +-----------+ (xxx.jlex.java)
+------------------+
xxx.jlex.java ---> | jikes (or javac) | ---> Yylex.class
+------------------+
+--------+
Yylex.class-------> | java | ---> output of Main
Main.class--------> | |
+--------+
The input to Jlex is a specification that includes a set of regular expressions and associated actions. The output of Jlex is a Java source file that defines a class named Yylex. Yylex includes a constructor that is called with one argument: the input stream (an InputStream or a Reader). It also includes a method called next_token, which returns the next token in the input.
The picture above assumes that a class named Main has been defined that contains the main program of interest. That program will declare an object of type Yylex, and will include calls to the Yylex constructor and its next_token method.
Format of a JLex Specification
A JLex specification has three parts, separated by double percent signs: 1. User code: this part of the specification will not be discussed here. 2. JLex directives: This includes macro definitions (described below). See the JLex Reference Manual for more information about this part of the specification. 3. Regular expression rules: These rules specify how to divide up the input into