Lex reports error because of ambiguous priority -
identifier [\._a-za-z0-9\/]+ comment "//" <*>{comment} { cout<<"comment\n"; char c; while((c= yyinput()) != '\n') { } } <initial>{s}{e}{t} { begin(sample_state); return set; } <sample_state>{identifier} { strncpy(yylval.str, yytext,1023); yylval.str[1023] = '\0'; return identifier; }
in above lex code, there no error when "// set name" parsed. please notice space after "//" in sentence parsed. however, when "//set name" parsed, there error reported. believe has forward slash appears in both identifier , comment. point going wrong? thanks.
the error caught yyerror
lex matches longest match in context. when input //set
, match {identifier}
pattern. since never set lex state other sample_state
in above code, it's hit problem when in state sample_state
, lexer returns identifier, rather matching {comment}
rule , skipping rest of line.
there number of ways avoid problem. have {comment}
rule match rest of line in pattern:
<*>"//".* { cout << "comment\n"; }
note this, don't need explicit reading of rest of line.
alternately, disallow multiple consecutive /
characters in identifer:
identifier (/?[._a-za-z0-9])*/?
Comments
Post a Comment