Documente Academic
Documente Profesional
Documente Cultură
Compiladores
30 de diciembre de 2016
Informe del compilador
Índice
1. Análisis léxico 2
1.1. Clase scanner.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.2. Función getStream . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2. Análisis sintáctico 4
2.1. Clase parser.h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.1.1. Gramática completa . . . . . . . . . . . . . . . . . . . . . . . 6
2.1.2. Tablas generadas con el Anagra . . . . . . . . . . . . . . . . . 7
2.1.3. Función analize . . . . . . . . . . . . . . . . . . . . . . . . . . 8
3. Análisis semántico 10
3.1. Tabla de simbolos . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
4. Conclusiones 12
5. Referencias 12
1
Informe del compilador
1. Análisis léxico
class Scanner
{
public :
Scanner ( string fileName ) ;
vector < string > getBuffer () ;
void initStaticTable () ;
void getStreamFromFile ( string fileName ) ;
void getStream ( string input ) ;
private :
unordered_map < int , unordered_map < char , int > > table ;
unordered_map < int , unordered_map < char , int > >
advance ;
unordered_map < int , bool > finalStates ;
unordered_map < int , string > typeStates ;
vector < string > buffer ;
unordered_map < string , string > static_table ;
int init_state ;
};
# endif // SCANNER_H
2
Informe del compilador
else
{
3
Informe del compilador
}
}
token = " " ;
state = init_state ;
}
// Token tmp (" $", line );
tmp = new Token ( " $ " , line ) ;
buffer . push_back ( tmp ) ;
}
2. Análisis sintáctico
class Production {
public :
string leftSide ;
vector < string > rightSide ;
Production ( string l )
4
Informe del compilador
{
leftSide = l ;
}
void insertSymbol ( string s )
{
rightSide . push_back ( s ) ;
}
void printProduction ()
{
cout < < leftSide < < " : " ;
for ( size_t i = 0; i < rightSide . size () ; i ++) {
cout < < rightSide [ i ] < < " " ;
}
cout < < endl ;
}
};
class Action {
public :
int type ;
int number ;
Action ( int t , int n ) : type ( t ) , number ( n ) {};
};
class Parser
{
public :
Parser () ;
void initGrammar () ;
void initActionTable () ;
void initGotoTable () ;
void analyze ( vector < string > buffer ) ;
private :
unordered_map < int , unordered_map < string , Action > >
actionTable ;
unordered_map < int , unordered_map < string , int > >
gotoTable ;
unordered_map < int , Production > grammar ;
stack < int > parsingStack ;
};
5
Informe del compilador
# endif // PARSER_H
Explicación: Para la fase análisis sintático se define una clase Production para alma-
cenar las distintas producciones de la gramática a procesar, una clase Acción para
identificar los tipos de acciones(shift,reduce,accept) y finalmente la clase Parser que
contiene dos estructuras unordered-map para almacenar las tablas Action y Goto,
una estructura para almacenar la gramática completa y una pila para realizar el
proceso de reconocimiento de cadenas.
S : FUN_DEC S | STAT S | ;
FUN_DEC : DATA_TYPE id left_par ARG_LIST right_par BLOCK ;
ARG_LIST : ARG COMMA_LIST | ;
ARG : DATA_TYPE id ;
COMMA_LIST : comma ARG COMMA_LIST | ;
DATA_TYPE : int_type | double_type | void_type |
float_type | long_type | bool_type | color_type |
char_type | byte_type | str_type ;
BLOCK : left_curly BODY right_curly ;
BODY : LOOP BODY | SELECTION BODY | STAT BODY | ;
LOOP : FOR_STAT | WHILE_STAT ;
FOR_STAT : for left_par INIT semicolon EXP semicolon EXP
right_par FOR_BODY ;
INIT : int_type assign_op EXP ;
FOR_BODY : STAT | BLOCK ;
WHILE_STAT : while left_par EXP right_par BLOCK ;
STAT : DEC | F_CALL2 | OBJ_ST ;
F_CALL2 : F_CALL1 semicolon ;
F_CALL1 : id left_par PARAM_LIST right_par ;
PARAM_LIST : EXP COMMA_LIST | ;
COMMA_LIST : comma EXP COMMA_LIST | ;
EXP : EXP OPER SIDE | SIDE | OBJ_EXP ;
SIDE : DATA | left_par EXP right_par ;
OPER : add_op | min_op | mult_op | assign_op | div_op |
mod_op | equal_op | nequal_op | lt_op | gt_op | lte_op
| gte_op | and_op | or_op | not_op | inc_op | dec_op
| add_assign | minus_assign | mult_assign | div_assign
;
6
Informe del compilador
7
Informe del compilador
8
Informe del compilador
9
Informe del compilador
3. Análisis semántico
10
Informe del compilador
string token ;
string dataType ;
bool declared ;
PropertiesRecord ( string i , string t )
{
id = i ;
token = t ;
// dataType =d;
// initialized = init ;
}
};
class SymbolTable
{
public :
SymbolTable () ;
void insert_symTab ( string key , PropertiesRecord p )
;
bool lookup_symTab ( string key ) ;
private :
unordered_map < string , PropertiesRecord > symTab ;
};
# endif // SYMBOLTABLE_H
11
Informe del compilador
4. Conclusiones
La función de un compiladores es leer un programa escrito es un lenguaje, en
este caso el lenguaje fuente, y lo traduce a un programa equivalente en otro
lenguaje, el lenguaje objeto.
Una sintaxis y lenguajes especı́ficos, ya que, al igual que el lenguaje humano, si
no lo escribimos correctamente el compilador no hará lo que deseamos. Y que
en la compilación hay dos partes: Análisis y Sı́ntesis. La parte del análisis divide
al programa fuente en sus elementos componentes y crea una representación
intermedia.
5. Referencias
1. Compiladores, Principios, técnicas y herramientas, Alfred V. Aho, Ravi Sethi,
Jeffrey D. Ullman. Addison – Wesley iberoamericana.
2. http://www.dlsi.ua.es/docencia/asignaturas/comp1/comp1.html
3. http://www.cps.unizar.es/ ezpeleta/COMPI
12