私は今 Bison を学んでいて、それを使っておもちゃのコンパイラを書いています。私が見つけたように、%union ディレクティブを使用してさまざまな値を格納できますが、残念ながら %union は独自のクラスをサポートしていません。どうすればそれらを保存できますか? と呼ばれる基本クラスが1つあるとしましょうobject_type
。このクラスには、いくつかの仮想関数とbool_type
、 、 などのいくつかの継承があります。任意のタイプのサブクラスを保持できるint_type
ポインターを作成できますが、保持できるオブジェクトは 1 つだけです。object_type*
たとえば、状態がある場合はどうなりOBJECT AND OBJECT
ますか?$1
ユニオンを使用する場合は、 andを使用し$3
て値を取得できます。しかし、必要な機能を持つ独自の型で作業することをお勧めします。解決策はありますか?前もって感謝します!
%{
#include <iostream>
using namespace std;
extern "C" {
int yylex(void);
int yyparse(void);
int yywrap() { return 1; }
} /* extern "C" */
void yyerror(const char *error) {
cerr << error << endl;
} /* error handler */
%}
/*============================================================================*/
/* Create Bison union and stack */
/*============================================================================*/
%code requires {
#ifndef __TYPES_HPP_INCLUDED__
#define __TYPES_HPP_INCLUDED__
#include "types.hpp"
#endif
}
%union {
object_type* pointer;
none_type* none_buffer;
bool_type* bool_buffer;
int_type* int_buffer;
float_type* float_buffer;
bytes_type* bytes_buffer;
} /* union */
g++ は次のエラーを返します: error: ‘bool_type’ does not name a type
.