segmentation fault with bison and flex

ⅰ亾dé卋堺 提交于 2019-12-11 00:54:49

问题


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);
}

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


回答1:


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

yyin = stdin;

Just before the main loop.

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



来源:https://stackoverflow.com/questions/3317572/segmentation-fault-with-bison-and-flex

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