Skip to content
Snippets Groups Projects
Commit 423ab6af authored by marivil's avatar marivil
Browse files

Funcionan comments multilinea. Fuente regex:...

parent 24a734b6
No related branches found
No related tags found
1 merge request!1Arbol
......@@ -16,7 +16,6 @@
extern int yylineno;
// Crear hoja String
ast_t *mkSlf(unsigned tag, char *str)
{
ast_t *res = xmalloc(sizeof(ast_t));
......@@ -26,7 +25,6 @@ ast_t *mkSlf(unsigned tag, char *str)
return res;
}
// Crear hoja Char
ast_t *mkClf(unsigned tag, char cVal)
{
ast_t *res = xmalloc(sizeof(ast_t));
......@@ -36,7 +34,6 @@ ast_t *mkClf(unsigned tag, char cVal)
return res;
}
// Crear hoja double (número)
ast_t *mkDlf(unsigned tag, double dval)
{
ast_t *res = xmalloc(sizeof(ast_t));
......@@ -46,7 +43,6 @@ ast_t *mkDlf(unsigned tag, double dval)
return res;
}
// Crear nodo (padre de l y r)
ast_t *mkNd(unsigned tag, ast_t *l, ast_t *r)
{
ast_t *res = xmalloc(sizeof(ast_t));
......@@ -57,10 +53,6 @@ ast_t *mkNd(unsigned tag, ast_t *l, ast_t *r)
return res;
}
// Añadir nodo nd como hijo de lst
// Se busca por la derecha hasta que no se encuentra una nada
// Se añade como hijo derecho de la hoja (último nodo en el que se ha buscado)
// un nodo con nd como hijo izquierdo
ast_t *appR(unsigned tag, ast_t *lst, ast_t *nd)
{
if (lst == NULL)
......@@ -86,28 +78,20 @@ ast_t *appR(unsigned tag, ast_t *lst, ast_t *nd)
return lst;
}
// ----------------------------------------------------------------------------
static double expr(ast_t *root)
{
// Procesar un nodo terminal
int m, n, temp;
switch (tag(root))
{
// Operador ternario (lo más complicado aquí)
case '?':
// Interpretar la expresión izquierda
if (expr(left(root)) != 0.0)
{
// Es la expresión izquierda
return expr(left(right(root)));
}
else
{
// Es la expresión derecha
return expr(right(right(root)));
}
// Los operadores lógicos es símplemente ejecutarlos y ya
case OR:
if (expr(left(root)) != 0.0 || expr(right(root)) != 0.0)
{
......@@ -180,15 +164,12 @@ static double expr(ast_t *root)
{
return 0.0;
}
// Las operaciones aritméticas, más de lo mismo: ejecutarlas
case '+':
return expr(left(root)) + expr(right(root));
case '-':
// Como operador unario (cambio de signo)
if (left(root) == NULL)
{
return -expr(right(root));
// Como operador binario (resta)
}
else
{
......@@ -209,15 +190,12 @@ static double expr(ast_t *root)
{
return 0.0;
}
// Los terminales: devolver su valor correcto
case FLOAT:
return dv(root);
case CHAR:
return cv(root);
// Variables: leerlas de la tabla de símbolos
case IDENT:
return read(sv(root));
// Funciones predefinidas: ejecutarlas
case SIN:
return sin(expr(left(root)));
case COS:
......@@ -275,14 +253,12 @@ static double expr(ast_t *root)
}
}
return (double)m;
// Strange coisas
default:
prError((unsigned short)lnum(root), "Unknown tag in expr AST %u\n", tag(root), NULL);
break;
}
}
// Procesar el árbol (un nodo de sentencia)
static void proc(ast_t *root)
{
double cond;
......@@ -293,14 +269,9 @@ static void proc(ast_t *root)
evaluate(right(root));
break;
case '=':
// Insertar en la table de símbolos el identificador (izquierda)
// y su valor (derecha)
// sv = valor de String del nodo (nombre de la variable)
insertModify(sv(left(root)), expr(right(root)));
break;
case IF:
// Si no hay nodo izquierdo (String)
// imprimir solo la variable (nodo derecho)
cond = expr(left(root));
if (cond != 0.0)
{
......@@ -314,7 +285,7 @@ static void proc(ast_t *root)
break;
case WHILE:
if (left(root) != NULL)
{ //WHILE
{
cond = expr(left(left(root)));
while (cond != 0.0)
{
......@@ -326,7 +297,7 @@ static void proc(ast_t *root)
}
}
else
{ //DO-WHILE
{
evaluate(right(right(root)));
cond = expr(left(right(root)));
while (cond != 0.0)
......@@ -340,19 +311,13 @@ static void proc(ast_t *root)
}
break;
case PRINT:
// Si no hay nodo izquierdo (String)
// imprimir solo la variable (nodo derecho)
if (left(root) == NULL)
{
printf("%g\n", expr(right(root)));
// Si no hay nodo derecho (variable)
// imprimir solo la String (nodo izquierdo)
}
else if (right(root) == NULL)
{
printf("%s\n", sv(left(root)));
// Si hay nodo derecho e izquierdo
// imprimir la String seguido de la variable
}
else
{
......@@ -360,41 +325,31 @@ static void proc(ast_t *root)
}
break;
case READ:
// No hay que imprimir
if (left(root) == NULL)
{
double rval;
scanf("%lf", &rval);
// Cambiar el valor del símbolo en la tabla
insertModify(sv(right(root)), rval);
// Hay que imprimir (nodo izquierdo)
}
else
{
double rval;
printf("%s", sv(left(root)));
scanf("%lf", &rval);
// Cambiar el valor del símbolo en la tabla
insertModify(sv(right(root)), rval);
}
break;
default:
// Bro momento
prError((unsigned short)lnum(root), "Unknown tag in sentencia AST %u\n", tag(root), NULL);
break;
}
}
// Procesar el árbol entero (ejecutar el programa)
void evaluate(ast_t *root)
{
while (root != NULL)
{
// Ejecutar el subárbol izquierdo
proc(left(root));
// Pasar al subárbol derecho
root = right(root);
}
}
// ----- EOF ------
......@@ -56,10 +56,10 @@ unsigned keycodes[KWLEN] = {
DO
};
static void lower(char *s); /* Pasar todas las letras a minúsculas (definida abajo) */
static char *readStr(void); /* Leer y guardar String como "variable" (definida abajo) */
static void lower(char *s);
static char *readStr(void);
int yywrap(void) { return 1; } /* Al terminar la lectura de un fichero, pista */
int yywrap(void) { return 1; }
%}
LETTER ([a-zA-Z])
......@@ -73,7 +73,7 @@ FLOAT1 ({DIGITS}"."{DIGITS})
FLOAT2 ({DIGITS}{EXP})
FLOAT3 ({DIGITS}"."{DIGITS}{EXP})
IDENT (_*{LETTER}({LETTER}|{DIGIT})*)
IDENT ((_|{LETTER})(_|{LETTER}|{DIGIT})*)
NEWLN ([\n])
......@@ -91,7 +91,8 @@ STRSTART (["])
CHAR (\'.?\')
COMSIMP (\/\/.*)
COMMULT (\\\*.*\*\/)
COMMULT "/*"([^*]|\*+[^*/])*\*+"/"
%%
{WSPC} ;
{NEWLN} {
......@@ -101,31 +102,37 @@ COMMULT (\\\*.*\*\/)
return yytext[0];
}
{COMSIMP} ;
{COMMULT} ;
{COMMULT} {
int i;
for(i = 0; i< yyleng; i++){
if(yytext[i] == '\n')
yylineno ++;
}
}
{IDENT} {
int i ;
char *res;
lower(yytext); /* Se pasa a minúsculas */
/* Se busca si es palabra una clave */
//lower(yytext); Lenguaje case-sensitive
//Palabras clave
for (i = 0; i<KWLEN; i++ ){
if (strcmp(keywords[i],yytext)==0)
return keycodes[i];
}
/* Si no es una palabra clave */
yyStr(yylval)=sdup(yytext); /* Se copia el identificador leído */
yyFlag(yylval)=fIDENT; /* Se apunta que es un identificador */
return IDENT; /* Se devuelve que se ha leído un identificador */
//Identificadores
yyStr(yylval)=sdup(yytext);
yyFlag(yylval)=fIDENT;
return IDENT;
}
{DIGITS} {
/* Se lee el número entero */
long int li;
sscanf(yytext,"%ld",&li);
/* Se castea a double */
yyFloat(yylval) = (double)li;
yyFlag(yylval) = fFLOAT;
......@@ -133,7 +140,6 @@ COMMULT (\\\*.*\*\/)
}
{FLOAT} {
/* Se lee el número decimal */
sscanf(yytext,"%lf",&( yyFloat(yylval) ));
yyFlag(yylval) = fFLOAT;
......@@ -141,7 +147,6 @@ COMMULT (\\\*.*\*\/)
}
{STRSTART} {
/* Se lee la String */
yyStr(yylval) = readStr();
yyFlag(yylval) = fSTR;
......@@ -187,68 +192,51 @@ COMMULT (\\\*.*\*\/)
}
. {
/* Cualquier otro caracter: error */
prError(yylineno,"Unexpected character in input: %c [%d]\n",yytext[0],yytext[0],NULL);
}
%%
/* Pasar la String s a minúsculas */
static void lower(char *s) {
unsigned l = strlen(s);
/* Vamos de atrás a adelante */
while (l>0) {
--l;
/* Si es una letra mayúscula, pues se hace minúscula. Easy */
if (s[l]>='A' && s[l]<='Z') s[l] = s[l]+'a'-'A';
}
}
/* Añadir caracter c a la String s, acutalizando su tamaño */
static void addStr(char **s, unsigned long *len, char c) {
char buf[2];
buf[0] = c;
buf[1] = '\0';
/* Si la cadena actualmente tiene tantos caracteres (o más) */
/* como undica su tamaño */
if (strlen(*s) >= *len) {
char *ss;
/* Se alocan 1024 caracteres más (más el \0 final) */
ss=xmalloc(*len+1025);
/* Se copia */
strcpy(ss,*s);
/* Se libera la anterior */
xfree(*s);
*s=ss;
/* Se actualiza el tamaño */
*len = *len+1024;
}
/* Se concatena la cadena actual con el caracter + \0 */
strcat(*s,buf);
}
/* Leer y almacenar una String */
static char *readStr(void) {
int c;
char *buff; /* Buffer donde se almacena la String */
unsigned long len = 256; /* Longitud máxima: 256 caracteres */
char *buff;
unsigned long len = 256;
buff=xmalloc(257); /* Se aloca memoria para la String */
buff[0]='\0'; /* Se pone el primer caracter como '\0' */
buff=xmalloc(257);
buff[0]='\0';
/* Se van leyendo caracteres: */
do {
/* Se lee un caracter */
c = input();
/* Caracteres erróneos */
if (c < ' ') prError(yylineno,"Unexpected symbol in string literal [%d]\n",c,NULL);
/* Si se lee el terminador de cadena, se finaliza el bucle */
if (c == '"') break;
/* Las subcadenas '\\' y '\"' se leen como solo '\' y '"', respectivamente */
if (c=='\\') {
c == input();
if (c!='\\' && c !='"') {
......@@ -257,7 +245,6 @@ static char *readStr(void) {
}
}
/* Se añade el caracter leído a la cadena */
addStr(&buff,&len,c);
} while (1);
......
//(10 < 11);
//? print("Hola"); : print("Adios");
var x = 5;
var x += 2;
print(x);
/*
NJpasds
*/
var x_ = 5;
var x_ += 2;
print(x_);
dasd
/* HOLA PUTA*/
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment