1

ケースをサンプルするだけ

test.grm

%{
#include <stdio.h>
%}
%token id
%start program

%%    
program:   exp    
exp:   ID    
ID: id

bison -d test.grm -o test.c aute test.h を生成します

#ifndef YY_TEST_H
# define YY_TEST_H
#ifndef YYDEBUG
# define YYDEBUG 0
#endif
#if YYDEBUG
extern int yydebug;
#endif

#ifndef YYTOKENTYPE
# define YYTOKENTYPE
   enum yytokentype {
     id = 258
   };
#endif

#if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
typedef int YYSTYPE;
# define YYSTYPE_IS_TRIVIAL 1
# define yystype YYSTYPE /* obsolescent; will be withdrawn */
# define YYSTYPE_IS_DECLARED 1
#endif
extern YYSTYPE yylval;
#ifdef YYPARSE_PARAM
#if defined __STDC__ || defined __cplusplus
int yyparse (void *YYPARSE_PARAM);
#else
int yyparse ();
#endif
#else /* ! YYPARSE_PARAM */
#if defined __STDC__ || defined __cplusplus
int yyparse (void);
#else
int yyparse ();
#endif
#endif /* ! YYPARSE_PARAM */
#endif /* !YY_TEST_H  */

#include がこのファイルに含まれていないことがわかります。%{ %} のヘッド ファイルで定義された定義を .grm が使用する場合に問題が発生します。

これが私の質問です。test.h を自動生成するにはどうすればよいですか? %{ %} に含まれるものを含めます。

4

1 に答える 1

2

生成されたヘッダーtest.hにはコードは含まれず、パーサー API のみが含まれます。{%との間のコードを必要とするコードはありません%}

生成された C ファイルには、 と の間のコードとアクション コードの両方が含まれています{%(%}もちろん、生成されたパーサー ロジックも一緒に)。したがって、問題もありません。

生成されたパーサー API と独自の API の両方を含むヘッダー ファイルが必要な場合は#include、逆の方法で -ing を実行する必要があります。生成さtest.hれたものを独自のヘッダー ファイルに含めます。

于 2013-06-24T17:05:02.740 に答える