Generating Lexical Analyser and Parser with JavaCC

JavaCC is a lexer and parser generator for LL(k) grammars. You specify a language’s lexical and syntactic description in a JJ file, then run javacc on the JJ file. You will get seven java files as output, including a lexer and a parser.

We’ll look at three things you can do with JavaCC

  1. Do a simple syntax check only  ( Lexical Analysis )
  2. Make an actual interpreter ( Syntactic Analysis )
  3. Generate code  ( Java Byte Code or Machine Code )

JavaCC Installation Guide :

  • Download JavaCC from here
  • Extract JavaCC files
  • You may want to make the javacc script accessible from your path (Set JavaCC path)
  • Test it by running javaCC to ensure that javaCC is configured properly
  • Set java path
  • Test by running java / javac command to ensure java path is set properly and working correctly

JavaCC input Format is :

  • header
  • Token specifications for lexical analysis
  • grammar

Try out the following 1st Basic Example :

Run following command from Command Prompt :

JavaCC will generate seven classes each in a separate file.

  1. TokenMgrError is a simple error class; it is used for errors detected by the lexical analyser and is a subclass of Throwable
  2. ParseException is another error class; it is used for errors detected by the parser and is a subclass of Exception and hence of Throwable
  3. Token is a class representing tokens. Each Token object has an integer field kind that represents the kind of the token (PLUS, NUMBER, or EOF) and a String field image, which represents the sequence of characters from the input file that the token represents
  4. SimpleCharStream is an adapter class that delivers characters to the lexical analyser
  5. BasicConstants is an interface that defines a number of classes used in both the lexical analyser and the parser
  6. BasicTokenManager is the lexical analyser
  7. Basic is the parser

Next Step is to compile these classes with a javac command. For example

Next is

Text inside the example.txt file is

Here is a video tutorial