bison和flex的分割错误 [英] segmentation fault with bison and flex

查看:200
本文介绍了bison和flex的分割错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用oreilly书学习lex和yacc.我尝试了本书中的以下示例,但它给出了细分错误.

I was trying learn lex and yacc using the oreilly book. I tried following example from book, but it gives segmentation fault.

%{
 /**
  * A lexer for the basic grammar to use for recognizing English sentences.
  */

  #include <stdio.h>
  extern FILE *yyin;
%}

%token NOUN PRONOUN VERB ADVERB ADJECTIVE PREPOSITION CONJUNCTION

%%
sentence: subject VERB object{ printf("Sentence is valid.\n");}
 ;

subject: NOUN
 | PRONOUN
 ;
object:  NOUN
 ;
%%


main()
{
 while(!feof(yyin)) {
  yyparse();
 }

}
yyerror(char *s)
{
 fprintf(stderr, "%s\n", s);
}

我正在使用flex和bison. 在while循环中,我在主函数中遇到分段错误.它根本没有进入循环.

i'm using flex and bison. I'm getting segmentation fault in main function, in the while loop. It is not entering at all to the loop.

有什么想法吗? 谢谢, 罗伯特

Any thoughts? Thanks, Robert

推荐答案

yyin是否真的在某个地方给出了有意义的值?也许尝试分配它:

Is yyin actually given a meaningful value somewhere? Perhaps try assigning it:

yyin = stdin;

就在主循环之前.

并且可能尝试不将其定义为外部",除非它实际上是在其他地方定义的.

and maybe try not defining it "extern" unless it's actually defined somewhere else.

这篇关于bison和flex的分割错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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