The figure below contains a complete and working grammar file
- example for a simple arithmetic language.
+
These examples come from the test/src/grammar and
+ test/src/java directories. The source code has been
+ simplified in these examples in order to be easier to read. Please
+ refer to the original source code in the above directories for the
+ complete compilable versions. Also note that the corresponding C#
+ examples are found in the test/src/csharp
+ directory.
+
+
The figure below contains a complete and working example of a
+ grammar file for a simple arithmetic language.
+
To create a parser for the grammar above, the parser source
+ code must first be created. If the grammar above has been stored
+ in the file arithmetic.grammar, this can be
+ accomplished with the following command:
+
+
# java -jar lib/grammatica-.jar arithmetic.grammar --javaoutput test
+
+
This will create the files ArithmeticAnalyzer.java,
+ ArithmeticConstants.java,
+ ArithmeticParser.java, and
+ ArithmeticTokenizer.java in the test
+ subdirectory. These files contain the source code for the parser.
+ In order to call the parser, the method in the figure below can
+ be inserted into another Java class.
+
+
+
+
In the case of the arithmetic language, is it also interesting
+ to analyze and evaluate the contents of the string. This can be
+ done by subclassing the ArithmeticAnalyzer class and
+ overloading the callback methods for the relevant productions. A
+ small example of this can be seen in the figure below.