1 / 15

Lecture 8

Lecture 8. Syntax-Directed Translation. Syntax-Directed Translation. Translation process driven by the syntactic structure of the program, as given by the parser Semantic actions are integrated in the parsing process In this view, compilation is divided in two parts:

bertac
Download Presentation

Lecture 8

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 8 Syntax-Directed Translation Joey Paquet, 2000, 2002

  2. Syntax-Directed Translation • Translation process driven by the syntactic structure of the program, as given by the parser • Semantic actions are integrated in the parsing process • In this view, compilation is divided in two parts: • analysis (syntactic, semantic) • synthesis (code generation and optimization) • The semantic analysis becomes the link between analysis and synthesis Joey Paquet, 2000, 2002

  3. Syntax-Directed Translation • Semantic actions (implemented as semantic routines) finish the analysis phase by performing semantic checking in productions that need such checks • Semantic actions provide a meaning for the productions • They are the starting point for code generation (synthesis) • Thus, the semantic routines are the heart of the compiler Joey Paquet, 2000, 2002

  4. Syntax-Directed Translation • Semantic routines can be formalized using attribute grammars • They augment ordinary context-free grammars with attributes that represent semantic properties such as type, value or correctness used in semantic analysis (checking) and code generation (translation) • It is useful to keep checking and translation facilities distinct in the semantic routines’ implementation • Semantic checking is machine-independent and code generation is not, so separating them gives more flexibility to the compiler Joey Paquet, 2000, 2002

  5. Attribute Migration • Attributes gathered from a child in the abstract syntax tree are called synthetized attributes • Attributes gathered from a sibling in the abstract syntax tree are called inherited attributes Joey Paquet, 2000, 2002

  6. Example: Attribute Migration E1  id = E2 E= : [E2: v: ] [(id:)  defVar] [id.val  v] [E1 .val:  v] E1  E2  E3 E* : [E2 : v2: ] [E3 : v3: ] [E1 .val:  v2  v3 ] E1  E2 + E3 E+ : [E2 : v2: ] [E3 : v3: ] [E1 .val:  v2 + v3 ] E id Eid : [(id:)  defVar] [(E.val: ) id.val] E  const Econst : [const : v : ] [(E.val: )v] Y=3*X+Z E (v3*vx+vz :) id (vY:) = E (v(3*vx)+vz:) E (v3*vx:) + E(vZ:) E (3:) * E(vX:) id (vZ:) const (3:) id (vX:) Joey Paquet, 2000, 2002

  7. E  TE’ E’  +TE’ |  T  FT’ T’  FT’ |  F  id | const T’1 E’2   T’3  Example 2: Attribute Migration • Problems arise when rules are factorized: a+b*c E T1 E’1 F1 + T2 id (va : ) F2 T’2 id (vb : ) F3 * id (vc : ) Joey Paquet, 2000, 2002

  8. T’1 E’2   T’3  Example 2: Attribute Migration • Solution: migrate attributes sideways: a+b*c E T1 E’1 E  TE’ E’  +TE’ |  T  FT’ T’  FT’ |  F  id | const F1 + T2 id (va : ) F2 T’2 id (vb : ) F3 * id (vc : ) Joey Paquet, 2000, 2002

  9. Attr. Migration: Implementation Parse(){ type Es; lookahead = NextToken() if (E(Es);Match('$')) return(true); else return(false); } Joey Paquet, 2000, 2002

  10. Attr. Migration: Implementation E(type &Es){ type Ts,E's if (lookahead is in [0,1,(]) if (T(Ts);E'(Ts,E's);) // E' inherits from T write(E->TE') Es = E's // Synthetised attribute sent up return(true) else return(false) else return(false) } Joey Paquet, 2000, 2002

  11. Attr. Migration: Implementation E'(type &Ti, type &E's){ type Ts,E'2s if (lookahead is in [+]) if (Match('+');T(Ts);E'(Ts,E'2s)) // E' inherits from T write(E'->TE') E's = semcheckop(Ti,E'2s) // Semantic check & synthetized // attribute sent up return(true) else return(false) else if (lookahead is in [$,)] write(E'->epsilon) E's = Ti // Synth. attr. is inhertied // from T (sibling, not child) // and sent up return(true) else return(false) } Joey Paquet, 2000, 2002

  12. Attr. Migration: Implementation T(type &Ts){ type Fs, T's if (lookahead is in [0,1,(]) if (F(Fs);T'(Fs,T's);) // T' inherits from F write(T->FT') Ts = T's // Synthetized attribute sent up return(true) else return(false) else return(false) } Joey Paquet, 2000, 2002

  13. Attr. Migration: Implementation T'(type Fi, type &T's){ type Fs, T'2s if (lookahead is in [*]) if (Match('*');F(Fs);T'(Fs,T'2s)) // T' inherits from F write(T'->*FT') T's = semcheckop(Fi,T'2s) // Sem. check + synth. // attribute sent up return(true) else return(false) else if (lookahead is in [+,$,)] write(T'->epsilon) T's = Fi // Synthetized attr. is // inhertied from F (sibling, // not child) & sent up the tree return(true) else return(false) } Joey Paquet, 2000, 2002

  14. Attr. Migration: Implementation F(type &Fs){ type Es if (lookahead is in [0]) if (Match('id')) write(F->id) Fs = gettype(id.name,table) // Gets the attribute ``type'' // from the symbol table and // sends it up the tree as Fs return(true) else return(false) else if (lookahead is in [(]) if (Match('(');E(Es);Match(')')) write(F->(E)) Fs = Es // Synthetized attribute sent // up the tree return(true) else return(false) else return(false) } Joey Paquet, 2000, 2002

  15. Attr. Migration: Implementation type semcheckop(type ti,type ts){ if (ti == ts) return(ti) else return(typerror) } type gettype(name, table){ if (name is in table) return (type) else return(typerror) } Joey Paquet, 2000, 2002

More Related