Você está na página 1de 32

Análise Semântica

Etapa Final
PROF. DR. ANDRÉ LUIS MENESES SILVA
CONTATO: ANDRELUMESI@ACADEMICO.UFS.BR
Agenda

 Tutorial
 Nova Gramática
 Análise Semântica
 AbstractVisitor
 Visitor
 SemanticVisitor
Tutorial

 Nesse novo tutorial, o exemplo anterior foi alterado com as


seguintes mudanças:
 Adição do tipo de dado boolean
 Suporte a definição de funções
 Introdução dos comandos while, call e assign
Gramática Antiga

 Só dava suporte a expressões.


Gramática Antiga

 Só dava suporte a expressões.


 exp → exp + exp | exp * exp | exp ^ exp | call | assign | num | id
 call → id (params) | id ( )
 params → exp, params | exp
 assign → id = exp
Gramática Antiga

 Só dava suporte a expressões.


 exp → exp + exp | exp * exp | exp ^ exp | call | assign | num | id
 call → id (params) | id ( )
 params → exp, params | exp Exemplos:
 assign → id = exp
• 3+4*5

• soma(1+2, b, c)

• a= 3+5
Nova Gramática

 exp → exp + exp | exp * exp Novas Regras 


           | exp ^ exp | call 
           | assign | num | id • program → funcdecl | funcdecl program
 call → id (params) | id ( )
• funcdecl → signature body
• signature → id id ( sigParams)
 params → exp, params | exp • sigparams → ID ID
 assign → id = exp                     | ID ID COMMA sigparams
