나는 바이슨과 플렉스를 사용하여 단순화 된 C 파서를 만들고있다. 필자는 Bison 파일에서 함수를 감지하는 문법 규칙을 작성했으며, 파서가 함수의 이름을 C 모니터링 프로그램에 보내도록합니다. 여기에 구현 한 것에 매우 단순화 된 예제입니다들소와 함수 이름을 반환하는 방법?
내 모니터 프로그램은
/*monitor.h*/
#ifndef MONITOR_H
#define MONITOR_H
extern int noLig;
extern char* yytext;
#endif
/*monitor.c*/
#include <stdio.h>
#include <monitor.h>
#include <project.tab.h>
int noLig=0;
int main (int argc, char * argv[]) {
printf("flex-\n");
int err_code=yyparse();
if (err_code == 0) {
printf("It went fine\n");
} else {printf("It didn't go well\n");}
return 0;
}
project.l 파일
%{
#include <stdio.h>
#include <monitor.h>
#include <project.tab.h>
%}
%%
"\"[a-zA-Z0-9]+"\" {ECHO; yylval.str=yytext; return STR;}
[.,;=()\[\]\{\}] { return yytext[0]; }
"char" {ECHO; printf("-"); yylval.str=yytext; return TYPE;}
"int" {ECHO; printf("-");yylval.str=yytext; return TYPE;}
"float" {ECHO; printf("-");yylval.str=yytext; return TYPE;}
"double" {ECHO; printf("-");yylval.str=yytext;return TYPE;}
[a-zA-Z][a-zA-Z0-9]* {ECHO; printf("-");yylval.str = yytext; return VAR;}
[ \t\n\b]+ {noLig++;}
"//".* {}
. {printf(":%cwhat is this",yytext[0]);}
%%
project.y 파일
%{
#include <stdio.h>
#include <monitor.h>
int yylex();
int yyerror();
%}
%union {
char *str;
int i;
}
%define parse.error verbose
%type <i> INT
%type <str> TYPE STR VAR
%token TYPE INT STR VAR
%start Program
%%
Program: function_l
;
function_l: function
| function_l function
;
function: TYPE VAR '(' param_prototype_ld ')' '{' instruction_l '}'
{printf("\n\nbison-\n%s\n",$1);}
;
param_prototype_ld: /*empty*/
| param_prototype_l
;
param_prototype_l: param_prototype
| param_prototype_l ',' param_prototype
;
param_prototype: TYPE VAR
;
instruction_l: /*empty*/
| VAR ';'
| VAR instruction_l
;
%%
int yyerror (char* s) {
fprintf(stderr, "Compilator3000:l.%d: %s\n", noLig, s);
}
TEST.C 파일
int main (int arg) {
a;
}
은 경고없이, 확실히 컴파일합니다. 내가 ./monitor < test.c
을 실행할 때, 나는 다음과 같은 출력을 얻을 :
flex-
int-main-int-arg-a-
bison-
int main (int arg) {
a;
}
It went fine
이유는 무엇입니까 전체 기능 블록을 반환 $1
들소 변수? 반환 유형 만 얻으려면 어떻게해야합니까? (결국 내 목표는 반환 유형, 함수 이름 및 인수 유형을 인쇄하는 것입니다.)
을 만드는 데 사용'의 내용 yytext' 각 경기와 변화, 그리고해야 할 수 있습니다 'strdup'와 같은 함수로 복사하십시오. – kdhp