Adminitrivia Lctur 4: Paring If you do not hav a group, pla pot a rqut on Piazzza ( th Form projct tam... itm. B ur to updat your pot if you find on. W will aign orphan to group randomly in a fw day. Programming Contt coming up. Lat modifid: Tu Sp 24 2:59:58 23 CS64: Lctur #4
A Glanc at th Map Sourc cod Lxical Analyi Tokn Paring AST Smantic Analyi Dcoratd AST W ar hr Lat modifid: Tu Sp 24 2:59:58 23 CS64: Lctur #4 2
Rviw: BNF BNF i anothr pattrn-matching languag; Alphabt typically t of tokn, uch a from lxical analyi, rfrrd to a trminal ymbol or trminal. Matching rul hav form: X : α α 2 α n, whr X i from a t of nontrminal ymbol (or nontrminal or mta-variabl), n, and ach α i i a trminal or nontrminal ymbol. For mphai, may writ X : ǫ whn n =. Rad X : α α 2 α n, a An X may b formd from th concatnation of an α, α 2,..., α n. Dignat on nontrminal a th tart ymbol. St of all matching rul i a contxt-fr grammar. Lat modifid: Tu Sp 24 2:59:58 23 CS64: Lctur #4 3
Rviw: Drivation String (of trminal) T i in th languag dcribd by grammar G, (T L(G)) if thr i a drivation of T from th tart ymbol of G. Drivation of T = τ τ k from nontrminal A i qunc of ntntial form: A α α 2... α 2 α 22... τ...τ k whr ach α ij i a trminal or nontrminal ymbol. W ay that α α m Bα m+ α n α α m β β p α m+ α n if B : β β p i a production. ( m n). If Φ and Φ ar ntntial form, thn Φ = Φ 2 man that or mor tp turn Φ into Φ 2. Φ + = Φ 2 man or mor tp do it. So if S i tart ymbol of G, thn T L(G) iff S + = T. Lat modifid: Tu Sp 24 2:59:58 23 CS64: Lctur #4 4
Exampl of Drivation. : ID 2. : ( ) 3. : / 4. : 5. : + 6. : - Altrnativ Notation : ID ( ) / : ǫ + - Problm: Driv - ID / ( ID / ID ) 3 6 = / = ID / = - ID / = - ID / ( ) 4 3 = - ID / ( ) = - ID / ( / ) = - ID / ( ID / ) 4 = - ID / ( ID / ) = - ID / ( ID / ID ) 4 = - ID / ( ID / ID ) 2 Lat modifid: Tu Sp 24 2:59:58 23 CS64: Lctur #4 5
Typ of Drivation Contxt fr man can rplac nontrminal in any ordr (i.., rgardl of contxt) to gt am rult (a long a you u am production). So, if w u a particular rul for lcting nontrminal to produc from, can charactriz drivation by jut liting production. Prviou xampl wa lftmot drivation: alway choo lftmot nontrminal. Compltly charactrizd by lit of production: 3,, 6, 2, 4, 3,, 4,, 4. Lat modifid: Tu Sp 24 2:59:58 23 CS64: Lctur #4 6
Drivation and Par Tr A lftmot drivation alo compltly charactrizd by par tr: - ID / ( ID / ID ) What i th rightmot drivation for thi?
Drivation and Par Tr A lftmot drivation alo compltly charactrizd by par tr: - ID / ( ID / ID ) What i th rightmot drivation for thi? 3 2 3 = / = / ( ) = / ( / ) 4 = / ( / ID ) = / ( / ID ) 4 = / ( ID / ID ) = / ( ID / ID ) 4 6 = / ( ID / ID ) = ID / ( ID / ID ) = - ID / ( ID / ID ) Lat modifid: Tu Sp 24 2:59:58 23 CS64: Lctur #4 7
Ambiguity Only on drivation for prviou xampl. What about ID / ID / ID? Claim thr ar two par tr, corrponding to two lftmot drivation. What ar thy? If thr xit vn on tring lik ID / ID / ID in L(G), w ay G i ambiguou (vn if othr tring only hav on par tr). Lat modifid: Tu Sp 24 2:59:58 23 CS64: Lctur #4 8
Ambiguity Only on drivation for prviou xampl. What about ID / ID / ID? Claim thr ar two par tr, corrponding to two lftmot drivation. What ar thy? - ID/ ID / ID - ID / If thr xit vn on tring lik ID / ID / ID in L(G), w ay G i ambiguou (vn if othr tring only hav on par tr). ID / ID Lat modifid: Tu Sp 24 2:59:58 23 CS64: Lctur #4 8
Rviw: Syntax-Dirctd Tranlation Want th tructur of ntnc, not jut whthr thy ar in th languag, bcau thi driv tranlation. Aociat tranlation rul to ach production, jut a Flx aociatd action with matching pattrn. Bion notation: : / { $$ = dodivid($, $3); } provid way to rfr to and t mantic valu on ach nod of a par tr. Comput th mantic valu from lav up th par tr. Sam a th ordr of a rightmot drivation in rvr (a.k.a a canonical drivation). Altrnativly, jut prform arbitrary action in th am ordr. Lat modifid: Tu Sp 24 2:59:58 23 CS64: Lctur #4 9
Exampl: Conditional tatmnt Problm: if-l or if-lif-l tatmnt in Python (l optional). Aum that only(indntd) uit may b ud for thn and l clau, that nontrminal tmt dfin an individual tatmnt(on pr lin), and that nontrminal xpr dfin an xprion. Lxr uppli INDENT and DEDENT. A cond i a kind of tmt. Lat modifid: Tu Sp 24 2:59:58 23 CS64: Lctur #4
Exampl: Conditional tatmnt Problm: if-l or if-lif-l tatmnt in Python (l optional). Aum that only(indntd) uit may b ud for thn and l clau, that nontrminal tmt dfin an individual tatmnt(on pr lin), and that nontrminal xpr dfin an xprion. Lxr uppli INDENT and DEDENT. A cond i a kind of tmt. xpr :... tmt :... cond... cond : "if" xpr : uit lif l uit: INDENT tmt DEDENT tmt: tmt tmt tmt lif: ǫ "lif" xpr : uit lif l : ǫ "l" : uit Lat modifid: Tu Sp 24 2:59:58 23 CS64: Lctur #4
Exampl: Conditional tatmnt in Java Problm: if-l in Java. Aum that nontrminal tmt dfin an individual tatmnt (including a block in {}). Lat modifid: Tu Sp 24 2:59:58 23 CS64: Lctur #4
Exampl: Conditional tatmnt in Java Problm: if-l in Java. Aum that nontrminal tmt dfin an individual tatmnt (including a block in {}). xpr :... tmt :... cond... cond : "if" ( xpr ) tmt l l : ǫ "l" tmt But thi don t quit work: rcogniz corrct tatmnt and rjct incorrct on, but i ambiguou. E.g., if (foo) if (bar) walk(); l chwgum(); Do w chw gum if foo i fal? That i, i thi quivalnt to if (foo) { if (bar) walk(); } l chwgum(); /*or*/ if (foo) { if (bar) walk(); l chwgum(); }? Lat modifid: Tu Sp 24 2:59:58 23 CS64: Lctur #4
Exampl rolvd: Conditional tatmnt in Java Th rul i uppod to b ach l attach to th nart opn if on th lft, which i capturd by: xpr :... tmt :... cond... tmt_clod :... cond_clod... cond_clod : "if" ( xpr ) tmt_clod "l" tmt_clod cond : "if" ( xpr ) tmt "if" ( xpr ) tmt_clod "l" tmt Thi do not allow u to intrprt a if (foo) if (bar) walk(); l chwgum(); if (foo) { if (bar) walk(); } l chwgum(); But it not xactly clar, i it? Lat modifid: Tu Sp 24 2:59:58 23 CS64: Lctur #4 2
Puzzl: NFA to BNF Problm: What BNF grammar accpt th am tring a thi NFA? ǫ 2 3 ǫ 4 ǫ 5 6 7 ǫ Lat modifid: Tu Sp 24 2:59:58 23 CS64: Lctur #4 3
Puzzl: NFA to BNF Problm: What BNF grammar accpt th am tring a thi NFA? ǫ 2 ǫ 3 ǫ 4 5 6 7 A convntional anwr (from cla): S: S2 Z S3 Z S2: Z Z Z: Z ǫ S3: Z Z Z S2: S2 S2 S2 S3: S3 S3 S3 ǫ Lat modifid: Tu Sp 24 2:59:58 23 CS64: Lctur #4 3
Puzzl: NFA to BNF Problm: What BNF grammar accpt th am tring a thi NFA? ǫ 2 ǫ 3 ǫ 4 5 6 7 ǫ Gnral anwr (adaptabl to any NFA), with on nontrminal pr tat: S: S S4 S4: S5 S4 S: S2 S S5: S6 S5 S2: S3 S2 S6: S7 S6 S3: S S3 ǫ S7: S4 S7 ǫ Nontrminal Sk i th t of tring that will gt m from Sk in th NFA to a final tat in th NFA. Lat modifid: Tu Sp 24 2:59:58 23 CS64: Lctur #4 3