Prof. José de
Siga as
Faça
Program ::= [ VarDec { VarDec } ] CompositeCommand
CompositeCommand ::= “begin”
Command { Command } “end”
Command ::= PrintCommand | AssignmentCommand
| WhileCommand | ForCommand |
IfCommand
| CompositeCommand
VarDec ::= Type Id { “,” Id } “;”
Type ::= “integer” | “boolean”
AssignmentCommand ::=
PrintCommand ::= “print” OrExpr “;”
WhileCommand ::= “while” OrExpr “do”
Command
ForCommand ::= “for” Id “=” OrExpr “to”
OrExpr “do” Command
IfCommand ::= “if” OrExpr “then”
Command
[ “else” Command ]
OrExpr ::= AndExpr [ "or"
AndExpr ]
AndExpr ::= RelExpr [ "and"
RelExpr ]
RelExpr ::= AddExpr [ RelOp AddExpr ]
AddExpr ::= MultExpr { AddOp MultExpr }
MultExpr ::= SimpleExpr { MultOp
SimpleExpr }
SimpleExpr ::= Number | "true"
| "false" |
| '(' OrExpr ')' |
"not" SimpleExpr | AddOp SimpleExpr
|
RelOp ::= '<' | '<=' | '>' |
'>='| '==' | '<>'
AddOp ::= '+'| '-'
MultOp ::= '*' | '/' | '%'
Number
::= Digit { Digit }
Digit
::= '0'| '1' | ... | '9'
Algumas
1. {
e } representam
2.
3. Number
representa
4. identificadores
devem
1. qualquer
Algumas observações sobre a linguagem:
1. variáveis
booleanas podem
2. variáveis minúsculas e maiúsculas são consideradas diferentes. Todas as palavras-chave são minúsculas;
3. o escopo de uma variável é do ponto de declaração ao final do programa;
4. a
variável do comando “for i = expr1 to expr2 do ” inicia-se
com expr1 e é incrementada de
5. função
getInteger retorna um inteiro lido do teclado. Esta função, em C, é
int getInteger() {
char s[512];
int n;
gets(s);
sscanf(s, “%d”, &n);
return n;
}
O
integer n, i;
boolean ok;
begin
n = getInteger;
for i = 1 to n do
print i;
n = getInteger;
ok = true;
while i < n do
begin
if n/i == 0 or (n – 1)%i == 0 then
ok = false;
i = i + 1;
end
if ok then print 1; else print 0;
end
- se uma
- se uma
- se,
- se o
- se os tipos da variáveis e expressões de um comando “for” são todos inteiros;
- se o
- se leia e escreva escrevem e lêem
Veja
Você deve entregar, impressos:
Exemplo 1.
integer n, i;
boolean ok;
begin
n = getInteger;
for i = 1 to n do
print i;
n = getInteger;
ok = true;
i = 2;
while i < n do
begin
if n/i == 0 or (n – 1)%i == 0 then
ok = false;
i = i + 1;
end
if ok then print 1; else print 0;
end
Exemplo
2.
integer a, b, c, delta;
begin
a = getInteger;
b = getInteger;
c = getInteger;
$$ assume que delta seja um quadrado perfeito ou < 0
delta = b*b – 4*a*c;
if delta < 0 then print -1;
else
begin
if delta == 0 then print 0;
else
print 1;
$$ delta = n*n, logo, n = sqrt(delta)
ok = false;
n = 1;
while not ok do
begin
if n*n == delta then ok = true;
else n = n + 1;
end
print (-b + n)/(2*a);
print (-b - n)/(2*a);
end
end