From 020c18ab628c387285ae35061a02aace511d442e Mon Sep 17 00:00:00 2001 From: Santiago Lo Coco Date: Sun, 10 Apr 2022 15:04:27 -0300 Subject: [PATCH] Update flex-* and bison-* files Co-authored-by: Ezequiel Bellver Co-authored-by: Juan Barmasch --- src/frontend/lexical-analysis/flex-actions.c | 12 +++ src/frontend/lexical-analysis/flex-actions.h | 45 ++++++++++- src/frontend/lexical-analysis/flex-patterns.l | 51 +++++++++++++ .../syntactic-analysis/bison-actions.h | 1 + .../syntactic-analysis/bison-grammar.y | 75 ++++++++++++++++--- 5 files changed, 173 insertions(+), 11 deletions(-) diff --git a/src/frontend/lexical-analysis/flex-actions.c b/src/frontend/lexical-analysis/flex-actions.c index a27ccc3..1dd6af6 100644 --- a/src/frontend/lexical-analysis/flex-actions.c +++ b/src/frontend/lexical-analysis/flex-actions.c @@ -12,6 +12,18 @@ TokenID IntegerPatternAction(const char * lexeme) { return INTEGER; } +TokenID DoublePatternAction(const char * lexeme) { + LogDebug("DoublePatternAction: '%s'.", lexeme); + yylval = atof(lexeme); + return DOUBLE; +} + +TokenID StringPatternAction(const char * lexeme) { + LogDebug("DoublePatternAction: '%s'.", lexeme); + yylval = lexeme; + return STRING; +} + void IgnoredPatternAction(const char * lexeme) { LogDebug("IgnoredPatternAction: '%s'.", lexeme); } diff --git a/src/frontend/lexical-analysis/flex-actions.h b/src/frontend/lexical-analysis/flex-actions.h index 1087d5b..2562494 100644 --- a/src/frontend/lexical-analysis/flex-actions.h +++ b/src/frontend/lexical-analysis/flex-actions.h @@ -23,13 +23,50 @@ typedef enum TokenID { SUB, MUL, DIV, + POW, - // Paréntesis. + // Brackets. OPEN_PARENTHESIS, CLOSE_PARENTHESIS, + OPEN_BRACES, + CLOSE_BRACES, + OPEN_BRACKETS, + CLOSE_BRACKETS, + FUNCTION, + IF, + ELSE, + FOR, + PRINT, + SEMICOLON, + + COMPOSITE, + DERIVATIVE, + INTEGRATE, + BETWEEN, + AND, + EVALUATE, + IN, + + COLON, + COMMA, + GREATER, + GREATER_EQUAL, + LESSER, + LESSER_EQUAL, + ASSIGN, + EQUAL, + + ADD_ASSIGN, + INCREMENT, + SUB_ASSIGN, + DECREMENT, + QUOTE, + // Tipos de dato. - INTEGER + INTEGER, + DOUBLE, + STRING } TokenID; /** @@ -41,8 +78,12 @@ typedef enum TokenID { TokenID IntegerPatternAction(const char * lexeme); +TokenID DoublePatternAction(const char * lexeme); + void IgnoredPatternAction(const char * lexeme); TokenID UnknownPatternAction(const char * lexeme); +TokenID StringPatternAction(const char * lexeme); + #endif diff --git a/src/frontend/lexical-analysis/flex-patterns.l b/src/frontend/lexical-analysis/flex-patterns.l index eeb9183..d9070e6 100644 --- a/src/frontend/lexical-analysis/flex-patterns.l +++ b/src/frontend/lexical-analysis/flex-patterns.l @@ -16,6 +16,8 @@ /* Patrones reutilizables. */ crlf \r\n digit [0-9] +char [a-zA-Z] +decimal [.] endline \n whitespace [ \f\n\r\t\v] @@ -25,12 +27,61 @@ whitespace [ \f\n\r\t\v] "-" { return SUB; } "*" { return MUL; } "/" { return DIV; } +"^" { return POW; } + +"fun" { return FUNCTION; } +"function" { return FUNCTION; } + +"if" { return IF; } +"else" { return ELSE; } +"for" { return FOR; } +"print" { return PRINT; } +";" { return SEMICOLON; } + +"o" { return COMPOSITE; } +"'"+ { return DERIVATIVE; } +"derivative" { return DERIVATIVE; } + +"integrate" { return INTEGRATE; } +"int" { return INTEGRATE; } + +"between" { return BETWEEN; } +"and" { return AND; } +"~" { return AND; } + +"evaluate" { return EVALUATE; } +"in" { return IN; } + +"{" { return OPEN_BRACES; } +"}" { return CLOSE_BRACES; } + +"{" { return OPEN_BRACKETS; } +"}" { return CLOSE_BRACKETS; } "(" { return OPEN_PARENTHESIS; } ")" { return CLOSE_PARENTHESIS; } +":" { return COLON; } +"," { return COMMA; } +">" { return GREATER; } +">=" { return GREATER_EQUAL; } +"<" { return LESSER; } +"<=" { return LESSER_EQUAL; } +"=" { return ASSIGN; } +"==" { return EQUAL; } + +"+=" { return ADD_ASSIGN; } +"++" { return INCREMENT; } +"-=" { return SUB_ASSIGN; } +"--" { return DECREMENT; } +"\"" { return QUOTE; } + {digit}+ { return IntegerPatternAction(yytext); } +{digit}+{decimal}{digit}+ { return DoublePatternAction(yytext); } + +{char}+ { return StringPatternAction(yytext); } + {whitespace} { IgnoredPatternAction(yytext); } . { return UnknownPatternAction(yytext); } diff --git a/src/frontend/syntactic-analysis/bison-actions.h b/src/frontend/syntactic-analysis/bison-actions.h index f94cf43..af18666 100644 --- a/src/frontend/syntactic-analysis/bison-actions.h +++ b/src/frontend/syntactic-analysis/bison-actions.h @@ -26,5 +26,6 @@ int ConstantFactorGrammarAction(const int value); // Constantes. int IntegerConstantGrammarAction(const int value); +int DoubleConstantGrammarAction(const double value); #endif diff --git a/src/frontend/syntactic-analysis/bison-grammar.y b/src/frontend/syntactic-analysis/bison-grammar.y index 5b500ce..4d60429 100644 --- a/src/frontend/syntactic-analysis/bison-grammar.y +++ b/src/frontend/syntactic-analysis/bison-grammar.y @@ -9,33 +9,90 @@ %token SUB %token MUL %token DIV +%token POW %token OPEN_PARENTHESIS %token CLOSE_PARENTHESIS +%token OPEN_BRACES +%token CLOSE_BRACES +%token OPEN_BRACKETS +%token CLOSE_BRACKETS + +%token FUNCTION +%token IF +%token ELSE +%token FOR +%token PRINT +%token SEMICOLON +%token COMPOSITE +%token DERIVATIVE +%token INTEGRATE +%token BETWEEN +%token AND +%token EVALUATE +%token IN + +%token COLON +%token COMMA +%token GREATER +%token GREATER_EQUAL +%token LESSER +%token LESSER_EQUAL +%token ASSIGN +%token EQUAL +%token ADD_ASSIGN +%token INCREMENT +%token SUB_ASSIGN +%token DECREMENT +%token QUOTE + %token INTEGER +%token DOUBLE +%token STRING // Reglas de asociatividad y precedencia (de menor a mayor): %left ADD SUB %left MUL DIV +%left POW %% -program: expression { $$ = ProgramGrammarAction($1); } +program: expression SEMICOLON { $$ = ProgramGrammarAction($1); } ; -expression: expression ADD expression { $$ = AdditionExpressionGrammarAction($1, $3); } - | expression SUB expression { $$ = SubtractionExpressionGrammarAction($1, $3); } - | expression MUL expression { $$ = MultiplicationExpressionGrammarAction($1, $3); } - | expression DIV expression { $$ = DivisionExpressionGrammarAction($1, $3); } - | factor { $$ = FactorExpressionGrammarAction($1); } +expression: expression ADD expression { $$ = AdditionExpressionGrammarAction($1, $3); } + | expression SUB expression { $$ = SubtractionExpressionGrammarAction($1, $3); } + | expression MUL expression { $$ = MultiplicationExpressionGrammarAction($1, $3); } + | expression DIV expression { $$ = DivisionExpressionGrammarAction($1, $3); } + | expression POW expression { $$ = PowerExpressionGrammarAction($1, $3); } + | function { $$ = FunctionExpressionGrammarAction($1); } + | factor { $$ = FactorExpressionGrammarAction($1); } ; -factor: OPEN_PARENTHESIS expression CLOSE_PARENTHESIS { $$ = ExpressionFactorGrammarAction($2); } - | constant { $$ = ConstantFactorGrammarAction($1); } +boolean: string LESSER constant { $$ = LesserExpressionGrammarAction($1, $3); } + | string LESSER_EQUAL constant { $$ = LesserEqualExpressionGrammarAction($1, $3); } + | string GREATER constant { $$ = GreaterExpressionGrammarAction($1, $3); } + | string GREATER_EQUAL constant { $$ = GreaterEqualExpressionGrammarAction($1, $3); } + | string EQUAL constant { $$ = EqualExpressionGrammarAction($1, $3); } ; -constant: INTEGER { $$ = IntegerConstantGrammarAction($1); } +factor: OPEN_PARENTHESIS expression CLOSE_PARENTHESIS { $$ = ExpressionFactorGrammarAction($2); } + | constant { $$ = ConstantFactorGrammarAction($1); } ; + +constant: DOUBLE { $$ = DoubleConstantGrammarAction($1); } + | INTEGER { $$ = IntegerConstantGrammarAction($1) } + ; + +function: FUNCTION string ASSIGN expression SEMICOLON { $$ = FunctionGrammarAction($2, $4); } + | FUNCTION string ASSIGN expression COLON boolean SEMICOLON { $$ = RestrictedFunctionGrammarAction($2, $4, $6); } + | FUNCTION string { $$ = PiecewiseFunctionGrammarAction(); } + ; + +string: STRING { $$ = StringGrammarAction($1); } + ; + +piecewise: FUNCTION string ASSIGN OPEN_BRACES expression COLON boolean CLOSE_BRACES SEMICOLON %%