• Aucun résultat trouvé

Lab 7 Analyses LR(1) et LALR(1)

N/A
N/A
Protected

Academic year: 2022

Partager "Lab 7 Analyses LR(1) et LALR(1)"

Copied!
14
0
0

Texte intégral

(1)

IFT313

Introduction aux langage formels

Francis Bisson

Département d’informatique Université de Sherbrooke

Lab 7

Analyses LR(1) et LALR(1)

(2)

EXERCICE 1

IFT313 2

(3)

IFT313/IFT451 © Froduald Kabanza

3

Exercice 1

Étant donné la grammaire G= ({S}, {i, e, o}, R, S), avec R =

{

1. S → iSeS 2. S → iS 3. S → o }

a. Donnez l’AFD LR(1).

b. Donner la table d’analyse LR(1).

c. Est-ce une grammaire LR(1)?

d. Donnez l’AFD LALR(1).

e. Donnez la table d’analyse LALR(1).

f. Est-ce une grammaire LALR(1)?

Note: c’est la grammaire approximant « if then else » vue au Lab 6.

(4)

IFT313/IFT451 © Froduald Kabanza

4

1. a-b AFD LR(1) et Table d’analyse LR(1)

actions goto

1.c.

La grammaire n’est pas LR(1) parce qu’il y a un conflit shift/reduce dans l’entrée [9,e] de table LR(1)

S’® .S, $ 0 S® .iSeS, $ S® .iS, $ S® .o, $

S’® S 1 . , $

S

i e o $ S

0 s2 s3 1

1 acc

2 s8 s7 4

3 r3

4 s5 r2

5 s2 s7 6

6 r1

7 r3 r3

8 s8 s7 9

9 r2

s10

r1

10 s8 s7 11

11 r1 r1

1. S® iSeS 2. S® iS 3. S® o

S® o., $ 3 o

S® i . SeS, $ 2 S® i . S, $

. iSeS, $/e

. iS, $/e S® . o, $/e

S® iSe 5 . S, $ S® . iSeS, $

. iS, $ S® . o, $

S® iS 4 . eS, $

S® iS . , $

i

S

e S® iSeS 6 . , $

S® o., $/e 7 o

S® i . SeS, $/e 8 S® i . S, $/e S® . iSeS, $/e

. iS, $/e S® . o, $/e S

7 i

i

S® iS . 9 eS, $/e S® iS . , $/e

S

o i

S® iSe 10 . S, $/e S® . iSeS, $/e

. iS, $/e S® . o, $/e

S® iSeS 11 . , $/e S

e i

o

o

(5)

IFT313/IFT451 © Froduald Kabanza

5

1.d-e AFD LALR(1) et Table d’analyse LALR(1)

actions goto

1.f.

La grammaire n’est pas LR(1) parce qu’il y a un conflit shift/reduce dans l’entrée [4,e] de table LALR(1)

S’® .S, $ 0 S® .iSeS, $ S® .iS, $ S® .o, $

S’® S 1 . , $

S i e o $ S

0 s2 s3 1

1

acc

2 s2 s3 4

3 r3 r3

4 r2

s5 r2

5 s2 s3 6

6 r1 r1

1. S® iSeS 2. S® iS 3. S® o o

S® i . SeS, $/e 2. iSeS, $/e

. iS, $/e S® . o, $/e

S® iSe . 5 S, $/e S® . iSeS, $/e

. iS, $/e S® . o, $/e

S® iS . 4 eS, $/e S® iS . , $/e

i

S

e

S® iSeS 6 . , $/e S® o., $/e 3 o

o

S

i

i

(6)

EXERCICE 2

IFT313 6

(7)

IFT313 7

Exercice 2

Soit la grammaire G= ({S, E}, { id, +, ), ( }, R, S), avec R =

{

1. S → E 2. E → id 3. E → id (E) 4. E → E + id }

Les numéros a) à d) sont à faire à domicile.

a. Donnez l’AFD LR(0) de la grammaire.

b. Donnez la table d’analyse LR(0).

c. Est-ce une grammaire LR(0)? Expliquez.

d. Donnez la table d’analyse SLR(1).

e. Es-ce une grammaire SLR(1) ? Expliquez.

f. Donnez l’AFD LR(1).

g. Est-ce une grammaire LR(1)?

h. Donnez l’AFD LALR(1).

i. Donnez la table d’analyse LALR(1).

j. Est-ce une grammaire LALR(1)?

(8)

IFT313 11

2.f. AFD LR(1)

Closure([S’®S;{$}])

={[S’ ®S;{$}], [S ®E;{$}], [E ®id;{$,+}], [E ®id(E) ;{$,+}], [E ®E+id;{$,+}]}

Closure([S’®S;{$}])

={[S’ ®S;{$}]}

Closure({[S®E;{$}], [E ®E+id;{$,+}]})

={[S®E;{$}], [E ®E+id;{$,+}]}

Closure({[E ®id;{$,+}], [E ®id(E) ;{$,+}]})

