Quantcast

back tracing of input not happening

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

back tracing of input not happening

momo
Hi,

I have recently started using parboiled and while constructing my custom rules I found out that when the first character of an input matches rule A but the complete string don't match that pattern and the next rule B pattern can match this input exactly, instead of back-tracing and matching the input with the rule B and succeeding, parboiled is throwing error "invalid input" at the not-matching character with respect to the rule A.

Is this expected behaviour of parboiled? Or could there be a problem with my grammar? If it is expected, could you suggest a workaround?

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

Re: back tracing of input not happening

mathias
Administrator
Hey Momo,

could provide the relevant bit of your code together with the sample input you are using?
Seeing the actually code, input and error message will make it much easier for us to be able recommend a solution.

Cheers,
Mathias

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

On 12.6.2015, at 13:22, momo [via parboiled users] <[hidden email]> wrote:

> Hi,
>
> I have recently started using parboiled and while constructing my custom rules I found out that when the first character of an input matches rule A but the complete string don't match that pattern and the next rule B pattern can match this input exactly, instead of back-tracing and matching the input with the rule B and succeeding, parboiled is throwing error "invalid input" at the not-matching character with respect to the rule A.
>
> Is this expected behaviour of parboiled? Or could there be a problem with my grammar? If it is expected, could you suggest a workaround?
>
> Regards,
> Momo
>
> If you reply to this email, your message will be added to the discussion below:
> http://users.parboiled.org/back-tracing-of-input-not-happening-tp4024407.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: back tracing of input not happening

momo
Hi Mathias,

Thanks for the response. PFB the details.
Rules used:
Rule Expression() {
        return Sequence(
                Atom(),
                ZeroOrMore(
                        " AND ",
                        Atom(),
                        push(new CalcNode("AND", pop(1), pop()))
                )
        );
}
Rule Atom() {
        return FirstOf(Parens(), Phrase(), Regex());
}
Rule Parens() {
        return Sequence("( ", Expression(), ") ");
}
Rule Alphabet() {
    return FirstOf(CharRange('a', 'z'), CharRange('A', 'Z'), Digit(), '_', '-');
}
Rule Regex() {
    return Sequence(
    ZeroOrMore(TestNot(IgnoreCase("NOT ")), FirstOf(OneOrMore(Alphabet()), AnyOf(",^$(){}[]|?+*"))),
    push(new CalcNode(matchOrDefault(" ")))
    );
}
Sample 1:
Input:a AND b
Output:AND(a,b)

Sample 2:
Input:ab(c|d)+ AND xyz
Output:AND(REGEX("ab(c|d)+"), xyz)

Sample 3:
Input:(a AND b)
Output:AND(REGEX("(a"), REGEX("b)"))
Here the required output is AND(a,b)

Problem 1:
For input (a AND b), I expect Parens() rule to be used but instead it is matching it with Regex() rule and returning me wrong output.

Problem 2:
I need a rule where space between words is treated as "AND" but when I try to use such a rule, I get error.
Sample input:multiple words
Output:
Invalid input, expected " AND " (line 1, pos 5):
abc klm
    ^
abc klm = abc klm

Exception in thread "main" java.lang.IllegalStateException

Can you suggest me any workaround or whatever I might be missing here to meet my needs?

Thanks & Regards,
Momo
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: back tracing of input not happening

mathias
Administrator
> Problem 1:
> For input (a AND b), I expect Parens() rule to be used but instead it is matching it with Regex() rule and returning me wrong output.

Your `Parens` rule requires a space after then `(` character, no?

> Problem 2:
> I need a rule where space between words is treated as "AND" but when I try to use such a rule, I get error.

Assume this would be the `Phrase` rule, which you haven’t shown.
Can you show what you currently have?

Cheers,
Mathias

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

On 16.6.2015, at 07:34, momo [via parboiled users] <[hidden email]> wrote:

> Hi Mathias,
>
> Thanks for the response. PFB the details.
> Rules used:
> Rule Expression() {
>         return Sequence(
>                 Atom(),
>                 ZeroOrMore(
>                         " AND ",
>                         Atom(),
>                         push(new CalcNode("AND", pop(1), pop()))
>                 )
>         );
> }
> Rule Atom() {
>         return FirstOf(Parens(), Phrase(), Regex());
> }
> Rule Parens() {
>         return Sequence("( ", Expression(), ") ");
> }
> Rule Alphabet() {
>     return FirstOf(CharRange('a', 'z'), CharRange('A', 'Z'), Digit(), '_', '-');
> }
> Rule Regex() {
>     return Sequence(
>     ZeroOrMore(TestNot(IgnoreCase("NOT ")), FirstOf(OneOrMore(Alphabet()), AnyOf(",^$(){}[]|?+*"))),
>     push(new CalcNode(matchOrDefault(" ")))
>     );
> }
> Sample 1:
> Input:a AND b
> Output:AND(a,b)
>
> Sample 2:
> Input:ab(c|d)+ AND xyz
> Output:AND(REGEX("ab(c|d)+"), xyz)
>
> Sample 3:
> Input:(a AND b)
> Output:AND(REGEX("(a"), REGEX("b)"))
> Here the required output is AND(a,b)
>
> Problem 1:
> For input (a AND b), I expect Parens() rule to be used but instead it is matching it with Regex() rule and returning me wrong output.
>
> Problem 2:
> I need a rule where space between words is treated as "AND" but when I try to use such a rule, I get error.
> Sample input:multiple words
> Output:
> Invalid input, expected " AND " (line 1, pos 5):
> abc klm
>     ^
> abc klm = abc klm
>
> Exception in thread "main" java.lang.IllegalStateException
>
> Can you suggest me any workaround or whatever I might be missing here to meet my needs?
>
> Thanks & Regards,
> Momo
>
> If you reply to this email, your message will be added to the discussion below:
> http://users.parboiled.org/back-tracing-of-input-not-happening-tp4024407p4024409.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: back tracing of input not happening

momo
> Problem 1:
> For input (a AND b), I expect Parens() rule to be used but instead it is matching it with Regex() rule and returning me wrong output.

Your `Parens` rule requires a space after then `(` character, no?

The input can be (abc AND xyz). There may not be any space between '(' and the words. I am extending BaseParser and overriding fromStringLiteral method as:
protected Rule fromStringLiteral(String string) {
        return string.endsWith(" ") ?
                Sequence(String(string.substring(0, string.length() - 1)), WhiteSpace()) :
                String(string);
}
I think it is automatically trimming all trailing whitespaces.

> Problem 2:
> I need a rule where space between words is treated as "AND" but when I try to use such a rule, I get error.

Assume this would be the `Phrase` rule, which you haven’t shown.
Can you show what you currently have?

I solved it by introducing one more rule:
    Rule Space() {
    return Sequence(
    Regex(),
    ZeroOrMore(
    TestNot(" AND "), OneOrMore(' '),
        Atom(),
        push(new CalcNode("SPACE", pop(1), pop())))
    );
    }
Loading...