Escolar Documentos
Profissional Documentos
Cultura Documentos
Facultad de Ingeniera
2012
ANALIZADOR RSC
PARA LENGUAJE C
Trabajo Prctico Final
COMPILADORES
Integrantes:
CRUZ, Silvia Eugenia Licenciatura en Sistemas 1005
RIOS, Flavio Oscar Licenciatura en Sistemas 713
SAGRERO, Marcelo Javier Licenciatura en Sistemas 1078
10/07/12
Analizador RSC - Lenguaje C Trabajo Prctico Final - 2012
INTRODUCCION
DESARROLLO
El Analizador RSC que aqu presentamos analiza sintcticamente cdigo en lenguaje C. As
de acuerdo a una entrada con una porcin del cdigo debe poder identificar las estructuras
principales, declaraciones de constantes, declaraciones de variables, estructuras de control (IF-
ELSE, SWITCH) y estructuras repetitivas (FOR, WHILE, DO WHILE).
Se debe tener en cuenta que el mismo reconoce las estructuras anidadas en ms de un nivel
de profundidad y que las acciones deben estar despus de dichas estructuras encerradas entre
llaves ({}).
De esta manera es capaz de realizar el anlisis completo del cdigo ingresado mostrando al
usuario un detalle de los elementos que componen dicho cdigo. En el caso de que la entrada sea
incorrecta, el anlisis se detendr en la palabra que produjo el error y solo mostrara los elementos
hasta all encontrados.
Estructura de un programa:
/*Zona de funciones*/
Libreras #include<librera.extension>
string frase1,frase2;
string frase1;
string frase2;
Variable= valor1;
pi=3.14;
Total= 5 + operando;
Total= 5 + 5;
Total=operando + 5;
Declaracin de variables;
Inicializacin de variables;
sentencias
void main()
sentencias
sentencias
calculo(pi , 3.14);
printf(esto,es,un,ejemplo);
{sentencias}
else
{sentencias}
printf(error);
else
printf(todo,bien);
default: sentencias
switch(opcion)
{ case 1:
if(80==10)
{ printf(ejemplo);
break;
case 2: printf(sigo);
default: function(valores,100);
Sentencias
printf( i );
Sentencia DO-WHILE do
Sentencias
}while(condicin);
do
printf( i )
i=i +1;
sentencias
printf( i );
i=i +1;
<librerias>:= <libreria>
|<librerias> <comentrios> <libreria>
|
<s_num>:= <S_NUM>
<include>:= <INCLUDE>
<s_menor>:= <S_MEN>
<s_mayor>:= <S_MAY>
<pto>:= <PTO>
<declaraciones>:= <declaracion>
|<declaraciones> <comentarios> <declaracion>
|
<asignaciones> := <asignacin>
|<asignaciones> <comentarios> <asignacin>
|
<ident> := <IDENT>
<s_igual> := <S_IGUAL>
<expr> := <IDENT>
|<NUMERO>
|<REAL>
<fun_rec>:= <funcion>
|<fun_rec> <comentarios> <funcion>
|
<arg>:= <exp>
|<arg> <coma> <expr>
|
<main>:= <MAIN>
<argumentos>:= <argumento>
|<argumentos> <coma> <argumento>
|
<sentencia>:= <sent_if>
|<asignacion>
|<sent_while>
|<sent_for>
|<sent_do>
|<sent_case>
|<call_func>
|<operaciones>
/****************************** IF ******************************************/
<sent_if>:= <pr_if> <condiciones> <llave_a> <sentencias> <llave_c>
<comentarios> <else>
<pr_if>:= <IF>
<pr_else>:= <ELSE>
<op_log>:= <S_MAYI>
|<s_menor>
|<s_mayor>
|<S_DIGUAL>
|<S_MENI>
<while>:= <WHILE>
<for>:= <FOR>
<do>:= <DO>
<cases>:= <case>
|<cases> <case>
<break>:= <BREAK>
<default>:= <DEFAULT>
<pr_case>:= <CASE>
<switch>:= <SWITCH>
<tipof>:= <void>
|<int>
|<float>
|<char>
|<string>
<tipod>:= |<int>
|<float>
|<char>
|<string>
<void>:= <VOID>
<int>:= <INT>
<float>:= <FLOAT>
<char>:= <CHAR>
<string>:= <STRING>
<expr_rec>:= |<expr_com>
|<expr_rec> <expr_com>
<expr_com>:= <IDENT>
|<NUMERO>
<coment_a>:= <COMENT_A>
<coment_c>:= <COMENT_C>
<par_a>:= <PAR_A>
<par_c>:= <PAR_C>
<coma>:= <COMA>
<llave_a>:= <LLAVE_A>
<llave_c>:= <LLAVE_C>
<dos_ptos>:= <DOS_PTOS>
<operador>:= <s_mas>
|<s_menos>
|<s_por>
|<s_div>
<s_por>:= <S_POR>
<s_div>:= <S_DIV>
<s_mas>:= <S_MAS>
<s_menos>:= <S_MENOS>
<s_dmas>:= <S_DMAS>
<s_dmen>:= <S_DMEN>
<asigna>:= <asignacion2>
|<asigna> <asignacion2>
/******************************************************************************/
/* Fichero : analex.l" */
%header{
#include "yystype.h"
%}
%{
#include "anasint.h"
int linea=1;
%}
nuevalinea \n
ident [a-zA-Z]([a-zA-Z]|[0-9])*
numero [0-9]+
real {numero}\.{numero}+
s_meni "<="
s_mayi ">="
s_digual "=="
s_dmas "++"
s_dmen "--"
coment_a "/*"
coment_c "*/"
%%
{blanco} {;}
{nuevalinea} {linea++;}
%%
/* Fichero: anasint.y */
%header{
#include "yystype.h"
%}
%{
#include <stdio.h>
#include <string.h>
#include "analex.h"
char pr[1000];
char ident[1000];
char simb[100];
char num[1000];
char opr[100];
int band_opr=0;
int band_i = 0;
int band_s = 0;
int band_n = 0;
int band_pr = 0;
void mostrar()
printf("******************************");
printf("******************************************************************************");
printf("\n****************************************************************************");
printf("\n");
%}
%token
%token <texto> S_MENI S_DIGUAL COMA PAR_C PAR_A LLAVE_A LLAVE_C S_IGUAL
%token <texto> PTO PTO_COMA S_NUM S_MEN S_MAY S_MAYI COMILLAS DOS_PTOS
%define PURE
%%
librerias: libreria
|;
|;
s_num: S_NUM {if (band_s == 0) {strcpy(simb,$1); strcat(simb," "); band_s = 1;} else
{if(strstr(simb,$1) == NULL) {strcat(simb,$1); strcat(simb," ");}}; };
include: INCLUDE {if (band_pr == 0) {strcpy(pr,$1); strcat(pr," "); band_pr = 1;} else {if(strstr(pr,$1)
== NULL) {strcat(pr,$1); strcat(pr," ");}}; };
s_menor: S_MEN {if (band_opr == 0) {strcpy(opr,$1); strcat(opr," "); band_opr = 1;} else
s_mayor: S_MAY {if (band_opr == 0) {strcpy(opr,$1); strcat(opr," "); band_opr = 1;} else
{if(strstr(opr,$1) == NULL) {strcat(opr,$1); strcat(opr," ");}}; };
pto: PTO {if (band_s == 0) {strcpy(simb,$1); strcat(simb," "); band_s = 1;} else {if(strstr(simb,$1) ==
NULL) {strcat(simb,$1); strcat(simb," ");}}; };
declaraciones: declaracion
|;
|ident
|;
asignaciones : asignacion
|;
ident : IDENT {if (band_i == 0) {strcpy(ident,$1); strcat(ident," "); band_i = 1;} else {if(strstr(ident,$1)
== NULL) {strcat(ident,$1); strcat(ident," ");}};}
s_igual : S_IGUAL {if (band_opr == 0) {strcpy(opr,$1); strcat(opr," "); band_opr = 1;} else
{if(strstr(opr,$1) == NULL) {strcat(opr,$1); strcat(opr," ");}}; };
expr : IDENT {if (band_i == 0) {strcpy(ident,$1); strcat(ident," "); band_i = 1;} else
{if(strstr(ident,$1) == NULL) {strcat(ident,$1); strcat(ident," ");}};}
comentarios fun_rec
fun_rec: funcion
|;
arg: expr
|;
main: MAIN {if (band_pr == 0) {strcpy(pr,$1); strcat(pr," "); band_pr = 1;} else {if(strstr(pr,$1) ==
NULL) {strcat(pr,$1); strcat(pr," ");}}; }
argumentos: argumento
|;
|;
/************************************************************************/
|sentencias
|;
|;
sentencia: sent_if
|asignacion
|sent_while
|sent_for
|sent_do
|sent_case
|call_func
|operaciones
/************************ IF ********************************************/
comentarios else
|;
pr_if: IF {if (band_pr == 0) {strcpy(pr,$1); strcat(pr," "); band_pr = 1;} else {if(strstr(pr,$1) == NULL)
{strcat(pr,$1); strcat(pr," ");}}; };
pr_else: ELSE{if (band_pr == 0) {strcpy(pr,$1); strcat(pr," "); band_pr = 1;} else {if(strstr(pr,$1) ==
NULL) {strcat(pr,$1); strcat(pr," ");}}; };
op_log: S_MAYI {if (band_opr == 0) {strcpy(opr,$1); strcat(opr," "); band_opr = 1;} else
{if(strstr(opr,$1) == NULL) {strcat(opr,$1); strcat(opr," ");}}; };
|s_menor
|s_mayor
/************************WHILE ***************************************/
while: WHILE {if (band_pr == 0) {strcpy(pr,$1); strcat(pr," "); band_pr = 1;} else {if(strstr(pr,$1) ==
NULL) {strcat(pr,$1); strcat(pr," ");}}; };
/*****************************SENTENCIA FOR*****************************/
for: FOR{if (band_pr == 0) {strcpy(pr,$1); strcat(pr," "); band_pr = 1;} else {if(strstr(pr,$1) ==
NULL) {strcat(pr,$1); strcat(pr," ");}}; }
|ident s_dmen
|s_dmas ident
|s_dmen ident
do: DO {if (band_pr == 0) {strcpy(pr,$1); strcat(pr," "); band_pr = 1;} else {if(strstr(pr,$1) ==
NULL) {strcat(pr,$1); strcat(pr," ");}}; };
cases: case
|cases case
|break pto_coma
break: BREAK{if (band_pr == 0) {strcpy(pr,$1); strcat(pr," "); band_pr = 1;} else {if(strstr(pr,$1) ==
NULL) {strcat(pr,$1); strcat(pr," ");}}; };
default: DEFAULT{if (band_pr == 0) {strcpy(pr,$1); strcat(pr," "); band_pr = 1;} else {if(strstr(pr,$1)
== NULL) {strcat(pr,$1); strcat(pr," ");}}; };
pr_case: CASE{if (band_pr == 0) {strcpy(pr,$1); strcat(pr," "); band_pr = 1;} else {if(strstr(pr,$1) ==
NULL) {strcat(pr,$1); strcat(pr," ");}}; };
switch: SWITCH{if (band_pr == 0) {strcpy(pr,$1); strcat(pr," "); band_pr = 1;} else {if(strstr(pr,$1)
== NULL) {strcat(pr,$1); strcat(pr," ");}}; };
tipof: void
|int
|float
|char
|string
tipod: |int
|float
|char
|string
void: VOID {if (band_pr == 0) {strcpy(pr,$1); strcat(pr," "); band_pr = 1;} else {if(strstr(pr,$1) ==
NULL) {strcat(pr,$1); strcat(pr," ");}}; }
int: INT {if (band_pr == 0) {strcpy(pr,$1); strcat(pr," "); band_pr = 1;} else {if(strstr(pr,$1) ==
NULL) {strcat(pr,$1); strcat(pr," ");}}; }
float: FLOAT{if (band_pr == 0) {strcpy(pr,$1); strcat(pr," "); band_pr = 1;} else {if(strstr(pr,$1) ==
NULL) {strcat(pr,$1); strcat(pr," ");}}; }
char: CHAR{if (band_pr == 0) {strcpy(pr,$1); strcat(pr," "); band_pr = 1;} else {if(strstr(pr,$1) ==
NULL) {strcat(pr,$1); strcat(pr," ");}}; }
string: STRING{if (band_pr == 0) {strcpy(pr,$1); strcat(pr," "); band_pr = 1;} else {if(strstr(pr,$1) ==
NULL) {strcat(pr,$1); strcat(pr," ");}}; }
|;
expr_rec: |expr_com
|expr_rec expr_com
expr_com: IDENT ;
|NUMERO ;
coment_a: COMENT_A
coment_c: COMENT_C
par_a: PAR_A{if (band_s == 0) {strcpy(simb,$1); strcat(simb," "); band_s = 1;} else {if(strstr(simb,$1)
== NULL) {strcat(simb,$1); strcat(simb," ");}};}
par_c: PAR_C{if (band_s == 0) {strcpy(simb,$1); strcat(simb," "); band_s = 1;} else {if(strstr(simb,$1)
== NULL) {strcat(simb,$1); strcat(simb," ");}}; }
coma: COMA {if (band_s == 0) {strcpy(simb,$1); strcat(simb," "); band_s = 1;} else
{if(strstr(simb,$1) == NULL) {strcat(simb,$1); strcat(simb," ");}};}
llave_a: LLAVE_A {if (band_s == 0) {strcpy(simb,$1); strcat(simb," "); band_s = 1;} else
{if(strstr(simb,$1) == NULL) {strcat(simb,$1); strcat(simb," ");}}; }
llave_c: LLAVE_C {if (band_s == 0) {strcpy(simb,$1); strcat(simb," "); band_s = 1;} else
|comentarios
operador: s_mas
|s_menos
|s_por
|s_div
s_por: S_POR{if (band_opr == 0) {strcpy(opr,$1); strcat(opr," "); band_opr = 1;} else {if(strstr(opr,$1)
== NULL) {strcat(opr,$1); strcat(opr," ");}}; };
s_div: S_DIV{if (band_opr == 0) {strcpy(opr,$1); strcat(opr," "); band_opr = 1;} else {if(strstr(opr,$1)
== NULL) {strcat(opr,$1); strcat(opr," ");}}; };
asigna : asignacion2
|asigna asignacion2
/**********************************************************************/
%%
mostrar();
CONCLUSION
El presente informe fue realizado partiendo de un ejemplo prctico proporcionado por la ctedra,
este material junto con la bibliografa pertinente nos permiti tener una idea bsica de cmo
iniciar el proyecto.
Debido a que el trabajo realizado solo utiliza un conjunto reducido de expresiones regulares y
tokens, este analizador no permite el uso de la totalidad de la sintaxis del lenguaje de
programacin C. Sin embargo reconoce una gran cantidad de elementos y analiza si la porcin de
cdigo ingresada posee o no errores de sintaxis.
Lo ms difcil para nosotros fue tener en cuenta la recursividad ya que no siempre resulta como
uno espera y la correccin de errores es muy complicada porque las etiquetas estn muy
relacionadas unas con otras, es por eso que se intento hacerlo de la manera mas simple posible
para sea fcil de modificar o para encontrar y corregir errores mas rpidamente.
El alcance del analizador RSC podra ser ampliado si se realiza la definicin de nuevos lexemas
que permitan reconocer otros tokens y crear as expresiones regulares para reconocer las
estructuras faltantes como ser arreglos, registros, punteros, etc.
Finalmente es intencin de este grupo que el presente material sirva como referencia para futuros
alumnos de la ctedra de compiladores. Esperamos que el analizador aqu presentado pueda
continuar siendo desarrollado y profundizado, logrando as una herramienta mucho ms compleja
y eficiente.
BIBLIOGRAFIA