Grmr2JavaReport

From Eclipsepedia

Jump to: navigation, search

Contents

Generation of Java from Grammar file

This article describes the issues encountered, while working on the generation of Java code from a grammar file using Gymnast, Emfatic and Grammar2Ecore frameworks.

Assumptions

  • Eclipse plug-in project with a sample grammar file (in this example, SVG5.ast) is open as shown in Figure 1. Grammar file has a value for parserGenerator option as javacc.
  • srcGymnast (in Figure 1) is the source folder to which Gymnast runtime core plug-in is added manually. Please refer "Bugzilla entry 233805"
Grmr2JavaReport1.jpg
Figure 1 Project structure
  • Dependent Plug-ins are added in the MANIFEST.MF file. Hence right clicking on the grammar file shows options like 'Generate .genmodel and Java', 'Generate .genmodel' etc. Refer Figure 2.
Grmr2JavaReport2.jpg
Figure 2 Context menu for grammar file

Issues

This section describes the issues faced and the workaround for the same.

Parser error in Gymnast generated Javacc file

After checking the well-formedness for the grammar file, click on 'Generate AST' from the context menu. Gymnast generates AST and Parser packages in the 'src' folder (refer Figure 3). Open the Gymnast generated Javacc file (in this case, file svg.jj in the parser package). The Outline view in eclipse shows parser error. This is because Gymnast additionally adds the word "public" in the generated JavaCC file. This issue has been reported in "Bugzilla entry 233803"

Grmr2JavaReport3.jpg
Figure 3 Parser error in JavaCC

Workaround for this issue is to manually remove the additional word "public" that causes the parser error in the Javacc file.

Incomplete generation of parser files

It can happen that all the files in the parser package are not generated automatically by the Gymnast. To generate all the missing files in the parser package, click on 'Compile with JavaCC' from the context menu of JavaCC file ('svg.jj'). This will generate the missing files (refer Figure 4)

Grmr2JavaReport4.jpg
Figure 4 Compile with JavaCC to generate missing files

JavaCC options for the project

Unrelated to the above issues, the JavaCC console also displays some warnings as shown in Figure 5.

Grmr2JavaReport5.jpg
Figure 5 JavaCC warnings

These warnings can be handled by changing the JavaCC options for this project. Goto Project -> Properties -> JavaCC Options and select the tab 'JavaCC Options'. Default values are shown in Figure 6.

Grmr2JavaReport6.jpg
Figure 6 JavaCC options - default values

Change the values of LOOKAHEAD, STATIC, JDK_VERSION as shown in the Figure 7.

Grmr2JavaReport7.jpg
Figure 7 JavaCC options - adjusted values

Assumption of Non-Static methods by Gymnast

Compilation errors can still be seen after solving the above issues (refer Figure 8). This is because the Java files starting with Ext... (generated by Gymnast) assume that some the methods in parser files (generated by JavaCC) are not static.

Grmr2JavaReport8.jpg
Figure 8 Assumption of Non-Static methods

Workaround for this issue is to manually remove the qualifier 'static' for the methods in the parser files that was giving the compilation errors.

Case Sensitivity Issue

After the AST and Parser packages are made free from compilation errors, generate the Java code for the grammar file by clicking 'Generate .genmodel and Java' from the context menu of grammar file (Figure 2). This process generates three packages whose names are based on the value for the option 'genModelBasePackage' in the grammar file. In this example, the value for 'genModelBasePackage' is set to 'in.svg.model'. Except for the 'Util' package, other two packages have problem markers (refer Figure 9). The problem is caused by import statements (in Ecorizer.java, EcorizerImpl.java, and svgPackageImpl.java) as shown in Figure 10. It tries to import 'in.svg.ast.SvgASTNode' but only 'in.svg.ast.svgASTNode' exists (refer Figure 10).

Grmr2JavaReport9.jpg
Figure 9 Packages with problem markers

Workaround is to manually change the occurrences of 'SvgASTNode' to 'svgASTNode'.

Grmr2JavaReport10.jpg
Figure 10 Errors due to import statements

Conclusion

This article identified some of the issues one might encounter while trying to generate Java code from a grammar file (.ast). Currently following the workaround for each of the issues mentioned in this article removes all the problem markers and the project looks as shown in Figure 11 with generated Java code.

Grmr2JavaReport11.jpg
Figure 11 Error free project

Organization