Skip to content

Commit

Permalink
Merge pull request #2 from JackCloudman/dev
Browse files Browse the repository at this point in the history
Add Symbol table like hoc 3
  • Loading branch information
JackCloudman authored May 15, 2019
2 parents 0dee66c + ab0c3be commit d5db1f5
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 11 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Gram=Objects/y.tab.c Objetcs/y.tab.h

all: $(Gram) Objects/lex.yy.c Objects/Object.c Objects/stringObject.c Objects/listObject.c Objects/intObject.c Objects/init.c Objects/abstract.c
@gcc -I include -o Lala Objects/y.tab.c Objects/lex.yy.c Objects/Object.c Objects/stringObject.c Objects/listObject.c Objects/intObject.c Objects/init.c Objects/abstract.c
all: $(Gram) Objects/lex.yy.c Objects/Object.c Objects/stringObject.c Objects/listObject.c Objects/intObject.c Objects/init.c Objects/abstract.c Objects/Symbol.c
@gcc -I include -o Lala Objects/y.tab.c Objects/lex.yy.c Objects/Object.c Objects/stringObject.c Objects/listObject.c Objects/intObject.c Objects/init.c Objects/abstract.c Objects/Symbol.c
@echo Compiled

$(Gram): Objects/LLang.y
Expand Down
18 changes: 13 additions & 5 deletions Objects/LLang.l
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <stdio.h>
#include <stdlib.h>
#include "Lala.h"
#define YYSTYPE APLLObject
#include "Symbol.h"
#include "y.tab.h"
extern LLIntObject* LLInt_Make(int);
extern LLStringObject* LLString_Make(char*);
Expand All @@ -12,24 +12,32 @@ extern YYSTYPE yylval;
%}
/* Add your Flex definitions here */
/* Some definitions are already provided to you*/
op [-+*\/()]
op [-+*\/()=]
ws [ \t]+
digits [0-9]
number (0|[1-9]+{digits}*)\.?{digits}*
STRING \"(\\.|[^"\\])*\"
VAR [_]*[a-zA-Z][a-zA-Z0-9_]*
%%
{VAR} {
Symbol *s;
if((s=lookup(yytext))==0)
s = install(yytext,INDEF,0);
yylval.sym = s;
return s->type==INDEF?VAR:s->type;
}
{number} {
int dato;
RmWs(yytext);
sscanf(yytext,"%d",&dato);
yylval=(LLObject*) LLInt_Make(dato);
yylval.val=(LLObject*) LLInt_Make(dato);
return object;}
{op} |
\n {return *yytext;}
{ws} { /* Do nothing */ }
. { /* Do nothing */ }
{STRING} {
yylval = (LLObject*)LLString_Make(yytext);
yylval.val = (LLObject*)LLString_Make(yytext);
return object;
}
%%
Expand All @@ -45,4 +53,4 @@ void RmWs(char* str) {
j++;
}
str[j] = '\0';
}
}
24 changes: 21 additions & 3 deletions Objects/LLang.y
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,43 @@
#include <stdio.h>
#include "Lala.h"
#include "abstract.h"
#include "Symbol.h"
void yyerror (char *s);
int yylex ();
void warning(char *s, char *t);
extern void init();
%}
%token object
%union {
LLObject* val;
Symbol *sym;
}
%token <val> object
%token <sym> VAR BLTIN INDEF
%type <val> exp asgn

%right '='
%left '+' '-'
%left '*' '/'
%%
list:
| list'\n'
| list exp '\n' {LL_FUNC_PRINT($2,"\n");printf(">>> ");}
| list exp '\n' {LL_FUNC_PRINT($2,"\n");printf(">>> ");}
| list asgn '\n' {printf(">>> ");}
| list error '\n' {yyerrok;}
;
asgn: VAR '=' exp {$$=$1->u.val=$3; $1->type=VAR;}
;
exp: object { $$ = $1;}
| VAR {if($1->type == INDEF)
printf("Error: '%s' no esta definido\n",$1->name);
$$ = $1->u.val;}
| asgn
| exp '+' exp { $$ = LL_FUNC_ADD($1,$3); }
| exp '-' exp { $$ = LL_FUNC_SUB($1,$3); }
| exp '*' exp { $$ = LL_FUNC_MUL($1,$3);}
| exp '/' exp { $$ = LL_FUNC_DIV($1,$3); }
| '(' exp ')' { $$ = $2;}
|BLTIN '(' exp ')' { $$=(*($1->u.ptr))($3);}
;
%%
#include <stdio.h>
Expand All @@ -40,4 +58,4 @@ void warning(char *s, char *t){
fprintf (stderr, "%s: %s", progname, s);
if(t)
fprintf (stderr, " %s", t);
}
}
39 changes: 39 additions & 0 deletions Objects/Symbol.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include "Symbol.h"
static Symbol *symlist=0; /* tabla de simbolos: lista ligada */

Symbol *lookup(char *s) /* encontrar s en la tabla de símbolos */
{
Symbol *sp;
for (sp = symlist; sp != (Symbol *)0; sp = sp->next){
if (strcmp(sp->name, s)== 0)
return sp;
}
return 0; /* 0 ==> no se encontró */
}
/*La funcion install crea la variable en la tabla
de simbolos si esta que esta no existe*/
Symbol *install(char *s,int t,LLObject* o)
{
Symbol *sp;
char *emalloc();

sp = (Symbol *) emalloc(sizeof(Symbol));

sp->name = emalloc(strlen(s)+ 1) ; /* +1 para '\0' */
strcpy(sp->name, s);

sp->type = t;
sp->u.val = o;
sp->next = symlist; /* poner al frente de la lista */
symlist = sp;
return sp;
}

/* revisar el regreso desde malloc */
char *emalloc(unsigned n){
char *p;
p =(char*) malloc(n);
if(p == 0)
perror("out of memory");
return p;
}
1 change: 0 additions & 1 deletion include/Object.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,4 @@
LLObject_HEAD
}LLObject;
typedef LLObject *APLLObject;
#define YYSTYPE APLLObject
#endif
20 changes: 20 additions & 0 deletions include/Symbol.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef __SYMBOL_H__
#define __SYMBOL_H__
#include "Lala.h"
#include <string.h>
/*entrada a tabla de simbolos,
es una lista simplemente ligada*/
typedef struct Symbol {
char *name;
short type; /* VAR, BLTIN, UNDEF */

union {
LLObject* val; /* si es VAR */
LLObject* (*ptr)(); /* sí es BLTIN */
} u;

struct Symbol *next; /* para ligarse a otro */
} Symbol;

Symbol *install(char *s,int t, LLObject*), *lookup(char *s);
#endif

0 comments on commit d5db1f5

Please sign in to comment.