Headline

Parse companies with parser combinators in Language:Java

Characteristics

We implement a functor-based combinator library for top-down parsing with which we parse concrete textual syntax for companies. Such parsing implements Feature:Parsing. Salaries can be totaled along with parsing. The library supports plain acceptance and proper parsing into an intermediate representation. There are acceptor and parser combinators for all common EBNF constructs as well as building blocks for lexical analysis. Class inheritance can also be used to attach semantic actions to applications of parser combinators.

Illustration

The data model is implemented as plain textual files:

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 } } } } }

Feature:Parsing is implemented with helper classes using the library:

public class Parsing {

    public static Boolean parseCompany(String file) throws FileNotFoundException, IOException {
        Input input = new Input(new FileReader(file));
        Double totalValue = new CompanyParser().parse(input);
        if (totalValue != null) {
            return true;
        } else {
            return false;
        }
    }

    public static boolean acceptCompany(String file) throws FileNotFoundException, IOException {
        Input input = new Input(new FileReader(file));
        return (new CompanyAcceptor().accept(input));
    }

}

Helper org.softlang.company.features.parser.CompanyParser:

    /** 
     * Parse companies.
     */
    public static Parser<Double> company() {
        return new Parser<Double>() {
            public Double parse(Input i) {
                return (
                        mkSnd(
                            sequence(
                                SPECIAL("company"),
                                STRING,
                                SPECIAL("{")),
                            mkFst(
                                mkFunction(
                                    mkStar(department()),
                                    sumList
                                ),
                                sequence(
                                    SPECIAL("}"),
                                    EOF // Test that all input has been consumed.
                        )))).parse(i);
                }
        };
    }

Feature:Total is implemented as part of Feature:Parsing with a semantic action:

        /* 
         * Total is implemented by the Parser 
         * See org.softlang.company.features.parser.CompanyParser
         */
    public static Double total(String file) throws FileNotFoundException, IOException {
        Input input = new Input(new FileReader(file));
        return (new CompanyParser().parse(input));
    }

Test cases are implemented for all Namespace:Features. There is also an invalid input:

company "foobar" { department "barfoo" { } }

Relationships

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

For lexer-based text processing in pure Language:Java see Contribution:javaScanner.

For lexing/tokenization with Technology:ANTLR see Contribution:antlrLexer.

For a custom made lexer in pure Language:Java see Contribution:javaLexer.

For parsing with semantic actions with Technology:ANTLR see Contribution:antlrParser.

For recursive-descent parsing in pure Language:Java] see Contribution:javaParser.

For parser combinators in pure Language:Java] see Contribution:javaParseLib.

For object/text mapping from test to companies with Technology:ANTLR see Contribution:antlrObjects.

For object/text mapping from text to trees with Technology:ANTLR see Contribution:antlrTrees.

Architecture

The contribution follows a standardized structure:

  • inputs contains input files for tests
  • src/main/java contains the following packages:
    • javaf.prelude for function patterns.
    • parseLib.acceptor for accaptance.
    • parseLib.parser for parsing.
    • parseLib.util for type declarations.
    • org.softlang.company.features for implementations of Functional requirements.
      • org.softlang.company.features.parser for lexer, acceptor and parser for companies.
  • 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.