我正在尝试使用 oreilly 的书来学习 lex 和 yacc。我尝试了书中的示例,但它给出了分段错误。
%{
/**
* 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 循环中的 main 函数中遇到分段错误。它根本没有进入循环。
有什么想法吗?谢谢,罗伯特