UFRN – DIMAP

DIM0339 - Compiladores

Segundo Semestre de 2005

 

Exercícios - Análise semântica

 

1.      Exercício 5.4 do livro-texto.

2.      Exercício 5.5 do livro-texto.

3.      Exercício 5.6 do livro-texto.

4.      (tirado de http://www.math.grin.edu/~stone/courses/languages/spring-1996/exercise-2.html): Here's a simple syntax for assignment statements in a Pascal-like language:

<assignment> ::= <variable> <- <expression> <expression> ::= <variable> + <variable>
| <variable> and <variable>
| <expression> = <expression>
| <variable> <variable> ::= A | B | C | #A | #B | #C

The variables A, B, and C are implicitly declared to be of type integer, while #A, #B, and #C are of type Boolean. Only integer variables may be added, only Boolean values may be anded, and only expressions of the same type may be compared for equality; the result of an addition is an integer, the result of an and is a Boolean, and the result of an equality test is a Boolean. A value can be assigned only to a variable of the same type.

Convert this BNF into an attribute grammar in which assignments have a Boolean attribute, OK, which is true just in case none of the subexpressions of the assignment involves any type conflict.

5.      (tirado de http://www.cs.dartmouth.edu/~jaa/CS118.99S/Homeworks/hw.01.html): Consider the following grammar for floating point numbers:

float --> digitList . digitList
digitList --> digit
| digit digitList
digit --> 0 | 1 | 2 | ... | 9

Define attributes for this grammar in order to properly calculate the value of a float. (Hint: Use a "count" attribute which keeps track of the number of digits in a digit list.)

6.      The grammar for floating point numbers given above can be rewritten so that a "count" attribute is unnecessary and attribute value equations involving exponentiation are avoided. Give such an attribute grammar for floating point numbers.

7.      Considere a seguinte gramática para declarações simples similares às de Pascal:

Decl --> var-list : type
| Decl Decl
var-list --> var-list , id

| id
type --> integer | real

Escrever um esquema de tradução para armazenar na tabela de símbolos o tipo de cada variável.