4

Can a rule be conditionally discarded after being matched and continue to try other rules with lower precedence?

<SOME_STATE>{rule} {
    if(condition) {
        return TOKEN;
    }
    // discard
    // continue and try the other rules below...
}

<SOME_STATE>{other_rule} {
    return OTHER_TOKEN;
}

...

PS: condition depends on other resolutions that can't be matched with regex

PS2: I searched the manuals already :)

PS3: I can't solve this by pushing a new state

marcio
  • 10,002
  • 11
  • 54
  • 83
  • Just to be sure, is your question about [this re2c](http://re2c.org/)? If yes, which version and which command line options are you using? – Alex Mar 31 '15 at 05:58
  • Yes, version is re2c 0.13.5. Manual is at http://re2c.org/manual.html – marcio Apr 01 '15 at 00:29

1 Answers1

1

I assume you're using default command line options, please let me know if otherwise (example -f may change things, but eventually I've to check).

As short answer I would say no, it is not possible.

Long answer:

Personally I find the question a bit weird, maybe you need to re-think the scanner logic (ignore the token at the upper level or check the condition before the re2c block then use a different re2c block?).

Even if possible saving the cursor and with some goto would be still inefficient because the first rule would be always matched.

#include <stdio.h>
#include <string.h>
#define RET(n) printf("%d\n", n); return n
int scan(int i, char *s, int l){
char *p = s;
char *q;
#define YYCTYPE         char
#define YYCURSOR        p
#define YYLIMIT         (s+l)
#define YYMARKER        q
/*!re2c
    re2c:yyfill:enable = 0;
    any = [\000-\377];
*/
YYCTYPE *sc = YYCURSOR; /* save cursor */
/*!re2c
    "rule"  {printf("rule match\n");if(i==1) {RET(1);}}
    any     {goto other;}
*/
other:
YYCURSOR = sc;
/*!re2c
    "rule2" {printf("rule2 match\n"); RET(2);}
    any     {if(YYCURSOR==YYLIMIT) RET(0);}
*/
}
int main(int argc, char **argv) {
    int i;
    for (i=1; i < argc; i++) {
        fprintf(stderr, "[%d]:%s\n", i, argv[i]);
        scan(i, argv[i], strlen(argv[i]));
    }
    return 0;
}

If you may provide a minimal example of your function implemntation, rules and command line options would be helpful.

Alex
  • 3,264
  • 1
  • 25
  • 40