Documentos de Académico
Documentos de Profesional
Documentos de Cultura
Laboratorio4 PDF
Laboratorio4 PDF
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
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
5
Práctica Nro. 05
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 }