{{announcement.body}}
{{announcement.title}}

A Use Case With JavaCC [Snippet]

DZone 's Guide to

A Use Case With JavaCC [Snippet]

Here's a simple JavaCC use case — check it out!

· Java Zone ·
Free Resource

Let's say you have the following file:

fsdfsdfsdf 
sfsfasdfsdf  dfdsfsdfs f 
sfsfasdfsdf  
 Barcelona, Total SCORE 8 (4+4). 
          having 100% success rate. 

sfsdfsdfdf sfasdfum 34749n afsf

 Barcelona, Total SCORE 2 (6-4). 
          having 100% success rate.    



Our job is to extract the percentage. Here is the corresponding grammar file:

options
{
  LOOKAHEAD = 1;
}

PARSER_BEGIN(ScoreGrammer)
package com.test.grammer;

public class ScoreGrammer
{
  public static void main(String args[]) throws Exception
  {
    ScoreGrammer parser;
    try
    {
      parser = new ScoreGrammer(ScoreGrammer.class.getResource("some file.txt").openStream());
    }
    catch (java.io.FileNotFoundException e)
    {
      return;
    }
    Model model = new Model();
    try
    {

      parser.Parse(model);     

    }
    catch(EOFException e) {
    }
    catch (ParseException e)
    {
      System.out.println("Encountered errors during parse." + e);
    }
    System.out.println("definitions parsed successfully." );
    System.out.println(model);
  }
}

PARSER_END(ScoreGrammer)

SKIP :
{
  " "
| "\t"
| "\n"
| "\r"
| ","
}


TOKEN [IGNORE_CASE]:
{
  < BARCELONA: "BARCELONA" &gt;
}

TOKEN:
{
<ALPHA: (["A" - "Z", "a" - "z"])+ &gt;
| <#DIGIT: (["0" - "9"])+&gt; // Defines local pattern (private regular expression)
| <LPARAN: "("&gt;
| <RPARAN: ")"&gt;
| <PLUS: "+"&gt;
| <MINUS: "-"&gt;
| <PERCENT: "%"&gt;
| <DOT: "."&gt;
| <ANY: (~["A" - "Z", "a" - "z", ",", "0" - "9", "(", ")", "+", ",", "%", "\t", "\n", "\r", " "])+ &gt;
}


JAVACODE
void jumpToBarcelona() {
  Token tok;    
  while (true) {
    tok = getToken(1);

    if (tok.kind == BARCELONA) {
      break;
    }
    if (tok.kind == EOF) {
      throw new EOFException();
    }   
    tok = getNextToken();
  }
}

/** Top level production. */


void Parse(Model model) :  
{
  Token t;
  Variations v = null;  
}
{
  (
    jumpToBarcelona() 
    t = < BARCELONA &gt;
    {
      v = new Variations();
      v.setBeginLine(t.beginLine);
    }
    (
      (< ALPHA &gt;)+
      (
        [ < DIGIT &gt; ] 
        < LPARAN &gt; 
        < DIGIT &gt;
        (
          < PLUS &gt;
        | < MINUS &gt;
        )
        < DIGIT &gt; 
        t = < RPARAN &gt;
        {
          v.setEndColumn(t.endColumn);
        }
        < DOT &gt;
        (
          (
            < ALPHA &gt; 
            t = < DIGIT &gt;
            {
              v.percent(t.image);
              model.addVarition(v);
            }
            < PERCENT &gt;
          )
        )
      )?
    )
  )*
}


Hope you found this simple example useful!

Further Reading

Parser Generators: ANTLR Vs. JavaCC

Extracting Javadoc Documentation From Source Files Using JavaParser 

Topics:
java ,code snippet ,grammar file ,java cc ,use case ,example code

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}