Comments (2)
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.
Thanks for the help, I get it now.
from funcj.
Related Issues (20)
- Try/Option etc recursion scheme methods
- IList::flatmap does not assign result properly HOT 1
- Allow data to be serialised even if a ctor can't be found
- Add support for Avro
- Allow dynamic type tags to be disabled
- funcj.codec - add support for type aliases
- Codec support for configurable field naming
- Codec: registerAllowedPackage support for package wildcards
- Add support to JSON and XML streaming codecs for support pretty-printing
- `sepBy1` should return `NonEmpty<T>` HOT 1
- `choice` and `or` combinators do not work correctly with `string` HOT 2
- Strange behaviour for `choice` HOT 4
- Need help with my grammar definition HOT 4
- Partial parsing fails HOT 5
- Calling many() on an uninitialized reference throws exception but it shouldn't. HOT 3
- Request: Change visibility of various parser fields in `JsonCombParser` to public HOT 7
- Example parsers with specific Token data structure vs Chr HOT 3
- Help: Parsing expressions with operator precedence. HOT 2
- Parser doesn't try the alternative choice if first parser fails HOT 2
- Code coverage not complete for property tests in StateTe.java HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from funcj.