!!"# !!$!!"%'($)( !($' (!
*$+!$,$-$-!.,$$)+$ '$,/$,'$#! +'$0$,-#1234!
$,'!$,'$5$# $,3+$!-$$$-$6$+!-$-'$+ -1'$
7 $ ,$ + '$ ,'$, -$1!,-8, " $!!$-,+! #$$!1'$
4!$,'!$,'$5$# $,(5,-!.,+!$0,'$#! +$$,-/$+$!-'
'$9! '$-$1!'$'$9$,# ', +!,-),7 $.!' -$#!
1 :, #$$!'$, ;!:-,'$ -!. -,"*,7 $, '$<!' 1 7 $ ,
#$$!'!$,$),-$-$,(=.!$''$ ,$,-! "1 $-!'$#$$!1'$
4!$,'!$,'$5$# $,'>+#$",$+$$-1, +!$!0$!,1(<
.8 ,$'$91 $0 ?!, , *)+!$,$, $# !$,<!' -!, *<(
*, +$!-$ '$9!+-!$, !$# !$, -$-$ , -!' 1( @,'
$,*<-.8,$,-! "1 #$$!'!'$A'!$, $)#!:9,><5$)(
7 /,$ '$,!.$ $0 0$!,1 B C(C '$>+#$ 7 $ !+! ><5$)(
*,-0$!,1+$!-$ -A!$,+$9$,$)#!:9,.,',$*<(=$ "$
$$+ 7 $ $,-!,0$-,7 $+!$,$- $ ,'$$,-$-+'$$,+$9$,(
$-$ ,$ A , -!., ? - !, $ , $, ,$ -$-!: .-$$!
-$!$! 0$!,1'$>+#$+-A'$-$+'$$$ 1(
! "
# $
! " # !
% "
"& !'
( " "
( !
) (
* ! +
! (
( & $ ,
!
( !
( ! ! &
! $
! -
# " ! !
( ./ ./
. .$
011℄) .
& . .
3 43
.# . #
. #.# ( & (#.#&
3 %(
(!#.# .
5 6
! 4 & -
.# *# 07"89℄
& " ! 5 -
! %
5 : *# 07"89℄ & $
(!
$
4 " .
. 011℄-& $
("#& *#
« Ç«
&
: (
%; ;!!
*#*< %$
(! ( *# #
5 : * 0589℄
!
& " "
! "
-
"
# ! "
"
! "
5! -
- "
* !
& "
* : & % ; ;! %
$ ! ( &
)- :& (
= 6# 0>1?℄ " : 01@℄
* 071A℄ *
*<
) : "" *
" "
& ( . #*#
4 " *# 3
& "* & $
*#'$ (
! *
*#)-3 ! '
: 01@℄
! !
3
"" %; ;!
-& ! "B8 **<
"" * & (
%{
int intval;float realval1;float realval2;float real;int cantreal;
%}
%init{
%init}
%%
(\ |\t)+ {break;}
\n {break;}
"+" {return TokenDef.MAS;}
"*"{return TokenDef.POR;}
"−"{return TokenDef.MENOS;}
"/"{return TokenDef.DIVIDE;}
"("{return TokenDef.PAREN_A;}
")"{return TokenDef.PAREN_C;}
INIT{intval=0;}
([0−9]
ACTION{Integer aux=new Integer(yytextchar()+"");
intval=intval*10 + aux.intValue();}
)+ {System.out.println("Natural: "+intval);
yylval_update(new Symbol(TokenDef.ENTERO,new ent()));
((ent)(yylval().value)).valor.put(Integer.parseInt(yytext(),10));
return TokenDef.ENTERO;}
INIT{realval1=0;realval2=0;cantreal=1;}
([0−9]
ACTION{Integer aux=new Integer(yytextchar()+"");
realval1=realval1*10 + aux.intValue();}
) + \. ([0−9]
ACTION{Integer aux=new Integer(yytextchar()+"");
cantreal=cantreal*10;
realval2=realval2*10 + aux.intValue();}
)+
{real=realval1+(realval2/cantreal); System.out.println("Real: "+real);
yylval_update(new Symbol(TokenDef.REAL,new real()));
((real)(yylval().value)).valor.put(new Float(yytext()).floatValue());
return TokenDef.REAL;}
%%
import output.TokenDef;
import java.lang.Integer;
import java.lang.Float;
import java.io.*;
import java.lang.System;
%{
class GLOBAL {}
%}
class ent {int valor;}
class real {float valor;}
class exp {int valor_i; float valor;}
%token MAS, MENOS, POR, DIVIDE, PAREN_A, PAREN_C
%token <ent> ENTERO
%token <real> REAL
%type <exp> Expr, Factor, Term, Numero
%start S
%%
S : Expr {System.out.println(" El valor es: " + $1.valor);}
;
Expr : Expr MAS Term
ATTRIBUTION { $$.valor = $1.valor + $3.valor; }
| Expr MENOS Term
ATTRIBUTION { $$.valor = $1.valor − $3.valor; }
| Term
ATTRIBUTION { $$.valor = $1.valor ; }
;
Term : Term POR Factor
ATTRIBUTION { $$.valor = $1.valor * $3.valor; }
| Term DIVIDE Factor
ATTRIBUTION { $$.valor = $1.valor / $3.valor; }
| Factor
ATTRIBUTION { $$.valor = $1.valor ; }
;
Factor : PAREN_A Expr PAREN_C
ATTRIBUTION { $$.valor = $2.valor ; }
| Numero
ATTRIBUTION { $$.valor = $1.valor ; }
;
Numero : ENTERO
ATTRIBUTION{ $$.valor = $1.valor ;}
| REAL
ATTRIBUTION { $$.valor = $1.valor ;}
;
5BC( & (& & /
( ( & (
$ "3 "
. "!
( ,
! - "
"
/! ! !
* 3 -08D℄4
!&&E
$ & ./
"
/-& " - *<!
! &
! *"$ "
*-!" (
% - *< 3
- "
" ("3 -
"& *
;* 4 4
"%0F1?℄088℄
011℄ * 6 6
GH11*HH; BD1B@I B111
07"89℄ 5 7" ) F 6 *
H 9889 " 7
9889 B9IBDI
071A℄ *7 !!64
#! / <B? FB *B11A JDB8?
0589℄ *5 & < * 6 "
# " " F .% 9889
01@℄ $# !% & "&'%(
G B11@
0>1?℄ >, 64, ) **-GK/ HB11?
08D℄ * . 6 6 F 5 5 7" 4 / )
F + # " , #%, G988D
0F1?℄ F !(-#%4-)-/-/
6 "6 /1?B@IB11?
088℄ 4 # F 5 7 64, L .,!
% + 4 - 9888 6 /H4#F 4
#) H4#)HM88 1@B8@ 6 4 <"