带有flex和bison的无效字符错误

问题描述 投票:-1回答:1
%{ 
#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指的是我上面指出的代码。

赞赏解决上述错误的任何指针。

c++ bison flex-lexer bisonc++
1个回答
2
投票

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

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

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

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

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