후 플렉스에서 문자열을 반환 :나는 다음과 같은 코드 플렉스에 문자열을 읽고 있어요 경우 토큰 화
및 here에서 가져 %x str
%%
char string_buf[MAX_STR_CONST];
char *string_buf_ptr;
\" string_buf_ptr = string_buf; BEGIN(str);
<str>\" { /* saw closing quote - all done */
BEGIN(INITIAL);
*string_buf_ptr = '\0';
/* return string constant token type and
* value to parser
*/
}
<str>\n {
/* error - unterminated string constant */
/* generate error message */
}
<str>\\[0-7]{1,3} {
/* octal escape sequence */
int result;
(void) sscanf(yytext + 1, "%o", &result);
if (result > 0xff)
/* error, constant is out-of-bounds */
*string_buf_ptr++ = result;
}
<str>\\[0-9]+ {
/* generate error - bad escape sequence; something
* like '\48' or '\0777777'
*/
}
<str>\\n *string_buf_ptr++ = '\n';
<str>\\t *string_buf_ptr++ = '\t';
<str>\\r *string_buf_ptr++ = '\r';
<str>\\b *string_buf_ptr++ = '\b';
<str>\\f *string_buf_ptr++ = '\f';
<str>\\(.|\n) *string_buf_ptr++ = yytext[1];
<str>[^\\\n\"]+ {
char *yptr = yytext;
while (*yptr)
*string_buf_ptr++ = *yptr++;
}
. 이제 Flex는 yytext
또는 yytext
에있는 문자 중 하나만 반환하지만 내 문자열은 'string_buf_ptr'에 저장됩니다. 스캐너에서 어떻게 검색합니까? here이 가리키고 있듯이 yytext
을 현재 토큰 이상으로 수정하면 복잡 할 수 있습니다. 그래서는이 작업을 수행하는 간단한 스캐너에이 문자열을 반환하는 것입니다 : 당신이 세계로 string_buf_ptr를 선언하는 경우
ntoken = yylex();
while(ntoken) {
prinf("%s\n", yytext);
ntoken = yylex();
}