You are on page 1of 20

MANUAL DE

PRCTICAS DE LABORATORIO
Para la Unidad de Aprendizaje de:
COMPILADORES
Del Programa Educativo Ingeniera
en Computacin
Elaborado por:
Autor(es del !anual
"ec#a:
$evisado por:
"ec#a:
Centro Universitario UAEM Valle de Teotihuacn
ndice.
PRCTICA 1. Mquina virtual TM para el compilador Tiny...................................................................3
Obetivo!..........................................................................................................................3
Introducci"n!....................................................................................................................3
Material y equipo a utili#ar!.............................................................................................3
$e%arrollo!.......................................................................................................................3
&videncia%!.......................................................................................................................1'
Re(erencia%!.....................................................................................................................)*
PRCTICA 1. Mquina virtual TM para el !"pila#!r Tin$
Objetivo:
Construir la !%&uina virtual &ue ser% la base para el desarrollo del Compilador
'in()
Introduccin:
*a ma&uina virtual '! es una pe&ue+a m%&uina virtual objetivo simple de donde
deriva su nombre '! ('in( mac#ine, &ue utiliza cdigo ensamblado en lugar de
cdigo binario o #e-adecimal ( cuenta con una memoria de instruccin de slo
lectura, una memoria de datos, ( un conjunto de oc#o registros de propsito
general) 'odos .stos registros utilizan direcciones enteras no negativas &ue
comienzan en /) El registro 0 es el contador del programa ( es el 1nico registro
especial)
Material y equipo a utilizar:
Listar los materiales y cantidades a utilizar. Ejemplo.
De%ripi&n Canti#a#
Un compilador de lenguaje C (2amilia 3CC de U4I5
de pre2erencia
6
Un procesador de te-to 6
Una Computadora con 7istema 8perativo 9indo:s o
*inu-
6
Desarrollo:
Describir los pasos para llevar a cabo las actividades de la prctica
satisfactoriamente. Ejemplo:
6) Escribir el siguiente cdigo 2uente en lenguaje C:
/****************************************************/
/* File: tm.c */
/* The TM ("Tiny Machine") computer */
/* Compiler Construction: Principles and Practice */
/* Kenneth C. Louden */
/****************************************************/
include !stdio.h"
include !stdli#.h"
include !strin$.h"
include !ctype.h"
i%nde% T&'(
de)ne T&'( *
endi%
i%nde% F+L,(
de)ne F+L,( -
endi%
/******* const *******/
de)ne .+//&0,.1( *-23 /* increase %or lar$e pro$rams */
de)ne /+//&0,.1( *-23 /* increase %or lar$e pro$rams */
de)ne 450&(6, 7
de)ne PC0&(6 8
de)ne L.4(,.1( *2*
de)ne 95&/,.1( 2-
/******* type *******/
typede% enum :
opcl&&; /* re$ operands r;s;t */
opcl&M; /* re$ r; mem d<s */
opcl&+ /* re$ r; int d<s */
= 5PCL+,,>
typede% enum :
/* && instructions */
op?+LT; /* && halt; operands are i$nored */
op.4; /* && read into re$(r)> s and t are i$nored */
op5'T; /* && @rite %rom re$(r); s and t are i$nored */
op+//; /* && re$(r) A re$(s)<re$(t) */
op,'B; /* && re$(r) A re$(s)Cre$(t) */
opM'L; /* && re$(r) A re$(s)*re$(t) */
op/.D; /* && re$(r) A re$(s)/re$(t) */
op&&Lim; /* limit o% && opcodes */
/* &M instructions */
opL/; /* &M re$(r) A mem(d<re$(s)) */
op,T; /* &M mem(d<re$(s)) A re$(r) */
op&MLim; /* Limit o% &M opcodes */
/* &+ instructions */
opL/+; /* &+ re$(r) A d<re$(s) */
opL/C; /* &+ re$(r) A d > re$(s) is i$nored */
opELT; /* &+ i% re$(r)!- then re$(8) A d<re$(s) */
opEL(; /* &+ i% re$(r)!A- then re$(8) A d<re$(s) */
opE6T; /* &+ i% re$(r)"- then re$(8) A d<re$(s) */
opE6(; /* &+ i% re$(r)"A- then re$(8) A d<re$(s) */
opE(F; /* &+ i% re$(r)AA- then re$(8) A d<re$(s) */
opE4(; /* &+ i% re$(r)GA- then re$(8) A d<re$(s) */
op&+Lim /* Limit o% &+ opcodes */
= 5PC5/(>
typede% enum :
sr5K+H;
sr?+LT;
sr.M(M0(&&;
sr/M(M0(&&;
sr1(&5/.D./(
= ,T(P&(,'LT>
typede% struct :
int iop >
int iar$* >
int iar$2 >
int iar$I >
= .4,T&'CT.54>
/******** Jars ********/
int iloc A - >
int dloc A - >
int traceKa$ A F+L,(>
int icountKa$ A F+L,(>
.4,T&'CT.54 iMem L.+//&0,.1(M>
int dMem L/+//&0,.1(M>
int re$ L450&(6,M>
char * opCodeTa#LM
A :"?+LT";".4";"5'T";"+//";",'B";"M'L";"/.D";"NNNN";
/* && opcodes */
"L/";",T";"NNNN"; /* &M opcodes */
"L/+";"L/C";"ELT";"EL(";"E6T";"E6(";"E(F";"E4(";"NNNN"
/* &+ opcodes */
=>
char * step&esultTa#LM
A :"5K";"?alted";".nstruction Memory Fault";
"/ata Memory Fault";"/iJision #y -"
=>
char p$m4ameL2-M>
F.L( *p$m >
char in0LineLL.4(,.1(M >
int lineLen >
int inCol >
int num >
char @ordL95&/,.1(M >
char ch >
int done >
/********************************************/
int opClass( int c )
: i% ( c !A op&&Lim) return ( opcl&& )>
else i% ( c !A op&MLim) return ( opcl&M )>
else return ( opcl&+ )>
= /* opClass */
/********************************************/
Joid @rite.nstruction ( int loc )
: print%( "OPd: "; loc) >
i% ( (loc "A -) QQ (loc ! .+//&0,.1() )
: print%("ORsOId;"; opCodeTa#LiMemLlocM.iopM; iMemLlocM.iar$*)>
s@itch ( opClass(iMemLlocM.iop) )
: case opcl&&: print%("O*d;O*d"; iMemLlocM.iar$2; iMemLlocM.iar$I)>
#reaS>
case opcl&M:
case opcl&+: print%("OId(O*d)"; iMemLlocM.iar$2; iMemLlocM.iar$I)>
#reaS>
=
print% ("Tn") >
=
= /* @rite.nstruction */
/********************************************/
Joid $etCh (Joid)
: i% (<<inCol ! lineLen)
ch A in0LineLinColM >
else ch A U U >
= /* $etCh */
/********************************************/
int nonBlanS (Joid)
: @hile ((inCol ! lineLen)
QQ (in0LineLinColM AA U U) )
inCol<< >
i% (inCol ! lineLen)
: ch A in0LineLinColM >
return T&'( > =
else
: ch A U U >
return F+L,( > =
= /* nonBlanS */
/********************************************/
int $et4um (Joid)
: int si$n>
int term>
int temp A F+L,(>
num A - >
do
: si$n A *>
@hile ( nonBlanS() QQ ((ch AA U<U) VV (ch AA UCU)) )
: temp A F+L,( >
i% (ch AA UCU) si$n A C si$n >
$etCh()>
=
term A - >
nonBlanS()>
@hile (isdi$it(ch))
: temp A T&'( >
term A term * *- < ( ch C U-U ) >
$etCh()>
=
num A num < (term * si$n) >
= @hile ( (nonBlanS()) QQ ((ch AA U<U) VV (ch AA UCU)) ) >
return temp>
= /* $et4um */
/********************************************/
int $et9ord (Joid)
: int temp A F+L,(>
int len$th A ->
i% (nonBlanS ())
: @hile (isalnum(ch))
: i% (len$th ! 95&/,.1(C*) @ord Llen$th<<M A ch >
$etCh() >
=
@ordLlen$thM A UT-U>
temp A (len$th GA -)>
=
return temp>
= /* $et9ord */
/********************************************/
int sSipCh ( char c )
: int temp A F+L,(>
i% ( nonBlanS() QQ (ch AA c) )
: $etCh()>
temp A T&'(>
=
return temp>
= /* sSipCh */
/********************************************/
int at(5L(Joid)
: return ( G nonBlanS ())>
= /* at(5L */
/********************************************/
int error( char * ms$; int line4o; int inst4o)
: print%("Line Od";line4o)>
i% (inst4o "A -) print%(" (.nstruction Od)";inst4o)>
print%(" OsTn";ms$)>
return F+L,(>
= /* error */
/********************************************/
int read.nstructions (Joid)
: 5PC5/( op>
int ar$*; ar$2; ar$I>
int loc; re$4o; line4o>
%or (re$4o A - > re$4o ! 450&(6, > re$4o<<)
re$Lre$4oM A - >
dMemL-M A /+//&0,.1( C * >
%or (loc A * > loc ! /+//&0,.1( > loc<<)
dMemLlocM A - >
%or (loc A - > loc ! .+//&0,.1( > loc<<)
: iMemLlocM.iop A op?+LT >
iMemLlocM.iar$* A - >
iMemLlocM.iar$2 A - >
iMemLlocM.iar$I A - >
=
line4o A - >
@hile (G %eo%(p$m))
: %$ets( in0Line; L.4(,.1(C2; p$m ) >
inCol A - >
line4o<<>
lineLen A strlen(in0Line)C* >
i% (in0LineLlineLenMAAUTnU) in0LineLlineLenM A UT-U >
else in0LineL<<lineLenM A UT-U>
i% ( (nonBlanS()) QQ (in0LineLinColM GA U*U) )
: i% (G $et4um())
return error("Bad location"; line4o;C*)>
loc A num>
i% (loc " .+//&0,.1()
return error("Location too lar$e";line4o;loc)>
i% (G sSipCh(U:U))
return error("Missin$ colon"; line4o;loc)>
i% (G $et9ord ())
return error("Missin$ opcode"; line4o;loc)>
op A op?+LT >
@hile ((op ! op&+Lim)
QQ (strncmp(opCodeTa#LopM; @ord; 3) GA -) )
op<< >
i% (strncmp(opCodeTa#LopM; @ord; 3) GA -)
return error(".lle$al opcode"; line4o;loc)>
s@itch ( opClass(op) )
: case opcl&& :
/***********************************/
i% ( (G $et4um ()) VV (num ! -) VV (num "A 450&(6,) )
return error("Bad )rst re$ister"; line4o;loc)>
ar$* A num>
i% ( G sSipCh(U;U))
return error("Missin$ comma"; line4o; loc)>
i% ( (G $et4um ()) VV (num ! -) VV (num "A 450&(6,) )
return error("Bad second re$ister"; line4o; loc)>
ar$2 A num>
i% ( G sSipCh(U;U))
return error("Missin$ comma"; line4o;loc)>
i% ( (G $et4um ()) VV (num ! -) VV (num "A 450&(6,) )
return error("Bad third re$ister"; line4o;loc)>
ar$I A num>
#reaS>
case opcl&M :
case opcl&+ :
/***********************************/
i% ( (G $et4um ()) VV (num ! -) VV (num "A 450&(6,) )
return error("Bad )rst re$ister"; line4o;loc)>
ar$* A num>
i% ( G sSipCh(U;U))
return error("Missin$ comma"; line4o;loc)>
i% (G $et4um ())
return error("Bad displacement"; line4o;loc)>
ar$2 A num>
i% ( G sSipCh(U(U) QQ G sSipCh(U;U) )
return error("Missin$ LParen"; line4o;loc)>
i% ( (G $et4um ()) VV (num ! -) VV (num "A 450&(6,))
return error("Bad second re$ister"; line4o;loc)>
ar$I A num>
#reaS>
=
iMemLlocM.iop A op>
iMemLlocM.iar$* A ar$*>
iMemLlocM.iar$2 A ar$2>
iMemLlocM.iar$I A ar$I>
=
=
return T&'(>
= /* read.nstructions */
/********************************************/
,T(P&(,'LT stepTM (Joid)
: .4,T&'CT.54 currentinstruction >
int pc >
int r;s;t;m >
int oS >
pc A re$LPC0&(6M >
i% ( (pc ! -) VV (pc " .+//&0,.1() )
return sr.M(M0(&& >
re$LPC0&(6M A pc < * >
currentinstruction A iMemL pc M >
s@itch (opClass(currentinstruction.iop) )
: case opcl&& :
/***********************************/
r A currentinstruction.iar$* >
s A currentinstruction.iar$2 >
t A currentinstruction.iar$I >
#reaS>
case opcl&M :
/***********************************/
r A currentinstruction.iar$* >
s A currentinstruction.iar$I >
m A currentinstruction.iar$2 < re$LsM >
i% ( (m ! -) VV (m " /+//&0,.1())
return sr/M(M0(&& >
#reaS>
case opcl&+ :
/***********************************/
r A currentinstruction.iar$* >
s A currentinstruction.iar$I >
m A currentinstruction.iar$2 < re$LsM >
#reaS>
= /* case */
s@itch ( currentinstruction.iop)
: /* && instructions */
case op?+LT :
/***********************************/
print%("?+LT: O*d;O*d;O*dTn";r;s;t)>
return sr?+LT >
/* #reaS> */
case op.4 :
/***********************************/
do
: print%("(nter Jalue %or .4 instruction: ") >
Wush (stdin)>
Wush (stdout)>
$ets(in0Line)>
lineLen A strlen(in0Line) >
inCol A ->
oS A $et4um()>
i% ( G oS ) print% (".lle$al JalueTn")>
else re$LrM A num>
=
@hile (G oS)>
#reaS>
case op5'T :
print% ("5'T instruction prints: OdTn"; re$LrM ) >
#reaS>
case op+// : re$LrM A re$LsM < re$LtM > #reaS>
case op,'B : re$LrM A re$LsM C re$LtM > #reaS>
case opM'L : re$LrM A re$LsM * re$LtM > #reaS>
case op/.D :
/***********************************/
i% ( re$LtM GA - ) re$LrM A re$LsM / re$LtM>
else return sr1(&5/.D./( >
#reaS>
/*************** &M instructions ********************/
case opL/ : re$LrM A dMemLmM > #reaS>
case op,T : dMemLmM A re$LrM > #reaS>
/*************** &+ instructions ********************/
case opL/+ : re$LrM A m > #reaS>
case opL/C : re$LrM A currentinstruction.iar$2 > #reaS>
case opELT : i% ( re$LrM ! - ) re$LPC0&(6M A m > #reaS>
case opEL( : i% ( re$LrM !A - ) re$LPC0&(6M A m > #reaS>
case opE6T : i% ( re$LrM " - ) re$LPC0&(6M A m > #reaS>
case opE6( : i% ( re$LrM "A - ) re$LPC0&(6M A m > #reaS>
case opE(F : i% ( re$LrM AA - ) re$LPC0&(6M A m > #reaS>
case opE4( : i% ( re$LrM GA - ) re$LPC0&(6M A m > #reaS>
/* end o% le$al instructions */
= /* case */
return sr5K+H >
= /* stepTM */
/********************************************/
int doCommand (Joid)
: char cmd>
int stepcntA-; i>
int printcnt>
int step&esult>
int re$4o; loc>
do
: print% ("(nter command: ")>
Wush (stdin)>
Wush (stdout)>
$ets(in0Line)>
lineLen A strlen(in0Line)>
inCol A ->
=
@hile (G $et9ord ())>
cmd A @ordL-M >
s@itch ( cmd )
: case UtU :
/***********************************/
traceKa$ A G traceKa$ >
print%("Tracin$ no@ ")>
i% ( traceKa$ ) print%("on.Tn")> else print%("oX.Tn")>
#reaS>
case UhU :
/***********************************/
print%("Commands are:Tn")>
print%(" s(tep !n" "T
"(Yecute n (de%ault *) TM instructionsTn")>
print%(" $(o "T
"(Yecute TM instructions until ?+LTTn")>
print%(" r(e$s "T
"Print the contents o% the re$istersTn")>
print%(" i(Mem !# !n"" "T
"Print n iMem locations startin$ at #Tn")>
print%(" d(Mem !# !n"" "T
"Print n dMem locations startin$ at #Tn")>
print%(" t(race "T
"To$$le instruction traceTn")>
print%(" p(rint "T
"To$$le print o% total instructions eYecuted"T
" (U$oU only)Tn")>
print%(" c(lear "T
"&eset simulator %or ne@ eYecution o% pro$ramTn")>
print%(" h(elp "T
"Cause this list o% commands to #e printedTn")>
print%(" Z(uit "T
"Terminate the simulationTn")>
#reaS>
case UpU :
/***********************************/
icountKa$ A G icountKa$ >
print%("Printin$ instruction count no@ ")>
i% ( icountKa$ ) print%("on.Tn")> else print%("oX.Tn")>
#reaS>
case UsU :
/***********************************/
i% ( at(5L ()) stepcnt A *>
else i% ( $et4um ()) stepcnt A a#s(num)>
else print%(",tep countNTn")>
#reaS>
case U$U : stepcnt A * > #reaS>
case UrU :
/***********************************/
%or (i A -> i ! 450&(6,> i<<)
: print%("O*d: O3d "; i;re$LiM)>
i% ( (i O 3) AA I ) print% ("Tn")>
=
#reaS>
case UiU :
/***********************************/
printcnt A * >
i% ( $et4um ())
: iloc A num >
i% ( $et4um ()) printcnt A num >
=
i% ( G at(5L ())
print% (".nstruction locationsNTn")>
else
: @hile ((iloc "A -) QQ (iloc ! .+//&0,.1()
QQ (printcnt " -) )
: @rite.nstruction(iloc)>
iloc<< >
printcntCC >
=
=
#reaS>
case UdU :
/***********************************/
printcnt A * >
i% ( $et4um ())
: dloc A num >
i% ( $et4um ()) printcnt A num >
=
i% ( G at(5L ())
print%("/ata locationsNTn")>
else
: @hile ((dloc "A -) QQ (dloc ! /+//&0,.1()
QQ (printcnt " -))
: print%("OPd: OPdTn";dloc;dMemLdlocM)>
dloc<<>
printcntCC>
=
=
#reaS>
case UcU :
/***********************************/
iloc A ->
dloc A ->
stepcnt A ->
%or (re$4o A -> re$4o ! 450&(6, > re$4o<<)
re$Lre$4oM A - >
dMemL-M A /+//&0,.1( C * >
%or (loc A * > loc ! /+//&0,.1( > loc<<)
dMemLlocM A - >
#reaS>
case UZU : return F+L,(> /* #reaS> */
de%ault : print%("Command Oc unSno@n.Tn"; cmd)> #reaS>
= /* case */
step&esult A sr5K+H>
i% ( stepcnt " - )
: i% ( cmd AA U$U )
: stepcnt A ->
@hile (step&esult AA sr5K+H)
: iloc A re$LPC0&(6M >
i% ( traceKa$ ) @rite.nstruction( iloc ) >
step&esult A stepTM ()>
stepcnt<<>
=
i% ( icountKa$ )
print%("4um#er o% instructions eYecuted A OdTn";stepcnt)>
=
else
: @hile ((stepcnt " -) QQ (step&esult AA sr5K+H))
: iloc A re$LPC0&(6M >
i% ( traceKa$ ) @rite.nstruction( iloc ) >
step&esult A stepTM ()>
stepcntCC >
=
=
print%( "OsTn";step&esultTa#Lstep&esultM )>
=
return T&'(>
= /* doCommand */
/********************************************/
/* ( [ ( C ' T . 5 4 B ( 6 . 4 , ? ( & ( */
/********************************************/
main( int ar$c; char * ar$JLM )
: i% (ar$c GA 2)
: print%("usa$e: Os !)lename"Tn";ar$JL-M)>
eYit(*)>
=
strcpy(p$m4ame;ar$JL*M) >
i% (strchr (p$m4ame; U.U) AA 4'LL)
strcat(p$m4ame;".tm")>
p$m A %open(p$m4ame;"r")>
i% (p$m AA 4'LL)
: print%(")le UOsU not %oundTn";p$m4ame)>
eYit(*)>
=
/* read the pro$ram */
i% ( G read.nstructions ())
eYit(*) >
/* s@itch input )le to terminal */
/* reset( input )> */
/* readCeJalCprint */
print%("TM simulation (enter h %or help)...Tn")>
do
done A G doCommand ()>
@hile (G done )>
print%(",imulation done.Tn")>
return ->
=
;) Compile el programa)
<) Describa el programa desde el punto de vista algortmico
=) Ejecute el programa)
>) E-pli&ue los resultados de la ejecucin)
?) 'raduzca al espa+ol la inter2az del programa)
Evidencias:
Describir las evidencias de desempeo que el estudiante debe entregar. Ejemplo:
6) Elaborar un reporte de laboratorio &ue contenga lo siguiente:
6) Portada conteniendo datos de identi@cacin su@cientes ( adecuados)
;) Introduccin
<) Desarrollo del tema con la descripcin de las actividades realizadas
=) $esultados ( discusin en base a las preguntas del apartado siguiente)
>) Problemas &ue se presentaron al realizar la pr%ctica)
?) Aibliogra2a consultada en 2ormato APA)
;) Contestar ( comentar las siguientes preguntas:
6) BCu. tipo de programa es el programa escrito D)
;) BCual es la utilidad del programaD)
<) BCual es su ar&uitectura b%sica ( su 2uncionamientoD)
<) Entregar el reporte en 2ormato PD", te- u 8D')
Referencias:
Referencias recomendadas para la prctica. Ejemplo:
Requeri#a'
*ouden, E) C) (6FF0 GConstruccin de Compiladores principios ( pr%cticaH)
U7A) Internacional '#omson Editores)
Su(eri#a'
A#o, A) I), $avi, 7), Ullman, D) J) (6FF/) Compiladores Principios '.cnicas (
#erramientas) 6ed) U7A) Addison 9esle()
Crens#a:, J) 9) (6FF>) *E'K7 AUI*D A C8!PI*E$L) $ecuperado el 6F de
enero de ;/6=, de #ttp:MMcompilers)iecc)comMcrens#a:M
Appel, A) 9), Jens, P) (;//;) G!odern Compiler implementation in JavaH) ;a
ed) U7A)Cambridge Universit( Press)
9il#elm, $) (6FF>) GCompiler DesignH) U7A) AddisonN9esle()

You might also like