tags:

views:

90

answers:

1

Hi,

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  A: 

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.

Gian