={[E ®id;{$,+}], [E ®id(E) ;{$,+}]}

0 1

2

3

Closure([E ®E+id;{$,+}])

={[E ®E+id;{$,+}]}

4

Closure([E ®id(E);{$,+}])

={[E ®id(E) {$,+}], [E ®id;{), +}], [E ®id(E);{), +}], [E ®E+id;{), +}]}

5

Closure([E ®E+id;{$,+}])

={[E ®E+id;{$,+}]}

6

Closure({[E ®id(E);{$,+}], [E ®E+id;{),+}]})

={[E ®id(E);{$,+}], [E ®E+id;{),+}]}

7

Closure([E ®id(E);{$,+}])

={[E ®id(E);{$,+}]}

8

Closure({[E ®id;{), +}], [E ®id(E);{), +}])

={[E ®id;{), +}], [E ®id(E);{), +}]}

9

Closure([E ®E+id;{),+}]})

={[E ®E+id;{),+}]}

10

Goto(10, id)

=Closure([E ®E+id;{),+}]})

={[E ®E+id;{),+}]}

12

Closure([E ®id(E);{),+}])

={[E ®id(E) {),+}], [E ®id;{), +}], [E ®id(E);{), +}], [E ®E+id;{), +}]}

11

Closure({[E ®id(E);{),+}],

[E ®E+id;{),+}]})

={[E ®id(E);{),+}], [E ®E+id;{),+}]}

13

Closure([E ®id(E);{),+}])

={[E ®id(E);{),+}]}

14

S

E id

(

id

+

E

id

(

id

)

+

id +

E

)

(9)

IFT313 12

id + ( ) $ S E

0 s3 1 2

1 acc

2 s4 r1

3 r2 s5 r2

4 s6

5 s8 7

6 r4 r4

7 s10 s9

8 r2 s11 r2

9 r3 r3

10 s12

11 s8

12 r4 r4

13 s10 s14

14 r3 r3

8. C’est une grammaire LR(1) parce que la table d’analyse LR(1) n’a pas de conflit.

7. Table d’analyse LR(1)

action/goto goto

(10)

IFT313 13

8. AFD LALR(1)

{[S’ ®S;{$}], [S ®E;{$}], [E ®id;{$,+}], [E ®id(E) ;{$,+}], [E ®E+id;{$,+}]}

{[S’ ®S ;{$}]}

{[S®E;{$}], [E ®E+id;{$,+}]}

{[E ®id;{$,+, )}], [E ®id(E) ;{$,+, )}]}

0 1

2

3

{[E ®E+id;{$,+,)}]}

4

{[E ®id(E) {$,+,)}], [E ®id;{), +}], [E ®id(E);{), +}], [E ®E+id;{), +}]}

5

{[E ®E+id;{$,+,)}]}

6

{[E ®id(E);{$,+, )}], [E ®E+id;{),+}]}

7

{[E ®id(E);{$,+,)}]}

8

S

E id

(

id

+

E

id

) +

Obtenu de l’AFD LR(1) en fusionnant les états ayant les mêmes couleurs : 8-3; 4-10; 5-11; 7-13; 9-14. L’état 8 correspond au 9 dans l’AFD LR(1).

Sans surprise, c’est le même AFD que l’AFD LR(0) mis à part les lookahead,

vu que la grammaire est SLR(1).

(11)

IFT313 14

2.g Table d’analyse LALR(1)

id + ( ) $ S E

0 s3 1 2

1 acc

2 s4 r1

3 r2 s5 r2 r2

4 s6

5 s3 7

6 r4 r4 r4

7 s4 s8

8 r3 r3 r3

2.h C’est une grammaire LALR(1) parce que la table d’analyse LALR(1) n’a pas de conflit. On s’y attend puisque la grammaire est SLR(1). En fait la table LALR(1) est la même que la table SLR(1).

action/goto goto

Références

Documents relatifs

C.heck slew speed adjustment proced- ure in section IV. Check stepper motor damping ad- justment procedure in section IV. Verify proper LED current. Refer to

Integral operators of Hammerstein type play an important role in various fields of nonlinear oscillations. Zabreiko for his constant interest and very valuable advice

As we worked on a list of diagnostic messages for CompCert’s parser, we made a series of changes to the grammar and automaton (without affecting the language), so as to limit the

It asserts that, if we can reach state s 0 under the assumption that the next input symbol is z, and if the automaton has a transition labeled z from s 0 to s 00 , then we can

The completeness theorem states that if the automaton passes the completeness validator (which we describe further on), if a prefix w of the input is valid, and if enough fuel

What if the lookahead token was (say) ’2’ instead of ’}’ ?.. [ SEMICOLON RPAREN and 34 more tokens ] Based on this, can one propose an accurate diagnostic message ?.. No.

Choose a diagnostic message based on the LR automaton’s state, ignoring its stack entirely?. Is this a

Confirmer ainsi le r´ esultat de la question pr´ ec´