Está en la página 1de 8

Universidad Nacional de San Agustin

Escuela Profesional de
Ciencia de la Computación
Compiladores

Práctica Nro. 05
Alumno :
Diego A. Gutierrez
Profesor:
Velazco Paredes, Yuber

16 de agosto de 2020
Práctica Nro. 05

1. Objetivo
Escribir analizadores léxicos y ensamblarlos con los analizadores sintácticos predic-
tivos.

1.1. Proponga una gramática para la evaluación de expre-


siones aritméticas. El programa debe leer un archivo de
texto plano (programa1.txt), generar la forma postfija y
mostrar el resultado
/* Analizador sintáctico de infija a postfija
Exp –>Term Resto
Resto –>mas Term printf(’+’) Resto
Resto –>menos Term printf(’-’) Resto
Resto –>menos Term printf(’*’) Resto
Resto –>menos Term printf(’/’) Resto
Term –>’(’ exp ’)’
Term –>printf(num.valor) num
*/
1
2
3 #i n c l u d e <s t d i o . h>
4 #i n c l u d e <c o n i o . h>
5 #i n c l u d e <c t y p e . h>
6 #i n c l u d e <s t r i n g >
7 #i n c l u d e <f s t r e a m >
8
9
10 #i n c l u d e <i o s t r e a m >
11 #i n c l u d e <s t r i n g >
12 #i n c l u d e <s t a c k >
13 #i n c l u d e < s t d l i b . h>
14 #i n c l u d e <cctype >
15 #i n c l u d e <cmath>
16 #i n c l u d e <s t r i n g . h>
17 #i n c l u d e <c s t d l i b >
18 #i n c l u d e <sstream>

1
Práctica Nro. 05

19 u s i n g namespace s t d ;
20
21 #d e f i n e MAS '+ '
22 #d e f i n e MENOS '− '
23 #d e f i n e MULT ' ∗ '
24 #d e f i n e DIVI ' / '
25 #d e f i n e NUM 256
26 #d e f i n e FIN −1
27 c h a r lexema [ 8 0 ] ;
28 i n t tok ;
29 v o i d Exp ( ) ;
30 int scanner ( ) ;
31 std : : s t r i n g p o s t f i j a ;
32 error ()
33 {
34 p r i n t f ( " E r r o r de s i n t a x i s " ) ;
35 }
36
37 p ar e a ( i n t t )
38 {
39 i f ( tok==t )
40 tok=s c a n n e r ( ) ;
41 else
42 error ();
43 }
44
45 int scanner ()
46 {
47 int c , i ;
48 do c=g e t c h a r ( ) ;
49 w h i l e ( c==' ' ) ;
50 i f ( c==' \n ' )
51 r e t u r n FIN ;
52 i f ( c==MAS | | c==MENOS | | c == MULT | |
53 c == DIVI | | c == ' ( ' | | c == ' ) ' )
54 return c ;
55 i f ( i s d i g i t ( c ))

2
Práctica Nro. 05

56 {
57 i =0;
58 do { lexema [ i ++]=c ; c=g e t c h a r ( ) ;
59 } while ( i s d i g i t ( c ) ) ;
60 lexema [ i ] = 0 ;
61 ungetc ( c , s t d i n ) ;
62 r e t u r n NUM;
63 }
64 }
65 Term ( )
66 {
67 i f ( tok==NUM)
68 {
69 p r i n t f ( " %s " , lexema ) ;
70 p o s t f i j a = p o s t f i j a + lexema + ' ' ;
71 parea (NUM) ;
72 }
73 e l s e i f ( tok == ' ( ' )
74 {
75 parea ( ' ( ' ) ;
76 Exp ( ) ;
77 parea ( ' ) ' ) ;
78 }
79
80 else
81 p r i n t f ( " e r r r o term " ) ;
82 }
83 Resto ( )
84 {
85 Term ( ) ;
86 i f ( tok == MAS)
87 {
88 parea (MAS) ;
89 Term ( ) ;
90 p r i n t f ( "+" ) ;
91 p o s t f i j a = p o s t f i j a + "+"+ " " ;
92 Resto ( ) ;

3
Práctica Nro. 05

93 }
94 i f ( tok == MENOS)
95 {
96 parea (MENOS) ;
97 Term ( ) ;
98 p r i n t f ( "−" ) ;
99 p o s t f i j a = p o s t f i j a + "−"+ " " ;
100 Resto ( ) ;
101 }
102 i f ( tok == MULT)
103 {
104
105 parea (MULT) ;
106 Term ( ) ;
107 p r i n t f ( "∗" ) ;
108 p o s t f i j a = p o s t f i j a + "∗"+ " " ;
109 Resto ( ) ;
110 }
111 i f ( tok == DIVI )
112 {
113 parea ( DIVI ) ;
114 Term ( ) ;
115 p r i n t f ( "/" ) ;
116 p o s t f i j a = p o s t f i j a + "/"+ " " ;
117 Resto ( ) ;
118 }
119
120 e l s e // cadena v a c i a
121 ;
122 }
123 v o i d Exp ( )
124 {
125 Resto ( ) ;
126
127
128 }
129

