Escolar Documentos
Profissional Documentos
Cultura Documentos
Programao
Centro de Informtica, UFPE
Luis Carlos (lcsm@cin.ufpe.br)
Hermano Moura
(hermano@cin.ufpe.br)
Objetivo
Introduzir conceitos de
semntica formal de linguagens
de programao
Motivao: Qual o
significado do seguinte
programa?
program simples =
var x : int := 3
in
x := x + 5
end.
Motivao
Mtodos Formais
Aplicaes em Linguagens
de Programao
Implementador:
Ciclo de vida de LP
baseado em especificaes
projeto
formais
especificao
formal
prottipos
manuais,
livros
compiladores
Introduo
sintaxe
semntica
pragmtica
Sintaxe
Gramticas Livres de
Contexto
Estrutura principal:
Comando <-[[ if Expresso then Comando else
Comando ]]
Significado:
10
11
Ferramentas de
Implementao
O desenvolvedor no utiliza
linguagens de uso geral para
implementao da linguagem.
Introduo
sintaxe
semntica
pragmtica
13
Semntica
Objetivo:
Tipos de semntica
Formalismos Utilizados
Semntica De Aes
19
Semntica De Aes
20
Notao de Aes
Definio de Aes
23
Definio
24
Principais Operadores
unfolding a -- executa a ao a.
unfold -- Desvia o fluxo da execuo
para a ltima ao unfolding
executada.
26
Exemplos de Aes:
| complete
and thenor
| complete
|complete
| fail
| complete
unfolding
and then| | complete
| fail| and then
| | unfold
27
Exemplo de Descries
Comandos vazio
Sintaxe:
Comando --> [[ ; ]]
Semntica:
28
Exemplo de Descries
Sequncia de Comandos:
Sintaxe:
Semntica:
29
30
Definio
Principais Operadores
Funcionais
Exemplos de Aes
give 10
| give 20
then
| give sum(2,the given integer#1)
33
Exemplos de Aes(cont.)
| | give 1
| and then
| | give 2
then
| give product(the given integer#1,
| the given integer # 2)
34
Exemplo de Descrio
Linguagens de Expresses
1+2
4
5*2
35
Exemplo de descrio
Constantes Numricas.
Sintaxe:
Expresso <-- [[ Integer ]].
Semntica:
avalie _ :: Expresso --> action.
avalie [[ x : Integer ]] = give valuation of
x.
36
Exemplo de descrio
Soma de Expresses.
Sintaxe:
Expresso <-- [[ Expresso + Expresso ]].
Semntica:
avalie _ :: Expresso --> action.
avalie [[ x + y ]] =
|avalie x and then avalie y
then
| give sum(the given integer#1, the given integer#2).
37
Utilizao
38
Utilizao
Qual a semntica de 1 + 2 ?
1o Passo: Analise Sinttica:
1 + 2 ==> [[ [[ 1 ]] + [[ 2 ]] ]]
39
Utilizao
Qual a semntica de 1 + 2 ?
2o Passo: Executar Funo Semntica:
avalie [[ [[ 1 ]] + [[ 2 ]] ]]
40
Utilizao
Qual a semntica de 1 + 2 ?
2o Passo: Executar Funo Semntica:
| | avalie [[ 1 ]]
| and then
| | avalie [[ 2 ]]
then
| give sum
| (the given integer#1, the given integer # 2)
41
Utilizao
Qual a semntica de 1 + 2 ?
2o Passo: Executar Funo Semntica:
| | give valuation of 1
| and then
| | give valuation of 2
then
| give sum
| (the given integer#1, the given integer # 2)
42
Utilizao
Qual a semntica de 1 + 2 ?
2o Passo: Executar Funo Semntica:
| | give 1
| and then
| | give 2
then
| give sum
| (the given integer#1, the given integer # 2)
43
Semntica de Aes:
Faceta Declarativa
44
Definio
45
Principais Operadores
Declarativos
Exemplos de Aes
bind x to 10
| | bind x to 20
| and then
| | bind y to 1
hence
| give sum(the value bound to x,
| the value bound to y)
47
Outros Operadores
Declarativos
Exemplos:
| bind x to 1
moreover
| bind x to 2
| bind x to 2
before
| bind y to the
| integer bound to
x
| bind x to 1
hence
| furthermore bind y to 2
49
Exemplo de Descrio
let x = 1 in x + 2
let x = 1 in let y = 3 in x * y + 1
50
Exemplo de descrio
Declarao de Constantes.
Sintaxe:
Expresso <--[[ let Identifier = Expresso in
Expresso ]].
Semntica:
avalie _ :: Expresso --> action.
elabore [[ let i = x in y ]] =
| furthermore
| | avalie x then bind token of i to the given value
hence
| avalie y.
51
Exemplo de descrio
Expresses de Constantes.
Sintaxe:
Expresso<--[[ Identifier ]].
Semntica:
avalie _ :: Expresso --> action.
avalie [[i : Identifier]] =
give the value bound i.
52
Semntica de Aes:
Faceta Imperativa
53
Definio
54
Principais Aes
Exemplos de Aes
| allocate a cell
then
| | store 10 in the given cell
| and then
| | store 20 in the given cell
| and then
| | give the integer stored in the given cell.
56
Exemplo de Descrio
Linguagem com declaraes e
variveis e comandos imperativos:
int x;
int y;
x = 1;
y = x + 1;
57
Exemplo de Descrio
Declarao de Variveis
Sintaxe:
Semntica:
58
Exemplo de Descrio
Comando de Atribuio
Sintaxe:
Semntica:
59
Exemplo de Descrio
Semntica:
60
Exemplo de Descrio
Comando While
Sintaxe:
Semntica:
61
Notao de Aes:
Faceta Reflexiva
62
Descrio
63
Principais Operadores
64
Exemplos
| give abstraction of
| | give 10
then
| enact the given abstraction.
bound to a
| give abstraction of
| | give the given integer
then
| enact application the given
| abstraction to 5
| | bind a to 5
| hence
| | give closure abstraction of
| | | give the integer
then
| enact the given abstraction
65
Exemplos de Descrio
Declarao de Procedimentos
Sintaxe:
Semntica:
66
Exemplos de Descrio
Chamada a procedimento
Sintaxe:
Semntica:
execute [[ i () ]] =
enact the abstraction bound to i.
67
Pratica
68
http://www.cin.ufpe.br/~rat/download/abaco-beta230.zip
http://www.cin.ufpe.br/~if686/laboratorio.prj
69