Update flex-* and bison-* files

Co-authored-by: Ezequiel Bellver <ebellver@itba.edu.ar>
Co-authored-by: Juan Barmasch <jbarmasch@itba.edu.ar>
This commit is contained in:
Santiago Lo Coco 2022-04-10 15:04:27 -03:00
parent 2979002fed
commit 020c18ab62
5 changed files with 173 additions and 11 deletions

View File

@ -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);
}

View File

@ -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

View File

@ -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); }

View File

@ -26,5 +26,6 @@ int ConstantFactorGrammarAction(const int value);
// Constantes.
int IntegerConstantGrammarAction(const int value);
int DoubleConstantGrammarAction(const double value);
#endif

View File

@ -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
%%