解析器 - 调用yytext时的分段故障 [英] Parser - Segmentation fault when calling yytext

查看:164
本文介绍了解析器 - 调用yytext时的分段故障的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的解析器正在识别语法,并使用yylineno指示正确的错误行。我想打印导致错误的符号。

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)
{
  extern int yylineno;  // defined and maintained in lex.yy.c
  extern char *yytext;  // defined and maintained in lex.yy.c

  cerr << "error: " << s << " -> " << yytext << " @ line " << yylineno << endl;
  //exit(1);
}

当我写的语法不能接受的东西时, p>

I get this error when I write something not acceptable by the grammar:


错误:语法错误 - >分段错误

error: syntax error -> Segmentation fault

我不应该使用yytext吗?如果没有什么变量包含导致语法错误的符号?

Am I not supposed to used yytext? If not what variable contains the symbol that caused the syntax error?

感谢

推荐答案

您是使用 lex flex 吗?如果您使用的是 lex yytext char [], code> char *。

Are you using lex or flex? If you're using lex,yytext is a char[], not a char*.

EDIT 如果您不使用你应该是,它在各方面都是优越的,从它的出现近30年前。 已过时。

EDIT If you aren't using flex you should be, it is superior in every way and has been from the moment of its appearance nearly 30 years ago. lex was obsoleted on that day.

这篇关于解析器 - 调用yytext时的分段故障的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