• body → { stms }
• stms → stm | stm stms
• stm → exp ;| while ( exp ) body
• return exp ;
• call → id ( params )
Exemplo de código
int some (int a, int b){
    a = 88 + 44;
    b = 70;
    sumparabola(1, 2, 3);        
    while (true){
        c = 38;
        sumparabola(5, true, false);
        while (c){
            sumparabola(5, true, true);
        }
    }
    some();
    sumparabolac(2);
    return true;
}
Exemplo de código Importante notar:
• Tipagem dinâmica de variáveis (feita em
int some (int a, int b){ tempo de execução)
    a = 88 + 44;
    b = 70; • Tipagem estática na definição de funções 
    sumparabola(1, 2, 3);         (parâmetros)
    while (true){
        c = 38;
        sumparabola(5, true, false);
        while (c){
            sumparabola(5, true, true);
        }
    }
    some();
    sumparabolac(2);
    return true;
}
Exemplo de código Importante notar:
• Tipagem dinâmica de variáveis (feita em
int some (int a, int b){ tempo de execução)
    a = 88 + 44;
    b = 70; • Tipagem estática na definição de funções 
    sumparabola(1, 2, 3);         (parâmetros)
    while (true){
        c = 38;
        sumparabola(5, true, false); Análise Semântica deve:
        while (c){ • Deve fazer a checagem de tipos
            sumparabola(5, true, true);
• O que será avaliado?
        }
    } • Chamadas de função
    some(); • Expressão do comando while
    sumparabolac(2);
• Retorno da função
    return true;
}
Exemplo de código Importante notar:
• Tipagem dinâmica de variáveis (feita em
int some (int a, int b){ tempo de execução)
    a = 88 + 44;
    b = 70; • Tipagem estática na definição de funções 
    sumparabola(1, 2, 3);         (parâmetros)
    while (true){
        c = 38;
        sumparabola(5, true, false);
Como fazer?
Análise Semântica deve:
        while (c){ • Deve fazer a checagem de tipos
            sumparabola(5, true, true);
• O que será avaliado?
        }
    } • Chamadas de função
    some(); • Expressão do comando while
    sumparabolac(2);
• Retorno da função
    return true;
}
Exemplo de código Como fazer?
• Tabela de Símbolos
int some (int a, int b){ • Uma lista dinâmica
    a = 88 + 44;
    b = 70; • Visitor Semântico
    sumparabola(1, 2, 3);        
    while (true){
        c = 38;
        sumparabola(5, true, false);
        while (c){
            sumparabola(5, true, true);
        }
    }
    some();
    sumparabolac(2);
    return true;
}
Exemplo de código Como fazer?
• Tabela de Símbolos
int some (int a, int b){ • Table = []
    a = 88 + 44;
    b = 70;
    sumparabola(1, 2, 3);        
    while (true){
        c = 38;
        sumparabola(5, true, false);
        while (c){
            sumparabola(5, true, true);
        }
    }
    some();
    sumparabolac(2);
    return true;
}
Exemplo de código Como fazer?
• beginScope('main')
int some (int a, int b){ • Table = [{'scope': 'main'}]
    a = 88 + 44;
    b = 70;
    sumparabola(1, 2, 3);        
    while (true){
        c = 38;
        sumparabola(5, true, false);
        while (c){
            sumparabola(5, true, true);
        }
    }
    some();
    sumparabolac(2);
    return true;
}
Exemplo de código
Como fazer?
• addFunction
int some (int a, int b){ • Table = [{'scope': 'main', 'some': {'bindable': 'fun', 
    a = 88 + 44;
    b = 70;                  'params': ['a', 'int', 'b', 'int'], 'type': 'int'}}
    sumparabola(1, 2, 3);        
    while (true){
        c = 38;
        sumparabola(5, true, false);
        while (c){
            sumparabola(5, true, true);
        }
    }
    some();
    sumparabolac(2);
    return true;
}
Exemplo de código
Como fazer?
• beginScope('some')
int some (int a, int b){ • Table = [{'scope': 'main', 'some': {'bindable': 'fun', 'params':
    a = 88 + 44;
    b = 70;                        ['a', 'int', 'b', 'int'], 'type': 'int'}}, {'scope': 'some'}]
    sumparabola(1, 2, 3);        
    while (true){
        c = 38;
        sumparabola(5, true, false);
        while (c){
            sumparabola(5, true, true);
        }
    }
    some();
    sumparabolac(2);
    return true;
}
Exemplo de código
Como fazer?
• addVar(a)
int some (int a, int b){ • Table = [{'scope': 'main', 'some': {'bindable': 'fun', 'params': ['a', 'int', 'b',
    a = 88 + 44;
    b = 70; 'int'], 'type': 'int'}}, {'scope': 'some', 'a': {'bindable': 'var', 'type': 'int'}}]
    sumparabola(1, 2, 3);        
    while (true){
        c = 38;
        sumparabola(5, true, false);
        while (c){
            sumparabola(5, true, true);
        }
    }
    some();
    sumparabolac(2);
    return true;
}
Exemplo de código
Como fazer?
• addVar(b)
int some (int a, int b){ • Table = [{'scope': 'main', 'some': {'bindable': 'fun', 'params': ['a', 'int', 'b',
    a = 88 + 44;
    b = 70; 'int'], 'type': 'int'}}, {'scope': 'some', 'a': {'bindable': 'var', 'type': 'int'}, 'b':
    sumparabola(1, 2, 3);        {'bindable': 'var', 'type': 'int'}}]
    while (true){
        c = 38;
        sumparabola(5, true, false);
        while (c){
            sumparabola(5, true, true);
        }
    }
    some();
    sumparabolac(2);
    return true;
}
Exemplo de código
Como fazer?
• addVar(a)
int some (int a, int b){ • Table = [{'scope': 'main', 'some': {'bindable': 'fun', 'params': ['a', 'int', 'b',
    a = 88 + 44;
    b = 70; 'int'], 'type': 'int'}}, {'scope': 'some', 'a': {'bindable': 'var', 'type': 'int'}, 'b':
    sumparabola(1, 2, 3);        {'bindable': 'var', 'type': 'int'}}]
    while (true){
        c = 38;
        sumparabola(5, true, false);
        while (c){
            sumparabola(5, true, true);
        }
    }
    some();
    sumparabolac(2);
    return true;
}
Exemplo de código
Como fazer?
• addVar(b)
int some (int a, int b){ • Table = [{'scope': 'main', 'some': {'bindable': 'fun', 'params': ['a', 'int', 'b',
    a = 88 + 44;
    b = 70; 'int'], 'type': 'int'}}, {'scope': 'some', 'a': {'bindable': 'var', 'type': 'int'}, 'b':
    sumparabola(1, 2, 3);        {'bindable': 'var', 'type': 'int'}}]
    while (true){
        c = 38;
        sumparabola(5, true, false);
        while (c){
            sumparabola(5, true, true);
        }
    }
    some();
    sumparabolac(2);
    return true;
}
Exemplo de código
Como fazer?
• getbindable('sumparabola')
int some (int a, int b){ • Table = [{'scope': 'main', 'some': {'bindable': 'fun', 'params': ['a', 'int', 'b',
    a = 88 + 44;
    b = 70; 'int'], 'type': 'int'}},
    sumparabola(1, 2, 3);                {'scope': 'some', 'a': {'bindable': 'var', 'type': 'int'}, 'b': {'bindable':
    while (true){            'var', 'type': 'int'}}]
        c = 38;
        sumparabola(5, true, false);
        while (c){
            sumparabola(5, true, true);
        }
    }
    some();
    sumparabolac(2);
    return true;
}
Exemplo de código
Como fazer?
• getbindable('sumparabola')
int some (int a, int b){ • Table = [{'scope': 'main', 'some': {'bindable': 'fun', 'params': ['a', 'int', 'b',
    a = 88 + 44;
    b = 70; 'int'], 'type': 'int'}},
    sumparabola(1, 2, 3);                {'scope': 'some', 'a': {'bindable': 'var', 'type': 'int'}, 'b': {'bindable':
    while (true){            'var', 'type': 'int'}}]
        c = 38;
        sumparabola(5, true, false); Como não existe
        while (c){ bindable
            sumparabola(5, true, true); sumparabola
        }
    }
    some(); ERRO SEMÂNTICO
    sumparabolac(2);
    return true;
}
Exemplo de código
Como fazer?
• Visitor.visitExp(self) == 'boolean'
int some (int a, int b){ Table = [{'scope': 'main', 'some': {'bindable': 'fun', 'params': ['a', 'int', 'b',
    a = 88 + 44;
    b = 70; 'int'], 'type': 'int'}},
    sumparabola(1, 2, 3);                {'scope': 'some', 'a': {'bindable': 'var', 'type': 'int'}, 'b': {'bindable':
    while (true){            'var', 'type': 'int'}}]
        c = 38;
        sumparabola(5, true, false);
        while (c){
            sumparabola(5, true, true);
        }
    }
    some();
    sumparabolac(2);
    return true;
}
Exemplo de código
Como fazer?
• Visitor.visitExp(self) == 'boolean'
int some (int a, int b){ Table = [{'scope': 'main', 'some': {'bindable': 'fun', 'params': ['a', 'int', 'b',
    a = 88 + 44;
    b = 70; 'int'], 'type': 'int'}},
    sumparabola(1, 2, 3);                {'scope': 'some', 'a': {'bindable': 'var', 'type': 'int'}, 'b': {'bindable':
    while (true){            'var', 'type': 'int'}}]
        c = 38;
        sumparabola(5, true, false);
        while (c){
Como true é
            sumparabola(5, true, true); boolean
        }
    } Ok !!!
    some();
    sumparabolac(2);
    return true;
}
Exemplo de código
Como fazer?
• addVar(c)
int some (int a, int b){ Table = [{'scope': 'main', 'some': {'bindable': 'fun', 'params': ['a', 'int', 'b',
    a = 88 + 44;
    b = 70; 'int'], 'type': 'int'}}, {'scope': 'some', 'a': {'bindable': 'var', 'type': 'int'}, 'b':
    sumparabola(1, 2, 3);        {'bindable': 'var', 'type': 'int'}, 'c': {'bindable': 'var', 'type': 'int'}}]
    while (true){
        c = 38;
        sumparabola(5, true, false);
        while (c){
            sumparabola(5, true, true);
        }
    }
    some();
    sumparabolac(2);
    return true;
}
Exemplo de código
Como fazer?
• getbindable('sumparabola')
int some (int a, int b){ Table = [{'scope': 'main', 'some': {'bindable': 'fun', 'params': ['a', 'int', 'b',
    a = 88 + 44;
    b = 70; 'int'], 'type': 'int'}}, {'scope': 'some', 'a': {'bindable': 'var', 'type': 'int'}, 'b':
    sumparabola(1, 2, 3);        {'bindable': 'var', 'type': 'int'}, 'c': {'bindable': 'var', 'type': 'int'}}]
    while (true){
        c = 38;
        sumparabola(5, true, false); Como não existe
        while (c){ bindable
            sumparabola(5, true, true); sumparabola
        }
    }
    some(); ERRO SEMÂNTICO
    sumparabolac(2);
    return true;
}
Exemplo de código
Como fazer?
• getbindable('sumparabola')
int some (int a, int b){ Table = [{'scope': 'main', 'some': {'bindable': 'fun', 'params': ['a', 'int', 'b',
    a = 88 + 44;
    b = 70; 'int'], 'type': 'int'}}, {'scope': 'some', 'a': {'bindable': 'var', 'type': 'int'}, 'b':
    sumparabola(1, 2, 3);        {'bindable': 'var', 'type': 'int'}, 'c': {'bindable': 'var', 'type': 'int'}}]
    while (true){
        c = 38;
        sumparabola(5, true, false);
        while (c){ Como c é int
            sumparabola(5, true, true);
        }
    }
ERRO SEMÂNTICO
    some();
    sumparabolac(2);
    return true;
}
Exemplo de código
Como fazer?
• getbindable('some')
int some (int a, int b){ Table = [{'scope': 'main', 'some': {'bindable': 'fun', 'params': ['a', 'int', 'b',
    a = 88 + 44;
    b = 70; 'int'], 'type': 'int'}}, {'scope': 'some', 'a': {'bindable': 'var', 'type': 'int'}, 'b':
    sumparabola(1, 2, 3);        {'bindable': 'var', 'type': 'int'}, 'c': {'bindable': 'var', 'type': 'int'}}]
    while (true){
        c = 38;
        sumparabola(5, true, false);
        while (c){
   Apesar de existir
            sumparabola(5, true, true); bindable some, o
        } parâmetro da chamada é
    } incompatível.
    some();
    sumparabolac(2);
    return true;
}
Exemplo de código
Como fazer?
• getbindable('some')
int some (int a, int b){ Table = [{'scope': 'main', 'some': {'bindable': 'fun', 'params': ['a', 'int', 'b',
    a = 88 + 44;
    b = 70; 'int'], 'type': 'int'}}, {'scope': 'some', 'a': {'bindable': 'var', 'type': 'int'}, 'b':
    sumparabola(1, 2, 3);        {'bindable': 'var', 'type': 'int'}, 'c': {'bindable': 'var', 'type': 'int'}}]
    while (true){
        c = 38;
        sumparabola(5, true, false);
        while (c){
   Apesar de existir
            sumparabola(5, true, true); bindable some, o
        } parâmetro da chamada é
    } incompatível.
    some();
    sumparabolac(2);
    return true;
}
AbstractVisitor

 Código
Código

 SemanticVisitor
TableSymbol

 Código

Você também pode gostar