2
我正在爲類的項目,我們必須建立一個分析器。我們目前正處於在yacc中構建解析器的階段。目前令我困惑的是我讀過你需要爲每個非終端分配一個類型。在某些情況下,雖然我有類似:分配多個數據類型到非終端在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 }
...
你怎麼能assing一個類型EXPR,因爲不能將它有時是一個整數或雙或布爾等?
他們沒有給出'$ $`的任何例子,具體來說,只有'$ 1`。我不相信這會實際工作。 –
2011-02-05 09:07:20