diff --git a/.gitignore b/.gitignore index 1431f70..3b5d790 100644 --- a/.gitignore +++ b/.gitignore @@ -7,6 +7,8 @@ cache/ log/ obj/ node_modules/ +.idea/ +cmake-build-debug/ # Files: *.csproj.user diff --git a/src/backend/domain-specific/calculator.c b/src/backend/domain-specific/calculator.c index 5095af3..4f0b1e5 100644 --- a/src/backend/domain-specific/calculator.c +++ b/src/backend/domain-specific/calculator.c @@ -1,10 +1,6 @@ #include "calculator.h" #include "math.h" -/** - * Implementación de "calculator.h". - */ - double Add(const double leftAddend, const double rightAddend) { return leftAddend + rightAddend; } @@ -27,6 +23,3 @@ double Power(const double base, const double exponent) { return pow(base, exponent); } -// double (* function) (double) declareFunction(char * expression) { - -// } diff --git a/src/backend/support/nodes.h b/src/backend/support/nodes.h new file mode 100644 index 0000000..c6737bc --- /dev/null +++ b/src/backend/support/nodes.h @@ -0,0 +1,165 @@ +#ifndef NODES_H +#define NODES_H + +#include +#include + +typedef struct variable_t { + char * name; + struct expression_t * value; +} variable_t; + +typedef struct while_t { + struct boolean_t * boolean; + struct block_t * block; +} while_t; + +typedef struct expression_t { + struct algebraic_op_t * algebraic_op; + double * number; + struct variable_t * variable; + struct derivative_t * derivative; + struct integrate_t * integrate; + struct evaluate_t * evaluate; + struct negative_t * negative; + struct parenthesis_exp_t * parenthesis_exp; + + struct expression_t * next; +} expression_t; + +typedef struct function_t { + char * name; + expression_t * expression; + struct domain_t * domain; + struct taylor_t * taylor; + struct composite_t * composite; + + struct function_t * next; +} function_t; + +typedef struct statement_t { + struct while_t * while_st; + struct print_t * print; + struct for_t * for_st; + struct if_t * if_st; + struct integrate_t * integrate; + struct derivative_t * derivative; + struct evaluate_t * evaluate; + struct variable_t * variable; + + struct statement_t * next; +} statement_t; + +typedef struct block_t { + statement_t * statement; +} block_t; + +typedef struct print_t { + expression_t * exp; + char * string; +} print_t; + +typedef struct for_t { + variable_t * variable_t; + struct boolean_t * boolean; + block_t * block; +} for_t; + +typedef struct if_t { + struct boolean_t * boolean; + block_t * block; + block_t * else_block; +} if_t; + +typedef struct boolean_t { + struct domain_t * domain; + struct not_bool_t * not_bool; + struct bool_op_t * bool_op; +} boolean_t; + +typedef struct bool_op_t { + boolean_t * left; + boolean_t * right; + int op; +} bool_op_t; + +typedef struct not_bool_t { + boolean_t * boolean; +} not_bool_t; + +typedef struct domain_one_op_t { + expression_t * left; + expression_t * right; + int oper; +} domain_one_op_t; + +typedef struct domain_two_op_t { + expression_t * left; + expression_t * mid; + expression_t * right; + int oper1; + int oper2; +} domain_two_op_t; + +typedef struct domain_t { + domain_one_op_t * domain_one; + domain_two_op_t * domain_two; +} domain_t; + +typedef struct parenthesis_exp_t { + expression_t * expression; +} parenthesis_exp_t; + +typedef struct negative_t { + expression_t * expression; +} negative_t; + +typedef struct derivative_t { + function_t * function; + double * value; + double * error; +} derivative_t; + +typedef struct evaluate_t { + function_t * function; + double * value; +} evaluate_t; + +typedef struct integrate_t { + function_t * function; + double * value; + double * error; + + expression_t * lvalue; + expression_t * rvalue; +} integrate_t; + +typedef struct algebraic_op_t { + expression_t * left; + expression_t * right; + int op; +} algebraic_op_t; + +typedef struct composite_t { + function_t * outter_funct; + function_t * inner_funct; +} composite_t; + +typedef struct numArray_t { + expression_t * expressions; + size_t size; + + struct numArray_t * next; +} numArray_t; + +typedef struct taylor_t { + int num; + function_t * function; +} taylor_t; + +typedef struct id_t { + variable_t * variable; + function_t * function; +} id_t; + +#endif \ No newline at end of file diff --git a/src/frontend/syntactic-analysis/bison-actions.c b/src/frontend/syntactic-analysis/bison-actions.c index c3f43b8..1cfbe28 100644 --- a/src/frontend/syntactic-analysis/bison-actions.c +++ b/src/frontend/syntactic-analysis/bison-actions.c @@ -1,67 +1,537 @@ #include "../../backend/domain-specific/calculator.h" #include "../../backend/support/logger.h" +#include "../../backend/support/nodes.h" #include "bison-actions.h" #include +#include #include -/** - * Implementación de "bison-grammar.h". - */ - void yyerror(const char * string) { LogError("Mensaje: '%s' debido a '%s' (linea %d).", string, yytext, yylineno); - // LogError("En ASCII es:"); - // LogErrorRaw("\t"); - // const int length = strlen(yytext); - // for (int i = 0; i < length; ++i) { - // LogErrorRaw("[%d]", yytext[i]); - // } - // LogErrorRaw("\n\n"); } int ProgramGrammarAction(const int value) { - // LogDebug("ProgramGrammarAction(%d)", value); state.succeed = true; state.result = value; return value; } -int AdditionExpressionGrammarAction(const int leftValue, const int rightValue) { - LogDebug("AdditionExpressionGrammarAction(%d, %d)", leftValue, rightValue); - return Add(leftValue, rightValue); +statement_t * EvaluateGrammarAction(evaluate_t * evaluate) { + statement_t * statement = malloc(sizeof(statement_t)); + evaluate_t * evalAux = malloc(sizeof(evaluate_t)); + evalAux->function = evaluate->function; + evalAux->value = evaluate->value; + statement->evaluate = evalAux; + statement->next = NULL; + return statement; } -int SubtractionExpressionGrammarAction(const int leftValue, const int rightValue) { - LogDebug("SubtractionExpressionGrammarAction(%d, %d)", leftValue, rightValue); - return Subtract(leftValue, rightValue); +statement_t * IntegrateGrammarAction(integrate_t * integrate) { + statement_t * statement = malloc(sizeof(statement_t)); + integrate_t * intAux = malloc(sizeof(integrate_t)); + intAux->function = integrate->function; + intAux->error = integrate->error; + intAux->lvalue = integrate->lvalue; + intAux->rvalue = integrate->rvalue; + statement->integrate = intAux; + statement->next = NULL; + return statement; } -int MultiplicationExpressionGrammarAction(const int leftValue, const int rightValue) { - LogDebug("MultiplicationExpressionGrammarAction(%d, %d)", leftValue, rightValue); - return Multiply(leftValue, rightValue); +statement_t * DerivativeGrammarAction(derivative_t * derivative) { + statement_t * statement = malloc(sizeof(statement_t)); + derivative_t * derAux = malloc(sizeof(derivative_t)); + derAux->function = derivative->function; + derAux->error = derivative->error; + derAux->value = derivative->value; + statement->derivative = derAux; + statement->next = NULL; + return statement; } -int DivisionExpressionGrammarAction(const int leftValue, const int rightValue) { - LogDebug("DivisionExpressionGrammarAction(%d, %d)", leftValue, rightValue); - return Divide(leftValue, rightValue); +statement_t * StatementDeclareFunctionGrammarAction(function_t * function) { + statement_t * statement = malloc(sizeof(statement_t)); + function_t * funAux = malloc(sizeof(function_t)); + funAux->name = funAux->name; + funAux->expression = funAux->expression; + funAux->domain = funAux->domain; + funAux->taylor = funAux->taylor; + funAux->composite = funAux->composite; + funAux->next = NULL; + statement->evaluate = funAux; + statement->next = NULL; + return statement; } -int FactorExpressionGrammarAction(const int value) { - LogDebug("FactorExpressionGrammarAction(%d)", value); - return value; +statement_t * IfGrammarAction(if_t * if_st) { + statement_t * statement = malloc(sizeof(statement_t)); + if_t * ifAux = malloc(sizeof(if_t)); + ifAux->block = if_st->block; + ifAux->boolean = if_st->boolean; + statement->if_st = ifAux; + statement->next = NULL; + return statement; } -int ExpressionFactorGrammarAction(const int value) { - LogDebug("ExpressionFactorGrammarAction(%d)", value); - return value; +statement_t * ForGrammarAction(for_t * for_st) { + statement_t * statement = malloc(sizeof(statement_t)); + for_t * forAux = malloc(sizeof(for_t)); + forAux->block = for_st->block; + forAux->boolean = for_st->boolean; + forAux->variable_t = for_st->variable_t; + statement->for_st = forAux; + statement->next = NULL; + return statement; } -int ConstantFactorGrammarAction(const int value) { - LogDebug("ConstantFactorGrammarAction(%d)", value); - return value; +statement_t * WhileGrammarAction(while_t * while_st) { + statement_t * statement = malloc(sizeof(statement_t)); + while_t * whileAux = malloc(sizeof(while_t)); + whileAux->block = while_st->block; + whileAux->boolean = while_st->boolean; + statement->while_st = whileAux; + statement->next = NULL; + return statement; } -int IntegerConstantGrammarAction(const int value) { - LogDebug("IntegerConstantGrammarAction(%d)", value); - return value; +statement_t * PrintGrammarAction(print_t * print) { + statement_t * statement = malloc(sizeof(statement_t)); + print_t * printAux = malloc(sizeof(print_t)); + printAux->exp = print->exp; + printAux->string = print->string; + statement->print = printAux; + statement->next = NULL; + return statement; } + +void expressionShallowCopy(expression_t * src, expression_t * dest) { + dest->algebraic_op = src->algebraic_op; + dest->variable = src->variable; + dest->derivative = src->derivative; + dest->integrate = src->integrate; + dest->evaluate = src->evaluate; + dest->negative = src->negative; + dest->parenthesis_exp = src->parenthesis_exp; + dest->number = src->number; + dest->next = src->next; +} + +void statementShallowCopy(statement_t * src, statement_t * dest) { + dest->while_st = src->while_st; + dest->print = src->print; + dest->for_st = src->for_st; + dest->if_st = src->if_st; + dest->integrate = src->integrate; + dest->derivative = src->derivative; + dest->evaluate = src->evaluate; + dest->variable = src->variable; + + dest->next = src->next; +} + +void compositeShallowCopy(composite_t * src, composite_t * dest) { + dest->outter_funct = src->outter_funct; + dest->inner_funct = src->inner_funct; +} + +expression_t * ExpressionOperatorGrammarAction(expression_t * firstExpression, int token, expression_t * secondExpression) { + expression_t * expression = malloc(sizeof(expression_t)); + expression_t * firstExpressionAux = malloc(sizeof(expression_t)); + expression_t * secondExpressionAux = malloc(sizeof(expression_t)); + + expressionShallowCopy(firstExpression, firstExpressionAux); + expressionShallowCopy(secondExpression, secondExpressionAux); + + expression->algebraic_op->op = token; + expression->algebraic_op->left = firstExpressionAux; + expression->algebraic_op->right = secondExpressionAux; + return expression; +} + +expression_t * AlgebraicExpressionGrammarAction(algebraic_op_t * algebraicOp) { + expression_t * expression = malloc(sizeof(expression_t)); + algebraic_op_t * algebraicAux = malloc(sizeof(algebraic_op_t)); + algebraicAux->left = algebraicOp->left; + algebraicAux->right = algebraicOp->right; + algebraicAux->op = algebraicOp->op; + expression->algebraic_op = algebraicAux; + return expression; +} + +expression_t * EvaluateExpressionGrammarAction(evaluate_t * evaluate) { + expression_t * expression = malloc(sizeof(expression_t)); + evaluate_t * evalAux = malloc(sizeof(evaluate_t)); + evalAux->function = evaluate->function; + evalAux->value = evaluate->value; + expression->evaluate = evalAux; + return expression; +} + +expression_t * DerivativeExpressionGrammarAction(derivative_t * derivative) { + expression_t * expression = malloc(sizeof(expression_t)); + derivative_t * derAux = malloc(sizeof(derivative_t)); + derAux->function = derivative->function; + derAux->value = derivative->value; + derAux->error = derivative->error; + expression->derivative = derAux; + return expression; +} + +expression_t * IntegrateExpressionGrammarAction(integrate_t * integrate) { + expression_t * expression = malloc(sizeof(expression_t)); + integrate_t * intAux = malloc(sizeof(integrate_t)); + intAux->function = integrate->function; + intAux->lvalue = integrate->lvalue; + intAux->rvalue = integrate->rvalue; + intAux->error = integrate->error; + expression->integrate = intAux; + return expression; +} + +expression_t * NegativeExpressionGrammarAction(expression_t * negative) { + expression_t * expression = malloc(sizeof(expression_t)); + negative_t * negAux = malloc(sizeof(negative_t)); + negAux->expression = negative; + expression->negative = negAux; + return expression; +} + +expression_t * ParenthesisExpressionGrammarAction(expression_t * exp) { + expression_t * expression = malloc(sizeof(expression_t)); + parenthesis_exp_t * parAux = malloc(sizeof(parenthesis_exp_t)); + parAux->expression = exp; + expression->parenthesis_exp = parAux; + return expression; +} + +expression_t * VariableExpressionGrammarAction(variable_t * variable) { + expression_t * expression = malloc(sizeof(expression_t)); + variable_t * varAux = malloc(sizeof(variable_t)); + varAux->value = variable->value; + varAux->name = variable->name; + expression->variable = varAux; + return expression; +} + +statement_t * VariableStatementGrammarAction(variable_t * variable) { + statement_t * statement = malloc(sizeof(statement_t)); + variable_t * varAux = malloc(sizeof(variable_t)); + varAux->name = variable->name; + varAux->value = variable->value; + statement->variable = varAux; + return statement; +} + +variable_t * VariableGrammarAction(char * name, expression_t * expression) { + variable_t * variable = malloc(sizeof(variable_t)); + expression_t * expAux = malloc(sizeof(expression_t)); + size_t size = strlen(name); + char * nameAux = malloc(size + 1); + + expressionShallowCopy(expression, expAux); + strcpy(nameAux, name); + + variable->value = expAux; + variable->name = nameAux; + return variable; +} + +function_t * TaylorFunctionGrammarAction(taylor_t * taylor) { + function_t * function = malloc(sizeof(function_t)); + taylor_t * taylorAux = malloc(sizeof(taylor_t)); + taylorAux->function = taylor->function; + taylorAux->num = taylor->num; + function->taylor = taylorAux; + return function; +} + +void functionShallowCopy(function_t * src, function_t * dest) { + dest->taylor = src->taylor; + dest->expression = src->expression; + dest->name = src->name; + dest->composite = src->composite; + dest->domain = src->domain; +} + +composite_t * CompositeIdGrammarAction(id_t * id1, id_t * id2) { + composite_t * composite = malloc(sizeof(composite_t)); + function_t * funAux1 = malloc(sizeof(function_t)); + function_t * funAux2 = malloc(sizeof(function_t)); + + funAux1 = id1->function; + funAux2 = id2->function; + + composite->inner_funct = funAux1; + composite->outter_funct = funAux2; + return composite; +} + +composite_t * CompositeCompGrammarAction(composite_t * composite, id_t * id) { + composite_t * compAux = malloc(sizeof(composite_t)); + function_t * funAux1 = malloc(sizeof(function_t)); + function_t * funAux2 = malloc(sizeof(function_t)); + + compositeShallowCopy(composite, compAux); + + funAux1->composite = composite; + funAux2 = id->function; + + composite->inner_funct = funAux1; + composite->outter_funct = funAux2; + return composite; +} + +domain_t * DomainOneOptOperatorGrammarAction(expression_t * firstExpression, int token, expression_t * secondExpression) { + domain_t * domain = malloc(sizeof(expression_t)); + expression_t * firstExpressionAux = malloc(sizeof(expression_t)); + expression_t * secondExpressionAux = malloc(sizeof(expression_t)); + + expressionShallowCopy(firstExpression, firstExpressionAux); + expressionShallowCopy(secondExpression, secondExpressionAux); + + domain->domain_one->left = firstExpressionAux; + domain->domain_one->right = secondExpressionAux; + domain->domain_one->oper = token; + + return domain; +} + +domain_t * DomainTwoOptOperatorGrammarAction(expression_t * firstExpression, int token1, expression_t * secondExpression, int token2, expression_t * thirdExpression) { + domain_t * domain = malloc(sizeof(expression_t)); + expression_t * firstExpressionAux = malloc(sizeof(expression_t)); + expression_t * secondExpressionAux = malloc(sizeof(expression_t)); + expression_t * thirdExpressionAux = malloc(sizeof(expression_t)); + + expressionShallowCopy(firstExpression, firstExpressionAux); + expressionShallowCopy(secondExpression, secondExpressionAux); + expressionShallowCopy(thirdExpression, thirdExpressionAux); + + domain->domain_two->left = firstExpressionAux; + domain->domain_two->mid = secondExpressionAux; + domain->domain_two->right = thirdExpression; + domain->domain_two->oper1 = token1; + domain->domain_two->oper2 = token2; + + return domain; +} + +boolean_t * DomainGrammarAction(domain_t * domain) { + boolean_t * boolean = malloc(sizeof(boolean_t)); + domain_t * domainAux = malloc(sizeof(domain_t)); + + domainAux->domain_one = domain->domain_one; + domainAux->domain_two = domain->domain_two; + + boolean->domain = domainAux; + + return boolean; + +} + +void booleanShallowCopy(boolean_t * source, boolean_t * dest) { + dest->domain = source->domain; + dest->bool_op = source->bool_op; + dest->not_bool = source->not_bool; +} + +boolean_t * BooleanOperatorGrammarAction(boolean_t * left, int token, boolean_t * right) { + boolean_t * boolean = malloc(sizeof(boolean_t)); + boolean_t * leftAux = malloc(sizeof(boolean_t)); + boolean_t * rightAux = malloc(sizeof(boolean_t)); + + booleanShallowCopy(left, leftAux); + booleanShallowCopy(right, rightAux); + + boolean->bool_op->left = leftAux; + boolean->bool_op->right = rightAux; + boolean->bool_op->op = token; + + return boolean; + +} + +boolean_t * NotBooleanAction(boolean_t * notBoolean) { + boolean_t * boolean = malloc(sizeof(boolean_t)); + not_bool_t * notBooleanAux = malloc(sizeof(not_bool_t)); + + booleanShallowCopy(notBoolean, notBooleanAux->boolean); + + boolean->not_bool = notBooleanAux; + + return boolean; +} + +function_t * DeclareFunctionGrammarAction(id_t * id, function_t * func) { + function_t * function = malloc(sizeof(function_t)); + function_t * funcAux = malloc(sizeof(function_t)); + + functionShallowCopy(func, function); + + function->name = id; + + return function; +} + +function_t * DeclareFunctionListGrammarAction(id_t * id, function_t * func) { + function_t * function = malloc(sizeof(function_t)); + + functionShallowCopy(func, function); + + function->name = id; + + return function; +} + +function_t * FunctionsGrammarAction(function_t * func1, function_t * func2) { + function_t * funcFinal = malloc(sizeof(function_t)); + function_t * funcAux = malloc(sizeof(function_t)); + + functionShallowCopy(func1, funcFinal); + functionShallowCopy(func2, funcAux); + + funcFinal->next = funcAux; + return funcFinal; +} + +function_t * CompositeFunctionGrammarAction(composite_t * composite) { + function_t * function = malloc(sizeof(function_t)); + composite_t * compAux = malloc(sizeof(composite_t)); + + compositeShallowCopy(composite, compAux); + + function->composite = compAux; + return function; +} + +function_t * ExpressionFunctionGrammarAction(expression_t * expression) { + function_t * function = malloc(sizeof(function_t)); + expression_t * expAux = malloc(sizeof(expression_t)); + + expressionShallowCopy(expression, expAux); + function->expression = expAux; + return function; +} + +void domainShallowCopy(domain_t * src, domain_t * dest) { + dest->domain_one = src->domain_one; + dest->domain_two = src->domain_two; +} + +function_t * ExpressionDomainFunctionGrammarAction(expression_t * expression, domain_t * domain) { + function_t * function = malloc(sizeof(function_t)); + expression_t * expAux = malloc(sizeof(expression_t)); + domain_t * domAux = malloc(sizeof(domain_t)); + + expressionShallowCopy(expression, expAux); + domainShallowCopy(domain, domAux); + function->expression = expAux; + function->domain = domAux; + return function; +} + +expression_t * ExpressionsGrammarAction(expression_t * exp1, expression_t * exp2) { + expression_t * expFinal = malloc(sizeof(expression_t)); + expression_t * expAux = malloc(sizeof(expression_t)); + + expressionShallowCopy(exp1, expFinal); + expressionShallowCopy(exp2, expAux); + + expFinal->next = expAux; + return expFinal; +} + +print_t * PrintExpression(expression_t * expression) { + print_t * print = malloc(sizeof(print_t)); + expression_t * expAux = malloc(sizeof(expression_t)); + + expressionShallowCopy(expAux, expression); + + print->exp = expAux; + return print; +} + +print_t * PrintExpression(char * string) { + print_t * print = malloc(sizeof(print_t)); + char * strAux = malloc(strlen(string)); + + strcpy(strAux, string); + + print->string = strAux; + return print; +} + +statement_t * StatementsGrammarAction(statement_t * stat1, statement_t * stat2) { + statement_t * statFinal = malloc(sizeof(statement_t)); + statement_t * statAux = malloc(sizeof(statement_t)); + + statementShallowCopy(stat1, statFinal); + statementShallowCopy(stat2, statAux); + + statFinal->next = statAux; + return statFinal; +} + +void booleanShallowCopy(boolean_t * src, boolean_t * dest) { + dest->domain = src->domain; + dest->not_bool = src->not_bool; + dest->bool_op = src->bool_op; +} + +void blockShallowCopy(block_t * src, block_t * dest) { + dest->statement = src->statement; +} + +if_t * IfBlockGrammarAction(boolean_t * boolean, block_t * block) { + if_t * if_st = malloc(sizeof(if_t)); + boolean_t * boolAux = malloc(sizeof(boolean_t)); + block_t * blockAux = malloc(sizeof(block_t)); + + booleanShallowCopy(boolean, boolAux); + blockShallowCopy(block, blockAux); + + if_st->boolean = boolAux; + if_st->block = blockAux; + return if_st; +} + +if_t * IfBlockGrammarAction(boolean_t * boolean, block_t * block) { + if_t * if_st = malloc(sizeof(if_t)); + boolean_t * boolAux = malloc(sizeof(boolean_t)); + block_t * blockAux = malloc(sizeof(block_t)); + + booleanShallowCopy(boolean, boolAux); + blockShallowCopy(block, blockAux); + + if_st->boolean = boolAux; + if_st->block = block; + return if_st; +} + +if_t * IfElseBlockGrammarAction(boolean_t * boolean, block_t * block, block_t * else_block) { + if_t * if_st = malloc(sizeof(if_t)); + boolean_t * boolAux = malloc(sizeof(boolean_t)); + block_t * blockAux = malloc(sizeof(block_t)); + block_t * elseBlockAux = malloc(sizeof(block_t)); + + booleanShallowCopy(boolean, boolAux); + blockShallowCopy(block, blockAux); + blockShallowCopy(else_block, elseBlockAux); + + if_st->boolean = boolAux; + if_st->block = blockAux; + if_st->else_block = elseBlockAux; + return if_st; +} + +while_t * WhileBlockGrammarAction(boolean_t * boolean, block_t * block) { + while_t * while_st = malloc(sizeof(while_t)); + boolean_t * boolAux = malloc(sizeof(boolean_t)); + block_t * blockAux = malloc(sizeof(block_t)); + + booleanShallowCopy(boolean, boolAux); + blockShallowCopy(block, blockAux); + + while_st->boolean = boolAux; + while_st->block = block; + return while_st; +} \ No newline at end of file diff --git a/src/frontend/syntactic-analysis/bison-actions.h b/src/frontend/syntactic-analysis/bison-actions.h index af18666..64bd974 100644 --- a/src/frontend/syntactic-analysis/bison-actions.h +++ b/src/frontend/syntactic-analysis/bison-actions.h @@ -28,4 +28,14 @@ int ConstantFactorGrammarAction(const int value); int IntegerConstantGrammarAction(const int value); int DoubleConstantGrammarAction(const double value); +statement_t * EvaluateGrammarAction(evaluate_t * evaluate); +statement_t * IntegrateGrammarAction(integrate_t * integrate); +statement_t * DerivativeGrammarAction(derivative_t * derivative); +statement_t * IfGrammarAction(if_t * if_st); +statement_t * ForGrammarAction(for_t * for_st); +statement_t * WhileGrammarAction(while_t * while_st); +statement_t * PrintGrammarAction(print_t * print); +statement_t * DeclareVariableGrammarAction(); +statement_t * AssignVariableGrammarAction(); + #endif diff --git a/src/frontend/syntactic-analysis/bison-grammar.y b/src/frontend/syntactic-analysis/bison-grammar.y index abf90ab..f535e6e 100644 --- a/src/frontend/syntactic-analysis/bison-grammar.y +++ b/src/frontend/syntactic-analysis/bison-grammar.y @@ -1,65 +1,95 @@ %{ #include "bison-actions.h" +#include "nodes.h" %} -// IDs de los tokens generados desde Flex: +%union { + variable_t * variable; + function_t * function; + statement_t * statement; + expression_t * expression; + derivative_t * derivative; + evaluate_t * evaluate; + integrate_t * integrate; + algebraic_op_t * algebraic_op; + parenthesis_exp_t * parenthesis_exp; + boolean_t * boolean; + domain_t * domain; + int intNumber; + double doubleNumber; + char * string; + int token; +} -%token ADD -%token SUB -%token MUL -%token DIV -%token POW -%token FUNCTION -%token IF -%token ELSE -%token FOR -%token WHILE -%token PRINT -%token SEMICOLON -%token COMPOSITE -%token SQUOTE -%token DERIVATIVE -%token INTEGRATE -%token BETWEEN -%token AND_INT -%token EVALUATE -%token IN -%token OPEN_BRACES -%token OPEN_BRACKETS -%token CLOSE_BRACES -%token CLOSE_BRACKETS -%token OPEN_PARENTHESIS -%token CLOSE_PARENTHESIS -%token ASSIGN -%token EQUAL -%token AND -%token OR -%token NOT -%token ADD_ASSIGN -%token INCREMENT -%token SUB_ASSIGN -%token DECREMENT -%token MUL_ASSIGN -%token DIV_ASSIGN -%token DQUOTE -%token COMMA -%token COLON -%token LESSER -%token LESSER_EQUAL -%token GREATER -%token GREATER_EQUAL -%token NOT_EQUAL -%token VAR -%token ERR -%token TAYLOR -%token ESCAPESTRING -%token DOUBLE -%token INTEGER -%token ID +%type variable +%type declare_variable +%type function +%type functions +%type declare_function +%type statement +%type expression +%type derivative +%type integrate +%type evaluate +%type algebraic_op +%type parenthesis_exp +%type boolean +%type domain -// Reglas de asociatividad y precedencia (de menor a mayor): +%token ADD +%token SUB +%token MUL +%token DIV +%token POW +%token FUNCTION +%token IF +%token ELSE +%token FOR +%token WHILE +%token PRINT +%token SEMICOLON +%token COMPOSITE +%token SQUOTE +%token DERIVATIVE +%token INTEGRATE +%token BETWEEN +%token AND_INT +%token EVALUATE +%token IN +%token OPEN_BRACES +%token OPEN_BRACKETS +%token CLOSE_BRACES +%token CLOSE_BRACKETS +%token OPEN_PARENTHESIS +%token CLOSE_PARENTHESIS +%token ASSIGN +%token EQUAL +%token AND +%token OR +%token NOT +%token ADD_ASSIGN +%token INCREMENT +%token SUB_ASSIGN +%token DECREMENT +%token MUL_ASSIGN +%token DIV_ASSIGN +%token DQUOTE +%token COMMA +%token COLON +%token LESSER +%token LESSER_EQUAL +%token GREATER +%token GREATER_EQUAL +%token NOT_EQUAL +%token VAR +%token ERR +%token TAYLOR +%token ESCAPESTRING +%token DOUBLE +%token INTEGER +%token ID %precedence CLOSE_PARENTHESIS %precedence ELSE @@ -76,170 +106,170 @@ program: statements { $$ = ProgramGrammarAction($1); } ; -statement: evaluate SEMICOLON - | integrate SEMICOLON - | derivative SEMICOLON - | declare_function SEMICOLON - | if_statement - | for_statement - | while_statement - | print SEMICOLON - | declare_variable SEMICOLON - | assign_variable SEMICOLON +statement: evaluate SEMICOLON { $$ = EvaluateGrammarAction($1); } + | integrate SEMICOLON { $$ = IntegrateGrammarAction($1); } + | derivative SEMICOLON { $$ = DerivativeGrammarAction($1); } + | declare_function SEMICOLON { $$ = StatementDeclareFunctionGrammarAction($1); } + | if_statement { $$ = IfGrammarAction($1); } + | for_statement { $$ = ForGrammarAction($1); } + | while_statement { $$ = WhileGrammarAction($1); } + | print SEMICOLON { $$ = PrintGrammarAction($1); } + | declare_variable SEMICOLON { $$ = VariableStatementGrammarAction($1); } + | assign_variable SEMICOLON { $$ = VariableStatementGrammarAction($1); } ; -expression: positive_constant - | evaluate - | integrate - | derivative - | ID - | expression ADD expression - | expression SUB expression - | expression MUL expression - | expression DIV expression - | expression POW expression - | OPEN_PARENTHESIS expression CLOSE_PARENTHESIS - | SUB expression +expression: positive_constant { $$ = PositiveConstantGrammarAction($1) } + | evaluate { $$ = EvaluateGrammarAction($1) } + | integrate { $$ = IntegrateGrammarAction($1) } + | derivative { $$ = DerivativeGrammarAction($1) } + | ID { $$ = IdGrammarAction($1) } + | expression ADD expression { $$ = ExpressionOperatorGrammarAction($1, $2, $3) } + | expression SUB expression { $$ = ExpressionOperatorGrammarAction($1, $2, $3) } + | expression MUL expression { $$ = ExpressionOperatorGrammarAction($1, $2, $3) } + | expression DIV expression { $$ = ExpressionOperatorGrammarAction($1, $2, $3) } + | expression POW expression { $$ = ExpressionOperatorGrammarAction($1, $2, $3) } + | OPEN_PARENTHESIS expression CLOSE_PARENTHESIS { $$ = ParenthesisExpressionGrammarAction($2) } + | SUB expression { $$ = NegativeExpressionGrammarAction($2) } ; -domain: expression LESSER expression - | expression LESSER_EQUAL expression - | expression GREATER_EQUAL expression - | expression GREATER expression - | expression EQUAL expression - | expression NOT_EQUAL expression - | expression LESSER expression LESSER expression - | expression LESSER_EQUAL expression LESSER expression - | expression LESSER expression LESSER_EQUAL expression - | expression LESSER_EQUAL expression LESSER_EQUAL expression +domain: expression LESSER expression { $$ = DomainOneOptOperatorGrammarAction($1, $2, $3) } + | expression LESSER_EQUAL expression { $$ = DomainOneOptOperatorGrammarAction($1, $2, $3) } + | expression GREATER_EQUAL expression { $$ = DomainOneOptOperatorGrammarAction($1, $2, $3) } + | expression GREATER expression { $$ = DomainOneOptOperatorGrammarAction($1, $2, $3) } + | expression EQUAL expression { $$ = DomainOneOptOperatorGrammarAction($1, $2, $3) } + | expression NOT_EQUAL expression { $$ = DomainOneOptOperatorGrammarAction($1, $2, $3) } + | expression LESSER expression LESSER expression { $$ = DomainTwoOptOperatorGrammarAction($1, $2, $3, $4, $5) } + | expression LESSER_EQUAL expression LESSER expression { $$ = DomainTwoOptOperatorGrammarAction($1, $2, $3, $4, $5) } + | expression LESSER expression LESSER_EQUAL expression { $$ = DomainTwoOptOperatorGrammarAction($1, $2, $3, $4, $5) } + | expression LESSER_EQUAL expression LESSER_EQUAL expression { $$ = DomainTwoOptOperatorGrammarAction($1, $2, $3, $4, $5) } ; -boolean: domain - | boolean OR boolean - | boolean AND boolean - | OPEN_PARENTHESIS boolean CLOSE_PARENTHESIS - | NOT boolean +boolean: domain { $$ = DomainGrammarAction($1) } + | boolean OR boolean { $$ = BooleanOperatorGrammarAction($1, $2, $3) } + | boolean AND boolean { $$ = BooleanOperatorGrammarAction($1, $2, $3) } + | OPEN_PARENTHESIS boolean CLOSE_PARENTHESIS { $$ = $2 } + | NOT boolean { $$ = NotBooleanAction($2) } ; -positive_constant: DOUBLE - | INTEGER +positive_constant: DOUBLE { $$ = DoubleGrammarAction($1) } + | INTEGER { $$ = IntegerGrammarAction($1) } ; -declare_function: FUNCTION ID ASSIGN function - | FUNCTION ID ASSIGN OPEN_BRACES functions CLOSE_BRACES +declare_function: FUNCTION ID ASSIGN function { $$ = DeclareFunctionGrammarAction($2, $4) } + | FUNCTION ID ASSIGN OPEN_BRACES functions CLOSE_BRACES { $$ = DeclareFunctionListGrammarAction($2, $5) } ; -function: expression - | expression COLON domain - | composite - | taylor +function: expression { $$ = ExpressionFunctionGrammarAction($1) } + | expression COLON domain { $$ = ExpressionDomainFunctionGrammarAction($1, $3) } + | composite { $$ = CompositeFunctionGrammarAction($1) } + | taylor { $$ = TaylorFunctionGrammarAction($1) } ; -functions: function - | function COMMA functions +functions: function { $$ = $1 } + | function COMMA functions { $$ = FunctionsGrammarAction($1, $3) } ; -composite: ID COMPOSITE ID - | composite COMPOSITE ID +composite: ID COMPOSITE ID { $$ = CompositeIdGrammarAction($1, $3) } + | composite COMPOSITE ID { $$ = CompositeCompGrammarAction($1, $3) } ; -expressions: expression - | expression COMMA expressions +expressions: expression { $$ = $1 } + | expression COMMA expressions { $$ = ExpressionsGrammarAction($1, $3) } ; -ids: ID - | ID COMMA ids +ids: ID { $$ = $1 } + | ID COMMA ids { $$ = DoubleGrammarAction($1) } ; -numArray: OPEN_BRACKETS expressions CLOSE_BRACKETS +numArray: OPEN_BRACKETS expressions CLOSE_BRACKETS { $$ = DoubleGrammarAction($1) } ; -numArrays: numArray - | numArray COMMA numArrays +numArrays: numArray { $$ = DoubleGrammarAction($1) } + | numArray COMMA numArrays { $$ = DoubleGrammarAction($1) } ; -arrayNumArray: OPEN_BRACKETS numArrays CLOSE_BRACKETS +arrayNumArray: OPEN_BRACKETS numArrays CLOSE_BRACKETS { $$ = DoubleGrammarAction($1) } ; -idArray: OPEN_BRACKETS ids CLOSE_BRACKETS +idArray: OPEN_BRACKETS ids CLOSE_BRACKETS { $$ = DoubleGrammarAction($1) } ; -escapeString: ESCAPESTRING - | ESCAPESTRING ADD escapeString +escapeString: ESCAPESTRING { $$ = DoubleGrammarAction($1) } + | ESCAPESTRING ADD escapeString { $$ = DoubleGrammarAction($1) } ; -escapeStrings: escapeString - | escapeString COMMA escapeStrings +escapeStrings: escapeString { $$ = $1 } + | escapeString COMMA escapeStrings { $$ = DoubleGrammarAction($1) } ; -escapeStringArray: OPEN_BRACKETS escapeStrings CLOSE_BRACKETS +escapeStringArray: OPEN_BRACKETS escapeStrings CLOSE_BRACKETS { $$ = DoubleGrammarAction($1) } ; -evaluate: EVALUATE ID IN OPEN_PARENTHESIS expression CLOSE_PARENTHESIS - | EVALUATE ID IN numArray - | ID OPEN_PARENTHESIS expression CLOSE_PARENTHESIS - | EVALUATE idArray IN OPEN_PARENTHESIS expression CLOSE_PARENTHESIS - | EVALUATE idArray IN numArray - | idArray OPEN_PARENTHESIS expression CLOSE_PARENTHESIS +evaluate: EVALUATE ID IN OPEN_PARENTHESIS expression CLOSE_PARENTHESIS { $$ = DoubleGrammarAction($1) } + | EVALUATE ID IN numArray { $$ = DoubleGrammarAction($1) } + | ID OPEN_PARENTHESIS expression CLOSE_PARENTHESIS { $$ = DoubleGrammarAction($1) } + | EVALUATE idArray IN OPEN_PARENTHESIS expression CLOSE_PARENTHESIS { $$ = DoubleGrammarAction($1) } + | EVALUATE idArray IN numArray { $$ = DoubleGrammarAction($1) } + | idArray OPEN_PARENTHESIS expression CLOSE_PARENTHESIS { $$ = DoubleGrammarAction($1) } ; -integrate: INTEGRATE ID BETWEEN OPEN_PARENTHESIS expression AND_INT expression CLOSE_PARENTHESIS - | INTEGRATE ID OPEN_PARENTHESIS expression AND_INT expression CLOSE_PARENTHESIS - | INTEGRATE idArray BETWEEN OPEN_PARENTHESIS expression AND_INT expression CLOSE_PARENTHESIS - | INTEGRATE idArray OPEN_PARENTHESIS expression AND_INT expression CLOSE_PARENTHESIS - | integrate ERR positive_constant +integrate: INTEGRATE ID BETWEEN OPEN_PARENTHESIS expression AND_INT expression CLOSE_PARENTHESIS { $$ = DoubleGrammarAction($1) } + | INTEGRATE ID OPEN_PARENTHESIS expression AND_INT expression CLOSE_PARENTHESIS { $$ = DoubleGrammarAction($1) } + | INTEGRATE idArray BETWEEN OPEN_PARENTHESIS expression AND_INT expression CLOSE_PARENTHESIS { $$ = DoubleGrammarAction($1) } + | INTEGRATE idArray OPEN_PARENTHESIS expression AND_INT expression CLOSE_PARENTHESIS { $$ = DoubleGrammarAction($1) } + | integrate ERR positive_constant { $$ = DoubleGrammarAction($1) } ; -squotes: SQUOTE - | SQUOTE squotes +squotes: SQUOTE { $$ = DoubleGrammarAction($1) } + | SQUOTE squotes { $$ = DoubleGrammarAction($1) } ; -print: PRINT OPEN_PARENTHESIS expression CLOSE_PARENTHESIS - | PRINT OPEN_PARENTHESIS escapeString CLOSE_PARENTHESIS +print: PRINT OPEN_PARENTHESIS expression CLOSE_PARENTHESIS { $$ = PrintExpression($3) } + | PRINT OPEN_PARENTHESIS escapeString CLOSE_PARENTHESIS { $$ = PrintExpression($3) } ; -statements: statement - | statement statements +statements: statement { $$ = $1 } + | statement statements { $$ = StatementsGrammarAction($1, $2) } ; -derivative: DERIVATIVE ID IN OPEN_PARENTHESIS expression CLOSE_PARENTHESIS - | DERIVATIVE INTEGER ID IN OPEN_PARENTHESIS expression CLOSE_PARENTHESIS - | ID squotes OPEN_PARENTHESIS expression CLOSE_PARENTHESIS - | DERIVATIVE idArray IN OPEN_PARENTHESIS expression CLOSE_PARENTHESIS - | DERIVATIVE INTEGER idArray IN OPEN_PARENTHESIS expression CLOSE_PARENTHESIS - | idArray squotes OPEN_PARENTHESIS expression CLOSE_PARENTHESIS - | derivative ERR positive_constant +derivative: DERIVATIVE ID IN OPEN_PARENTHESIS expression CLOSE_PARENTHESIS { $$ = DoubleGrammarAction($1) } + | DERIVATIVE INTEGER ID IN OPEN_PARENTHESIS expression CLOSE_PARENTHESIS { $$ = DoubleGrammarAction($1) } + | ID squotes OPEN_PARENTHESIS expression CLOSE_PARENTHESIS { $$ = DoubleGrammarAction($1) } + | DERIVATIVE idArray IN OPEN_PARENTHESIS expression CLOSE_PARENTHESIS { $$ = DoubleGrammarAction($1) } + | DERIVATIVE INTEGER idArray IN OPEN_PARENTHESIS expression CLOSE_PARENTHESIS { $$ = DoubleGrammarAction($1) } + | idArray squotes OPEN_PARENTHESIS expression CLOSE_PARENTHESIS { $$ = DoubleGrammarAction($1) } + | derivative ERR positive_constant { $$ = DoubleGrammarAction($1) } ; -block: OPEN_BRACES statements CLOSE_BRACES - | statement +block: OPEN_BRACES statements CLOSE_BRACES { $$ = DoubleGrammarAction($1) } + | statement { $$ = DoubleGrammarAction($1) } ; -if_statement: IF OPEN_PARENTHESIS boolean CLOSE_PARENTHESIS block - | IF OPEN_PARENTHESIS boolean CLOSE_PARENTHESIS block ELSE block +if_statement: IF OPEN_PARENTHESIS boolean CLOSE_PARENTHESIS block { $$ = IfBlockGrammarAction($3, $5) } + | IF OPEN_PARENTHESIS boolean CLOSE_PARENTHESIS block ELSE block { $$ = IfElseBlockGrammarAction($3, $5, $7) } ; -for_statement: FOR OPEN_PARENTHESIS declare_variable SEMICOLON boolean SEMICOLON assign_variable CLOSE_PARENTHESIS block - | FOR OPEN_PARENTHESIS SEMICOLON boolean SEMICOLON assign_variable CLOSE_PARENTHESIS block +for_statement: FOR OPEN_PARENTHESIS declare_variable SEMICOLON boolean SEMICOLON assign_variable CLOSE_PARENTHESIS block { $$ = DoubleGrammarAction($1) } + | FOR OPEN_PARENTHESIS SEMICOLON boolean SEMICOLON assign_variable CLOSE_PARENTHESIS block { $$ = DoubleGrammarAction($1) } ; -taylor: TAYLOR INTEGER ID +taylor: TAYLOR INTEGER ID { $$ = DoubleGrammarAction($1) } ; -while_statement: WHILE OPEN_PARENTHESIS boolean CLOSE_PARENTHESIS block +while_statement: WHILE OPEN_PARENTHESIS boolean CLOSE_PARENTHESIS block { $$ = WhileBlockGrammarAction($3, $5) } ; -declare_variable: VAR ID - | declare_variable ASSIGN expression +declare_variable: VAR ID { $$ = DoubleGrammarAction($1) } + | declare_variable ASSIGN expression { $$ = DoubleGrammarAction($1) } ; -assign_variable: ID ADD_ASSIGN expression - | ID SUB_ASSIGN expression - | ID MUL_ASSIGN expression - | ID DIV_ASSIGN expression - | ID INCREMENT - | ID DECREMENT - | ID ASSIGN expression +assign_variable: ID ADD_ASSIGN expression { $$ = DoubleGrammarAction($1) } + | ID SUB_ASSIGN expression { $$ = DoubleGrammarAction($1) } + | ID MUL_ASSIGN expression { $$ = DoubleGrammarAction($1) } + | ID DIV_ASSIGN expression { $$ = DoubleGrammarAction($1) } + | ID INCREMENT { $$ = DoubleGrammarAction($1) } + | ID DECREMENT { $$ = DoubleGrammarAction($1) } + | ID ASSIGN expression { $$ = DoubleGrammarAction($1) } ; %%