Presentation is loading. Please wait.

Presentation is loading. Please wait.

Concordia University Department of Computer Science and Software Engineering Click to edit Master title style COMPILER DESIGN Syntax-Directed Translation.

Similar presentations


Presentation on theme: "Concordia University Department of Computer Science and Software Engineering Click to edit Master title style COMPILER DESIGN Syntax-Directed Translation."— Presentation transcript:

1 Concordia University Department of Computer Science and Software Engineering Click to edit Master title style COMPILER DESIGN Syntax-Directed Translation Joey Paquet, 2000-2015 1COMP 442/6421 – Compiler Design

2 Concordia University Department of Computer Science and Software Engineering Translation process driven by the syntactic structure of the program, as given by the parser Semantic actions are integrated in the parsing process Semantic actions are functions whose goal is to accumulate semantic information about the program as the parsing is done. Different pieces of information are gathered during the parsing of different syntactical rules. These pieces of information are gathered in semantic values, or semantic records, or semantic attributes. Some of the information that must be gathered spans over the application of several syntactical rules. This raises the need to migrate this information across the parse tree, a process known as attribute migration. In syntax-directed translation, compilation is divided in two parts: analysis (syntactic, semantic) synthesis (code generation and optimization) The semantic analysis becomes the link between analysis and synthesis: code generation (synthesis) is conditional to positive analysis Syntax-directed translation Joey Paquet, 2000-2015 2COMP 442/6421 – Compiler Design

3 Concordia University Department of Computer Science and Software Engineering Semantic actions (implemented as semantic routines) finish the analysis phase by performing semantic checking in productions that need such checks, e.g. type checking Semantic actions assemble information in order to check and generate a meaning for the program elements generated by the productions They are the starting point for code generation (synthesis) Thus, the semantic routines are the heart of the compiler Syntax-directed translation Joey Paquet, 2000-2015 3COMP 442/6421 – Compiler Design

4 Concordia University Department of Computer Science and Software Engineering Semantic routines can be formalized using attribute grammars Attribute grammars 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 (front/back end) Syntax-directed translation and attribute grammars Joey Paquet, 2000-2015 4COMP 442/6421 – Compiler Design

5 Concordia University Department of Computer Science and Software Engineering An attribute is a property of a programming language construct, including data type, value, memory location/size, translated code, etc. Implementation-wise, they are also called semantic records. The process of computing the value of an attribute is called binding. Static binding concerns binding that can be done at compile-time, and dynamic binding happens at run-time, e.g. for polymorphism. In our project, we are concerned solely on static compile-time binding. Attributes Joey Paquet, 2000-2015 5COMP 442/6421 – Compiler Design

6 Concordia University Department of Computer Science and Software Engineering Static attribute binding is done by gathering, propagating, and aggregating attributes while traversing the parse tree. Attributes are gathered at tree leaves, propagated across tree nodes, and aggregated at some parent nodes when all necessary information is available. This can be done as the program is being parsed using syntax-directed translation. Synthetized attributes : attributes gathered from a child in the syntax tree Inherited attributes : attributes gathered from a sibling in the syntax tree Attributes migration Joey Paquet, 2000-2015 6COMP 442/6421 – Compiler Design

7 Concordia University Department of Computer Science and Software Engineering Example: Semantic rules and attribute migration Joey Paquet, 2000-2015 7COMP 442/6421 – Compiler Design E 1  id = E 2 E = : [E 2 : v:] [(id:)  defVar] [id.valv] [E 1.val: v] E 1  E 2  E 3 E * : [E 2 : v 2 :] [E 3 : v 3 : ] [E 1.val: v 2 v 3 ] E 1  E 2 + E 3 E + : [E 2 : v 2 :] [E 3 : v 3 : ] [E 1.val: v 2 +v 3 ] E  idE id : [(id:)  defVar] [(E.val: )id.val] E  constE const :[const : v:] [(E.val: )v] Y=3*X+Z E(v 3*vx+vz :) E(v (3*vx)+vz :)id(v Y :) = E(v 3*vx :) id(v Z :) + E(v Z :) const(3:)id(v X :) * E(v X :)E(3:)

8 Concordia University Department of Computer Science and Software Engineering Problems arise when rules are factorized: Example 2: attribute migration Joey Paquet, 2000-2015 8COMP 442/6421 – Compiler Design E  TE’ E’  +TE’| T  FT’ T’  FT’| F  id|const E T1T1 T2T2 T’ 2 * F1F1 id(v a :) T’ 1  F2F2 id(v b :) E’ 2  T’ 3  F3F3 id(v c :) E’ 1 + a+b*c

9 Concordia University Department of Computer Science and Software Engineering Solution: migrate attributes sideways, i.e. inherited attributes Example 2: attribute migration Joey Paquet, 2000-2015 9COMP 442/6421 – Compiler Design E T1T1 T2T2 T’ 2 * F1F1 id(v a :) T’ 1  F2F2 id(v b :) E’ 2  T’ 3  F3F3 id(v c :) E’ 1 + a+b*c E  TE’ E’  +TE’| T  FT’ T’  FT’| F  id|const

