使用Flex和Bison指定前缀时编译错误

问题描述 投票:3回答:1

我正在尝试使用给定的前缀编译一个简单的野牛解析器-已解决in this question。我正在使用Bison版本2.4.1和flex版本2.5.35(我无法使用bison 2.6)。我遇到了一个编译器错误,使我无法正常运行:

即:

make
Making all in src
make[1]: Entering directory `/stage/pool14/eholum/yacc_example/src'
/bin/sh ../ylwrap parcalc.y y.tab.c parcalc.c y.tab.h parcalc.h y.output parcalc.output -- bison -y  -d -p="calcYY"
...yacc_example/src/parcalc.y:27.7-13: warning: type clash on default action: <result> != <>
conflicts: 4 shift/reduce
updating parcalc.h
gcc -DPACKAGE_NAME=\"calculator\" -DPACKAGE_TARNAME=\"calculator\" -DPACKAGE_VERSION=\"1.0\" -DPACKAGE_STRING=\"calculator\ 1.0\" -DPACKAGE_BUGREPORT=\"\" -DPACKAGE=\"calculator\" -DVERSION=\"1.0\" -I.     -g -O2 -MT parcalc.o -MD -MP -MF .deps/parcalc.Tpo -c -o parcalc.o parcalc.c
parcalc.c:1053: error: expected identifier or ‘(’ before ‘=’ token
parcalc.c:1061: error: expected identifier or ‘(’ before ‘=’ token
parcalc.c:1064: error: expected identifier or ‘(’ before ‘=’ token
parcalc.c:1067: error: expected identifier or ‘(’ before ‘=’ token
parcalc.c:1089: error: expected identifier or ‘(’ before ‘=’ token
parcalc.y:75:21: error: lexcalc.c: No such file or directory
make[1]: *** [parcalc.o] Error 1

我当前正在使用自动制作,但是如果我使用flex和bison进行构建,然后尝试使用c进行编译,则会遇到相同的错误。这是我的Makefile.am:

bin_PROGRAMS = calculator
AM_YFLAGS = -d -p="calcYY"
calculator_SOURCES = parcalc.y lexcalc.l

lex代码在lexcalc.l中(请注意,我使用%option指定前缀,但是使用cl标志时出现相同的错误):

%{
#include <stdio.h>
#include "parcalc.h"
%}

%option noyywrap
%option prefix="yy"

%%

[ \t] ; 

[0-9]+\.[0-9]+ { 
    yylval.flval = atof(yytext);
    return FLOAT;
}

[0-9]+ {
    yylval.inval = atoi(yytext);
    return INT;
}

- { return MINUS; }

\+ { return PLUS; }

\n {
    yylineno++; 
    return NEWLINE; 
}

. ;

%%

并且Bison代码在parcalc.y中:

%{
#include <stdio.h>
%}

%token INT FLOAT PLUS MINUS NEWLINE;

%union {
    int inval;
    float flval;
    char oper;
    char result[100];
}

%type <inval> INT;
%type <flval> calc FLOAT
%type <oper> PLUS MINUS;
%type <result> line;

%%

lines:
     line lines
    | line
    ;

line:
      NEWLINE
    | calc NEWLINE {
        printf("Result = %f\n", $1);   
      }
    ;

calc :
      calc PLUS calc { 
        ($$ = $1 + $3);
      }
    | calc MINUS calc { 
        ($$ = $1 - $3); 
      }
    | INT { 
        ($$ = $1);
      }
    | FLOAT { 
        ($$ = $1); 
      }
    ;

%%

extern int calcYYparse();
extern FILE *calcYYin;

int main() {

    FILE *myfile = fopen("infile.txt", "r");

    if (!myfile) {
        fprintf(stderr, "can't open infile.txt\n");
        return 1;
    }

    calcYYin = myfile;

    do {
        calcYYparse();
    } while (!feof(calcYYin));

    return 0;
}

void calcYYerror(const char *s) {
    fprintf(stderr, "Unrecognized format\n");
}

我应该注意,当我丢弃前缀时,一切似乎都可以正常工作,因此,我猜想在生成的c文件中如何从外部声明事物时有些麻烦。浏览那些我还无法弄清楚的东西。如果有人搞砸了多个解析器,并且对如何使这些东西完美地结合在一起有一些见识,我将不胜感激。

c compiler-errors bison flex-lexer
1个回答
2
投票

感谢Michael发现了将前缀标志传递给bison编译器的问题。即

AM_YFLAGS = -d -p="calcYY"

AM_YFLAGS = -d -p"calcYY"

也有点相关,前缀仅更改yyparse,yylex,yyerror,yynerrs,yylval,yylloc,yychar和yydebug。因此,yiny无需更改。

© www.soinside.com 2019 - 2024. All rights reserved.