Headline

Parsing text to trees and walk them with ANTLR4 Listeners

Realtionships

This an ANTLR4 version of Contribution:antlrTrees using the Listener pattern.

Illustration

Total and Cut are implementations of the CompanyBaseListener using the listener pattern (Observer pattern). To execute Cut or Total the walk method of an instance of ParseTreeWalker is used. The Listener to execute is given as an argument to walk.

An example from TotaListenerTest:

    ParseTreeWalker walker = new ParseTreeWalker();
    TotalListener extractor = new TotalListener(parser);
    walker.walk(extractor, tree);
    System.out.println("Total = " + extractor.getTotal());
The variable tree conatins the ParseTree of the company.

An example from the TotalListener:

public class TotalListener extends CompanyBaseListener {
    private float total = 0;

    //skip uninteresting part 

    public float getTotal() {
      return total;
    }
    @Override
    public void enterEmployee(@NotNull CompanyParser.EmployeeContext ctx) {
        // All FLOATS are salaries
        total += Float.parseFloat(ctx.FLOAT().getText());
    }
}

Usage

This contribution uses Technology:Gradle for building. Technology:Eclipse is supported.

See https://github.com/101companies/101simplejava/blob/master/README.md


There are no revisions for this page.

User contributions

    This user never has never made submissions.

    User edits

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.

    Syntax for editing wiki

    For you are available next options:

    will make text bold.

    will make text italic.

    will make text underlined.

    will make text striked.

    will allow you to paste code headline into the page.

    will allow you to link into the page.

    will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

    will allow you to paste image into the page.

    is list with bullets.

    is list with numbers.

    will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

    will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

    will allow your to insert code snippets from @worker.