Я работаю над проектом для класса, в котором мы должны построить парсер. В настоящее время мы находимся в стадии создания парсера в 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 }
...
Как вы можете назначить тип для expr, поскольку иногда он не может быть целым или двойным, или bool, и т. Д.?