flex和bison无效的字符错误

时间:2019-11-13 07:08:03

标签: c++ bison flex-lexer bisonc++

%{ 
#include <stdio.h>
#include <stdarg.h>
#include <iostream>
#include <ostream>
#include <string>

#ifndef TDM_PIN_MAP_TEST
#include <tdmPinMap.h>

namespace dc {
  class tdmPinMap; 
}
#endif
#undef YYLMAX
#define YYLMAX 100000
extern int lineno;
extern "C" int yywrap();
#ifndef TDM_PIN_MAP_TEST
int yyerror(dc::tdmPinMap &pm,std::string msg);
#else
int yyerror(std::string msg);
#endif
extern "C" char yytext[];
extern "C" int yylex();
%}

#ifndef TDM_PIN_MAP_TEST
%parse-param {dc::tdmPinMap &pm}
#endif

%union
{
   int val;
   char *str;
}

%token EQUALS
%token COMMA
%token SEMICOLON
%token PACKAGE
%token PIN
%token PORT
%token <str> ALPHANUMERIC
%type <str> port_name
%type <str> pin_name
%type <str> package_name

%%
             ;
pin_name         : ALPHANUMERIC
             {
               $$ = $1;
             }
             ;
%%

#ifndef TDM_PIN_MAP_TEST
int yyerror(dc::tdmPinMap &pm,std::string msg)
#else
int yyerror(std::string msg)
#endif
{
  return 1;
}

我的问题:

我收到以下代码的无效字符错误。

#ifndef TDM_PIN_MAP_TEST
%parse-param {dc::tdmPinMap &pm}
#endif

仅在未定义parse-param宏的情况下,我才想定义TDM_PIN_MAP_TEST,但是出现以下错误。

bison -d -v -d  pinMap.ypp
pinMap.ypp:28.1: invalid character: `#'
pinMap.ypp:28.2-7: syntax error, unexpected identifier
make: *** [pinmap_yacc.cpp] Error 1

行号28指的是我上面指出的代码。

感谢您解决上述错误的任何指针。

1 个答案:

答案 0 :(得分:2)

Bison不提供任何有条件地包含指令或规则的机制。在C代码块外部类似于C预处理程序指令(例如#ifndef)的行将被标记为语法错误。

如果要对两个不同的应用程序使用相同的野牛源文件,则必须使用一些外部宏预处理器。

我建议避免使用cpp,因为它不会涉及任何有关bison语法的知识,并且可能会对bison语法文件进行许多不必要的更改,包括在代码块和意外的宏中扩展预处理程序指令。野牛规则中的替换。

您所需的条件包含看起来非常简单,因此您可以使用Shell脚本来完成。或者,您可以使用m4,因为bison依赖它,所以必须使用它。