2011-02-05 2 views
2

私はパーサを構築する必要があるクラス用のプロジェクトに取り組んでいます。現在、yaccでパーサーを構築する段階にあります。私が現在混乱させているのは、それぞれの非終端記号に型を割り当てる必要があるということです。どのようになど、それは時々、整数またはダブル、またはブール値にはできませんので、exprに形をASSINGできyaccの非終端子に複数のデータ型を割り当てる

... 
%union { 
    Type dataType; 
    int integerConstant; 
    bool boolConstant; 
    char *stringConstant; 
    double doubleConstant; 
    char identifier[MaxIdentLen+1]; // +1 for terminating null 
    Decl *decl; 
    List<Decl*> *declList; 
} 

%token <identifier> T_Identifier 
%token <stringConstant> T_StringConstant 
%token <integerConstant> T_IntConstant 
%token <doubleConstant> T_DoubleConstant 
%token <boolConstant> T_BoolConstant 

... 

%% 
... 
Expr    : /* some rules */ 
        | Constant { /* Need to figure out what to do here */ } 
        | /* some more rules */ 
        ; 

Constant   : T_IntConstant { $$=$1 } 
        | T_DoubleConstant { $$=$1 } 
        | T_BoolConstant { $$=$1 } 
        | T_StringConstant { $$=$1 } 
        | T_Null { $$=$1 } 
... 

:いくつかのケースでは、私のようなものがあるでしょうけれども?

答えて

3

あなたは

TypesConstant   : T_IntConstant { $<integerConstant>$=$1 } 
         | T_DoubleConstant { $<doubleConstant>$=$1 } 
         | ... 

によってルールでタイプを追加することができますが、より詳細http://www.gnu.org/software/bison/manual/html_node/Action-Types.html#Action-Typesを参照してください。

+0

「$ $」の例は特になく、 '$ 1'のみです。私はこれが実際に動くと確信していません。 –

+0

バイソン2.3で試しました。 – Rudi

+0

@Chris:あなたが確信していない場合は、[Mid-Rule Actions](http://www.gnu.org/software/bison/manual)の最初のショーなど、他のページでの使用例があります。 /html_node/Mid_002dRule-Actions.html#Mid_002dRule-Actions)ページを開きます。 –

関連する問題