In the following code that skips comments, what’s meaning of BEGIN(INITIAL) ?
%x C_COMMENT
"/*" { BEGIN(C_COMMENT); }
"*/" { BEGIN(INITIAL); }
. { }
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
INITIAL is a state which is implicitly declared in all lex programs. (C_COMMENT is also a state, but it is not built-in into lex, so it is declared explicitly.)
BEGIN(statename) just means enter the state statename. So what this lex snippet does is the following: If an “/*” is read it enters the state C_COMMENT, when it sees a “*/” it goes back to the default state.
You could now add rules which ignore all input (except “*/” of course) when in the C_COMMENT state, while doing other things with the input when you are not (or when you are in the INITIAL state).