Parser - Segmentation fault when calling yytext

前端 未结 2 1200
既然无缘
既然无缘 2021-01-25 08:43

My parser is recognizing the grammar and indicating the correct error line using yylineno. I want to print the symbol wich caused the error.

int yyerror(string s         


        
2条回答
  •  逝去的感伤
    2021-01-25 09:08

    Depending on the version of lex you are using, yytext may be an array or may be a pointer. Since it is defined in a different compilation unit, if it is an array and you declare it as a pointer, you won't see any error messages from the compiler or linker (linker generally don't do type checking). Instead it will treat the first several characters in the array as a pointer and try to dereference it and probably crash.

    If you are using flex, you can add a %pointer declaration to the first section of your .l file to ensure that it is a pointer and not an array

提交回复
热议问题