JFlex is a lexical analyzer generator (also known as scanner generator) for Java.
JFlex takes as input a specification with a set of regular expressions and corresponding actions. It generates Java source of a lexer that reads input, matches the input against the regular expressions in the spec file, and runs the corresponding action if a regular expression matched. Lexers usually are the first front-end step in compilers, matching keywords, comments, operators, etc, and generating an input token stream for parsers.
JFlex lexers are based on deterministic finite automata (DFAs). They are fast, without expensive backtracking.
The top level directory of the JFLex git repository contains:
- cup A copy of the CUP runtime
- cup-maven-plugin A simple Maven plugin to generate a parser with CUP.
- docs the Markdown sources for the user manual
- jflex JFlex, the scanner/lexer generator for Java
- jflex-maven-plugin the JFlex maven plugin, that helps to integrate JFlex in your project
- jflex-unicode-plugin the JFlex unicode maven plugin, used for compiling JFlex
- testsuite the regression test suite for JFlex,
- third_party third-party librairies used by examples of the Bazel build system
Usage with Maven
Place grammar files in
Extend the project POM build section with the
<build> <plugins> <plugin> <groupId>de.jflex</groupId> <artifactId>jflex-maven-plugin</artifactId> <version>1.7.0</version> <executions> <execution> <goals> <goal>generate</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
- Voilà: Java code is produced in
generate-sourcesphase (which happens before the
compilephase) and included in the compilation scope.
Usage with ant
- Define ant task
<taskdef classname="jflex.anttask.JFlexTask" name="jflex" classpath="path-to-jflex.jar"/>
- Use it
<jflex file="src/grammar/parser.flex" destdir="build/generated/"/> <javac srcdir="build/generated/" destdir="build/classes/"/>
Usage with Bazel
We provide a jflex rule
load("@jflex_rules//jflex:jflex.bzl", "jflex") jflex( name = "", # Choose a rule name srcs = , # Add input lex specifications outputs = , # List expected generated files )
Usage in CLI
You can also use JFlex directly from the command line:
java -jar jflex-full-1.7.0.jar -d output src/grammar/parser.flex
Other build tools
See Build tool plugins.
Build from source
JFlex is free software, contributions are welcome. See the Contributing page for instructions.