-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-paramTDM_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是指我上面指出的代码。

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

4

1 回答 1

2

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

如果您想为两个不同的应用程序使用相同的野牛源文件,您将不得不使用一些外部宏预处理器。

我建议避免使用,cpp因为它不知道任何关于野牛语法的内容,并且可能会对野牛语法文件进行许多不希望的更改,包括在代码块中扩展预处理器指令和野牛规则中的意外宏替换。

您想要的条件包含看起来很简单,因此您可以使用 shell 脚本来完成。或者你可以使用m4,它必须存在,因为野牛依赖它。

于 2019-11-13T16:13:27.683 回答