我有两个文件正在尝试生成解析器。
文件1: drive.l
%{
#include<stdio.h>
#include<string.h>
#include"y.tab.h"
/*
#define START 1
#define STOP 2
#define FWD 3
#define BACK 4
#define UP 5
#define DOWN 6
#define TURN 7
#define LEFT 8
#define RIGHT 9
#define SET 10
#define VAR 11
#define DEGREE 12
#define EQUAL 13
*/
struct VarTab{
char VarName[50];
int Id;
struct Vartab* next;
};
struct DegTab{
int Deg,Min,Sec;
int Id;
struct DegTab* next;
};
struct VarTab* VHead=NULL;
struct DegTab* DHead=NULL;
int VTop = 0;
int DTop =0;
%}
delim [ \t]
ign {delim}+
var [a-zA-Z]+[a-zA-Z0-9]+
num [0-9]
degree {num}+((\.{num})|(\.{num}\.{num}))?
%%
{ign} /**/
\n {printf("\n");}
START {printf("<STR> ");return(START);}
STOP {printf("<STP> ");return(STOP);}
FWD {printf("<FWD> ");return(FWD);}
BACK {printf("<BAK> ");return(BACK);}
UP {printf("<UP> ");return(UP);}
TURN {printf("<TUR> ");return(TURN);}
LEFT {printf("<LFT> ");return(LEFT);}
RIGHT {printf("<RGT> ");return(RIGHT);}
SET {printf("<SET> ");return(SET);}
= {printf("<EQL> ");return(EQUAL);}
{var} {printf("<VARIABLE> ");RegVar();return(VAR);}
{degree} {printf("<DEG> ");RegDeg();return(DEGREE);}
%%
int RegVar(){
/*
int i;
char var[50];
char* rd = yytext;
for(i=0;i<=yyleng;i++){
var[i-1] = *rd;
i++;
rd++;
}
var[i-1]='\0';
VTop++;
struct VarTab* tmp;
tmp->Id = VTop;
strcpy(tmp->VarName,var);
tmp->next = NULL;
if(VHead==NULL)
VHead = tmp;
else{
struct VarTab* parse = VHead;
while(parse->next!= NULL)
parse = parse->next;
parse->next = tmp;
}*/
return VTop;
}
int RegDeg(){
/*
int D=0,M=0,S=0;
int st = 0;
int i;
char* rd;
for(i=1;i<=yyleng;i++){
if(st==0){
if(*rd != '.')
D = (D*10) + (*rd - 48);
else
st =1;
i++;
rd++;
}
if(st==1){
if(*rd!='.')
M = (M*10) + (*rd -48 );
else
st =2;
rd++;
i++;
}
if(st==2){
if(i<=yyleng){
S = (S*10) + (*rd -48);
rd++;
i++;
}
}
}
DTop++;
struct DegTab* tmp;
tmp->Deg = D;
tmp->Min = M;
tmp->Sec = S;
tmp->Id = DTop;
tmp->next = NULL;
if(DHead == NULL)
DHead = tmp;
else{
struct DegTab* parse;
parse = DHead;
while(parse->next!=NULL)
parse = parse->next;
parse->next = tmp;
}*/
文件2: drive.y
%{
#include<stdio.h>
#include<string.h>
/*#define START 1
#define STOP 2
#define FWD 3
#define BACK 4
#define UP 5
#define DOWN 6
#define TURN 7
#define LEFT 8
#define RIGHT 9
#define SET 10
#define VAR 11
#define DEGREE 12
#define EQUAL 13
*/
void yyerror(const char *str)
{
fprintf(stderr,"error: %s\n",str);
}
int yywrap()
{
return 1;
}
main()
{
yyparse();
}
%}
%token TURN START STOP FWD BACK UP DOWN LEFT RIGHT SET VAR EQUAL DEGREE
%%
command :
/*Empty*/
| statuscode
| valuecode
| motioncode
;
statuscode:
START
{
printf("Started.\n");
}
| STOP
{
prinf("Stoping.\n");
}
| UP
{
prinf("Up phase.\n");
}
| FWD
{
print("Forward.\n");
}
| BACK
{
printf("Backward.\n");
}
;
valuecode:
SET VAR EQUAL DEGREE
{
printf("Initialized a variable:\n");
}
;
motioncode:
TURN direction
{
printf("OK \n");
}
;
direction:
LEFT DEGREE
{
printf("Turning left with value - ");
}
| RIGHT DEGREE
{
printf("tunring Right with value -");
}
| LEFT VAR
{
printf("Turning Left with Variable -");
}
| RIGHT VAR
{
printf("Turning Right with Variable\m");
}
;
%%
我尝试使用以下命令编译代码并收到并错误。
$lex drive.l
$ yacc -d drive.y
$ cc lex.yy.c y.tab.c -o ex -lfl
drive.y: In function ‘yyparse’:
drive.y:98:24: warning: unknown escape sequence: '\m' [enabled by default]
/tmp/ccMVHgWf.o: In function `yyparse':
y.tab.c:(.text+0x35e): undefined reference to `prinf'
y.tab.c:(.text+0x36c): undefined reference to `prinf'
y.tab.c:(.text+0x37a): undefined reference to `print'
collect2: error: ld returned 1 exit status
我找不到错误,如果有人可以帮我解决这个问题,那将会很有帮助。
答案 0 :(得分:1)
很容易发现至少3个错误:
您忘记在-Wall
命令行上使用cc
来获取警告(或编译器用于启用警告的任何内容)
您的一个字符串(第98行的字符串)具有转义序列\m
,这是没有意义的。你可能意味着\n
您尝试在2个位置调用函数prinf
(未定义)。您可能打算致电printf
。您还尝试调用未定义的print
一次。