⚠️ The content of this document is partly outdated. Up-to-date information is available at https://wiki.eclipse.org/JDT_Core_Programmer_Guide/ECJ/Parse
JDT Core / HowTo: Generate the Parser
Where to get the parser generator
The parser files and resources are automatically generated using the Jikespg parser generator. This tools has been renamed Jikes Parser generator. You can find more information and latest releases at this link. The latest tool is provided in source format. We don't provide any help for compiling these source files. Refer to the link above if you have trouble to get binaries.
Our grammar is generated using the version 1.3 of Jikespg. If newer versions fail to generate resources from our grammar, please send request to the Jikes Parser Generator team.
Where to get the grammar
The latest grammar is always located in the java.g file. (N.B.: Older versions of the grammar, such as java_1_4.g and java_1_5.g are not supported and are no longer functional.). Go to the org.eclipse.jdt.core plugins directory (eclipse\plugins\org.eclipse.jdt.core where eclipse is the root of your eclipse installation) and open the grammar directory. Then search for the java.g file inside the folder grammar. Then copy its contents from:
--main options
%options ACTION, AN=JavaAction.java, GP=java, 
....
$end
-- need a carriage return after the $end
into a file called java.g. It is important to add a carriage return at the end of the last line. You can save this file where you want, we will assume from thereon you saved it in d:\temp\. If the end-of-line of the environment of the C compiler used to build jikespg is different from the one used when running the executable, you might end up with an exception at runtime. Be sure to used consistent end-of-line characters.
What to do with these files and update the parser class...
Assuming the Jikespg executable (jikespg.exe) is located in d:\jikespg.
  1. First in a console, run:
     	d:
     	cd \temp
     	d:\jikespg\jikespg.exe java.g
     
  2. You will get an output that looks like this:
     IBM Research Jikes Parser Generator            Fri Oct 14 09:22:32 2005
    %OPTIONS ACTION, AN=JavaAction.java, GP=java,
    %OPTIONS FILE-PREFIX=java, ESCAPE=$, PREFIX=TokenName, OUTPUT-SIZE=125 ,
    %OPTIONS NOGOTO-DEFAULT, SINGLE-PRODUCTIONS, LALR=1 , TABLE,
    %OPTIONS ERROR_MAPS
    %OPTIONS first follow
    %OPTIONS TRACE=FULL ,
    %OPTIONS VERBOSE
    %OPTIONS DEFERRED
    %OPTIONS NAMES=MAX
    %OPTIONS SCOPES
    Options in effect:
        ACTION  ACTFILE-NAME=JavaAction.java  BLOCKB=/.  BLOCKE=./  BYTE  CONFLI
        DEFAULT=5  NODEBUG  DEFERRED  NOEDIT  ERROR-MAPS  ESCAPE=$
        FILE-PREFIX=java  FIRST  FOLLOW  GENERATE-PARSER=JAVA  NOGOTO-DEFAULT
        HACTFILE-NAME=javahdr.java  HBLOCKB=/:  HBLOCKE=:/  LALR=1  LIST
        MAX-DISTANCE=30  MIN-DISTANCE=3  NAMES=MAXIMUM  NONT-CHECK  ORMARK=|
        OUTPUT-SIZE=125  PREFIX=TokenName  READ-REDUCE  SCOPES  NOSHIFT-DEFAULT
        SINGLE-PRODUCTIONS  STACK-SIZE=128  STATES  SUFFIX=  TABLE=SPACE
        TRACE=FULL  VERBOSE  WARNINGS  XREF
    
    *** The following Terminals are useless:
            const  goto
    
    This grammar is LALR(1).
    
    Number of Terminals: 110
    Number of Nonterminals: 306
    Number of Productions: 693
    Number of Single Productions: 232
    Number of Items: 2165
    Number of Scopes: 132
    Number of States: 954
    Number of Shift actions: 6057
    Number of Goto actions: 7493
    Number of Shift/Reduce actions: 593
    Number of Goto/Reduce actions: 1041
    Number of Reduce actions: 13922
    Number of Shift-Reduce conflicts: 0
    Number of Reduce-Reduce conflicts: 0
    Reallocating storage for SPACE table, adding 2820 entries
    
    Length of base Action Table: 12268
    Number of entries in base Action Table: 9488
    Percentage of increase: 29.3%
    Storage required for base Tables: 24536 Bytes, 24K
    Storage required for Rules: 2076 Bytes
    Number of unique terminal states: 727
    Number of Shift actions saved by merging: 4113
    Number of Reduce actions saved by merging: 967
    Number of Reduce saved by default: 8997
    
    Length of Terminal Check Table: 7913
    Length of Terminal Action Table: 7902
    Number of entries in Terminal Action Table: 7222
    Percentage of increase: 9.4%
    Storage required for Terminal Tables: 23717 Bytes, 24K
    Total storage required for Tables: 48253 Bytes, 48K
    
    
    Actions in Compressed Tables:
         Number of Shifts: 2211
         Number of Shift/Reduces: 326
         Number of Gotos: 7493
         Number of Goto/Reduces: 1041
         Number of Reduces: 3958
         Number of Defaults: 475
    
    Error maps storage:
        Storage required for ACTION_SYMBOLS_BASE map: 1908 Bytes
        Storage required for ACTION_SYMBOLS_RANGE map: 1709 Bytes
        Storage required for NACTION_SYMBOLS_BASE map: 1908 Bytes
        Storage required for NACTION_SYMBOLS_RANGE map: 960 Bytes
        Storage required for TERMINAL_INDEX map: 220 Bytes
        Storage required for NON_TERMINAL_INDEX map: 614 Bytes
        Storage required for STRING_BUFFER map: 12004 Bytes
    
    ***Warning: Base Check vector contains value > 127. 16-bit words used.
    Escaped symbol $eof is an invalid C variable.
    
    Escaped symbol $error is an invalid C variable.
     
    It can be quite different if the output changed since version 1.3 of jikespg. The important part is:
    This grammar is LALR(1).
    This creates some java source files and information files in the current directory.
    java.l Information generated by jikespg. Enumarate all the states created for the automaton, etc.
    JavaAction.java Contains the method consumeRule(int) of class org.eclipse.jdt.internal.compiler.parser.Parser, which handles all semantic action dispatches.
    javahdr.java Used to generate the resources files.
    javadcl.java Used to generate the resources files.
    javasym.java Contents of the class org.eclipse.jdt.core.compiler.TerminalTokens.
    javadef.java Contents of the class org.eclipse.jdt.internal.compiler.parser.ParserBasicInformation.
    javaprs.java You don't need this file. Its contents is already inlined in the Parser class.
  3. Now we need to update the different classes and resource files.
    1. Copy the contents of the JavaAction.java file into the consumeRule(int) method of the org.eclipse.jdt.internal.compiler.parser.Parser class.
    2. The class org.eclipse.jdt.internal.compiler.parser.ParserBasicInformation needs to be updated with the content of the file javadef.java. Don't copy the interface name. Simply copy the field declarations. The actual source of this class will guide you.
    3. Similarly, update the class org.eclipse.jdt.internal.compiler.parser.TerminalTokens with the contents of the file javasym.java.
    4. The last step is to update the resource files:
      Copy the jdtcore.jar file in d:\temp. Compile this source inside d:\temp. You will have a file UpdateParserFiles.class. Then run the following command-line:
       D:\temp>java -classpath jdtcore.jar:. UpdateParserFiles javadcl.java javahdr.java
       
      Once this done, you will end up with 25 new files inside d:\temp. They are called parser<n>.rsc, with n equals to 1..24 and readableNames.properties. All these files need to be moved to the org\eclipse\jdt\internal\compiler\parser folder. Now you are ready to execute and test the new parser.

Warning:

If the number of terminals is over 128, then the parser term_check field needs to be converted to a char[]. Also the generation of the parser resources files needs to be changed to support an array of char[].

NOTE:

Changing the parser is a risky operation if you miss one of the steps above. The resulting parser can be completely unpredictable. It can go from crashing to reporting invalid errors. Be sure that you followed all the steps and that all the files are updated and recompiled before you run it.