1 | ------
|
---|
2 | Usage
|
---|
3 | ------
|
---|
4 | Gerwin Klein <[email protected]>
|
---|
5 | Régis Décamps <[email protected]>
|
---|
6 | ------
|
---|
7 | November 2, 2007
|
---|
8 | ------
|
---|
9 |
|
---|
10 |
|
---|
11 | About this project
|
---|
12 |
|
---|
13 | This sample project parses a grammar with JFlex.
|
---|
14 |
|
---|
15 | The project management is done with Maven.
|
---|
16 |
|
---|
17 | The project contains a simple grammar for the "Toy programming language".
|
---|
18 | It is the example from the JLex web site with some small
|
---|
19 | modifications, to make it a bit more readable.
|
---|
20 | It does nothing really useful, because there is no parser for
|
---|
21 | the toy programming language. It's just a demonstration how a
|
---|
22 | small simple scanner looks like.
|
---|
23 |
|
---|
24 |
|
---|
25 | * JFlex
|
---|
26 |
|
---|
27 | JFlex is a parser generator. Given a grammar, JFlex generate
|
---|
28 | Java (TM) code to parse documents that follow this grammar.
|
---|
29 |
|
---|
30 | * Maven
|
---|
31 |
|
---|
32 | Maven is a project management framework.
|
---|
33 | The project is described in as a POM (project object model,
|
---|
34 | stored into <<<pom.xml>>>).
|
---|
35 | This document isn't intended to be a tutorial on the use of Maven 2,
|
---|
36 | you should consult the {{{http://maven.apache.org/}Maven web site}}.
|
---|
37 |
|
---|
38 | The integration of JFlex and Maven is done with
|
---|
39 | {{{http://jflex.sourceforge.net/maven-flex-plugin/}maven-flex-plugin}}.
|
---|
40 |
|
---|
41 |
|
---|
42 | Usage
|
---|
43 |
|
---|
44 | * mvn generate-sources
|
---|
45 |
|
---|
46 | The maven-jflex-plugin will read the grammar
|
---|
47 | <<<src/main/jflex/simple.jflex>>>
|
---|
48 | and generate a Java scanner <<<Yylex.java>>>
|
---|
49 | in <<<target/generated-sources/flex>>>
|
---|
50 |
|
---|
51 | This is defined by the following section
|
---|
52 |
|
---|
53 | +---------
|
---|
54 | <build>
|
---|
55 | <plugins>
|
---|
56 | <plugin>
|
---|
57 | <groupId>de.jflex.maven.plugin</groupId>
|
---|
58 | <artifactId>maven-jflex-plugin</artifactId>
|
---|
59 | <version>0.2</version>
|
---|
60 | <executions>
|
---|
61 | <execution>
|
---|
62 | <goals>
|
---|
63 | <goal>generate</goal>
|
---|
64 | </goals>
|
---|
65 | </execution>
|
---|
66 | </executions>
|
---|
67 | </plugin>
|
---|
68 | </plugins>
|
---|
69 | </build>
|
---|
70 | +------------
|
---|
71 |
|
---|
72 | By default, the maven-jflex-plugin generates a scanner/parser for every file
|
---|
73 | in <<<src/main/jflex/>>>.
|
---|
74 |
|
---|
75 |
|
---|
76 | * mvn test
|
---|
77 |
|
---|
78 | This goal test the generated scanner (if required, the lexer will be
|
---|
79 | generated and all Java classes will be compiled)
|
---|
80 | by running all tests in <<<src/test/java>>>.
|
---|
81 |
|
---|
82 | There is only one test in <<<src/test/java/YylexTest.java>>>.
|
---|
83 | In this test,
|
---|
84 | the scanner is run with the input file <<<src/test/resources/test.txt>>>.
|
---|
85 |
|
---|
86 | By default, the scanner outputs debugging information about each
|
---|
87 | returned token to <<<System.out>>> until the end of file is reached,
|
---|
88 | or an error occurs.
|
---|
89 | But in the test, the output is redirected into <<<target/output.actual>>>.
|
---|
90 |
|
---|
91 | The test is successful if every line match
|
---|
92 | with <<<src/test/resources/output.good>>>,
|
---|
93 | which is the expected scanner debugging information.
|
---|
94 |
|
---|
95 |
|
---|
96 | References
|
---|
97 |
|
---|
98 | * {{{http://jflex.sourceforge.net/maven-flex-plugin/}maven-flex-plugin}}
|
---|
99 |
|
---|
100 | * {{{http://maven.apache.org/}Maven web site}}.
|
---|