Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion practica2/Practica2 David&Jose/Leeme.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Autores: Jose Luis Pariente Martinez y David Heras Pino
Código ejecutado y probado en OS X El Capitan 10.11.1
Código ejecutado y probado en MacOS X El Capitan 10.11.1
2 changes: 2 additions & 0 deletions practica2/Practica2 David&Jose/entrega/Leeme.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Autores: Jose Luis Pariente Martinez y David Heras Pino
Código ejecutado y probado en MacOS X El Capitan 10.11.1
Binary file not shown.
116 changes: 116 additions & 0 deletions practica2/Practica2 David&Jose/entrega/practica2.l
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
%{
#include "y.tab.h"
#include <string.h>

int nLineas=1;
%}

entero [0-9]+
real {entero}+[.]{entero}([eE][-+]?{entero})?
iden [a-zA-Z][a-zA-Z0-9]*
cadena \"(\\.|[^\\"])*\"
operador [+\-*/&|!<>%]
cdc1 \"%d\"
cdc2 \"%f\"
cdc3 \"%c\"
input [^\r\n]
eol (\n|\r|\r\n)
coment ("//"{input}*{eol}?)

%%

{entero} {
return NUM_ENTERO;
}

{real} {
return NUM_REAL;
}

{cdc1} {
yylval.tipo = 1;
return CDC;
}
{cdc2} {
yylval.tipo = 2;
return CDC;
}
{cdc3} {
yylval.tipo = 3;
return CDC;
}

{cadena} {
return CADENA;
}

[ \t] ;

"define" {
return DEFINE;
}

"int" {
return INT;
}

"float" {
return FLOAT;
}

"char" {
return CHAR;
}

"int main()" {
return MAIN;
}

"printf" {
return PRINTF;
}

"scanf" {
return SCANF;
}

"if" {
return IF;
}

"else" {
return ELSE;
}

"while" {
return WHILE;
}

"for" {
return FOR;
}

{iden} {
strcpy(yylval.nombreId, yytext);
return ID;
}

{operador}|"&&"|"||"|"=="|"<="|">="|"!=" {
return OPERADOR;
}
"++"|"--" {
return OPERADOR2;
}
{coment} { nLineas++; }

\n { nLineas++; }

. {
return yytext[0];
}

%%

int yywrap(){
return 1;
}
262 changes: 262 additions & 0 deletions practica2/Practica2 David&Jose/entrega/practica2.y
Original file line number Diff line number Diff line change
@@ -0,0 +1,262 @@
%{

#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>

void yyerror(const char* msg) {
fprintf(stderr, "%s\n", msg);
}
int yylex(void);
FILE *yyin;

int num_sim = 0;
extern int nLineas;

struct simbolo{
char nombre[30];
int tipo;
int cte;
int inicializado;
};
struct simbolo tablaSimbolos[50];
int dim = 0;

int buscarSimbolo(struct simbolo tablaSimbolos[],char nombre[30],int dim);

int errores = 0;

%}

%union {
int tipo; // 1.int 2.float 3.char
char nombreId[30];
}

%token <nombreId> ID
%token DEFINE
%token NUM_ENTERO
%token NUM_REAL
%token CADENA
%token INT
%token FLOAT
%token CHAR
%token MAIN
%token OPERADOR
%token OPERADOR2
%token PRINTF
%token IF
%token FOR
%token ELSE
%token SCANF
%token WHILE
%token <tipo> CDC
%type <tipo> tipo valor_cte val expr
%type <nombreId> id

%%
programa: declaracionesCtes main {if (errores > 0) {
printf("\n Programa compilado con %d errores en %d lineas\n",errores,nLineas);
}else{
printf("\n Programa compilado sin errores y con %d lineas\n", nLineas);
}
}

