Professor: Gabriel Soares Baptista
shift/reduceNo LR(0):
shift vem das transições por terminalgoto vem das transições por não terminalreduceNo LR(0), uma redução entra em todas as colunas.
Se o estado contém:
$$ A \to \alpha \cdot $$
então colocamos:
$$ r(A \to \alpha) $$
em todas as colunas de ACTION.
SLR(1) usa os mesmos estados LR(0), mas coloca reduções apenas nas colunas de FOLLOW.
shift: igual ao LR(0)goto: igual ao LR(0)reduce: limitado por FOLLOWGramática:
$$ \begin{array}{rcl} (0) & S' & \to S \\ (1) & S & \to BB \\ (2) & B & \to aB \\ (3) & B & \to b \end{array} $$
B gera zero ou mais a seguidos de bS gera dois blocos B Bbb, abb, bab, aababb$$ I_0 = \left\{ \begin{array}{l} S' \to \cdot S \\ S \to \cdot BB \\ B \to \cdot aB \\ B \to \cdot b \end{array} \right\} $$
I0:
| Símbolo | Destino |
|---|---|
S |
I1 |
B |
I2 |
a |
I3 |
b |
I4 |
$$ I_1 = \left\{ \begin{array}{l} S' \to S \cdot \end{array} \right\} \qquad I_2 = \left\{ \begin{array}{l} S \to B \cdot B \\ B \to \cdot aB \\ B \to \cdot b \end{array} \right\} $$
$$ I_3 = \left\{ \begin{array}{l} B \to a \cdot B \\ B \to \cdot aB \\ B \to \cdot b \end{array} \right\} \qquad I_4 = \left\{ \begin{array}{l} B \to b \cdot \end{array} \right\} $$
$$ I_5 = \left\{ \begin{array}{l} S \to BB \cdot \end{array} \right\} \qquad I_6 = \left\{ \begin{array}{l} B \to aB \cdot \end{array} \right\} $$
| Origem | Símbolo | Destino |
|---|---|---|
I2 |
B |
I5 |
I2 |
a |
I3 |
I2 |
b |
I4 |
I3 |
B |
I6 |
I3 |
a |
I3 |
I3 |
b |
I4 |
| Estado | ACTION a |
ACTION b |
ACTION $ |
GOTO S |
GOTO B |
|---|---|---|---|---|---|
0 |
s3 |
s4 |
1 |
2 |
|
1 |
acc |
||||
2 |
s3 |
s4 |
5 |
||
3 |
s3 |
s4 |
6 |
||
4 |
r3 |
r3 |
r3 |
||
5 |
r1 |
r1 |
r1 |
||
6 |
r2 |
r2 |
r2 |
Entrada:
abb$
abb$| Pilha | Entrada | Ação |
|---|---|---|
0 |
a b b $ |
s3 |
0 a 3 |
b b $ |
s4 |
0 a 3 b 4 |
b $ |
r3, reduz $B \to b$ |
0 a 3 B 6 |
b $ |
r2, reduz $B \to aB$ |
0 B 2 |
b $ |
s4 |
0 B 2 b 4 |
$ |
r3, reduz $B \to b$ |
0 B 2 B 5 |
$ |
r1, reduz $S \to BB$ |
0 S 1 |
$ |
acc |
Gramática:
$$ \begin{array}{rcl} (0) & S' & \to S \\ (1) & S & \to Aa \\ (2) & S & \to bA \\ (3) & A & \to b \end{array} $$
babb$$ I_0 = \left\{ \begin{array}{l} S' \to \cdot S \\ S \to \cdot Aa \\ S \to \cdot bA \\ A \to \cdot b \end{array} \right\} $$
I0:
| Símbolo | Destino |
|---|---|
S |
I1 |
A |
I2 |
b |
I3 |
Ao ler b a partir de I0:
$$ I_3 = \left\{ \begin{array}{l} S \to b \cdot A \\ A \to b \cdot \\ A \to \cdot b \end{array} \right\} $$
| Item | Sugestão |
|---|---|
| $S \to b \cdot A$ e $A \to \cdot b$ | próximo b: fazer shift |
| $A \to b \cdot$ | reduzir $A \to b$ |
No LR(0), $A \to b \cdot$ reduz em todas as colunas.
| Estado | ACTION a |
ACTION b |
ACTION $ |
|---|---|---|---|
3 |
r3 |
s6 / r3 |
r3 |
shift/reduce na coluna b.
Depois de ler o primeiro b:
ba$: reduzir o primeiro b para Abb$: empilhar o segundo bNo LR(0):
$$ A \to \alpha \cdot \quad \Rightarrow \quad \text{reduz em todas as colunas} $$
No SLR(1):
$$ A \to \alpha \cdot \quad \Rightarrow \quad \text{reduz apenas em } FOLLOW(A) $$
Gramática:
$$ \begin{array}{rcl} S & \to Aa \\ S & \to bA \\ A & \to b \end{array} $$
De $S \to Aa$:
$$ a \in FOLLOW(A) $$
De $S \to bA$:
$$ FOLLOW(S) \subset FOLLOW(A) $$
$$ FOLLOW(A)=\{a,\$\} $$
Então, no estado I3:
$$ A \to b \cdot $$
reduz apenas nas colunas a e $.
| Estado | ACTION a |
ACTION b |
ACTION $ |
|---|---|---|---|
3 |
r3 |
s6 |
r3 |
Estados LR(0):
$$ I_0 = \left\{ \begin{array}{l} S' \to \cdot S \\ S \to \cdot Aa \\ S \to \cdot bA \\ A \to \cdot b \end{array} \right\} \qquad I_1 = \left\{ \begin{array}{l} S' \to S \cdot \end{array} \right\} $$
$$ I_2 = \left\{ \begin{array}{l} S \to A \cdot a \end{array} \right\} \qquad I_3 = \left\{ \begin{array}{l} S \to b \cdot A \\ A \to b \cdot \\ A \to \cdot b \end{array} \right\} $$
$$ I_4 = \left\{ \begin{array}{l} S \to Aa \cdot \end{array} \right\} \qquad I_5 = \left\{ \begin{array}{l} S \to bA \cdot \end{array} \right\} \qquad I_6 = \left\{ \begin{array}{l} A \to b \cdot \end{array} \right\} $$
| Origem | Símbolo | Destino |
|---|---|---|
I0 |
S |
I1 |
I0 |
A |
I2 |
I0 |
b |
I3 |
I2 |
a |
I4 |
I3 |
A |
I5 |
I3 |
b |
I6 |
| Estado | ACTION a |
ACTION b |
ACTION $ |
GOTO S |
GOTO A |
|---|---|---|---|---|---|
0 |
s3 |
1 |
2 |
||
1 |
acc |
||||
2 |
s4 |
||||
3 |
r3 |
s6 |
r3 |
5 |
|
4 |
r1 |
||||
5 |
r2 |
||||
6 |
r3 |
r3 |
ba$| Pilha | Entrada | Ação |
|---|---|---|
0 |
b a $ |
s3 |
0 b 3 |
a $ |
r3, reduz $A \to b$ |
0 A 2 |
a $ |
s4 |
0 A 2 a 4 |
$ |
r1, reduz $S \to Aa$ |
0 S 1 |
$ |
acc |
bb$| Pilha | Entrada | Ação |
|---|---|---|
0 |
b b $ |
s3 |
0 b 3 |
b $ |
s6 |
0 b 3 b 6 |
$ |
r3, reduz $A \to b$ |
0 b 3 A 5 |
$ |
r2, reduz $S \to bA$ |
0 S 1 |
$ |
acc |
FOLLOWshift por transições terminaisgoto por transições não terminaisreduce apenas em FOLLOW(A)acc para $S' \to S \cdot$Gramática:
$$ \begin{array}{rcl} (0) & S' & \to S \\ (1) & S & \to (L) \\ (2) & S & \to a \\ (3) & L & \to L,S \\ (4) & L & \to S \end{array} $$
S: expressãoL: lista de expressõesa, (a), (a,a), ((a),a)$$ I_0 = \left\{ \begin{array}{l} S' \to \cdot S \\ S \to \cdot (L) \\ S \to \cdot a \end{array} \right\} $$
| Símbolo | Destino |
|---|---|
S |
I1 |
( |
I2 |
a |
I3 |
I1, I2, I3$$ I_1 = \left\{ \begin{array}{l} S' \to S \cdot \end{array} \right\} $$
$$ I_2 = \left\{ \begin{array}{l} S \to ( \cdot L) \\ L \to \cdot L,S \\ L \to \cdot S \\ S \to \cdot (L) \\ S \to \cdot a \end{array} \right\} $$
$$ I_3 = \left\{ \begin{array}{l} S \to a \cdot \end{array} \right\} $$
I4, I5$$ I_4 = \left\{ \begin{array}{l} S \to (L \cdot ) \\ L \to L \cdot ,S \end{array} \right\} $$
$$ I_5 = \left\{ \begin{array}{l} L \to S \cdot \end{array} \right\} $$
I4: depois de reconhecer L dentro de parênteses),SI6, I7, I8$$ I_6 = \left\{ \begin{array}{l} S \to (L) \cdot \end{array} \right\} $$
$$ I_7 = \left\{ \begin{array}{l} L \to L, \cdot S \\ S \to \cdot (L) \\ S \to \cdot a \end{array} \right\} $$
$$ I_8 = \left\{ \begin{array}{l} L \to L,S \cdot \end{array} \right\} $$
| Origem | Símbolo | Destino |
|---|---|---|
I0 |
S |
I1 |
I0 |
( |
I2 |
I0 |
a |
I3 |
I2 |
L |
I4 |
I2 |
S |
I5 |
I2 |
( |
I2 |
I2 |
a |
I3 |
I4 |
) |
I6 |
I4 |
, |
I7 |
I7 |
S |
I8 |
I7 |
( |
I2 |
I7 |
a |
I3 |
Começamos com:
$$ FOLLOW(S)=\{\$\} $$
Da produção $S \to (L)$:
$$ \texttt{)} \in FOLLOW(L) $$
Da produção $L \to L,S$:
$$ \texttt{,} \in FOLLOW(L) $$
Em $L \to L,S$:
S está no fimS herda FOLLOW(L)S está no fimS herda FOLLOW(L)$$ FOLLOW(L)=\{\texttt{)},\texttt{,}\} $$
L: fecha parêntese ou vírgulaS: fim, fecha parêntese ou vírgula| Estado | ACTION ( |
ACTION ) |
ACTION a |
ACTION , |
ACTION $ |
GOTO S |
GOTO L |
|---|---|---|---|---|---|---|---|
0 |
s2 |
s3 |
1 |
||||
1 |
acc |
||||||
2 |
s2 |
s3 |
5 |
4 |
|||
3 |
r2 |
r2 |
r2 |
||||
4 |
s6 |
s7 |
|||||
5 |
r4 |
r4 |
|||||
6 |
r1 |
r1 |
r1 |
||||
7 |
s2 |
s3 |
8 |
||||
8 |
r3 |
r3 |
| Estado | Item completo | Redução | Colunas |
|---|---|---|---|
I3 |
$S \to a \cdot$ | r2 |
$FOLLOW(S)={$,\texttt{)},\texttt{,}}$ |
I5 |
$L \to S \cdot$ | r4 |
$FOLLOW(L)=\{\texttt{)},\texttt{,}\}$ |
I6 |
$S \to (L) \cdot$ | r1 |
$FOLLOW(S)={$,\texttt{)},\texttt{,}}$ |
I8 |
$L \to L,S \cdot$ | r3 |
$FOLLOW(L)=\{\texttt{)},\texttt{,}\}$ |
(a,a)$| Pilha | Entrada | Ação |
|---|---|---|
0 |
( a , a ) $ |
s2 |
0 ( 2 |
a , a ) $ |
s3 |
0 ( 2 a 3 |
, a ) $ |
r2, reduz $S \to a$ |
0 ( 2 S 5 |
, a ) $ |
r4, reduz $L \to S$ |
0 ( 2 L 4 |
, a ) $ |
s7 |
0 ( 2 L 4 , 7 |
a ) $ |
s3 |
0 ( 2 L 4 , 7 a 3 |
) $ |
r2, reduz $S \to a$ |
| Pilha | Entrada | Ação |
|---|---|---|
0 ( 2 L 4 , 7 S 8 |
) $ |
r3, reduz $L \to L,S$ |
0 ( 2 L 4 |
) $ |
s6 |
0 ( 2 L 4 ) 6 |
$ |
r1, reduz $S \to (L)$ |
0 S 1 |
$ |
acc |
| Técnica | Direção | Derivação | Decisão | Informação |
|---|---|---|---|---|
| LL(1) | esquerda para direita | mais à esquerda | expandir produção | FIRST, FOLLOW |
| LR(0) | esquerda para direita | mais à direita ao contrário | empilhar ou reduzir | estado LR(0) |
| SLR(1) | esquerda para direita | mais à direita ao contrário | empilhar ou reduzir | estado LR(0), FOLLOW |
Para cada não terminal:
FIRST das alternativas não podem se sobreporFOLLOW para decidirUma gramática é LR(0) se a tabela LR(0) não tem conflitos.
shift/reducereduce/reduceUma gramática é SLR(1) se a tabela com estados LR(0) e reduções por FOLLOW não tem conflitos.
FOLLOW é uma aproximação globalDentro da família LR:
$$ LR(0) \subset SLR(1) \subset LR(1) $$
(a,(a,a))$