2016-04-10 19 views
0

belirsiz bu dilbilgisi geçerli:Ben kesin bir gramer bu dilbilgisi dönüştürmek istediğiniz

: Ben çözümden çok daha karmaşıktır ama benim çözüm doğru olup olmadığından emin değilim hangi bir çözüm buldu
S -> if E then S 
    | if E then S else S 
    | a 
E -> b 

S -> if E then T else S 
    | if E then S 
    | a 
T -> if E then T else T 
    | a 
E -> b 

Çözümüm doğru mu?

cevap

1

Bana sorun değil. Standart çözelti gerçekten pek farklı değil: Standart solüsyon

stmt  : matched | unmatched 
matched : "if" expr "then" matched "else" matched 
      | other_stmt 
unmatched : "if" expr "then" unmatched 
      | "if" expr "then" matched "else" unmatched 

avantajı (sizin dilbilgisi içinde a) other_stmt yineleniyor olmadığını ve gramer diğer bileşik ifadelere uzatmak kolaydır.

stmt  : matched | unmatched 
matched : "if" expr "then" matched "else" matched 
      | "while" matched 
      | other_stmt 
unmatched : "if" expr "then" unmatched 
      | "if" expr "then" matched "else" unmatched 
      | "while" unmatched 
: Örneğin, while deyimi eklerseniz
İlgili konular