Grammatiche ad attributi
Definire una grammatica ad attributi
per generare il linguaggio:
L = {numeri interi in notazione decimale
divisibili per 3}.
Le grammatiche ad attributi sono molto
usate nella compilazione.
Queste grammatiche associano attributi
semantici a simboli sintattici e regole semantiche a regole sintattiche.
Aggiungono annotazioni alle produzioni
in modo da dare significato a cio' che generano.
Soluzione: un numero e' divisibile
per 3 quando lo e' la somma delle sue singole cifre.
C->0 {C.a = 0}
C->1 {C.a = 1}
C->2 {C.a = 2}
C->3 {C.a = 3}
C->4 {C.a = 4}
C->5 {C.a = 5}
C->6 {C.a = 6}
C->7 {C.a = 7}
C->8 {C.a = 8}
C->9 {C.a = 9} |
S1->S2C |
{S1.a := C.a + S2.a;
if
(S1.a mod 3 <> 0) then error} |
S1->C |
{S1.a := C.a; if (S1.a
mod 3 <> 0) then error} |
|
2) definire il linguaggio L = {anbncn
|
n>=1 }.
Sappiamo che questo
linguaggio e' di tipo 1. Partiamo da una grammatica di tipo 2 e aggiungiamo
le notazioni semantiche necessarie che generano L.
S-> AC |
{if A.count = C.count then
TRUE else error} |
A-> ab |
{A.count := 1} |
A-> aAb |
{A.count := A1.count + 1} |
C-> c |
{C.count := 1} |
C-> cC |
{C.count := C1.count + 1} |
© C o p y r i g h
t 1 9 9 4 - 2 0 0 4
w w w . d i z i o n a r i o i n f o r m a t i c o . c o m |
 |
|