Übung 3 ======= Author: Jan SchäferDate: 2011-05-13 15:39:59 CEST Table of Contents ================= 1 Übung 3 - 13. Mai 1.1 Bücher!!! 1.2 Aufgabe 1 1.2.1 Wann ist eine Grammatik mehrdeutig? 1.2.2 Woran erkennt man eine mehrdeutige? 1.2.3 a) 1.2.4 b) 1.2.5 c) 1.2.6 d) 1.2.7 e) 1.3 Aufgabe 2 1.3.1 a) Grammatik 1.3.2 b) Echter Compiler 1.4 Aufgabe 3 - XML Parser 1.4.1 Scanner 1.4.2 Grammatik 1.4.3 Parser 1.4.4 Tests 1 Übung 3 - 13. Mai ~~~~~~~~~~~~~~~~~~~~ [Übungsblatt] 1.1 Bücher!!! ============== - Appel: Modern Compiler Construction in Java L INF 43 - Wilhelm: Übersetzerbau L INF 243 1.2 Aufgabe 1 ============== 1.2.1 Wann ist eine Grammatik mehrdeutig? ------------------------------------------ - wenn es für ein Wort zwei verschiedene Links-(Rechts-)Ableitungen gibt. - wenn es für ein Wort mehrere Syntaxbäume gibt Skript: [Kapitel 2, Slide 14] 1.2.2 Woran erkennt man eine mehrdeutige? ------------------------------------------ - Beweis durch Beispiel - Prinzipiell unentscheidbares Problem + Typische mehrdeutige Produktionsregeln: - N -> N alpha N Beispiel: e) a + a: S -> S + S -> a + S -> a + a S -> S + S -> S + a -> a + a 1.2.3 a) --------- S -> 0S1 | 01 + mehrdeutig? nein + Sprache? regulär? L = {0^n1^n | n >= 2} 1.2.4 b) --------- S -> +SS | -SS | a + mehrdeutig? - Antwort 1 nein, da keine Linksrekursion - Antwort 2 nein, da für jedes Wort die leftmost Ableitung eindeutig ist 1.2.5 c) --------- S -> S(S)S | eps + mehrdeutig? Ja 1.2.6 d) --------- S -> aSaS | bSaS | eps - mehrdeutig 1.2.7 e) --------- S -> a | S+S | SS | S* | (S) - mehrdeutig 1.3 Aufgabe 2 ============== 1.3.1 a) Grammatik ------------------- + Lösung? S -> SM | eps M -> public | final | static + Einfachste Lösung: ModifierSet -> PFS | PSF | SPF | SFP | FPS | FSP P -> public | eps F -> final | eps S -> static | eps 1.3.2 b) Echter Compiler ------------------------- ModifierSet -> ModifierSet Modifier | eps Modifier -> public | static | final 1.4 Aufgabe 3 - XML Parser =========================== 1.4.1 Scanner -------------- + Token? * Straightforward <, >, ", /, =, ID * Einfacher zum Parsen OTAG, CTAG 1.4.2 Grammatik ---------------- * Lösung 1 xml ::= tag # tag ::= < ID attrlist tail tail ::= > body </ ID > /> body ::= tag body epsilon attrlist ::= attr attrlist epsilon attr ::= ATTRNAME STRINGLIT - Problem lookahead nötig body . < . tag body * Lösung ohne Lookahead Folgende Lösung geht ohne look-ahead, ist aber schwer lesbare Grammatik: xml ::= < tag tag ::= ID attr tail tail ::= > < tags / ID > /> tags ::= tag < tags eps attr ::= ID = STRINGLIT attr eps * Lösung 2 ohne Lookahead + Idee Definiere Token OTAG ::= "<" ID CTAG ::= "</" ID Alternativ: OTAG ::= "<" CTAG ::= "</" + Lösung xml ::= tag; tag ::= OTAG attrlist GT body CTAG OTAG attrlist SLASH GT ; attrlist ::= attr attrlist ; attr ::= ID EQ STRINGLIT; body ::= tag body ; 1.4.3 Parser ------------- + Probleme - EOF checken, sonst ok: <tag></tag>asdasdsasdasd 1.4.4 Tests ------------ - Aussagekräftige Namen wählen nicht: test1.xml, test2.xml, ... sondern: singletag.xml, nestedtag.xml, ... - ok/fail unterscheiden im namen/verzeichnis