%{ #include //#include "yacc_utils.h" #include #include #include #include #include #include #include #include #include "ExpressionValue.h" #include "InferTypeOfExpression.h" using namespace std; int yylex(void); void yyerror(char *); string getTypeValue(string type) { string result; if (type == "SIGNED_INTEGER") { result = toString(TYPE_SIGNED_INTEGER); } else if (type == "UNSIGNED_INTEGER") { result = toString(TYPE_UNSIGNED_INTEGER); } else if (type == "SIGNED_BITVECTOR") { result = toString(TYPE_SIGNED_BITVECTOR); } else if (type == "UNSIGNED_BITVECTOR") { result = toString(TYPE_UNSIGNED_BITVECTOR); } else if (type == "REAL") { result = toString(TYPE_REAL); } else { result = toString(TYPE_BOOL); } return result; } //Map for handling Inference Rule map mapOfTypeInferenceRules; %} /*This is the union used to define YYSTYPE*/ %union { char *string_value; int int_value; } %token t_String %type Operator %type t_Max %type t_Min %type t_Add %type t_Addall %type INT_NUMBER %type Integer %type InferredTypeTuple %type ArgumentForComputation %type Computation %type TypeTuple %type PrintString %type ParameterOfPrintBlock %type ParameterOfPrintStatement %type ParameterOfPrintWordList %type ParameterOfPrintWord %type OnceOrEvery %type t_KeyWordsOfPrint %token t_StringLiteral %token t_TypeValue %type OperandsNumbers %type TypeCastOperands %token t_OBrack t_CBrack t_Comma t_Eq t_OSquareBrack t_CSquareBrack t_OperatorString t_MinArgs t_MaxArgs %token t_Operand1 t_Operand2 t_Operand3 t_OtherOperandsString t_InferString t_InfinityString %token t_Hash t_Comments t_Add t_Max t_Min t_Addall INT_NUMBER t_ArgumentNumber t_Variable %token t_print t_FCBrack t_FOBrack t_Once t_Every t_SemiColon t_KeyWordsOfPrint t_printAll %token t_CastString %% ListOfInferenceRules : InferenceRule { ////cout << "Inference rule is there\n"; } | InferenceRule ListOfInferenceRules { ////cout << "Inference Rule\n"; } InferenceRule: t_OperatorString Operator t_MinArgs Integer t_MaxArgs Integer t_Operand1 TypeTuple t_Operand2 TypeTuple t_Operand3 TypeTuple t_OtherOperandsString TypeTuple t_InferString InferredTypeTuple PrintString TypeCastOperands { bool variableNumberOfArguments = ($4 != $6) ? true : false; bool unlimitedNumberOfArguments = ($6 == INFINITY_WIDTH) ? true : false; ////cout << "Unlimited operads:" << unlimitedNumberOfArguments; /** **Special Case** * Here handling only the operator that is applied to single type and * infinite number of arguments (Symmetric ) */ for (int i = $4; i <= $6; i++) { string signature; t_InferTypeOfExpression *inferenceRule = new t_InferTypeOfExpression(); string operatorSymbol = toString($2); int minargs = $4; int maxargs = i; vector operandTypes; if (unlimitedNumberOfArguments) { inferenceRule->setOperand1Type(inferenceRule->stringToTypeOfExpressionTupleWithNoComputations($8)); operandTypes.push_back(inferenceRule->getOperand1Type()); inferenceRule->setOperand2Type( inferenceRule->stringToTypeOfExpressionTupleWithNoComputations($10)); operandTypes.push_back(inferenceRule->getOperand2Type()); inferenceRule->setOperand3Type(inferenceRule->stringToTypeOfExpressionTupleWithNoComputations($12)); operandTypes.push_back(inferenceRule->getOperand3Type()); inferenceRule->setOtherOperandsType(inferenceRule->stringToTypeOfExpressionTupleWithNoComputations($14)); operandTypes.push_back(inferenceRule->getOtherOperandsType()); inferenceRule->setInferredType ($16); inferenceRule->setPrintExpressionFormat(toString($17)); inferenceRule->type_cast_operands = toString($18); //signature = inferenceRule->generateKey($2, operandTypes); // Key is only with one operand type signature = operatorSymbol + "@"; signature = signature + toString(operandTypes[0]->m_type); ////cout << "signature\n" << signature // << "\nInserting into the map\n"; mapOfTypeInferenceRules[signature] = inferenceRule; break; } else { if (i >= 1) { inferenceRule->operand1Type = inferenceRule->stringToTypeOfExpressionTupleWithNoComputations($8); operandTypes.push_back(inferenceRule->operand1Type); } if (i >= 2) { inferenceRule->operand2Type = inferenceRule->stringToTypeOfExpressionTupleWithNoComputations($10); operandTypes.push_back(inferenceRule->operand2Type); } if (i >= 3) { inferenceRule->operand3Type = inferenceRule->stringToTypeOfExpressionTupleWithNoComputations($12); operandTypes.push_back(inferenceRule->operand3Type); } if (i > 3) { inferenceRule->otherOperandsType = inferenceRule->stringToTypeOfExpressionTupleWithNoComputations($14); operandTypes.push_back(inferenceRule->otherOperandsType); } signature = inferenceRule->generateKey($2, operandTypes); inferenceRule->inferredType = $16; inferenceRule->type_cast_operands = toString($18); inferenceRule->printExpressionFormat=toString($17); mapOfTypeInferenceRules[signature] = inferenceRule; ////cout << "\nsignature " << signature << "\nInserting into the map\n"; mapOfTypeInferenceRules[signature] = inferenceRule; } /** * For Infinte length only consider the operator Symbol as Signature * push Only other Operand type */ ////cout<<"Inference accepted"<operatorSymbol = toString($2); inferenceRule->minargs = $4; inferenceRule->maxargs = i; inferenceRule->variableNumberOfArguments = variableNumberOfArguments; inferenceRule->unlimitedNumberOfArguments = unlimitedNumberOfArguments; inferenceRule->printExpressionFormat=toString($15); inferenceRule->type_cast_operands = toString($16); vector operandTypes; if (unlimitedNumberOfArguments) { //signature = inferenceRule->generateKey($2, operandTypes); inferenceRule->maxargs = INFINITY_WIDTH; operandTypes.push_back(inferenceRule->stringToTypeOfExpressionTupleWithNoComputations($8)); inferenceRule->setOperand1Type(operandTypes[0]); signature = inferenceRule->operatorSymbol + "@"; signature = signature + toString(operandTypes[0]->m_type); //cout << "signature\n" << signature << "\nInserting into the map\n"; inferenceRule->inferredType = $14; mapOfTypeInferenceRules[signature] = inferenceRule; break; } else { if (i >= 1) { inferenceRule->operand1Type = inferenceRule->stringToTypeOfExpressionTupleWithNoComputations($8); operandTypes.push_back(inferenceRule->operand1Type); } if (i >= 2) { inferenceRule->operand2Type = inferenceRule->stringToTypeOfExpressionTupleWithNoComputations($10); operandTypes.push_back(inferenceRule->operand2Type); } inferenceRule->otherOperandsType = inferenceRule->stringToTypeOfExpressionTupleWithNoComputations($12); operandTypes.push_back(inferenceRule->otherOperandsType); signature = inferenceRule->generateKey($2, operandTypes); inferenceRule->inferredType = $14; mapOfTypeInferenceRules[signature] = inferenceRule; //cout << "\nsignature " << signature << "\nInserting into the map\n"; mapOfTypeInferenceRules[signature] = inferenceRule; } //cout<<"Inference accepted"<operatorSymbol = toString($2); inferenceRule->minargs = $4; inferenceRule->maxargs = i; inferenceRule->printExpressionFormat=toString($15); inferenceRule->type_cast_operands = toString($16); vector operandTypes; inferenceRule->operand1Type = inferenceRule->stringToTypeOfExpressionTupleWithNoComputations($8); operandTypes.push_back(inferenceRule->operand1Type); if (i >= 2) { inferenceRule->operand2Type = inferenceRule->stringToTypeOfExpressionTupleWithNoComputations($10); operandTypes.push_back(inferenceRule->operand2Type); } if (i >= 3) { inferenceRule->operand3Type = inferenceRule->stringToTypeOfExpressionTupleWithNoComputations($12); operandTypes.push_back(inferenceRule->operand3Type); } signature = inferenceRule->generateKey($2, operandTypes); inferenceRule->variableNumberOfArguments = variableNumberOfArguments; inferenceRule->inferredType = $14; mapOfTypeInferenceRules[signature] = inferenceRule; //cout << "signature\n" << signature << "\nInserting into the map\n"; //cout<<"3 Operands"; } } | t_OperatorString Operator t_MinArgs Integer t_MaxArgs Integer t_Operand1 TypeTuple t_Operand2 TypeTuple t_InferString InferredTypeTuple PrintString TypeCastOperands { bool variableNumberOfArguments = ($4 != $6) ? true : false; bool unlimitedNumberOfArguments = ($6 == INFINITY_WIDTH) ? true : false; for (int i = $4; i <= $6; i++) { if ($6 != 2) { yyerror("Error while parsing file (Minargs != Maxargs or Maxargs not 2)"); } //cout<<"Inference rule of 2 operands:"; t_InferTypeOfExpression *inferenceRule = new t_InferTypeOfExpression(); string signature; inferenceRule->operatorSymbol = toString($2); inferenceRule->minargs = i; inferenceRule->maxargs = i; inferenceRule->printExpressionFormat=toString($13); inferenceRule->type_cast_operands = toString($14); vector operandTypes; inferenceRule->operand1Type = inferenceRule->stringToTypeOfExpressionTupleWithNoComputations($8); operandTypes.push_back(inferenceRule->operand1Type); if (i == 2) { inferenceRule->operand2Type = inferenceRule->stringToTypeOfExpressionTupleWithNoComputations($10); operandTypes.push_back(inferenceRule->operand2Type); } signature = inferenceRule->generateKey($2, operandTypes); inferenceRule->inferredType = $12; inferenceRule->variableNumberOfArguments = variableNumberOfArguments; mapOfTypeInferenceRules[signature] = inferenceRule; //cout << "\nsignature " << signature << "\nInserted into the map\n"; } } | t_OperatorString Operator t_MinArgs Integer t_MaxArgs Integer t_Operand1 TypeTuple t_InferString InferredTypeTuple PrintString TypeCastOperands { string signature; if ($6 != $4 || $4 != 1) { yyerror("Error while parsing file (Minargs != Maxargs or Maxargs not 1)"); } t_InferTypeOfExpression *inferenceRule = new t_InferTypeOfExpression(); inferenceRule->operatorSymbol = toString($2); inferenceRule->minargs = $4; inferenceRule->maxargs = $6; inferenceRule->printExpressionFormat=toString($11); inferenceRule->type_cast_operands = toString($12); vector operandTypes; inferenceRule->operand1Type = inferenceRule->stringToTypeOfExpressionTupleWithNoComputations($8); operandTypes.push_back(inferenceRule->operand1Type); signature = inferenceRule->generateKey($2, operandTypes); inferenceRule->inferredType = $10; mapOfTypeInferenceRules[signature] = inferenceRule; //cout << "\nsignature " << signature << "Inserted into map\n"; //cout<<"Single Oprand type inferencing \n"; } | Comments { //cout<<"Comments Accepted"<