flex와 bison을 사용하여 파서를 작성하려고합니다. 그러나 파일을 수정하는 방법과 상관없이 "1 행의 구문 오류"오류가 항상 나타납니다."줄 : 1 : 오류 : 구문 오류!"를 수정하는 방법
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "bool.h"
#include "parser.h"
#include "context.h"
#include "vtl4.tab.h"
extern FILE * yyin;
extern FILE * yyout;
extern int yylex();
extern int yywrap();
%}
%union {
struct simpleNode *ast;
double d;
int i;
bool b;
char* string;
struct symbol *sym;
}
%type <ast> root stmts stmt
%token <string> CONTENT
%token FINAL
%%
root:stmts FINAL {printf("root\n");$$ = process($1);traverse($$);}
;
stmts: {printf("stmts:stmt\n");$$ = 0;}
|stmts stmt {printf("stmts:stmts stmt\n");$$ = add_ybrother($1,$2);}
;
stmt:CONTENT {printf("stmt\n");$$ = text($1);}
;
%%
int main(){
FILE *src;
src = fopen("test.vm","r");
yyin = src;
yyparse();
fclose(src);
return 1;
}
int yywrap(){
return 1;
}
메이크 :
%{
#include<stdio.h>
#include<string.h>
#include "context.h"
#include "bool.h"
#include "vtl4.tab.h"
%}
%%
(.|\n)* {yylval.string = yytext;return CONTENT;}
<<EOF>> {return FINAL;}
%%
이는 vtl4.y 파일입니다
$asfdfsdf
sdfsdfs
sdfsdfsd
sdfsdfsd
sfsdfd
이이 vtl4.l 파일입니다 이 yyinput의 test.vm 파일입니다 :
CC=cc
FLEX=vtl4.l
BISON=vtl4.y
parse:vtl4.tab.c lex.yy.c
$(CC) -o out *.c -ll
vtl4.tab.c:$(BISON)
bison -d $(BISON) --report=all
lex.yy.c:$(FLEX)
flex $(FLEX)
유엔 ./out, 그것은 올바른 결과를 인쇄 할 것이지만, 항상 "라인 : 1 : 오류 : 구문 오류"라고 마침내! 나는 왜 그런지 몰라?
그것은 내가 Yacc에 규칙을
root:stmts FINAL {printf("root\n");$$ = process($1);traverse($$);}
root:stmts {printf("root\n");$$ = process($1);traverse($$);}
에
<<EOF>> {return FINAL;}
<<EOF>> {yyterminate();}
에 렉스 규칙을 편집하고 수정할 때 잘 작동
하지만 왜 그런지 모르겠습니다.