yytext contains characters not in match

房东的猫 提交于 2019-11-29 16:42:40

I don't see how that output could be produced from your lexer, but it is easy to see how it could be produced in your parser.

Basically, it is not correct to retain the value of yytext:

yylval.s = yytext;  /* DON'T DO THIS */

In effect, that is a dangling pointer because yytext is pointing to private memory inside the lexer framework, and the pointer is only valid until the next time the lexer is called. Since the parser generally needs to look at the next input token before executing a reduction action, it is almost certain that the pointer in the s member of each terminal in the production will have been invalidated by the time the action is executed.

If you want to keep the string value of the token pointed to by yytext, you must copy it:

yylval.s = strdup(yytext);

and then you will be responsible for freeing the copy when you no longer need it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!