4
Práctica Nro. 05

130 do uble Resolver_Ecuacion ( s t r i n g e x p r e s i o n )


131 {
132 s t a c k <double> p i l a ;
133 char token ;
134 double num1 , num2 , r e s u l t a d o ;
135 f o r ( i n t i =0 ; i < e x p r e s i o n . s i z e ( ) ; i +=2)
136 {
137 token = e x p r e s i o n [ i ] ;
138 s w i t c h ( token ) {
139 c a s e '− ' :
140 num1=p i l a . top ( ) ;
141 p i l a . pop ( ) ;
142 num2=p i l a . top ( ) ;
143 p i l a . pop ( ) ;
144 r e s u l t a d o = num2 − num1 ;
145 p i l a . push ( r e s u l t a d o ) ;
146 break ;
147 c a s e '+ ' :
148 num1=p i l a . top ( ) ;
149 p i l a . pop ( ) ;
150 num2=p i l a . top ( ) ;
151 p i l a . pop ( ) ;
152 r e s u l t a d o = num2+num1 ;
153 p i l a . push ( r e s u l t a d o ) ;
154
155 break ;
156 case '∗ ' :
157 num1=p i l a . top ( ) ;
158 p i l a . pop ( ) ;
159 num2=p i l a . top ( ) ;
160 p i l a . pop ( ) ;
161 r e s u l t a d o = (num2 ) ∗ ( num1 ) ;
162
163 p i l a . push ( r e s u l t a d o ) ;
164
165 // cout << "\ t Operar : "<< num2 <<token << num1 << "="<<
166 break ;

5
Práctica Nro. 05

167 case '/ ' :


168 num1=p i l a . top ( ) ;
169 p i l a . pop ( ) ;
170 num2=p i l a . top ( ) ;
171 p i l a . pop ( ) ;
172 r e s u l t a d o = num2 / num1 ;
173 p i l a . push ( r e s u l t a d o ) ;
174 // cout << "\ t Operar : "<< num2 <<token << num1 << "="<<
175 break ;
176 default :
177 s t r i n g aux ="" ;
178 // i n t aux2 ;
179 w h i l e ( e x p r e s i o n [ i ] != ' ' ) {
180 // aux2 = i ++;
181 aux += s t r i n g ( 1 , e x p r e s i o n [ i ++]);
182 }
183 p i l a . push ( a t o f ( aux . c_str ( ) ) ) ; // In gr esa mos en l a p i l a
184 i −−;
185 }
186 }
187 r e t u r n p i l a . top ( ) ;
188 }
189 FILE ∗ f ;
190
191
192 i n t main ( )
193 {
194
195
196
197
198
199 tok=s c a n n e r ( ) ;
200 Exp ( ) ;
201 s t d : : cout<<s t d : : endl<<p o s t f i j a ;
202 double r e s u l t a d o = Resolver_Ecuacion ( p o s t f i j a ) ;
203 cout<<endl<<" E x p r e s i o n = " << p o s t f i j a <<e n d l ;

6
Práctica Nro. 05

204 cout<<endl<<"−>>>"<<r e s u l t a d o ;
205
206
207
208
209
210
211 return 0;
212 }

También podría gustarte