PART 4 - SYNTAX DIRECTED TRANSLATION. F. Wotawa TU Graz) Compiler Construction Summer term / 309
|
|
- Gwendolyn Little
- 6 years ago
- Views:
Transcription
1 PART 4 - SYNTAX DIRECTED TRANSLATION F. Wotawa TU Graz) Compiler Construction Summer term / 309
2 Setting Translation of context-free languages Information attributes of grammar symbols Values of attributes are defined by semantic rules 2 possibilities: Syntax directed definitions (high-level spec) Translation schemes (implementation details) Evaluation: (1) Parse input, (2) Generate parse tree, (3) Evaluate parse tree F. Wotawa TU Graz) Compiler Construction Summer term / 309
3 Syntax directed definitions Generalization of context-fee grammars Each grammar symbol has a set of attributes Synthesized vs. inherited attributes Attribute: string, number, type, memory location,... Value of attribute is defined by semantic rules Synthesized: Value of child node in parse tree Inherited: Value of parent node in parse tree Semantic rules define dependencies between attributes Dependency graph defines calculation order of semantic rules Semantic rules can have side effects F. Wotawa TU Graz) Compiler Construction Summer term / 309
4 Form of a syntax directed definition Grammar production: A α Associated semantic rule: b := f(c 1,..., c k ) f is a function Synthesized: b is a synthesized attribute of A and c 1,..., c k are grammar symbols of the production Inherited: b is an inherited attribute of a grammar symbol on the right side of the production and c 1,..., c k are grammar symbols of the production b depends on c 1,..., c k F. Wotawa TU Graz) Compiler Construction Summer term / 309
5 Example Calculator -program: val is a synthesized attribute for nonterminals E, T and F Production L En E E 1 +T E T T T 1 *F T F F (E) F digit Semantic Rule print(e.val) E.val := E 1.val + T.val E.val := T.val T.val := T 1.val F.val T.val := F.val F.val := E.val F.val := digit.lexval F. Wotawa TU Graz) Compiler Construction Summer term / 309
6 S-attributed grammar Attributed grammar exclusively using synthesized attributes Example-evaluation: 3*5+4n (annotated parse tree) L E.val=19 n E.val=15 + T.val=4 T.val=15 F.val=4 T.val=3 * F.val=5 digit.lexval=4 F.val=3 digit.lexval=5 digit.lexval=3 F. Wotawa TU Graz) Compiler Construction Summer term / 309
7 Inherited attributes Definition of dependencies of program language constructs and their context Example: (type checking) Production D T L T int T real L L 1, id L id Semantic Rule L.in := T.type T.type := integer T.type := real L 1.in := L.in addtype(id.entry, L.in) addtype(id.entry, L.in) F. Wotawa TU Graz) Compiler Construction Summer term / 309
8 Inherited attributes Annotated parse tree real id 1, id 2, id 3 D T.type=real L.in=real real L.in=real, id 3 L.in=real, id 2 id 1 F. Wotawa TU Graz) Compiler Construction Summer term / 309
9 Dependency graphs Show dependencies between attributes Each rule is represented in the form b := f(c 1,..., c k ) Nodes correspond to attributes; edges to dependencies Definition: for each node n in the parse tree do for each attribute a of the grammar symbol at n do construct a node in the dependency graph for a for each node n in the parse tree do for each semantic rule b := f(c 1,..., c k ) associated with the production used at n do for i := 1 to k do construct an edge from the node for c i to the node for b F. Wotawa TU Graz) Compiler Construction Summer term / 309
10 Dependency graph Example D T type 4 5 in L 6 real in 7 L 8, id 3 3 entry in 9 L, 10 id 2 2 entry id 1 1 entry F. Wotawa TU Graz) Compiler Construction Summer term / 309
11 Topological sort Arrangement of m 1,..., m k nodes in a directed, acyclic graph where edges point from smaller nodes to bigger nodes If m i m j is an edge, then the node m i is smaller than the node m j Important for order in which the attributes are calculated Example (cont.): 1 a 4 := real 2 a 5 := a 4 3 addtype(id 3.entry, a 5 ) 4 a 7 := a 5 5 addtype(id 2.entry, a 7 ) 6 a 9 := a 7 7 addtype(id 1.entry, a 9 ) F. Wotawa TU Graz) Compiler Construction Summer term / 309
12 Example - syntax trees Abstract syntax tree = simplified form of a parse tree Operators and keywords are supplied to intermediate nodes by leaf nodes Productions with only one element can collapse Examples: if-then-else + B S S 1 2 * F. Wotawa TU Graz) Compiler Construction Summer term / 309
13 Syntax trees Expressions Functions (return value: pointer to new node): mknode(op, left, right): node label op, 2 child nodes left, right mkleaf(id, entry): leaf id, entry in symbol table entry mkleaf(num, val): leaf num, value val Syntax directed definition: Production E E 1 + T E E 1 T E T T (E) T id T num Semantic Rule E.nptr := mknode( +, E 1.nptr, T.nptr) E.nptr := mknode(, E 1.nptr, T.nptr) E.nptr := T.nptr T.nptr := E.nptr T.nptr := mkleaf(id, id.entry) T.nptr := mkleaf(num, num.val) F. Wotawa TU Graz) Compiler Construction Summer term / 309
14 Syntax trees Expressions (ex.) Syntax tree for a-4+c E nptr E nptr + T nptr E - T nptr id + T nptr num id - id to entry for c id num 4 to entry for a F. Wotawa TU Graz) Compiler Construction Summer term / 309
15 Evaluation of S-attributed definitions Attributed definition exclusively using synthesized attributes Evaluation using bottom-up parser (LR-parser) Idea: store attribute information on stack State Val Semantic rule: A.a := f(x.x, Y.y, Z.z) X X.x Production: A XY Z Y Y.y Before XY Z is reduced to A, value top Z Z.z of Z.z stored in val[top], Y.y stored in val[top 1], X.x in val[top 2] F. Wotawa TU Graz) Compiler Construction Summer term / 309
16 Example - S-attributed evaluation Calculator -example: Production Code Fragment L En print(val[top 1]) E E 1 + T val[ntop] := val[top 2] + val[top] E T T T 1 F val[ntop] := val[top 2] val[top] T F F (E) val[ntop] := val[top 1] F digit Code executed before reduction ntop = top r + 1, after reduction: top := ntop F. Wotawa TU Graz) Compiler Construction Summer term / 309
17 Result for 3*5+4n Input state val Production used 3*5+4n *5+4n 3 3 *5+4n F 3 F digit *5+4n T 3 T F 5+4n T * 3 +4n T * n T * F 3 5 F digit +4n T 15 T T F +4n E 15 E T 4n E + 15 n E n E + F 15 4 F digit n E + T 15 4 T F n E 19 E E + T E n 19 L 19 L En F. Wotawa TU Graz) Compiler Construction Summer term / 309
18 L-attributed definitions Definition: A syntax directed definition is L-attributed if each inherited attribute of X j, 1 j n, on the right side of A X 1,..., X n is only dependent on: 1 the attributes X 1,..., X j 1 to the left of X j and 2 the inherited attributes of A Each S-attributed grammar is a L-attributed grammar Evaluation using depth-first order procedure df visit(n : node) for each child m of n, from left to right do evaluate inherited attributes of m df visit(m) end evaluate synthesized attributes of n end F. Wotawa TU Graz) Compiler Construction Summer term / 309
19 Translation schemes Translation scheme = context-free language with attributes for grammar symbols and semantic actions which are placed on the right side of a production between grammar symbols and are confined within {} Example: T T 1 F {T.val := T 1.val F.val} If only synthesized attributes are used, the action is always placed at the end of the right side of a production Note: Actions may not access attributes which are not calculated yet (limits positions of semantic actions) F. Wotawa TU Graz) Compiler Construction Summer term / 309
20 Translation schemes (cont.) If both inherited and synthesized attributes are used the following needs to be taken into consideration: 1 An inherited attribute of a symbol on the right side of a production has to be calculated in an action which is positioned to the left of the symbol 2 An action may not reference a synthesized attribute belonging to a symbol which is positioned to the right of the action 3 A synthesized attribute of a nonterminal on the left side can only be calculated if all referenced attributes have already been calculated actions like these are usually placed at the end of the right side F. Wotawa TU Graz) Compiler Construction Summer term / 309
21 Example translation scheme S A 1 A 2 {A 1.in := 1; A 2.in := 2} A a {print(a.in)} Above grammar does not fulfill the three conditions for translation schemes The inherited attribute A.in is not yet defined at the point in time when it should be printed But: For each L-attributed grammar a translation scheme can be found which fulfills the three conditions, e.g.: S {A 1.in := 1} A 1 {A 2.in := 2} A 2 A a {print(a.in)} F. Wotawa TU Graz) Compiler Construction Summer term / 309
22 Top-down translation Removal of left recursions in translation scheme is necessary E E 1 + T {E.val := E 1.val + T.val} E E 1 T {E.val := E 1.val T.val} Example: E T {E.val := T.val} T (E) {T.val := E.val} T num {T.val := num.val} F. Wotawa TU Graz) Compiler Construction Summer term / 309
23 Example top-down translation E T {R.i := T.val} R {E.val := R.s} R + T {R 1.i := R.i + T.val} R 1 {R.s := R 1.s} R T {R 1.i := R.i T.val} R 1 {R.s := R 1.s} R ɛ {R.s := R.i} T ( E ) {T.val := E.val} T num {T.val := num.val} F. Wotawa TU Graz) Compiler Construction Summer term / 309
24 Evaluation of E T.val = 9 R.i = 9 num.val = 9 - T.val = 5 R.i = 4 num.val = 5 + T.val = 2 R.i = 6 num.val = 2 ε F. Wotawa TU Graz) Compiler Construction Summer term / 309
25 Summary transformation Given translation scheme: A A 1 Y {A.a := g(a 1.a, Y.y)} A X {A.a := f(x.x)} After removal of left recursions: A XR R Y R ɛ Transformed scheme: A X {R.i := f(x.x)} R {A.a := R.s} R Y {R 1.i := g(r.i, Y.y)} R 1 {R.s := R 1.s} R ɛ {R.s := R.i} F. Wotawa TU Graz) Compiler Construction Summer term / 309
26 Predictive parsing with schemes Input: syntax-directed translation scheme; Outp.: Syntax-directed translator 1 For each nonterminal A, construct a function that has a formal parameter for each inherited attribute of A and that returns the values of the synthesized attributes of A. This function has a local variable for each attribute of each grammar symbol that appears in a production for A. 2 As previously described (see predictive parsing), the code for nonterminal A decides what production to use based on the current input symbol. 3 The code for each production does the following (evaluation from left to right): 1 Token X with synthesized attribute x: Save the value of x in a variable X.x. Generate a call to match token X. 2 Nonterminal B: Generate c := B(b 1,..., b k ); b 1,..., b k variables for inherited attributes of B; c variable for synthesized attribute of B. 3 For an action, copy the code into the parser, replacing each reference to an attribute by the variable for that attribute. F. Wotawa TU Graz) Compiler Construction Summer term / 309
27 Example - predictive parsing Grammar: E T {R.i := T.val} R {E.val := R.s} R op T {R 1.i := mknode(op.lexeme, R.i, T.nptr)} R 1 {R.s := R 1.s} R ɛ {R.s := R.i} T ( E ) {T.val := E.val} T num {T.val := num.val} Functions: function E : node function R(i : node) : node function T : node F. Wotawa TU Graz) Compiler Construction Summer term / 309
28 Parsing procedure R Procedure without translation scheme procedure R() begin if lookahead = op then begin match(op); T (); return R() end else begin return; end end F. Wotawa TU Graz) Compiler Construction Summer term / 309
29 Parsing function R function R (i: node) : node var nptr, i1, s1, s: node; oplexeme : char; begin if lookahead = op then begin oplexeme := lexval; match(op); nptr := T (); i1 := mknode(oplexeme,i,nptr); s1 := R(i1); s := s 1 end else s := i; return s end F. Wotawa TU Graz) Compiler Construction Summer term / 309
30 Bottom-up with inherited attribute Implementation of L-attributed grammars in bottom-up parsers For LL(1)-grammars and many LR(1)-grammars Removal of embedding actions from translation schemes: Actions have to be placed at end of right side of a production Ensured by new marker nonterminals Example: E T R R +T {print( + )}R T {print( )}R ɛ T num{print(num.val)} E T R R +T MR T NR ɛ T num{print(num.val)} M ɛ{print( + )} N ɛ{print( )} F. Wotawa TU Graz) Compiler Construction Summer term / 309
31 Inherited attributes on the stack Idea: Production A XY, synthesized attribute X.x and inherited attribute Y.y Before a reduction (of X Y ), X.x is on the stack In the case of Y.y = X.x (copy action), the value of X.x can be used whenever the value of Y.y is required Example: Parser for variable declarations real p,q,r F. Wotawa TU Graz) Compiler Construction Summer term / 309
32 Variable declaration - example D T {L.in := T.type} L T int {T.type := integer} T real {T.type := real} L L 1 {L 1.in := L.in}, id {addtype(id.entry, L.in)} L id {addtype(id.entry, L.in)} T real type in D L p in L, in L, q r F. Wotawa TU Graz) Compiler Construction Summer term / 309
33 Calculation using the stack Input state Production Used real p,q,r p,q,r real p,q,r T T real,q,r T p,q,r T L L id q,r T L,,r T L, q,r T L L L, id r T L, T L, r T L L L, id D D T L Implementation: Production Code Fragment D T L T int val[top] := integer T real val[top] := real L L, id addtype(val[top], val[top 3]) L id addtype(val[top], val[top 1]) F. Wotawa TU Graz) Compiler Construction Summer term / 309
34 Problems Positions of attributes on the stack need to be known When the reduction C c is conducted, it is unknown Production Semantic Rule whether the value of C.i is S aac C.i := A.s located in val[top 1] or in S aabc C.i := A.s val[top 2]! It depends on C c C.s := g(c.i) whether a B is located on the stack. Solution: Introduction of a marker M: S aac C.i := A.s S aabmc M.i := A.s; C.i := M.s C c C.s := g(c.i) M ɛ M.s := M.i F. Wotawa TU Graz) Compiler Construction Summer term / 309
35 Problems (cont.) Simulation of semantic rules which are no copy actions Usage of marker! S aanc N.i := A.s; C.i := N.s S aac C.i := f(a.s) N ɛ N.s := f(n.i) F. Wotawa TU Graz) Compiler Construction Summer term / 309
36 Bottom-up parsing with calculation of inherited attributes Input: L-attributed definition (and LL(1)-grammar) Output: Parser, which calculates attribute values on stack 1 Assumptions: Each nonterminal A has an inherited attribute A.i, each grammar symbol X has a synthesized attribute X.s. If X is a terminal, then X.s is the lexical value of X (supplied by the lexical analyser). The values are stored on the stack in form of an array val. 2 For each production A X 1... X n create n new markers (nonterminals) M 1,..., M n and replace the production with A M 1 X 1... M n X n. Note: synthesized values for X i are stored in the val array entry, which belongs to X i. Inherited values X i.i are stored in entries which are associated to M i. 3 Invariant: The new inherited attribute A.i (if existing) is always directly beneath the position of M 1 within the val array. F. Wotawa TU Graz) Compiler Construction Summer term / 309
37 Simplifications Reduction of markers: 1 If X j has no inherited attribute, then no marker M j is required positions of attributes on the stack are shifting! 2 If X 1.i exists and is calculated by X 1.i = A.i, then M 1 is not required F. Wotawa TU Graz) Compiler Construction Summer term / 309
38 Removal of inherited attributes Replacement of inherited attributes by synthesized ones Not always possible Requires modification of grammar! Example: Declarations in Pascal D L : T T integer char L L, id id convert to: D idl L, idl : T T integer char F. Wotawa TU Graz) Compiler Construction Summer term / 309
39 Difficult syntax directed definition The following definition cannot be processed by bottom-up parsers using current approaches S L L.count := 0 L L 1 1 L 1.count := L.count + 1 L ɛ print(l.count) Reason: L ɛ receives the number of 1s by means of inheritance However, as L ɛ is used in the reduction first, no value is specified yet! F. Wotawa TU Graz) Compiler Construction Summer term / 309
40 Recursive evaluators Evaluation of attributes Based on parse tree Not possible in conjunction with parsing Order of nodes which are visited during evaluation is arbitrary For each nonterminal a translation function exists Extensions may visit nodes more than once Order of node visits needs to regard the following: 1 Each inherited attribute of a node has to be calculated before the node is visited 2 Synthesized attributes are calculated before the node is left (for the last time) Order is determined by dependencies F. Wotawa TU Graz) Compiler Construction Summer term / 309
41 Example Recursive evaluators Production A LM A QR i A s Semantic Rules L.i := l(a.i) M.i := m(l.s) A.s := f(m.s) R.i := r(a.i) Q.i := q(r.s) A.s := f(q.s) i L s i M s i Q s i R s i A s function A(n, ai) if production(n) = A LM then li := l(ai) ls := L(child(n, 1), li) mi := m(ls) ms := M(child(n, 2), mi) return f(ms) if production(n) = A QR then ri := r(ai) rs := R(child(n, 2), ri) qi := q(rs) qs := Q(child(n, 1), qi) return f(qs) F. Wotawa TU Graz) Compiler Construction Summer term / 309
42 PART 5 - TYPE CHECKING F. Wotawa TU Graz) Compiler Construction Summer term / 309
43 Static Program Checking Type Checks Check of the used type. Error if operands are incompatible with the used operator. Example: (real + int). Flow-of-Control Checks Check if the transfer of the program execution is possible. Example: break needs an enclosing loop. goto label needs a defined label. Uniqueness Checks Check if an object has been defined exactly once. Example: In Pascal each identifier must be unique. Name-related Checks In some languages, names (e.g. for procedures) are used which need to occur at a different location (e.g. at the end of a procedure). F. Wotawa TU Graz) Compiler Construction Summer term / 309
44 Tasks Check if the type system of the language is satisfied. Separate type checker is not always necessary. token stream parse tree parser type syntax tree checker intermediate code generator intermediate representation Typesystems (Examples): If both operands of the arithmetic operators of addition, subtraction and multiplication are of type integer, then the result is of type integer The result of the unary & operator is a pointer to the object referred to by the operand. If the type of the operand is..., the type of the result is pointer of.... F. Wotawa TU Graz) Compiler Construction Summer term / 309
45 Type Expressions A type expression is: 1 a Basic Type integer, boolean, char, and real as well as a special Basic Type type error or void. 2 the Type Name 3 a composite type in the form of: 1 Arrays. array(i, T ); set of indexes I, type T 2 Products. T 1 T 2 3 Records. record((n 1 T 1 )... (N k T k )); name N i, types T i 4 Pointers. pointer(t ) 5 Functions. T 1 T 2 4 and type variables. F. Wotawa TU Graz) Compiler Construction Summer term / 309
46 Types Examples type row = record address: integer; lexeme: array[1..15] of char end; var table: array[1..101] of row; row can be represented as record((address integer), (lexeme array(1..15, char))). function f(a,b: char): integer; is represented as: char char pointer(integer). F. Wotawa TU Graz) Compiler Construction Summer term / 309
47 Graphical Representation of Types as DAG (Directed Acyclic Graph) pointer char or as a tree integer pointer char char integer F. Wotawa TU Graz) Compiler Construction Summer term / 309
48 Typesystems Set of Rules specified using attributed grammars (or verbally) Static vs. Dynamic Checking of Types Sound Typesystem = static type checking is sufficient Language is strongly typed = the compiler guarantees that an accepted program runs without type errors. But some checks can only occur dynamically table: array[0..255] of char; i: integer; The correctness of the call table[i] in the program can not be checked by the compiler. Error Recovery is important (even for type errors) F. Wotawa TU Graz) Compiler Construction Summer term / 309
49 Type Checker Spec Language Definition: Example: P D ; E D D ; D id : T T char integer array [ num ] of T T E literal num id E mod E E[E] E key: integer ; key mod 1999 array [256] of char array( , char) F. Wotawa TU Graz) Compiler Construction Summer term / 309
50 1. Secure Type Info Production P D ; E D D ; D D id : T T char T integer T array [ num ] of T 1 T T 1 Semantic Rule {addtype( id.entry, T.type)} {T.type := char} {T.type := integer} {T.type := array(1... num.val, T 1.type)} {T.type := pointer(t 1.type)} F. Wotawa TU Graz) Compiler Construction Summer term / 309
51 2. Type Checking Expressions Production Semantic Rule E literal E.type := char E num E.type := integer E id E.type := lookup( id.entry) if E 1.type = integer and E E 1 mod E 2 E.type := E 2.type = integer then integer else type error if E 1.type = array(s, t) and E E 1 [E 2 ] E.type := E 2.type = integer then t { else type error if E1.type = pointer(t) then t E E 1 E.type := else type error F. Wotawa TU Graz) Compiler Construction Summer term / 309
52 3. Type Checking Statements Production Semantic { Rule if id.type = E.type then void S id := E S.type := { else type error if E.type = boolean then S1.type S if E then S 1 S.type := { else type error if E.type = boolean then S1.type S while E do S 1 S.type := else type error if S 1.type = void and S S 1 ; S 2 S.type := S 2.type = void then void else type error F. Wotawa TU Graz) Compiler Construction Summer term / 309
53 4. Type Checking Functions T T T Syntax Extension: E E ( E ) Type Extraction + Type Checking: Definition FunctionCall Production Semantic Rule T T 1 T 2 T.type := T 1.type T 2.type if E 1.type = s t and E E 1 ( E 2 ) E.type := E 2.type = s then t else type error Example: root : ((real real) real) real F. Wotawa TU Graz) Compiler Construction Summer term / 309
54 Type Equivalence When are types equivalent??? structural equivalence name equivalence F. Wotawa TU Graz) Compiler Construction Summer term / 309
55 Structural Equivalence (1) function sequiv(s, t) : boolean; begin (2) if s and t are the same basic type then (3) return true (4) else if s = array(s 1, s 2 ) and t = array(t 1, t 2 ) then (5) return s 1 = t 1 and sequiv(s 2, t 2 ) (6) else if s = s 1 s 2 and t = t 1 t 2 then (7) return sequiv(s 1, t 1 ) and sequiv(s 2, t 2 ) (8) else if s = pointer(s 1 ) and t = pointer(t 1 ) then (9) return sequiv(s 1, t 1 ) (10) else if s = s 1 s 2 and t = t 1 t 2 then (11) return sequiv(s 1, t 1 ) and sequiv(s 2, t 2 ) (12) else return false end F. Wotawa TU Graz) Compiler Construction Summer term / 309
56 Encoding of Type Expressions Expression as Bit vector (efficient storage and comparison) Example: Type Constructor Encoding Basic Type Encoding pointer 01 boolean 0000 array 10 char 0001 freturns 11 integer 0010 real 0011 Type expression Encoding char f returns(char) pointer(f returns(char)) array(pointer(f returns(char))) F. Wotawa TU Graz) Compiler Construction Summer term / 309
57 Name vs. structural Equivalence Example (Pascal Programm) type link = cell; var next: link; last : link; p : cell; q,r : cell; Do all variables have the same type? Depends on the typesystem (and the compiler in pascal!) Implementation of the above example creates implicit types (e.g. type np : cell for variable p). F. Wotawa TU Graz) Compiler Construction Summer term / 309
58 Cyclic Typedefinition (Example) type link = cell; cell = record info: integer; next: link; end; cell = record cell = record info integer next pointer info integer next pointer cell F. Wotawa TU Graz) Compiler Construction Summer term / 309
59 Type conversion / Coercions Statement of the problem: x+i with x as a real- and i as an integer variable. There exist only operators for (real + real) or (int + int) Type conversion necessary! x = int2real(i) Implicit (by the compiler) or explicit (by the programmer) possible Implicit = Coercion Loss of information should be prevented (int real but not real int). Performance!!! for I := 1 to N do X[I] := int2real(1) (PASCAL; X is an array of reals) needs 48,4 µs for I := 1 to N do X[I] := 1.0 needs only 5,4 µs. F. Wotawa TU Graz) Compiler Construction Summer term / 309
60 Type conversion - Semantic Rules (1) Production E id Semantic Rule E.type := lookup(id.entry) E.txt := id.entry E E 1 op E 2 E.type := if E 1.type = integer and E 2.type = integer then integer else if E 1.type = integer and E 2.type = real then real else if E 1.type = real and E 2.type = integer then real else if E 1.type = real and E 2.type = real then real else type error F. Wotawa TU Graz) Compiler Construction Summer term / 309
61 Type conversion - Semantic Rules (2) Production Semantic Rule E E 1 op E 2 E.txt := if E 1.type = integer and E 2.type = integer then E 1.txt E 2.txt else if E 1.type = integer and E 2.type = real then int2real(e 1.txt) E 2.txt else if E 1.type = real and E 2.type = integer then E 1.txt int2real(e 2.txt) else if E 1.type = real and E 2.type = real then E 1.txt E 2.txt else type error E num E.type := integer E.txt := val E num.num E.type := real E.txt := val F. Wotawa TU Graz) Compiler Construction Summer term / 309
62 Overloading Symbols with different meaning (dependent on application context) mathematics: + operator (integer, reals, complex numbers) ADA: ()-Expression for array access AND function calls Overloading is resolved, when the meaning is clear (operator identification) Overloading can often be resolved by the types of operands. F. Wotawa TU Graz) Compiler Construction Summer term / 309
63 Overloading - Possible Types Example (ADA): function "*"(i,j: integer) return complex; function "*"(i,j: complex) return complex; integer integer integer Possible types for * are: integer integer complex complex complex complex Assumption: 2,3,5 are integer 3*5 is either integer or complex. So 2 * (3 * 5) must be of type integer. (3*5)*z is of type complex, if z is of type complex. F. Wotawa TU Graz) Compiler Construction Summer term / 309
64 Handling Overloading Instead of a type the set of all possible types must be stored in an attribute. Attribute types! E E E id E E 1 ( E 2 ) Example: 3*5 E.types = E.types E.types = {lookup( id.entry)} E.types = {t s E 2.types s t E 1.types} E: {i,c} E: {i} *: E: {i} { i i i, i i c, 3: {i} 5: {i} c c c } F. Wotawa TU Graz) Compiler Construction Summer term / 309
65 Uniqueness of Types Expressions may only have one type (otherwise type error) Production Semantic Rule E E E.types := E.types E.unique := if E.types = {t} then t else type error E id E.types := {lookup( id.entry)} E.types := {s s E 2.types (s s ) E 1.types} t := E.unique E E 1 ( E 2 ) S := {s s E 2.types s t E 1.types} E 2.unique := if S = {s} then s else type error E 1.unique := if S = {s} then s t else type error F. Wotawa TU Graz) Compiler Construction Summer term / 309
66 Polymorphic Functions Polymorphic Function = Function, whose argument may have a arbitrary type Polymorphic refers to functions and operators Examples: Built-in operators for array-access, pointer manipulation Reason for polymorphism: Code can be used for various data structures Example: finding the length of lists (e.g. ML) fun length(lptr) = if null(lptr) then 0 else length(tl(lptr)) length([sun,mon,tue]), length([1,2,3,4]) not possible in PASCAL! F. Wotawa TU Graz) Compiler Construction Summer term / 309
67 Type Variables Variables, that allow us to talk about unknown types Note: Type Variables as greek letters α, β,.... Type Inference = problem of deciding the type of an expression taking into account the application (of the expression). Example type link cell; procedure mlist ( lptr : link; procedure p) begin while lptr <> nil do begin p(lptr); lptr := lptr.next end end; mlist: link procedure void p: link void F. Wotawa TU Graz) Compiler Construction Summer term / 309
68 Example - Type Inference Program: function deref(p); begin return p end; Derivation: 1 Type of p is β (Assumption) 2 From p follows that p must be a pointer. Therefore it holds: β = pointer(α). 3 Furthermore, we know that the type of p must be α. 4 Therefore, it follows: α : pointer(α) α is the type of the function deref. F. Wotawa TU Graz) Compiler Construction Summer term / 309
69 Language for Polymorphism Type expression of the form α.e(α) denotes a polymorph type. Language definition: P D ; E D D ; D id : Q Q type variable. Q T T T T T T (T ) unary constructor ( T ) basic type type variable E E ( E ) E, E id F. Wotawa TU Graz) Compiler Construction Summer term / 309
70 Example deref : α.pointer(α) α ; q : pointer(pointer(integer)) ; deref(deref(q)) apply : α 0 deref 0 : pointer(α 0) α 0 apply : α i deref i : pointer(α i) α i q : pointer(pointer(integer)) F. Wotawa TU Graz) Compiler Construction Summer term / 309
71 Differences in Type Handling In distinction from former type handling (without polymorphism): 1 Arguments of polymorph functions in an expression may have different types. 2 The concept of type equivalence is different. pointer(α) = pointer(pointer(integer))??? 3 Calculated Types must be used in further consequence. The effect of the unification of two expressions must be preserved. α is assigned the type t. If α is referenced elsewhere, t must be used! Terms: Substitution, Instances, Unification F. Wotawa TU Graz) Compiler Construction Summer term / 309
72 Substitution, Instances Substitution = function that maps type variables to type expressions. S : type variables type expressions Example: α pointer(integer) Application of a substitution: function subst(t : type expression) : type expression begin if t is a basic type then return t else if t is a variable then return S(t) else if t is t 1 t 2 then return subst(t 1 ) subst(t 2 ) end S(t)... Instance. We write s < t s is instance of t. F. Wotawa TU Graz) Compiler Construction Summer term / 309
73 Examples Instances: pointer(integer) < pointer(α) pointer(real) < pointer(α) integer integer < α α pointer(α) < β α < β No Instances: integer real substitution on Basic Types not possible integer real α α inconsistent replacement of α integer α α α all occurrences must be replaced F. Wotawa TU Graz) Compiler Construction Summer term / 309
74 Unification 2 types t 1, t 2 are unifiable if there exists a substitution S, so that S(t 1 ) = S(t 2 ) holds. In praxis, we are interested in the Most General Unifier (MGU). 1 S(t 1 ) = S(t 2 ) 2 Every substitution S with S (t 1 ) = S (t 2 ) must be an instance of S. F. Wotawa TU Graz) Compiler Construction Summer term / 309
75 Checking Polymorphic Functions 2 Functions: 1 f resh(t) replaces all Variables in the type expression t with new variables. A pointer to the node representing the new expression is returned. 2 unify(m, n) unifies the two expressions m and n. As a side effect the substitution is performed. Translation Schema: Production Semantic Rule p := mkleaf(newtypevar); E E 1 ( E 2 ) unify(e 1.type, mknode(, E 2.type, p)); E.type := p E E 1, E 2 E.type := mknode(, E 1.type, E 2.type) E id E.type := f resh( id.type) F. Wotawa TU Graz) Compiler Construction Summer term / 309
76 Example Type Checking apply : α 0 deref 0 : pointer(α 0 ) α 0 apply : α i deref i : pointer(α i ) α i q : pointer(pointer(integer)) β Summary (Bottom-up type detection): Expression : Type Substitution q : pointer(pointer(integer)) deref i : pointer(α i ) α i deref i (q) : pointer(integer) α i = pointer(integer) deref 0 : pointer(α 0 ) α 0 deref 0 (deref i (q)) : integer α 0 = integer F. Wotawa TU Graz) Compiler Construction Summer term / 309
77 Unification Algorithm Input. A graph and a pair of nodes m and n, which should be unified. Output. True, if the nodes can be unified, False otherwise. Method. A node is represented by the record [constructor, lef t, right, set], where set is the Set of equivalent nodes. A node of set is chosen as representative of this set. In the beginning, each set contains only the node itself. find(n) returns the representative node union(m,n) merges the equivalence sets. The new representative node is a node which does not correspond with a variable. If there exists no such node, a former representative node is chosen as the new one. F. Wotawa TU Graz) Compiler Construction Summer term / 309
78 Algorithm - Pseudocode function unify(m, n : node) : boolean begin s := find(m); t := find(n); if s = t then return true else if s and t are nodes that represent the same basic type then return true else if s is an op-node with children s 1, s 2 and t is an op-node with children t 1, t 2 then begin union(s, t); return unify(s 1, t 1) and unify(s 2, t 2) end else if s or t represents a variable then begin union(s, t) return true end else return false end F. Wotawa TU Graz) Compiler Construction Summer term / 309
79 Example Unification Type expression: ((α 1 α 2 ) list(α 3 )) list(α 2 ) ((α 3 α 4 ) list(α 3 )) α 5.: 1 : 9 : 2 list : 8 : 10 α 5 : 14 : 3 list : 6 : 11 list : 13 α 1 : 4 α 2 : 5 α 3 : 7 α 4 : 12 Question: unif y(1, 9) =? F. Wotawa TU Graz) Compiler Construction Summer term / 309
PART 4 - SYNTAX DIRECTED TRANSLATION. F. Wotawa TU Graz) Compiler Construction Summer term / 264
PART 4 - SYNTAX DIRECTED TRANSLATION F. Wotawa (IST @ TU Graz) Compiler Construction Summer term 2015 109 / 264 Setting Translation of context-free languages Information attributes of grammar symbols Values
More informationSyntax-Directed Translation
Syntax-Directed Translation 1 Syntax-Directed Translation 1. We associate information with the programming language constructs by attaching attributes to grammar symbols. 2. Values of these attributes
More informationCompilers. 5. Attributed Grammars. Laszlo Böszörmenyi Compilers Attributed Grammars - 1
Compilers 5. Attributed Grammars Laszlo Böszörmenyi Compilers Attributed Grammars - 1 Adding Attributes We connect the grammar rules with attributes E.g. to implement type-checking or code generation A
More informationSyntax-Directed Translation Part I
1 Syntax-Directed Translation Part I Chapter 5 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007-2011 2 The Structure of our Compiler Revisited Character stream
More informationType systems. Static typing
Type system A type is a set of values and operations on those values A language s type system specifies which operations are valid for a type The aim of type checking is to ensure that operations are used
More informationAbstract Syntax Tree
Abstract Syntax Tree Condensed form of parse tree, useful for representing language constructs. The production S if B then s1 else s2 may appear as if-then-else B s1 s2 1 Abstract Syntax tree Chain of
More informationSyntax-Directed Translation. Concepts Introduced in Chapter 5. Syntax-Directed Definitions
Concepts Introduced in Chapter 5 Syntax-Directed Definitions Translation Schemes Synthesized Attributes Inherited Attributes Dependency Graphs Syntax-Directed Translation Uses a grammar to direct the translation.
More informationType Checking. Error Checking
Type Checking Error Checking Dynamic checking takes place while program is running Static checking takes place during compilation Type checks Flow-of-control checks Uniqueness checks Name-related checks
More informationPrinciples of Programming Languages
Principles of Programming Languages h"p://www.di.unipi.it/~andrea/dida2ca/plp- 14/ Prof. Andrea Corradini Department of Computer Science, Pisa Lesson 11! Syntax- Directed Transla>on The Structure of the
More informationPrinciples of Programming Languages
Principles of Programming Languages h"p://www.di.unipi.it/~andrea/dida2ca/plp- 14/ Prof. Andrea Corradini Department of Computer Science, Pisa Lesson 10! Con:nua:on of the course Syntax- Directed Transla:on
More informationComputer Science Department Carlos III University of Madrid Leganés (Spain) David Griol Barres
Computer Science Department Carlos III University of Madrid Leganés (Spain) David Griol Barres dgriol@inf.uc3m.es Introduction He am a driver might be syntactically correct but semantically wrong. Semantic
More information[Syntax Directed Translation] Bikash Balami
1 [Syntax Directed Translation] Compiler Design and Construction (CSc 352) Compiled By Central Department of Computer Science and Information Technology (CDCSIT) Tribhuvan University, Kirtipur Kathmandu,
More informationSyntax-Directed Translation
Syntax-Directed Translation 1 Syntax-Directed Translation 2 Syntax-Directed Translation 3 Syntax-Directed Translation In a syntax-directed definition, each production A α is associated with a set of semantic
More informationSyntax-Directed Translation. Introduction
Syntax-Directed Translation Introduction Translation of languages guided by context-free grammars Attach attributes to the grammar symbols Values of the attributes are computed by semantic rules associated
More informationSyntax-Directed Translation
Syntax-Directed Translation What is syntax-directed translation? The compilation process is driven by the syntax. The semantic routines perform interpretation based on the syntax structure. Attaching attributes
More informationSyntax-Directed Translation Part II
Syntax-Directed Translation Part II Chapter 5 Slides adapted from : Robert van Engelen, Florida State University Alessandro Artale, Free University of Bolzano Syntax-Directed Translation Schemes Syntax-directed
More informationLecture Compiler Construction
Lecture Compiler Construction Franz Wotawa wotawa@ist.tugraz.at Institute for Software Technology Technische Universität Graz Inffeldgasse 16b/2, A-8010 Graz, Austria Summer term 2017 F. Wotawa (IST @
More informationFormal Languages and Compilers Lecture IX Semantic Analysis: Type Chec. Type Checking & Symbol Table
Formal Languages and Compilers Lecture IX Semantic Analysis: Type Checking & Symbol Table Free University of Bozen-Bolzano Faculty of Computer Science POS Building, Room: 2.03 artale@inf.unibz.it http://www.inf.unibz.it/
More informationStatic Checking and Type Systems
1 Static Checking and Type Systems Chapter 6 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007-2009 2 The Structure of our Compiler Revisited Character stream Lexical
More informationSyntax Directed Translation
Syntax Directed Translation Rupesh Nasre. CS3300 Compiler Design IIT Madras Aug 2015 Character stream Lexical Analyzer Machine-Independent Code Optimizer F r o n t e n d Token stream Syntax Analyzer Syntax
More informationΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών. Lecture 8a Syntax-directed Transla1on Elias Athanasopoulos
ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών Lecture 8a Syntax-directed Transla1on Elias Athanasopoulos eliasathan@cs.ucy.ac.cy Syntax-directed TranslaPon (SDT) Μετάφραση Κατευθυνόμενη από τη Σύνταξη We
More informationCompilers. Type checking. Yannis Smaragdakis, U. Athens (original slides by Sam
Compilers Type checking Yannis Smaragdakis, U. Athens (original slides by Sam Guyer@Tufts) Summary of parsing Parsing A solid foundation: context-free grammars A simple parser: LL(1) A more powerful parser:
More informationLecture 7: Type Systems and Symbol Tables. CS 540 George Mason University
Lecture 7: Type Systems and Symbol Tables CS 540 George Mason University Static Analysis Compilers examine code to find semantic problems. Easy: undeclared variables, tag matching Difficult: preventing
More informationCompiler Principle and Technology. Prof. Dongming LU April 15th, 2019
Compiler Principle and Technology Prof. Dongming LU April 15th, 2019 PART TWO 6. Semantic Analysis Contents Part One 6.1 Attributes and Attribute Grammars Part Two 6.2 Algorithms for Attribute Computation
More informationSyntax-Directed Translation
Syntax-Directed Translation Grammar symbols are associated with attributes to associate information with the programming language constructs that they represent. Values of these attributes are evaluated
More informationCompilerconstructie. najaar Rudy van Vliet kamer 124 Snellius, tel rvvliet(at)liacs.
Compilerconstructie najaar 2012 http://www.liacs.nl/home/rvvliet/coco/ Rudy van Vliet kamer 124 Snellius, tel. 071-527 5777 rvvliet(at)liacs.nl college 5, dinsdag 9 oktober 2012 Static Type Checking 1
More informationSyntax Directed Translation
Syntax Directed Translation Beyond syntax analysis An identifier named x has been recognized. Is x a scalar, array or function? How big is x? If x is a function, how many and what type of arguments does
More information5. Syntax-Directed Definitions & Type Analysis
5. Syntax-Directed Definitions & Type Analysis Eva Rose Kristoffer Rose NYU Courant Institute Compiler Construction (CSCI-GA.2130-001) http://cs.nyu.edu/courses/spring15/csci-ga.2130-001/lecture-5.pdf
More informationConcepts Introduced in Chapter 6
Concepts Introduced in Chapter 6 types of intermediate code representations translation of declarations arithmetic expressions boolean expressions flow-of-control statements backpatching EECS 665 Compiler
More informationChapter 6 Intermediate Code Generation
Chapter 6 Intermediate Code Generation Outline Variants of Syntax Trees Three-address code Types and declarations Translation of expressions Type checking Control flow Backpatching Introduction Intermediate
More informationConcepts Introduced in Chapter 6
Concepts Introduced in Chapter 6 types of intermediate code representations translation of declarations arithmetic expressions boolean expressions flow-of-control statements backpatching EECS 665 Compiler
More informationIntermediate Code Generation Part II
Intermediate Code Generation Part II Chapter 6: Type checking, Control Flow Slides adapted from : Robert van Engelen, Florida State University Static versus Dynamic Checking Static checking: the compiler
More informationType Checking. Outline. General properties of type systems. Types in programming languages. Notation for type rules.
Outline Type Checking General properties of type systems Types in programming languages Notation for type rules Logical rules of inference Common type rules 2 Static Checking Refers to the compile-time
More informationOutline. General properties of type systems. Types in programming languages. Notation for type rules. Common type rules. Logical rules of inference
Type Checking Outline General properties of type systems Types in programming languages Notation for type rules Logical rules of inference Common type rules 2 Static Checking Refers to the compile-time
More informationLecture 14 Sections Mon, Mar 2, 2009
Lecture 14 Sections 5.1-5.4 Hampden-Sydney College Mon, Mar 2, 2009 Outline 1 2 3 4 5 Parse A parse tree shows the grammatical structure of a statement. It includes all of the grammar symbols (terminals
More informationSyntax-Directed Translation
Syntax-Directed Translation ALSU Textbook Chapter 5.1 5.4, 4.8, 4.9 Tsan-sheng Hsu tshsu@iis.sinica.edu.tw http://www.iis.sinica.edu.tw/~tshsu 1 What is syntax-directed translation? Definition: The compilation
More informationCOP5621 Exam 3 - Spring 2005
COP5621 Exam 3 - Spring 2005 Name: (Please print) Put the answers on these sheets. Use additional sheets when necessary. Show how you derived your answer when applicable (this is required for full cred
More informationSemantic Analysis Attribute Grammars
Semantic Analysis Attribute Grammars Martin Sulzmann Martin Sulzmann Semantic Analysis Attribute Grammars 1 / 18 Syntax versus Semantics Syntax Analysis When is a program syntactically valid? Formalism:
More informationSyntax-Directed Translation. CS Compiler Design. SDD and SDT scheme. Example: SDD vs SDT scheme infix to postfix trans
Syntax-Directed Translation CS3300 - Compiler Design Syntax Directed Translation V. Krishna Nandivada IIT Madras Attach rules or program fragments to productions in a grammar. Syntax directed definition
More informationSemantic Analysis computes additional information related to the meaning of the program once the syntactic structure is known.
SEMANTIC ANALYSIS: Semantic Analysis computes additional information related to the meaning of the program once the syntactic structure is known. Parsing only verifies that the program consists of tokens
More informationSEMANTIC ANALYSIS TYPES AND DECLARATIONS
SEMANTIC ANALYSIS CS 403: Type Checking Stefan D. Bruda Winter 2015 Parsing only verifies that the program consists of tokens arranged in a syntactically valid combination now we move to check whether
More informationSyntactic Directed Translation
Syntactic Directed Translation Translation Schemes Copyright 2016, Pedro C. Diniz, all rights reserved. Students enrolled in the Compilers class at the University of Southern California have explicit permission
More information5. Semantic Analysis!
5. Semantic Analysis! Prof. O. Nierstrasz! Thanks to Jens Palsberg and Tony Hosking for their kind permission to reuse and adapt the CS132 and CS502 lecture notes.! http://www.cs.ucla.edu/~palsberg/! http://www.cs.purdue.edu/homes/hosking/!
More informationAbout the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Compiler Design
i About the Tutorial A compiler translates the codes written in one language to some other language without changing the meaning of the program. It is also expected that a compiler should make the target
More informationIntermediate Code Generation
Intermediate Code Generation In the analysis-synthesis model of a compiler, the front end analyzes a source program and creates an intermediate representation, from which the back end generates target
More informationCOMP 181. Agenda. Midterm topics. Today: type checking. Purpose of types. Type errors. Type checking
Agenda COMP 181 Type checking October 21, 2009 Next week OOPSLA: Object-oriented Programming Systems Languages and Applications One of the top PL conferences Monday (Oct 26 th ) In-class midterm Review
More informationA programming language requires two major definitions A simple one pass compiler
A programming language requires two major definitions A simple one pass compiler [Syntax: what the language looks like A context-free grammar written in BNF (Backus-Naur Form) usually suffices. [Semantics:
More informationType checking of statements We change the start rule from P D ; E to P D ; S and add the following rules for statements: S id := E
Type checking of statements We change the start rule from P D ; E to P D ; S and add the following rules for statements: S id := E if E then S while E do S S ; S Type checking of statements The purpose
More informationSyntax-directed translation. Context-sensitive analysis. What context-sensitive questions might the compiler ask?
Syntax-directed translation Context-sensitive analysis The compilation process is driven by the syntactic structure of the program as discovered by the parser Semantic routines: interpret meaning of the
More informationType Systems. Seman&cs. CMPT 379: Compilers Instructor: Anoop Sarkar. anoopsarkar.github.io/compilers-class
Type Systems Seman&cs CMPT 379: Compilers Instructor: Anoop Sarkar anoopsarkar.github.io/compilers-class 1 Equality of types Main seman&c tasks involve liveness analysis and checking equality Equality
More informationCA Compiler Construction
CA4003 - Compiler Construction Semantic Analysis David Sinclair Semantic Actions A compiler has to do more than just recognise if a sequence of characters forms a valid sentence in the language. It must
More informationSummary: Semantic Analysis
Summary: Semantic Analysis 1 Basic Concepts When SA is performed: Semantic Analysis may be performed: In a two-pass compiler: after syntactic analysis is finished, the semantic analyser if called with
More information10/18/18. Outline. Semantic Analysis. Two types of semantic rules. Syntax vs. Semantics. Static Semantics. Static Semantics.
Outline Semantic Analysis In Text: Chapter 3 Static semantics Attribute grammars Dynamic semantics Operational semantics Denotational semantics N. Meng, S. Arthur 2 Syntax vs. Semantics Syntax concerns
More informationAnatomy of a Compiler. Overview of Semantic Analysis. The Compiler So Far. Why a Separate Semantic Analysis?
Anatomy of a Compiler Program (character stream) Lexical Analyzer (Scanner) Syntax Analyzer (Parser) Semantic Analysis Parse Tree Intermediate Code Generator Intermediate Code Optimizer Code Generator
More informationSemantic analysis and intermediate representations. Which methods / formalisms are used in the various phases during the analysis?
Semantic analysis and intermediate representations Which methods / formalisms are used in the various phases during the analysis? The task of this phase is to check the "static semantics" and generate
More informationContext-sensitive analysis. Semantic Processing. Alternatives for semantic processing. Context-sensitive analysis
Semantic Processing The compilation process is driven by the syntactic structure of the program as discovered by the parser Semantic routines: interpret meaning of the program based on its syntactic structure
More informationType Checking and Type Inference
Type Checking and Type Inference Principles of Programming Languages CSE 307 1 Types in Programming Languages 2 Static Type Checking 3 Polymorphic Type Inference Version: 1.8 17:20:56 2014/08/25 Compiled
More informationSemantic Analysis and Type Checking
Semantic Analysis and Type Checking The compilation process is driven by the syntactic structure of the program as discovered by the parser Semantic routines: interpret meaning of the program based on
More informationSemantic Processing (Part 2)
Semantic Processing (Part 2) All Projects Due: Fray 12-2-05, Noon Final: Monday, December 5, 2005, 10:15-12:05 Comprehensive 1 Recursive Type Definitions type MyRec is record f1: integer; f2: array of
More informationPART 3 - SYNTAX ANALYSIS. F. Wotawa TU Graz) Compiler Construction Summer term / 309
PART 3 - SYNTAX ANALYSIS F. Wotawa (IST @ TU Graz) Compiler Construction Summer term 2016 64 / 309 Goals Definition of the syntax of a programming language using context free grammars Methods for parsing
More informationCOMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou
COMP-421 Compiler Design Presented by Dr Ioanna Dionysiou Administrative! Any questions about the syllabus?! Course Material available at www.cs.unic.ac.cy/ioanna! Next time reading assignment [ALSU07]
More informationA Simple Syntax-Directed Translator
Chapter 2 A Simple Syntax-Directed Translator 1-1 Introduction The analysis phase of a compiler breaks up a source program into constituent pieces and produces an internal representation for it, called
More informationCOP4020 Programming Languages. Semantics Prof. Robert van Engelen
COP4020 Programming Languages Semantics Prof. Robert van Engelen Overview Static semantics Dynamic semantics Attribute grammars Abstract syntax trees COP4020 Spring 2011 2 Static Semantics Syntax concerns
More informationIntermediate Code Generation
Intermediate Code Generation Rupesh Nasre. CS3300 Compiler Design IIT Madras July 2018 Character stream Lexical Analyzer Machine-Independent Code Code Optimizer F r o n t e n d Token stream Syntax Analyzer
More information5. Semantic Analysis. Mircea Lungu Oscar Nierstrasz
5. Semantic Analysis Mircea Lungu Oscar Nierstrasz Thanks to Jens Palsberg and Tony Hosking for their kind permission to reuse and adapt the CS132 and CS502 lecture notes. http://www.cs.ucla.edu/~palsberg/
More informationFaculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology
Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology exam Compiler Construction in4303 April 9, 2010 14.00-15.30 This exam (6 pages) consists of 52 True/False
More informationFaculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology
Faculty of Electrical Engineering, Mathematics, and Computer Science Delft University of Technology exam Compiler Construction in4020 July 5, 2007 14.00-15.30 This exam (8 pages) consists of 60 True/False
More informationAbstract Syntax Trees Synthetic and Inherited Attributes
Abstract Syntax Trees Synthetic and Inherited Attributes Lecture 22 Sections 5.1-5.2 Robb T. Koether Hampden-Sydney College Mon, Mar 16, 2015 Robb T. Koether (Hampden-Sydney College)Abstract Syntax TreesSynthetic
More informationIntermediate Code Generation
Intermediate Code Generation Rupesh Nasre. CS3300 Compiler Design IIT Madras Aug 2015 Character stream Lexical Analyzer Machine-Independent Code Optimizer F r o n t e n d Token stream Syntax Analyzer Syntax
More informationStatic Semantics. Winter /3/ Hal Perkins & UW CSE I-1
CSE 401 Compilers Static Semantics Hal Perkins Winter 2009 2/3/2009 2002-09 Hal Perkins & UW CSE I-1 Agenda Static semantics Types Symbol tables General ideas for now; details later for MiniJava project
More informationSemantic Analysis. CSE 307 Principles of Programming Languages Stony Brook University
Semantic Analysis CSE 307 Principles of Programming Languages Stony Brook University http://www.cs.stonybrook.edu/~cse307 1 Role of Semantic Analysis Syntax vs. Semantics: syntax concerns the form of a
More informationCS558 Programming Languages
CS558 Programming Languages Winter 2017 Lecture 7b Andrew Tolmach Portland State University 1994-2017 Values and Types We divide the universe of values according to types A type is a set of values and
More informationMore On Syntax Directed Translation
More On Syntax Directed Translation 1 Types of Attributes We have productions of the form: A X 1 X 2 X 3... X n with semantic rules of the form: b:= f(c 1, c 2, c 3,..., c n ) where b and the c s are attributes
More informationFormal Languages and Compilers Lecture X Intermediate Code Generation
Formal Languages and Compilers Lecture X Intermediate Code Generation Free University of Bozen-Bolzano Faculty of Computer Science POS Building, Room: 2.03 artale@inf.unibz.it http://www.inf.unibz.it/
More informationChapter 4. Action Routines
Chapter 4 Action Routines Syntax and Semantics In general: Syntax form Semantics meaning In programming languages: Syntax part of the language definition that can be described via a context-free grammar
More informationSYED AMMAL ENGINEERING COLLEGE (An ISO 9001:2008 Certified Institution) Dr. E.M. Abdullah Campus, Ramanathapuram
CS6660 COMPILER DESIGN Question Bank UNIT I-INTRODUCTION TO COMPILERS 1. Define compiler. 2. Differentiate compiler and interpreter. 3. What is a language processing system? 4. List four software tools
More informationCOP4020 Programming Languages. Semantics Robert van Engelen & Chris Lacher
COP4020 Programming Languages Semantics Robert van Engelen & Chris Lacher Overview Static semantics Dynamic semantics Attribute grammars Abstract syntax trees Static Semantics Syntax concerns the form
More informationThe compilation process is driven by the syntactic structure of the program as discovered by the parser
Semantic Analysis The compilation process is driven by the syntactic structure of the program as discovered by the parser Semantic routines: interpret meaning of the program based on its syntactic structure
More informationA simple syntax-directed
Syntax-directed is a grammaroriented compiling technique Programming languages: Syntax: what its programs look like? Semantic: what its programs mean? 1 A simple syntax-directed Lexical Syntax Character
More informationQuestion Bank. 10CS63:Compiler Design
Question Bank 10CS63:Compiler Design 1.Determine whether the following regular expressions define the same language? (ab)* and a*b* 2.List the properties of an operator grammar 3. Is macro processing a
More informationUNIT IV INTERMEDIATE CODE GENERATION
UNIT IV INTERMEDIATE CODE GENERATION 2 Marks 1. Draw syntax tree for the expression a=b*-c+b*-c 2. Explain postfix notation. It is the linearized representation of syntax tree.it is a list of nodes of
More informationLR Parsing LALR Parser Generators
LR Parsing LALR Parser Generators Outline Review of bottom-up parsing Computing the parsing DFA Using parser generators 2 Bottom-up Parsing (Review) A bottom-up parser rewrites the input string to the
More informationintermediate-code Generation
intermediate-code Generation }sequence of intermediate representations source program High Level intermediate Representation Low Level intermediate Representation Target Code e.g. C programming language
More informationType Checking. CS308 Compiler Theory 1
Type Checking CS308 Compiler Theory 1 Type Checking A compiler has to do semantic checks in addition to syntactic checks. Semantic Checks Static done during compilation Dynamic done during run-time Type
More informationEvaluation of Semantic Actions in Predictive Non- Recursive Parsing
Evaluation of Semantic Actions in Predictive Non- Recursive Parsing José L. Fuertes, Aurora Pérez Dept. LSIIS School of Computing. Technical University of Madrid Madrid, Spain Abstract To implement a syntax-directed
More information5. Semantic Analysis. Mircea Lungu Oscar Nierstrasz
5. Semantic Analysis Mircea Lungu Oscar Nierstrasz Thanks to Jens Palsberg and Tony Hosking for their kind permission to reuse and adapt the CS132 and CS502 lecture notes. http://www.cs.ucla.edu/~palsberg/
More informationThere is a level of correctness that is deeper than grammar. There is a level of correctness that is deeper than grammar
Beyond Syntax There is a level of correctness that is deeper than grammar fie(a,b,c,d) int a, b, c, d; { } What is wrong with this program? (let me count the ways ) fee() { int f[3],g[0], h, i, j, k; char
More informationThe Compiler So Far. Lexical analysis Detects inputs with illegal tokens. Overview of Semantic Analysis
The Compiler So Far Overview of Semantic Analysis Adapted from Lectures by Profs. Alex Aiken and George Necula (UCB) Lexical analysis Detects inputs with illegal tokens Parsing Detects inputs with ill-formed
More informationCSE302: Compiler Design
CSE302: Compiler Design Instructor: Dr. Liang Cheng Department of Computer Science and Engineering P.C. Rossin College of Engineering & Applied Science Lehigh University April 3, 2007 Outline Recap Syntax-directed
More informationSection A. A grammar that produces more than one parse tree for some sentences is said to be ambiguous.
Section A 1. What do you meant by parser and its types? A parser for grammar G is a program that takes as input a string w and produces as output either a parse tree for w, if w is a sentence of G, or
More informationCOP4020 Spring 2011 Midterm Exam
COP4020 Spring 2011 Midterm Exam Name: (Please print Put the answers on these sheets. Use additional sheets when necessary or write on the back. Show how you derived your answer (this is required for full
More information10/26/17. Attribute Evaluation Order. Attribute Grammar for CE LL(1) CFG. Attribute Grammar for Constant Expressions based on LL(1) CFG
Attribute Evaluation Order Determining attribute 2 expected_type evaluation order actual_type actual_type for any attribute grammar is a 5 complex problem, 1 [1] [2] requiring
More informationCS 314 Principles of Programming Languages
CS 314 Principles of Programming Languages Lecture 5: Syntax Analysis (Parsing) Zheng (Eddy) Zhang Rutgers University January 31, 2018 Class Information Homework 1 is being graded now. The sample solution
More informationWe now allow any grammar symbol X to have attributes. The attribute a of symbol X is denoted X.a
Attribute Grammars Attribute Grammars were invented by Don Knuth as a way to unify all of the stages of compiling into one. They give a formal way to pass semantic information (types, values, etc.) around
More informationSymbol Tables. For compile-time efficiency, compilers often use a symbol table: associates lexical names (symbols) with their attributes
For compile-time efficiency, compilers often use a symbol table: associates lexical names (symbols) with their attributes What items should be entered? variable names defined constants procedure and function
More informationCOP4020 Programming Languages. Syntax Prof. Robert van Engelen
COP4020 Programming Languages Syntax Prof. Robert van Engelen Overview n Tokens and regular expressions n Syntax and context-free grammars n Grammar derivations n More about parse trees n Top-down and
More informationFunctional Programming. Pure Functional Programming
Functional Programming Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends only on the values of its sub-expressions (if any).
More informationCS 2210 Sample Midterm. 1. Determine if each of the following claims is true (T) or false (F).
CS 2210 Sample Midterm 1. Determine if each of the following claims is true (T) or false (F). F A language consists of a set of strings, its grammar structure, and a set of operations. (Note: a language
More informationType Checking. Chapter 6, Section 6.3, 6.5
Type Checking Chapter 6, Section 6.3, 6.5 Inside the Compiler: Front End Lexical analyzer (aka scanner) Converts ASCII or Unicode to a stream of tokens Syntax analyzer (aka parser) Creates a parse tree
More informationCompiler Lab. Introduction to tools Lex and Yacc
Compiler Lab Introduction to tools Lex and Yacc Assignment1 Implement a simple calculator with tokens recognized using Lex/Flex and parsing and semantic actions done using Yacc/Bison. Calculator Input:
More information