I'm working on a parser for RDDL and, as I've done it before, when I define union which contains types I use, I use pointers. E.g.
%union {
double d;
int i;
std::string *str;
std::vector<std::string> *vectorStr;
RDDLBlock *rddlBlock;
Domain *domain;
DefineType *defineType;
std::vector<DefineType*> *vectorDefineType;
DomainList *domainList;
std::vector<PvarDefinition*> *vectorPvarDefinition;
PvarDefinition *pVarDefinition;
CpfDefinition *cpfDefinition;
std::vector<CpfDefinition*> *vectorCpfDefinition;
PvarExpression *pVarExpression;
LogicalExpression *logicalExpression;
std::vector<LogicalExpression*> *vectorLogicalExpression;
LConstCaseList *lConstCaseList;
CaseDefine *caseDefine;
std::vector<CaseDefine*> *vectorCaseList;
Parameter *parameter;
ParameterList *parameterList;
ObjectDefine *objectDefine;
std::vector<ObjectDefine*> *objectsList;
PvariablesInstanceDefine* pvariablesInstanceDefine;
std::vector<PvariablesInstanceDefine*> *pvariablesInstanceList;
Instance *instance;
NonFluentBlock *nonFluentBlock;
}
This is the way I saw most people implement multiple token types in parsers. While searching for this answer on the web, all I saw are the examples and no explanation on why we have to use pointers. One of my tasks now is to 'clean pointers' where ever that is possible. So my question is, why do we (have to) use pointers in unions in this case?
EDIT: Added full list of types defined in union.