Bison 编译YaCC和Lex生成的C文件时出错

Bison 编译YaCC和Lex生成的C文件时出错,bison,yacc,flex-lexer,lex,Bison,Yacc,Flex Lexer,Lex,我有两个文件,我正在使用它们来尝试生成一个解析器 文件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 LE

我有两个文件,我正在使用它们来尝试生成一个解析器

文件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;

            }*/

我找不到错误,如果有人能帮我解决这个问题,那将非常有帮助。

至少有3个错误很容易被发现:

  • 您忘记在
    cc
    命令行上使用
    -Wall
    来获取警告(或编译器使用的任何工具来启用警告)

  • 您的一个字符串(第98行上的字符串)具有转义序列
    \m
    ,这是没有意义的。您可能是指
    \n

  • 您尝试在两个位置调用函数
    prinf
    (未定义)。您可能想调用
    printf
    。您还尝试调用未定义的
    print
    一次


我本想给prinf本身打电话,但却忘了添加它看起来的原型。谢谢
%{
#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