Coder Social home page Coder Social logo

Comments (2)

jon-hanson avatar jon-hanson commented on May 26, 2024

Firstly, you don't need to use both the choice and or combinators at the same time. choice(A, B, C) is a short-hand for A.or(B).or(C). I.e. you can define p either using choice:

final Parser<Chr, String> p = choice(string("128"), string("16"), string("1"));

or using or:

final Parser<Chr, String> p = string("128").or(string("16")).or(string("1"));

(both are equivalent)

Next, your grammar isn't LL(1) because for the rule:

P = "128" | "16" | "1"

we need to look 2 characters ahead when a "1" is encountered - in order to resolve between the 3 rules that begin with a "1". To resolve this you need to restructure the rule so that it first parses the "1" and then attempts to parse the remainder:

P = "1" R
R = "28" | "6" | <eof>

Using the funcj parser this would like something like this:

final Parser<Chr, String> p =
       string("1")
               .and(
                       choice(
                               string("28"),
                               string("6"),
                               Combinators.<Chr>eof().map(u -> "")
                       )
               ).map(a -> b -> a + b);

or (slightly cleaner):

final Parser<Chr, String> p =
        string("1")
                .andR(
                        choice(
                                string("28").map(u -> "128"),
                                string("6").map(u -> "16"),
                                Combinators.<Chr>eof().map(u -> "1")
                        )
                );

This now gives the desired result:

Success{value=128, next=StringInput{3,data="EOF"}
Success{value=16, next=StringInput{2,data="EOF"}
Success{value=1, next=StringInput{1,data="EOF"}

Hope that helps.

from funcj.

matozoid avatar matozoid commented on May 26, 2024

Thanks for the help, I get it now.

from funcj.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.