escaping - Regex clarification on escape sequences with lex -
i'm creating lexer.l file working intended except 1 part. have rule:
[\(\*.*\*\)] {}
which want make when encounter (* test *) in file, nothing it. when run lex lexer.l warning on lines rules \(, \*, , \) stating can never met. guess question why [\(\*.*\*\)] {} interfere \( , others? how can catch (* test *)?
languages comment syntax (*…*) typically allow nested comments, , nested comments cannot recognized (f)lex because nesting requires context-free grammar, , lexical scanner implements regular languages.
if comments not nest (so (* (* else *) comment, rather prefix of longer comment), can use regular expression
[(][*][^*]*[*]+([^*)][^*]*[*]+)*[)] if require nested comments, can use start conditions , stack (or simulated stack, below):
%x sc_comment %% int comment_nesting = 0; "(*" { begin(sc_comment); } <sc_comment>{ "(*" { ++comment_nesting; } "*"+")" { if (comment_nesting) --comment_nesting; else begin(initial); } "*"+ ; [^(*\n]+ ; [(] ; \n ; } that snippet taken this answer, small adjustment because answer recognizes nested /*…*/ comments. fuller explanation of code appears there.
Comments
Post a Comment