DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations

Trending

  • Hyperion Essbase Technical Functionality
  • Batch Request Processing With API Gateway
  • Execution Type Models in Node.js
  • Measuring Service Performance: The Whys and Hows

A Use Case With JavaCC [Snippet]

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

Mohammad Nadeem user avatar by
Mohammad Nadeem
·
Oct. 16, 19 · Code Snippet
Like (3)
Save
Tweet
Share
22.27K Views

Join the DZone community and get the full member experience.

Join For Free

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 

Use case

Published at DZone with permission of Mohammad Nadeem, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Trending

  • Hyperion Essbase Technical Functionality
  • Batch Request Processing With API Gateway
  • Execution Type Models in Node.js
  • Measuring Service Performance: The Whys and Hows

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: