mardi 20 septembre 2016

Could I specify patter match priority in lex code?

I've got a related thread in the site(My lex pattern doesn't work to match my input file, how to correct it?)

The problems I met, is about how "greedy" lex will do pattern match, e.g. I've got my lex file:

$ cat b.l
%{
#include<stdio.h>
%}
%%
"12" {printf("head\n");}
"34" {printf("tail\n");}
.* {printf("content\n");}
%%

What I wish to say is, when meet "12", print "head"; when meet "34", print "tail", otherwise print "content" for the longest match that doesn't contain either "12" or "34".

But the fact was, ".*" was a greedy match that whatever I input, it prints "content".

My requirement is, when I use

12sdf2dfsd3sd34

as input, the output should be

head
content
tail

So seems there're 2 possible ways:

1, To specify a match priority for ".*", it should work only when neither "12" and "34" works to match. Does lex support "priority"?

2, to change the 3rd expression, as to match any contiguous string that doesn't contain sub-string of "12", or "34". But how to write this regular expression?

Thanks.

Aucun commentaire:

Enregistrer un commentaire