Quantcast

AnyOf()does not match correctly

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

AnyOf()does not match correctly

Marzie
Dear friends,

I have a rule like below in order to match any of h,d,w characters:
Rule Term() {
                return this.Sequence(this.Number(),
                                this.Optional(this.AnyOf("hdw")));
        }
but it matches all characters and does not return the correct matches.
My parser should parse the expressions like : "2d+3w"  or "2h-3w" or "+2d" or ....

"d" stands for day
"w" stands for week
"h" stands for hour

and the parser is a simple calculator which adds or subtracts days, weeks or hours...

this is my whole parser class:
@BuildParseTree
public abstract class CalculatorParser extends BaseParser {

        public abstract Rule InputLine();

        Rule Expression() {
                return this.Sequence(
                                this.Optional(this.AnyOf("+-")),
                                this.Term(),
                                this.ZeroOrMore(this.Sequence(this.AnyOf("+-"), this.Term())));
        }

        Rule Term() {
                return this.Sequence(this.Number(),
                                this.Optional(this.AnyOf("hdw")));
        }

        Rule Number() {
                return this.Sequence(this.CharRange('1', '9'),
                                this.ZeroOrMore(this.CharRange('0', '9')));
        }
}



Thanks in advance,
Marzie
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: AnyOf()does not match correctly

mathias
Administrator
Marzie,

> My parser should parse the expressions like : "2d+3w"  or "2h-3w" or "+2d" or ....

your `AnyOf("hdw") is wrapped in an `Optional`.
Is that intentional?

Cheers,
Mathias

---
[hidden email]
http://www.parboiled.org

> On 14.7.2016, at 08:43, Marzie [via parboiled users] <[hidden email]> wrote:
>
> Dear friends,
>
> I have a rule like below in order to match any of h,d,w characters:
> Rule Term() {
>                 return this.Sequence(this.Number(),
>                                 this.Optional(this.AnyOf("hdw")));
>         }
> but it matches all characters and does not return the correct matches.
> My parser should parse the expressions like : "2d+3w"  or "2h-3w" or "+2d" or ....
>
> "d" stands for day
> "w" stands for week
> "h" stands for hour
>
> and the parser is a simple calculator which adds or subtracts days, weeks or hours...
>
> this is my whole parser class:
> @BuildParseTree
> public abstract class CalculatorParser extends BaseParser {
>
>         public abstract Rule InputLine();
>
>         Rule Expression() {
>                 return this.Sequence(
>                                 this.Optional(this.AnyOf("+-")),
>                                 this.Term(),
>                                 this.ZeroOrMore(this.Sequence(this.AnyOf("+-"), this.Term())));
>         }
>
>         Rule Term() {
>                 return this.Sequence(this.Number(),
>                                 this.Optional(this.AnyOf("hdw")));
>         }
>
>         Rule Number() {
>                 return this.Sequence(this.CharRange('1', '9'),
>                                 this.ZeroOrMore(this.CharRange('0', '9')));
>         }
> }
>
>
>
> Thanks in advance,
> Marzie
>
> If you reply to this email, your message will be added to the discussion below:
> http://users.parboiled.org/AnyOf-does-not-match-correctly-tp4024427.html
> To start a new topic under parboiled users, email [hidden email]
> To unsubscribe from parboiled users, click here.
> NAML

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: AnyOf()does not match correctly

Marzie
Thanks Mathias, I have solved the problem.
Loading...