Search code examples
cregexcompiler-constructionlex

How i can disable maximal munch rule in Lex?


Suppose i want to deal with certain patterns and have the other text(VHDL code) as it is in the output file.

For that purpose i would be required to write a master rule in the end as

(MY_PATTERN){
// do something with my pattern
}

(.*){
return TOK_VHDL_CODE;

}

Problem with this strategy is MY_PATTERN is useless in this case and would be matched with .* by maximum munch rule.

So how can i get this functionality ?


Solution

  • The easy way is to get rid of the * in your default rule at the end and just use

    .    { append_to_buffer(*yytext); }
    

    so your default rule takes all the stuff that isn't matched by the previous rules and stuffs it off in a buffer somehwere to be dealt with by someone else.