10 Concordia University Department of Computer Science and Software Engineering Attribute migration: implementation in recursive-descent predictive parser Joey Paquet, 2000-2015 10COMP 442/6421 – Compiler Design Parse(){ type Es; //semantic record created //before the call lookahead = NextToken() if (E(Es);Match('$')) //passed as a reference //to parsing functions //that will compute its value return(true); else return(false); }

11 Concordia University Department of Computer Science and Software Engineering Attribute migration: implementation in recursive-descent predictive parser Joey Paquet, 2000-2015 11COMP 442/6421 – Compiler Design E(type &Es){ type Ts,E's if (lookahead is in [0,1,(]) if (T(Ts);E'(Ts,E's);) // E' inherits Ts from T write(E->TE') Es = E's // Synthetised attribute sent up return(true) else return(false) else return(false) }

12 Concordia University Department of Computer Science and Software Engineering Attribute migration: implementation in recursive-descent predictive parser Joey Paquet, 2000-2015 12COMP 442/6421 – Compiler Design 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) }

13 Concordia University Department of Computer Science and Software Engineering Attribute migration: implementation in recursive-descent predictive parser Joey Paquet, 2000-2015 13COMP 442/6421 – Compiler Design T(type &Ts){ type Fs, T's if (lookahead is in [0,1,(]) if (F(Fs);T'(Fs,T's);) // T' inherits Fs from F write(T->FT') Ts = T's// Synthetized attribute sent up return(true) else return(false) else return(false) }

14 Concordia University Department of Computer Science and Software Engineering Attribute migration: implementation in recursive-descent predictive parser Joey Paquet, 2000-2015 14COMP 442/6421 – Compiler Design 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) // Semantic check and // synthetized attribute sent up return(true) else return(false) else if (lookahead is in [+,$,)] write(T'->epsilon) T's = Fi // Synthetized attribute is // inhertied from F sibling // and sent up the tree return(true) else return(false) }

15 Concordia University Department of Computer Science and Software Engineering Attribute migration: implementation in recursive-descent predictive parser Joey Paquet, 2000-2015 15COMP 442/6421 – Compiler Design 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 from E // sent up the tree as attribute // of F return(true) else return(false) }

16 Concordia University Department of Computer Science and Software Engineering Attribute migration: implementation in recursive-descent predictive parser Joey Paquet, 2000-2015 16COMP 442/6421 – Compiler Design 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) }

17 Concordia University Department of Computer Science and Software Engineering Attribute migration: example Joey Paquet, 2000-2015 17COMP 442/6421 – Compiler Design E T1T1 T2T2 T’ 2 * F1F1 id(v a :) T’ 1  F2F2 id(v b :) E’ 2  T’ 3  F3F3 id(v c :) E’ 1 + Es E’sTs T’s Ti Fi Fs E’s T’s Fs Ti T’s Fi Ts Fi T’s a+b*c

18 Concordia University Department of Computer Science and Software Engineering Click to edit Master title style Symbol table generation Joey Paquet, 2000-2015 18COMP 442/6421 – Compiler Design

19 Concordia University Department of Computer Science and Software Engineering Symbol tables: example and hints Joey Paquet, 2000-2015 19COMP 442/6421 – Compiler Design class MyClass1 { int mc1v1[2][4]; float mc1v2; MyClass2 mc1v3[3]; int mc1f1(int p1, MyClass2 p2[3]) { MyClass2 fv1[3]; … } int f2(MyClass1 f2p1[3]) { int mc1v1; … } class MyClass2 { int mc1v1[2][4]; float fp1; MyClass m2[3]; … } program { int m1; float[3][2] m2; MyClass[2] m3;... } float f1(int fp1[2][2], float fp2) { MyClass1[3] fv1; int fv2; … } int f2() {... }

20 Concordia University Department of Computer Science and Software Engineering Symbol tables: example and hints Joey Paquet, 2000-2015 20COMP 442/6421 – Compiler Design ::=#createGlobalTable# * ::=class id #createClassEntryAndTable# { * *}; ::=program #createProgramTable# ; * ::= id( ) #createFunctionEntryAndTable# ::= ; ::={ * *} ::= id *; #createVariableEntry# ::= ; |if( )then else ; |for( id ; ; ) ; |get( ); |put( ); |return( ); ::= ::= { *} | |  ::= | ::= ::= | ::=+ | - ::= | ::= | *id( ) |num |( ) |not | ::= *id * ::=id *. ::=[ ] ::=[ int ] ::=int | float | id ::= id * #createParameterEntry# * |  ::= * |  ::=, id * #createParameterEntry# ::=, Insert semantic actions (function calls) that create tables/entries when an identifier is declared. Attribute migration must be used when the information necessary to create an entry is distributed over different rules. The same process will be used to do further semantic processing: Expression type inference Type checking Code generation


Download ppt "Concordia University Department of Computer Science and Software Engineering Click to edit Master title style COMPILER DESIGN Syntax-Directed Translation."

Similar presentations


Ads by Google