Headline

ANTLR-based object-text mapping for Java

Characteristics

An Technology:ANTLR-based parser for a concrete syntax of companies is provided. The underlying grammar is LL(1). The parser translates text into company objects. To this end, a straightforward object model for companies is used. (In fact, the object model is very similar to the one of Contribution:javaComposition. The mapping is described by the semantic actions within the Technology:ANTLR-based parser description. Pretty printing is also supported in the manner that objects can be exported in the concrete syntax. Pretty printing provides an implementation of Feature:Unparsing. Basic operations are implemented on top of the object model in basic OO programming style.

Illustration

The data model is implemented as POJOs. For example Feature:Company looks like this:

/**
 * A company has a name and
 * consists of (possibly nested) departments.
 */
public class Company {

    private String name;
    private List<Department> depts = new LinkedList<Department>();

    public Company() { }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<Department> getDepts() {
        return depts;
    }

}

A Parser for Feature:Company will be generated by Technology:ANTLR using src/main/antlr/Company.g.

A Company if defined by:

company returns [Company c]:
  { $c = new Company(); }
  'company' STRING
  { $c.setName($STRING.text); }
  '{' 
  ( topdept=department
    { $c.getDepts().add($topdept.d); }
  )* 
  '}'
  ;

Feature:Parsing is implemented using the generated Parser:

public class Parsing {

    public static Company parse(String s)
            throws IOException, RecognitionException {
        /*
         * Parsing is implemented in the generated Parser
         * See src/main/antlr/Company.g
         */
        return parseCompany(s);
    }

}

Feature:Unparsing pretty prints the company:

    public void unparseCompany(Company c, String s)
            throws IOException {
        writer = new OutputStreamWriter(new FileOutputStream(s));
        write("company");
        space();
        write(c.getName());
        space();
        write("{");
        right();
        nl();
        for (Department d : c.getDepts())
            unparseDept(d);
        left();
        indent();
        write("}");
        writer.close();
    }

Feature:Total and Feature:Cut are implemented using POJO methods:

public class Total {

    public double total(Company c) {
        double total = 0;
        for (Department d : c.getDepts())
            total += total(d);
        return total;
    }

    public double total(Department d) {
        double total = 0;
        total += d.getManager().getSalary();
        for (Department sub : d.getSubdepts())
            total += total(sub);
        for (Employee e : d.getEmployees())
            total += total(e);
        return total;
    }

    public double total(Employee e) {
        return e.getSalary();
    }

}
public class Cut {

    public void cut(Company c) {
        for (Department d : c.getDepts())
            cut(d);
    }

    public void cut(Department d) {
        cut(d.getManager());
        for (Department sub : d.getSubdepts())
            cut(sub);
        for (Employee e : d.getEmployees())
            cut(e);
    }

    public void cut(Employee e) {
        e.setSalary(e.getSalary() / 2);
    }

}

Test cases are implemented for all Namespace:Features. There are also a valid and an invalid input:

company "ACME Corporation" { department "Research" { manager "Craig" { address "Redmond" salary 123456 } employee "Erik" { address "Utrecht" salary 12345 } employee "Ralf" { address "Koblenz" salary 1234 } } department "Development" { manager "Ray" { address "Redmond" salary 234567 } department "Dev1" { manager "Klaus" { address "Boston" salary 23456 } department "Dev1.1" { manager "Karl" { address "Riga" salary 2345 } employee "Joe" { address "Wifi City" salary 2344 } } } } }

This is not a company.

Relationships

This contribution shows Object-Text mapping using Technology:ANTLR and Language:Java.

For plain syntax checking with Technology:ANTLR see Contribution:antlrAcceptor.

For a basic example of lexing/tokenization with Technology:ANTLR see Contribution:antlrLexer.

For an implementation of Feature:Parsing using semantic actions with Technology:ANTLR see Contribution:antlrParser

For walking a parsed Feature:Company-tree with Technology:ANTLR see Contribution:antlrTrees.

Architecture

The contribution follows a standardized structure:

  • inputs contains input files for tests
  • src/main/antlr contains grammar files for Technology:ANTLR.
  • src/main/java contains the following packages:
  • src/test/java contains the following packages:

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.