;
declaracionesCtes: /*vacia*/
| declaracionCte declaracionesCtes
;
declaracionCte: '#' DEFINE ID valor_cte {
int pos = buscarSimbolo(tablaSimbolos, $3, dim);
if (pos != dim){
printf("\n ERROR lin %d: Constante redeclarado",nLineas);
errores++;
}
else {
//lo añado
tablaSimbolos[dim].tipo = $4;
tablaSimbolos[dim].cte = 1;
tablaSimbolos[dim].inicializado = 1;
strcpy(tablaSimbolos[dim].nombre,$3);
dim++;
}
}
;
valor_cte: NUM_ENTERO { $$ = 1; }
| NUM_REAL { $$ = 2; }
| CADENA { $$ = 3; }
;
declaracionVbles: /*vacia*/
| declaracionVble declaracionVbles
;
declaracionVble: tipo id ';' {
int pos = buscarSimbolo(tablaSimbolos, $2, dim);
if (pos != dim){
printf("\n ERROR lin %d: Identificador redeclarado",nLineas);
errores++;
}
else {
//lo añado
tablaSimbolos[dim].tipo = $1;
tablaSimbolos[dim].cte = 0;
tablaSimbolos[dim].inicializado = 0;
strcpy(tablaSimbolos[dim].nombre,$2);
dim++;
}
}
| tipo id '=' expr ';' {
int pos = buscarSimbolo(tablaSimbolos, $2, dim);
if (pos != dim){
printf("\n ERROR lin %d: Identificador redeclarado",nLineas);
errores++;
}
else {
if ($1 != $4){
printf("\n ERROR lin %d: No coinciden los tipos", nLineas);
errores++;
}else if ($4 != 4){ //el tipo 4 es indicador de que no existe la variable
//lo añado
tablaSimbolos[dim].tipo = $1;
tablaSimbolos[dim].cte = 0;
tablaSimbolos[dim].inicializado = 1;
strcpy(tablaSimbolos[dim].nombre,$2);
dim++;
}
}
}
;
tipo: INT { $$ = 1; }
| FLOAT { $$ = 2; }
| CHAR { $$ = 3; }
;
id: ID { strcpy($$, $1); }
;
expr: val { $$ = $1; }
| val OPERADOR expr { if ($1 < $3)
{
$$ = $3;
}else{
$$ = $1;
}
}
;
val: ID { int pos = buscarSimbolo(tablaSimbolos, $1, dim);
int tipo = tablaSimbolos[pos].tipo;
if (tipo == 0){
printf("\n ERROR lin %d: Variable %s no declarada", nLineas, $1);
errores++;
tipo = 4;
}
if (tablaSimbolos[pos].inicializado == 0){
printf("\n ERROR lin %d: Variable %s no inicializada", nLineas, $1);
errores++;
tipo = 4;
}
$$ = tipo;
}
| valor_cte { $$ = $1; }
;
main: MAIN '{' cuerpo '}'
;
cuerpo: declaracionVbles instrucciones
;
instrucciones: /*vacía*/
| instruccion instrucciones
;
instruccion: asig ';'
| visu ';'
| lect ';'
| cond
| repe
;
asig: ID '=' expr { int pos = buscarSimbolo(tablaSimbolos, $1, dim);
int tipo = tablaSimbolos[pos].tipo;
if (tipo == 0){
printf("\n ERROR lin %d: Variable %s no declarada", nLineas, $1);
errores++;
}
if (tablaSimbolos[pos].cte == 1){
printf("\n ERROR lin %d: No se puede modificar el valor de una constante", nLineas);
errores++;
}
if ((tipo == 1 && $3 == 2) || (tipo == 3 && $3 == 2)){ //Problemas con entero = float y char = float
printf("\n ERROR lin %d: No coinciden los tipos", nLineas);
errores++;
}

}
| ID OPERADOR2
;
visu: PRINTF '(' listExpr ')'
;
listExpr: /*vacia*/
| expr
| expr ',' listExpr
;
lect: SCANF '(' CDC ',' OPERADOR ID ')' { int pos = buscarSimbolo(tablaSimbolos, $6, dim);
int tipo = tablaSimbolos[pos].tipo;
if (tipo == 0){
printf("\n ERROR lin %d: Variable %s no declarada", nLineas, $6);
errores++;
}
if ($3 != tipo && tipo != 0){
printf("\n ERROR lin %d: Tipos incompatibles en scanf", nLineas);
errores++;
}
}
;
cond: bloqueIF
| bloqueIF else
;
bloqueIF: IF '(' expr ')' '{' instrucciones '}'
;
else: ELSE '{' instrucciones '}'
| ELSE cond
;
repe: for
| while
;
for: FOR '(' inicia ';' expFOR ';' increm ')' '{' instrucciones '}'
;
inicia: /*vacia*/
| asig
;
expFOR: /*vacia*/
| expr
;
increm: /*vacia*/
| ID OPERADOR2 { int pos = buscarSimbolo(tablaSimbolos, $1, dim);
int tipo = tablaSimbolos[pos].tipo;
if (tipo == 0){
printf("\n ERROR lin %d: Variable %s no declarada", nLineas, $1);
errores++;
}
else if (tablaSimbolos[pos].inicializado == 0){
printf("\n ERROR lin %d: Variable %s no inicializada", nLineas, $1);
errores++;
}
}
;
while: WHILE '(' expr ')' '{' instrucciones '}'
;


%%

int main()
{
yyin=fopen("practica2conErrores.c","r");
yyparse();

}

int buscarSimbolo(struct simbolo tablaSimbolos[],char nombre[50],int dim){
for (int i=0;i<dim;i++){
if (strcmp(nombre,tablaSimbolos[i].nombre) == 0){
return i;
}
}
return dim;
}

Loading