I require positive lookbehind assertions in lex (flex 2.5.35). After investigating the documentation, I don't see a direct way to do this. It has something similar to a lookahead assertion (the r/s syntax), but not lookbehind. What's the best way to achieve the same effect?
Here's an example: Say I have the folling rules in my scanner spec file:
a printf("matched a ");
b printf("matched b ");
c printf("matched c ");
d printf("matched d ");
How would I match a 'd' following a 'b', and the 'b' itself, so that on an input of 'abd' I would get:
matched a matched b matched d following b
But for a string 'acd'
matched a matched c matched d
The rule:
bd printf("matched d following b ");
obviously doesn't work since it consumes the b; for 'abd' it outputs:
matched a matched d following b
If I had pcre lookbehinds I could write:
(?<=b)d printf("matched d following b ");
and all would be well, but lex doesn't support this.
You can probably achieve what you want with start conditions, at the cost of a more complicated scanner. Start conditions allow you to conditionally enable rules based on what has matched previously. Here is an example:
%x matched_b
%%
<INITIAL,matched_b>{
a { printf("matched a\n"); BEGIN(INITIAL); }
b { printf("matched b\n"); BEGIN(matched_b); }
c { printf("matched c\n"); BEGIN(INITIAL); }
}
d printf("matched d\n");
<matched_b>d { printf("matched d following b\n"); BEGIN(INITIAL); }
With this scanner I get:
$ echo abcd | ./5615080
matched a
matched b
matched c
matched d
$ echo abdd | ./5615080
matched a
matched b
matched d following b
matched d
I can think of two possibilities, if you have only a lookahead assertion.
Invert your string, and search for your also inverted pattern. What normally was before your pattern is now ahead of it.
I think for some regex flavors it is possible to parse from right to the left, maybe this can help you together with your lookahead.
Otherwise you should post some example strings and what you expect as result, perhaps it can be achieved without using look behind assertions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With