Skip to content
Snippets Groups Projects
Commit 1bd1e24a authored by mandeca's avatar mandeca :speech_balloon:
Browse files

Compila.

parent 1134937e
No related branches found
No related tags found
No related merge requests found
File deleted
LIBS=-lm
LEXICAL=lex ac.lex.c
LEX_DEP=ac.l ac.syn.h
LEX_NAME=ac.lex.c
SYNTAX=syn ac.syn.c ac.syn.h
SYN_DEP=ac.y
SYN_NAME=ac.syn.c
ac: ac.lex.c ac.syn.c
gcc -O3 -o $@ $? symtab.c ast.c $(LIBS)
gcc -O3 $? symtab.c ast.c -o $@ $(LIBS)
lex: $(LEX_DEP)
ac.lex.c: $(LEX_DEP)
syn: $(SYN_DEP)
ac.syn.c: $(SYN_DEP)
ac.syn.h: $(SYN_DEP)
$(LEXICAL):
lex -o $@ $?
flex -o $(LEX_NAME) $?
$(SYNTAX):
yacc --defines -o $@ $?
bison --defines -o $(SYN_NAME) $?
clean:
rm ac ac.lex.c ac.syn.c
rm ac ac.lex.c ac.syn.*
This diff is collapsed.
/* A Bison parser, made by GNU Bison 3.0.4. */
/* Bison interface for Yacc-like parsers in C
Copyright (C) 1984, 1989-1990, 2000-2015 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* As a special exception, you may create a larger work that contains
part or all of the Bison parser skeleton and distribute that work
under terms of your choice, so long as that work isn't itself a
parser generator using the skeleton or a modified version thereof
as a parser skeleton. Alternatively, if you modify or redistribute
the parser skeleton itself, you may (at your option) remove this
special exception, which will cause the skeleton and the resulting
Bison output files to be licensed under the GNU General Public
License without this special exception.
This special exception was added by the Free Software Foundation in
version 2.2 of Bison. */
#ifndef YY_YY_AC_SYN_H_INCLUDED
# define YY_YY_AC_SYN_H_INCLUDED
/* Debug traces. */
#ifndef YYDEBUG
# define YYDEBUG 0
#endif
#if YYDEBUG
extern int yydebug;
#endif
/* Token type. */
#ifndef YYTOKENTYPE
# define YYTOKENTYPE
enum yytokentype
{
EQ = 258,
NE = 259,
LT = 260,
GT = 261,
DIV = 262,
UNARY = 263,
ID = 264,
PRINT = 265,
READ = 266,
SIN = 267,
COS = 268,
TAN = 269,
LN = 270,
FLOAT = 271,
STR = 272,
WHILE = 273,
IF = 274,
ELSE = 275,
DEL_BL_A = 276,
DEL_BL_C = 277
};
#endif
/* Tokens. */
#define EQ 258
#define NE 259
#define LT 260
#define GT 261
#define DIV 262
#define UNARY 263
#define ID 264
#define PRINT 265
#define READ 266
#define SIN 267
#define COS 268
#define TAN 269
#define LN 270
#define FLOAT 271
#define STR 272
#define WHILE 273
#define IF 274
#define ELSE 275
#define DEL_BL_A 276
#define DEL_BL_C 277
/* Value type. */
#if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
union YYSTYPE
{
#line 25 "ac.y" /* yacc.c:1909 */
struct syn_elem {
unsigned char type; /* Tipo de token */
union { /* Valor útil del token */
double real_value; /* Como número real */
long int_value; /* Como número entero */
char *string; /* Como cadena de caracteres */
struct ast_t *node;
} u;
} s;
#line 110 "ac.syn.h" /* yacc.c:1909 */
};
typedef union YYSTYPE YYSTYPE;
# define YYSTYPE_IS_TRIVIAL 1
# define YYSTYPE_IS_DECLARED 1
#endif
extern YYSTYPE yylval;
int yyparse (void);
#endif /* !YY_YY_AC_SYN_H_INCLUDED */
......@@ -11,13 +11,15 @@
#include <string.h>
#include "autils.h"
#include "ast.c"
#include "ast.h"
#include "ac.syn.h"
int yylex();
int yyerror(char *error);
extern int yylineno;
/* Árbol de sintaxis */
static ast_t ast = NULL;
static ast_t *ast = NULL;
%}
......@@ -29,7 +31,7 @@ static ast_t ast = NULL;
double real_value; /* Como número real */
long int_value; /* Como número entero */
char *string; /* Como cadena de caracteres */
struct ast_t *node;
struct ast_s *node;
} u;
} s;
}
......@@ -44,7 +46,7 @@ static ast_t ast = NULL;
%token <s> ID
%term PRINT READ SIN COS TAN LN
%token <S> FLOAT
%token <s> FLOAT
%token <s> STR
%term WHILE IF ELSE DEL_BL_A DEL_BL_C
......@@ -97,7 +99,7 @@ SENTENCE_GROUP
| SENTENCE_GROUP SENTENCE '\n'
{
$$.type = AST_NODE_id;
$$.U.node = newNode('s', NULL, $2.u.node);
$$.u.node = newNode('s', NULL, $2.u.node);
}
;
......@@ -107,7 +109,7 @@ SENTENCE
: ID '=' EXPR
{
$$.type = AST_NODE_id;
$$.u.node = newNode(ASSIG, newLeafString(ID, $1.u.string), $3.u.node);
$$.u.node = newNode('=', newLeafString(ID, $1.u.string), $3.u.node);
}
/* Impresión de un valor */
| PRINT EXPR
......@@ -137,7 +139,7 @@ SENTENCE
| READ STR ID
{
$$.type = AST_NODE_id;
$$.u.node = newNode(READ, newLeafString(STR, $2.u.string), newLeafString(ID, $3.u.string))
$$.u.node = newNode(READ, newLeafString(STR, $2.u.string), newLeafString(ID, $3.u.string));
}
| WHILE '(' EXPR ')' BLOCK
{
......@@ -263,9 +265,9 @@ EXPR
char programName[256] = "";
/* Gestión de errores */
int yyerror(char *str)
int yyerror(char *error)
{
printf("%s(%d): error -- %s\n", programName, yylineno, str);
printf("%s(%d): error -- %s\n", programName, yylineno, error);
return 1;
}
......@@ -279,7 +281,7 @@ int main(int argc, char *argv[])
strcpy(programName, argv[1]);
yyin = fopen(programName, "rb");
FILE *yyin = fopen(programName, "rb");
if (yyin == NULL)
{
fprintf(stderr, "Error intentando abrir el fichero %s\n", programName);
......
......@@ -6,6 +6,7 @@
#include "ast.h"
#include "autils.h"
#include "ac.syn.h"
#include "symtab.h"
#include <math.h>
......@@ -17,7 +18,8 @@ extern char programName[];
ast_t *newLeafString(unsigned tag, char *str)
{
mallocCheck(ast_t *res, sizeof(ast_t));
ast_t *res;
mallocCheck(res, sizeof(ast_t));
res->lineNum = (unsigned)yylineno;
res->tag = tag;
res->u.str = str;
......@@ -27,7 +29,8 @@ ast_t *newLeafString(unsigned tag, char *str)
ast_t *newLeafNum(unsigned tag, double dval)
{
mallocCheck(ast_t *res, sizeof(ast_t));
ast_t *res;
mallocCheck(res, sizeof(ast_t));
res->lineNum = (unsigned)yylineno;
res->tag = tag;
res->u.real = dval;
......@@ -36,7 +39,8 @@ ast_t *newLeafNum(unsigned tag, double dval)
ast_t *newNode(unsigned tag, ast_t *l, ast_t *r)
{
mallocCheck(ast_t *res, sizeof(ast_t));
ast_t *res;
mallocCheck(res, sizeof(ast_t));
res->lineNum = (unsigned)yylineno;
res->tag = tag;
res->u.child.left = l;
......@@ -67,75 +71,67 @@ ast_t *newRoot(unsigned tag, ast_t *lst, ast_t *nd)
static symbol evaluateExpr(ast_t *root)
{
symbol value; /* Value of the expression. */
switch (root->tag)
{
case EQ:
if ((evaluateExpr(root->u.child.left)).value.real == (evaluateExpr(root->u.child.right)).value.real)
{
symbol value;
value.type = REAL_id;
value.value.real = 1.0;
return value;
}
else
{
symbol value;
value.type = REAL_id;
value.value.real = 0.0;
return value;
}
case NE:
if ((evaluateExpr(root->u.child.left)).value != (evaluateExpr(root->u.child.right)).value)
if ((evaluateExpr(root->u.child.left)).value.real != (evaluateExpr(root->u.child.right)).value.real)
{
symbol value;
value.type = REAL_id;
value.value.real = 1.0;
return value;
}
else
{
symbol value;
value.type = REAL_id;
value.value.real = 0.0;
return value;
}
case LT:
if ((evaluateExpr(root->u.child.left)).real.value < (evaluateExpr(root->u.child.right)).real.value)
if ((evaluateExpr(root->u.child.left)).value.real < (evaluateExpr(root->u.child.right)).value.real)
{
symbol value;
value.type = REAL_id;
value.value.real = 1.0;
return value;
}
else
{
symbol value;
value.type = REAL_id;
value.value.real = 0.0;
return value;
}
case GT:
if ((evaluateExpr(root->u.child.left)).real.value > (evaluateExpr(root->u.child.right)).real.value)
if ((evaluateExpr(root->u.child.left)).value.real > (evaluateExpr(root->u.child.right)).value.real)
{
symbol value;
value.type = REAL_id;
value.value.real = 1.0;
return value;
}
else
{
symbol value;
value.type = REAL_id;
value.value.real = 0.0;
return value;
}
case '+':
symbol value;
value.type = REAL_id;
value.value.real = (evaluateExpr(root->u.child.left)).value.real + (evaluateExpr(root->u.child.right)).value.real;
return value;
......@@ -143,51 +139,43 @@ static symbol evaluateExpr(ast_t *root)
case '-':
if (root->u.child.left == NULL)
{
symbol value;
value.type = REAL_id;
value.value.real = - (evaluateExpr(root->u.child.right)).value.real;
return value;
}
else
{
symbol value;
value.type = REAL_id;
value.value.real = (evaluateExpr(root->u.child.left)).value.real - (evaluateExpr(root->u.child.right)).value.real;
return value;
}
case '*':
symbol value;
value.type = REAL_id;
value.value.real = (evaluateExpr(root->u.child.left)).value.real * (evaluateExpr(root->u.child.right)).value.real;
return value;
case '/':
symbol value;
value.type = REAL_id;
value.value.real = (evaluateExpr(root->u.child.left)).value.real / (evaluateExpr(root->u.child.right)).value.real;
return value;
case '%':
symbol value;
value.type = REAL_id;
value.value.real = fmod((evaluateExpr(root->u.child.left)).value.real, (evaluateExpr(root->u.child.right)).value.real);
return value;
case DIV:
symbol value;
value.type = REAL_id;
value.value.real = (int)((evaluateExpr(root->u.child.left)).value.real / (evaluateExpr(root->u.child.right)).value.real);
return value;
case '^':
symbol value;
value.type = REAL_id;
value.value.real = pow((evaluateExpr(root->u.child.left)).value.real, (evaluateExpr(root->u.child.right)).value.real);
return value;
case FLOAT:
symbol value;
value.type = REAL_id;
value.value.real = root->u.real;
return value;
......@@ -196,31 +184,27 @@ static symbol evaluateExpr(ast_t *root)
return get(root->u.str);
case SIN:
symbol value;
value.type = REAL_id;
value.value.real = sin(evaluateExpr(root->u.child.left).value.real);
return value;
case COS:
symbol value;
value.type = REAL_id;
value.value.real = cos(evaluateExpr(root->u.child.left).value.real);
return value;
case TAN:
symbol value;
value.type = REAL_id;
value.value.real = tan(evaluateExpr(root->u.child.left).value.real);
return value;
case LN:
symbol value;
value.type = REAL_id;
value.value.real = log(evaluateExpr(root->u.child.left).value.real);
return value;
default:
fprintf(stderr, "%s(%d): error -- Etiqueta desconocida en expresión AST %u\n", programName, lnum(root), root)->tag;
fprintf(stderr, "%s(%d): error -- Etiqueta desconocida en expresión AST %u\n", programName, root->lineNum, root->tag);
break;
}
}
......@@ -232,7 +216,7 @@ static void evaluateNode(ast_t *root)
switch (root->tag)
{
case '=':
edit(root->u.child.left->u.str, (evaluateExpr(root->u.child.right)).value.real);
edit(root->u.child.left->u.str, evaluateExpr(root->u.child.right));
break;
case PRINT:
......@@ -256,7 +240,10 @@ static void evaluateNode(ast_t *root)
{
symbol number;
number.type = REAL_id;
scanf("%lf", &number.value.real);
if (scanf("%lf", &number.value.real) != 1)
{
printf("Advertencia: input erróneo. El programa intentará continuar.\n");
}
edit(root->u.child.right->u.str, number);
}
......@@ -265,7 +252,10 @@ static void evaluateNode(ast_t *root)
symbol number;
number.type = REAL_id;
printf("%s", root->u.child.left->u.str);
scanf("%lf", &number.value.real);
if (scanf("%lf", &number.value.real) != 1)
{
printf("Advertencia: input erróneo. El programa intentará continuar.\n");
}
edit(root->u.child.right->u.str, number);
}
......@@ -300,13 +290,13 @@ static void evaluateNode(ast_t *root)
break;
default:
fprintf(stderr, "%s(%d): error -- Etiqueta desconocida en expresión AST %u\n", programName, lnum(root), root)->tag;
fprintf(stderr, "%s(%d): error -- Etiqueta desconocida en expresión AST %u\n", programName, root->lineNum, root->tag);
break;
}
}
void evaluate(ast_t *root)
void process(ast_t *root)
{
while (root != NULL)
{
......
......@@ -13,11 +13,18 @@ typedef struct ast_s {
unsigned lineNum;
union {
struct {
ast_t *left, *right;
struct ast_s *left, *right;
} child;
char *str;
double real;
} u;
} ast_t;
ast_t *newLeafString(unsigned tag, char *str);
ast_t *newLeafNum(unsigned tag, double dval);
ast_t *newNode(unsigned tag, ast_t *l, ast_t *r);
ast_t *newRoot(unsigned tag, ast_t *lst, ast_t *nd);
void process(ast_t *root);
#endif
\ No newline at end of file
lex deleted 100644 → 0
This diff is collapsed.
syn deleted 100644 → 0
This diff is collapsed.
/* A Bison parser, made by GNU Bison 3.0.4. */
/* Bison interface for Yacc-like parsers in C
Copyright (C) 1984, 1989-1990, 2000-2015 Free Software Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* As a special exception, you may create a larger work that contains
part or all of the Bison parser skeleton and distribute that work
under terms of your choice, so long as that work isn't itself a
parser generator using the skeleton or a modified version thereof
as a parser skeleton. Alternatively, if you modify or redistribute
the parser skeleton itself, you may (at your option) remove this
special exception, which will cause the skeleton and the resulting
Bison output files to be licensed under the GNU General Public
License without this special exception.
This special exception was added by the Free Software Foundation in
version 2.2 of Bison. */
#ifndef YY_YY_SYN_H_INCLUDED
# define YY_YY_SYN_H_INCLUDED
/* Debug traces. */
#ifndef YYDEBUG
# define YYDEBUG 0
#endif
#if YYDEBUG
extern int yydebug;
#endif
/* Token type. */
#ifndef YYTOKENTYPE
# define YYTOKENTYPE
enum yytokentype
{
EQ = 258,
NE = 259,
LT = 260,
GT = 261,
DIV = 262,
UNARY = 263,
ID = 264,
PRINT = 265,
READ = 266,
SIN = 267,
COS = 268,
TAN = 269,
LN = 270,
FLOAT = 271,
STR = 272,
WHILE = 273,
IF = 274,
ELSE = 275,
DEL_BL_A = 276,
DEL_BL_C = 277
};
#endif
/* Tokens. */
#define EQ 258
#define NE 259
#define LT 260
#define GT 261
#define DIV 262
#define UNARY 263
#define ID 264
#define PRINT 265
#define READ 266
#define SIN 267
#define COS 268
#define TAN 269
#define LN 270
#define FLOAT 271
#define STR 272
#define WHILE 273
#define IF 274
#define ELSE 275
#define DEL_BL_A 276
#define DEL_BL_C 277
/* Value type. */
#if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
union YYSTYPE
{
#line 24 "ac.y" /* yacc.c:1909 */
struct syn_elem {
unsigned char type; /* Tipo de token */
union { /* Valor útil del token */
double real_value; /* Como número real */
long int_value; /* Como número entero */
char *string; /* Como cadena de caracteres */
struct ast_s *node;
} u;
} s;
#line 110 "syn.h" /* yacc.c:1909 */
};
typedef union YYSTYPE YYSTYPE;
# define YYSTYPE_IS_TRIVIAL 1
# define YYSTYPE_IS_DECLARED 1
#endif
extern YYSTYPE yylval;
int yyparse (void);
#endif /* !YY_YY_SYN_H_INCLUDED */
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment