My flex file is given below. Beyond trivial symbols, it defines a state machine to read strings. So it starts whenever it encounters an "
and terminates on locating a following "
. Now when I feed this flex file an input with two strings followed by each other like this:
"this" "apple"
It correctly identifies this but fails to find apple. Why is this current behavior happening? I have put in BEGIN(INITIAL)
identifier but it does not work.
/* sample simple scanner
*/
%{
int num_lines = 0;
#define CLASS 10
#define LAMBDA 1
#define DOT 2
#define PLUS 3
#define OPEN 4
#define CLOSE 5
#define NUM 6
#define ID 7
#define INVALID 8
#define MAX_STR_CONST 256;
#define COMMENT 11;
char string_buf[256];
char *string_buf_ptr;
char string_buf_cmnt[256];
char *string_buf_ptr_cmnt;
int size = 0;
%}
%x str
%x comment1
%x comment2
%%
\" {
string_buf_ptr = (char*)malloc(8); size = 0; BEGIN(str);}
<str>\" { /* saw closing quote - all done */
/* return string constant token type and
* value to parser
*/
*string_buf_ptr = '\0'; /* apppend the end of string with null */
string_buf_ptr = string_buf_ptr - size; /* scale back string ptr to start */
int i = 0;
for (; i < size; i++){
yytext[i]=*(string_buf_ptr + i); /* copy each character to yytext */
}
yytext[i]='\0'; /* apppend the end of string with null */
free(string_buf_ptr);
BEGIN(INITIAL); /* go back to start */
return ID;
}
<str>\n {
/* error - unterminated string constant */
/* generate error message */
//printf("error is here\n");
}
<str>\\0 ;
<str>\\[0-7]{1,3} {
/* octal escape sequence */
int result;
(void) sscanf( yytext + 1, "%o", &result );
if (result == 0x00){
*string_buf_ptr++ = '0';
} else {
if ( result > 0xff ){
/* error, constant is out-of-bounds */}
else{*string_buf_ptr++ = result;}
}
size++;
}
<str>\\[0-9]+ {
/* generate error - bad escape sequence; something
* like '\48' or '\0777777'
*/
}
<str>\\n *string_buf_ptr++ = '\n'; size++;
<str>\\t *string_buf_ptr++ = '\t'; size++;
<str>\\r *string_buf_ptr++ = '\r'; size++;
<str>\\b *string_buf_ptr++ = '\b'; size++;
<str>\\f *string_buf_ptr++ = '\f'; size++;
<str>\\a *string_buf_ptr++ = '\a'; size++;
<str>\\(.|\n) *string_buf_ptr++ = yytext[1]; size++;
<str>[^\\\n\"]+ {
//printf("there\n");
char *yptr = yytext;
int i = 0;
while ( *yptr )
{
*string_buf_ptr++ = *yptr++;
yytext[i] = *(string_buf_ptr-1);
size++;
i++;
}
}
[ ]+ //printf("space\n");
%%
main(int argc, char **argv) {
int res;
yyin = stdin;
while(res = yylex()) {
printf("class: %d lexeme: %s line: %d\n", res, yytext, num_lines);
}
}