SlideShare a Scribd company logo
Compiler Construction
Week 11
Intermediate Code Generation
Semantic Analysis
2
Overview
 Intermediate representations span the gap between
the source and target languages:
 closer to target language;
 (more or less) machine independent;
 allows many optimizations to be done in a machine-independent way.
3
Types of Intermediate Languages
 High Level Representations (e.g., syntax trees):
 closer to the source language
 easy to generate from an input program
 code optimizations may not be straightforward.
 Low Level Representations (e.g., 3-address
code):
 closer to the target machine;
 easier for optimizations, final code generation;
4
Syntax Trees
A syntax tree shows the structure of a program by abstracting
away irrelevant details from a parse tree.
 Each node represents a computation to be performed;
 The children of the node represents what that computation is
performed on.
Syntax trees separate parsing from subsequent processing.
5
Syntax Trees: Example
Grammar :
E  E + T | T
T  T * F | F
F  ( E ) | id
Input: id + id * id
Parse tree:
Syntax tree:
6
Syntax Trees: Structure
 Expressions:
 leaves: identifiers or constants;
 internal nodes are labeled with operators;
 the children of a node are its operands.
 Statements:
 a node’s label indicates what kind of
statement it is;
 the children correspond to the components
of the statement.
7
Constructing Syntax Trees
General Idea: construct bottom-up using
synthesized attributes.
E → E + E { $$ = mkTree(PLUS, $1, $3); }
S → if ‘(‘ E ‘)’ S OptElse { $$ = mkTree(IF, $3, $5, $6); }
OptElse → else S { $$ = $2; }
| /* epsilon */ { $$ = NULL; }
S → while ‘(‘ E ‘)’ S { $$ = mkTree(WHILE, $3, $5); }
mkTree(NodeType, Child1, Child2, …) allocates space for the tree node and fills in its node
type as well as its children.
8
Three Address Code
 Low-level IR
 instructions are of the form ‘x = y op z,’ where x,
y, z are variables, constants, or “temporaries”.
 At most one operator allowed on RHS, so no
‘built-up” expressions.
Instead, expressions are computed using temporaries
(compiler-generated variables).
9
Three Address Code: Example
 Source:
if ( x + y*z > x*y + z)
a = 0;
 Three Address Code:
tmp1 = y*z
tmp2 = x+tmp1 // x + y*z
tmp3 = x*y
tmp4 = tmp3+z // x*y + z
if (tmp2 <= tmp4)
a = 0;
CSc 453: Intermediate Code Generation 10
An Intermediate Instruction Set
 Assignment:
 x = y op z (op binary)
 x = op y (op unary);
 x = y
 Jumps:
 if ( x op y ) goto L (L a label);
 goto L
 Pointer and indexed
assignments:
 x = y[ z ]
 y[ z ] = x
 x = &y
 x = *y
 *y = x.
 Procedure call/return:
 param x, k (x is the kth param)
 retval x
 call p
 enter p
 leave p
 return
 retrieve x
 Type Conversion:
 x = cvt_A_to_B y (A, B base types)
e.g.: cvt_int_to_float
 Miscellaneous
 label L
11
Three Address Code: Representation
 Each instruction represented as a structure called a
quadruple (or “quad”):
 contains info about the operation, up to 3 operands.
 for operands: use a bit to indicate whether constant or ST pointer.
E.g.:
x = y + z if ( x  y ) goto L
12
Code Generation: Approach
 function prototypes, global declarations:
 save information in the global symbol table.
 function definitions:
 function name, return type, argument type and number saved
in global table (if not already there);
 process formals, local declarations into local symbol table;
 process body:
 construct syntax tree;
 traverse syntax tree and generate code for the function;
 deallocate syntax tree and local symbol table.
13
Code Generation: Approach
codeGen_stmt(synTree_node S)
{
switch (S.nodetype) {
case FOR: … ; break;
case WHILE : … ; break;
case IF: … ; break;
case ‘=‘ : … ; break;
…
}
codeGen_expr(synTree_node E)
{
switch (E.nodetype) {
case ‘+’: … ; break;
case ‘*’ : … ; break;
case ‘–’: … ; break;
case ‘/’ : … ; break;
…
}
Recursively traverse syntax tree:
 Node type determines action at each node;
 Code for each node is a (doubly linked) list of three-address instructions;
 Generate code for each node after processing its children
recursively process the children,
then generate code for this node
and glue it all together.
14
Intermediate Code Generation
supporting Routines:
 struct symtab_entry *newtemp(typename t)
creates a symbol table entry for new temporary variable each
time it is called, and returns a pointer to this ST entry.
 struct instr *newlabel()
returns a new label instruction each time it is called.
 struct instr *newinstr(arg1, arg2, …)
creates a new instruction, fills it in with the arguments supplied,
and returns a pointer to the result.
15
Intermediate Code Generation…
 struct symtab_entry *newtemp( t )
{
struct symtab_entry *ntmp = malloc( … ); /* check: ntmp == NULL? */
ntmp->name = …create a new name that doesn’t conflict…
ntmp->type = t;
ntmp->scope = LOCAL;
return ntmp;
}
 struct instr *newinstr(opType, src1, src2, dest)
{
struct instr *ninstr = malloc( … ); /* check: ninstr == NULL? */
ninstr->op = opType;
ninstr->src1 = src1; ninstr->src2 = src2; ninstr->dest = dest;
return ninstr;
}
16
Intermediate Code for a Function
Code generated for a function f:
 begin with ‘enter f ’, where f is a pointer to the function’s
symbol table entry:
 this allocates the function’s activation record;
 activation record size obtained from f ’s symbol table information;
 this is followed by code for the function body;
 generated using codeGen_stmt(…)
 each return in the body (incl. any implicit return at the end of
the function body) are translated to the code
leave f /* clean up: f a pointer to the function’s symbol table entry */
return /* + associated return value, if any */
17
Simple Expressions
Syntax tree node for expressions augmented with the
following fields:
 type: the type of the expression (or “error”);
 code: a list of intermediate code instructions for evaluating the expression.
 place: the location where the value of the expression will be kept at runtime:
18
Simple Expressions
Syntax tree node for expressions augmented with the
following fields:
 type: the type of the expression (or “error”);
 code: a list of intermediate code instructions for evaluating the expression.
 place: the location where the value of the expression will be kept at runtime:
 When generating intermediate code, this just refers to a symbol table entry for a
variable or temporary that will hold that value;
 The variable/temporary is mapped to an actual memory location when going from
intermediate to final code.
19
Simple Expressions 1
Syntax tree node E Action during intermediate code generation
codeGen_expr(E)
{ /* E.nodetype == INTCON; */
E.place = newtemp(E.type);
E.code = ‘E.place = intcon.val’;
}
codeGen_expr(E)
{ /* E.nodetype == ID; */
/* E.place is just the location of id (nothing more to do) */
E.code = NULL;
}
id
E
intcon
E
20
Simple Expressions 2
Syntax tree node E Action during intermediate code generation
codeGen_expr(E)
{
/* E.nodetype == UNARY_MINUS */
codeGen_expr(E1); /* recursively traverse E1, generate code for it */
E.place = newtemp( E.type ); /* allocate space to hold E’s value */
E.code = E1.code  newinstr(UMINUS, E1.place, NULL, E.place);
}
codeGen_expr(E)
{
/* E.nodetype == ‘+’ … other binary operators are similar */
codeGen_expr(E1);
codeGen_expr(E2); /* generate code for E1 and E2 */
E.place = newtemp( E.type ); /* allocate space to hold E’s value */
E.code = E1.code  E2.code  newinstr(PLUS, E1.place, E2.place, E.place );
}
–
E1
+
E1 E2
E
E
Semantic Analysis
From Code Form To Program Meaning
Compiler or Interpreter
Translation Execution
Source Code
Target Code
Interpre-
tation
Specification of Programming Languages
 PLs require precise definitions (i.e. no
ambiguity)
 Language form (Syntax)
 Language meaning (Semantics)
 Consequently, PLs are specified using formal
notation:
 Formal syntax
 Tokens
 Grammar
 Formal semantics
 Attribute Grammars (static semantics)
 Dynamic Semantics
The Semantic Analyzer
 The principal job of the semantic analyzer is
to enforce static semantic rules.
 In general, anything that requires the
compiler to compare things that are separate
by a long distance or to count things ends up
being a matter of semantics.
 The semantic analyzer also commonly
constructs a syntax tree (usually first), and
much of the information it gathers is needed
by the code generator.
Attribute Grammars
 Context-Free Grammars (CFGs) are used to
specify the syntax of programming
languages
 E.g. arithmetic expressions
• How do we tie these rules to
mathematical concepts?
• Attribute grammars are annotated
CFGs in which annotations are used to
establish meaning relationships among
symbols
– Annotations are also known as
decorations
Attribute Grammars
Example
 Each grammar
symbols has a set of
attributes
 E.g. the value of E1 is the
attribute E1.val
 Each grammar rule
has a set of rules over
the symbol attributes.
Attribute Flow
 Context-free grammars are not tied to an
specific parsing order
 E.g. Recursive descent, LR parsing
 Attribute grammars are not tied to an specific
evaluation order
 This evaluation is known as the annotation or decoration of
the parse tree
Attribute Flow
Example
 The figure shows the
result of annotating
the parse tree for
(1+3)*2
 Each symbols has at
most one attribute
shown in the
corresponding box
 Numerical value in this
example
 Operator symbols have
no value
 Arrows represent
attribute flow
Attribute Flow
Example
Static and Dynamic Semantics
 Attribute grammars add basic semantic rules
to the specification of a language
 They specify static semantics
 But they are limited to the semantic form that
can be checked at compile time
 Other semantic properties cannot be checked
at compile time
 They are described using dynamic semantics
Dynamic Semantics
 Use to formally specify the behavior of a
programming language
 Semantic-based error detection
 Correctness proofs
 There is not a universally accepted notation
 Operational semantics
 Executing statements that represent changes in the state of a real
or simulated machine
 Axiomatic semantics
 Using predicate calculus (pre and post-conditions)
 Denotational semantics
 Using recursive function theory

More Related Content

PPT
5_IntermediateCodeGeneration.ppt
PPT
Compiler chapter six .ppt course material
PPT
Lecture 21 22
PPT
Interm codegen
PPTX
Intermediate Code Generation.pptx
PPTX
1 cc
PPTX
Chapter 6 - Intermediate Languages.pptxjfjgj
PPT
Intermediate code generation
5_IntermediateCodeGeneration.ppt
Compiler chapter six .ppt course material
Lecture 21 22
Interm codegen
Intermediate Code Generation.pptx
1 cc
Chapter 6 - Intermediate Languages.pptxjfjgj
Intermediate code generation

Similar to CC Week 11.ppt (20)

PDF
Compiler Design Introduction
PPTX
complier design unit 4 for helping students
PPT
introduction to computer vision and image processing
PPT
Symbol Table, Error Handler & Code Generation
PPTX
System Programming Overview
PPTX
The Phases of a Compiler
PPT
Introduction to Compiler
DOCX
Compiler Design Material
PPT
7068458.ppt
DOCX
Dineshmaterial1 091225091539-phpapp02
PDF
Lecture 01 introduction to compiler
PDF
Intermediate code generation
PDF
Introduction to compilers
PDF
lec00-Introduction.pdf
PPTX
Compiler Design Introduction
PPT
Cpcs302 1
PPTX
1 compiler outline
PPTX
CSC 204 PASSES IN COMPILER CONSTURCTION.pptx
PPT
phases of a compiler
PPTX
role of lexical anaysis
Compiler Design Introduction
complier design unit 4 for helping students
introduction to computer vision and image processing
Symbol Table, Error Handler & Code Generation
System Programming Overview
The Phases of a Compiler
Introduction to Compiler
Compiler Design Material
7068458.ppt
Dineshmaterial1 091225091539-phpapp02
Lecture 01 introduction to compiler
Intermediate code generation
Introduction to compilers
lec00-Introduction.pdf
Compiler Design Introduction
Cpcs302 1
1 compiler outline
CSC 204 PASSES IN COMPILER CONSTURCTION.pptx
phases of a compiler
role of lexical anaysis
Ad

More from KamranAli649587 (20)

PPTX
Register in assembly language and counters .pptx
PPTX
lecture introduction to assembly language .pptx
PPT
sect7--ch9--legal_priv_ethical_issues.ppt
PPTX
bba system analysis and algorithm development.pptx
PPTX
assignment and database algorithmno 4.pptx
PPT
Data structure and algorithm.lect-03.ppt
PDF
Ict in healthcare and well being use how it will be benefit
PPTX
Angular is and php fram work as idea of secience
PDF
UNIT-2-liang-barsky-clipping-algorithm-KM.pdf
PPT
Data design and analysis of computing tools
PPTX
graphs data structure and algorithm link list
PPT
lecture10 date structure types of graph and terminology
PPTX
Encoder-and-decoder.pptx
PPTX
Radio propagation model...pptx
PPT
Loops_and_FunctionsWeek4_0.ppt
PPT
Lecture+06-TypesVars.ppt
PPT
C++InputOutput.PPT
PDF
radiopropagation-140328202308-phpapp01.pdf
PPTX
cluster.pptx
PPT
Week11-EvaluationMethods.ppt
Register in assembly language and counters .pptx
lecture introduction to assembly language .pptx
sect7--ch9--legal_priv_ethical_issues.ppt
bba system analysis and algorithm development.pptx
assignment and database algorithmno 4.pptx
Data structure and algorithm.lect-03.ppt
Ict in healthcare and well being use how it will be benefit
Angular is and php fram work as idea of secience
UNIT-2-liang-barsky-clipping-algorithm-KM.pdf
Data design and analysis of computing tools
graphs data structure and algorithm link list
lecture10 date structure types of graph and terminology
Encoder-and-decoder.pptx
Radio propagation model...pptx
Loops_and_FunctionsWeek4_0.ppt
Lecture+06-TypesVars.ppt
C++InputOutput.PPT
radiopropagation-140328202308-phpapp01.pdf
cluster.pptx
Week11-EvaluationMethods.ppt
Ad

Recently uploaded (20)

PPTX
UNIT 4 Total Quality Management .pptx
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PDF
composite construction of structures.pdf
PPTX
Sustainable Sites - Green Building Construction
DOCX
573137875-Attendance-Management-System-original
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PPTX
web development for engineering and engineering
PPTX
Construction Project Organization Group 2.pptx
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PPT
Mechanical Engineering MATERIALS Selection
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
Geodesy 1.pptx...............................................
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PDF
Well-logging-methods_new................
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
UNIT 4 Total Quality Management .pptx
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
Foundation to blockchain - A guide to Blockchain Tech
composite construction of structures.pdf
Sustainable Sites - Green Building Construction
573137875-Attendance-Management-System-original
R24 SURVEYING LAB MANUAL for civil enggi
web development for engineering and engineering
Construction Project Organization Group 2.pptx
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Model Code of Practice - Construction Work - 21102022 .pdf
Mechanical Engineering MATERIALS Selection
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Geodesy 1.pptx...............................................
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Well-logging-methods_new................
Embodied AI: Ushering in the Next Era of Intelligent Systems

CC Week 11.ppt

  • 1. Compiler Construction Week 11 Intermediate Code Generation Semantic Analysis
  • 2. 2 Overview  Intermediate representations span the gap between the source and target languages:  closer to target language;  (more or less) machine independent;  allows many optimizations to be done in a machine-independent way.
  • 3. 3 Types of Intermediate Languages  High Level Representations (e.g., syntax trees):  closer to the source language  easy to generate from an input program  code optimizations may not be straightforward.  Low Level Representations (e.g., 3-address code):  closer to the target machine;  easier for optimizations, final code generation;
  • 4. 4 Syntax Trees A syntax tree shows the structure of a program by abstracting away irrelevant details from a parse tree.  Each node represents a computation to be performed;  The children of the node represents what that computation is performed on. Syntax trees separate parsing from subsequent processing.
  • 5. 5 Syntax Trees: Example Grammar : E  E + T | T T  T * F | F F  ( E ) | id Input: id + id * id Parse tree: Syntax tree:
  • 6. 6 Syntax Trees: Structure  Expressions:  leaves: identifiers or constants;  internal nodes are labeled with operators;  the children of a node are its operands.  Statements:  a node’s label indicates what kind of statement it is;  the children correspond to the components of the statement.
  • 7. 7 Constructing Syntax Trees General Idea: construct bottom-up using synthesized attributes. E → E + E { $$ = mkTree(PLUS, $1, $3); } S → if ‘(‘ E ‘)’ S OptElse { $$ = mkTree(IF, $3, $5, $6); } OptElse → else S { $$ = $2; } | /* epsilon */ { $$ = NULL; } S → while ‘(‘ E ‘)’ S { $$ = mkTree(WHILE, $3, $5); } mkTree(NodeType, Child1, Child2, …) allocates space for the tree node and fills in its node type as well as its children.
  • 8. 8 Three Address Code  Low-level IR  instructions are of the form ‘x = y op z,’ where x, y, z are variables, constants, or “temporaries”.  At most one operator allowed on RHS, so no ‘built-up” expressions. Instead, expressions are computed using temporaries (compiler-generated variables).
  • 9. 9 Three Address Code: Example  Source: if ( x + y*z > x*y + z) a = 0;  Three Address Code: tmp1 = y*z tmp2 = x+tmp1 // x + y*z tmp3 = x*y tmp4 = tmp3+z // x*y + z if (tmp2 <= tmp4) a = 0;
  • 10. CSc 453: Intermediate Code Generation 10 An Intermediate Instruction Set  Assignment:  x = y op z (op binary)  x = op y (op unary);  x = y  Jumps:  if ( x op y ) goto L (L a label);  goto L  Pointer and indexed assignments:  x = y[ z ]  y[ z ] = x  x = &y  x = *y  *y = x.  Procedure call/return:  param x, k (x is the kth param)  retval x  call p  enter p  leave p  return  retrieve x  Type Conversion:  x = cvt_A_to_B y (A, B base types) e.g.: cvt_int_to_float  Miscellaneous  label L
  • 11. 11 Three Address Code: Representation  Each instruction represented as a structure called a quadruple (or “quad”):  contains info about the operation, up to 3 operands.  for operands: use a bit to indicate whether constant or ST pointer. E.g.: x = y + z if ( x  y ) goto L
  • 12. 12 Code Generation: Approach  function prototypes, global declarations:  save information in the global symbol table.  function definitions:  function name, return type, argument type and number saved in global table (if not already there);  process formals, local declarations into local symbol table;  process body:  construct syntax tree;  traverse syntax tree and generate code for the function;  deallocate syntax tree and local symbol table.
  • 13. 13 Code Generation: Approach codeGen_stmt(synTree_node S) { switch (S.nodetype) { case FOR: … ; break; case WHILE : … ; break; case IF: … ; break; case ‘=‘ : … ; break; … } codeGen_expr(synTree_node E) { switch (E.nodetype) { case ‘+’: … ; break; case ‘*’ : … ; break; case ‘–’: … ; break; case ‘/’ : … ; break; … } Recursively traverse syntax tree:  Node type determines action at each node;  Code for each node is a (doubly linked) list of three-address instructions;  Generate code for each node after processing its children recursively process the children, then generate code for this node and glue it all together.
  • 14. 14 Intermediate Code Generation supporting Routines:  struct symtab_entry *newtemp(typename t) creates a symbol table entry for new temporary variable each time it is called, and returns a pointer to this ST entry.  struct instr *newlabel() returns a new label instruction each time it is called.  struct instr *newinstr(arg1, arg2, …) creates a new instruction, fills it in with the arguments supplied, and returns a pointer to the result.
  • 15. 15 Intermediate Code Generation…  struct symtab_entry *newtemp( t ) { struct symtab_entry *ntmp = malloc( … ); /* check: ntmp == NULL? */ ntmp->name = …create a new name that doesn’t conflict… ntmp->type = t; ntmp->scope = LOCAL; return ntmp; }  struct instr *newinstr(opType, src1, src2, dest) { struct instr *ninstr = malloc( … ); /* check: ninstr == NULL? */ ninstr->op = opType; ninstr->src1 = src1; ninstr->src2 = src2; ninstr->dest = dest; return ninstr; }
  • 16. 16 Intermediate Code for a Function Code generated for a function f:  begin with ‘enter f ’, where f is a pointer to the function’s symbol table entry:  this allocates the function’s activation record;  activation record size obtained from f ’s symbol table information;  this is followed by code for the function body;  generated using codeGen_stmt(…)  each return in the body (incl. any implicit return at the end of the function body) are translated to the code leave f /* clean up: f a pointer to the function’s symbol table entry */ return /* + associated return value, if any */
  • 17. 17 Simple Expressions Syntax tree node for expressions augmented with the following fields:  type: the type of the expression (or “error”);  code: a list of intermediate code instructions for evaluating the expression.  place: the location where the value of the expression will be kept at runtime:
  • 18. 18 Simple Expressions Syntax tree node for expressions augmented with the following fields:  type: the type of the expression (or “error”);  code: a list of intermediate code instructions for evaluating the expression.  place: the location where the value of the expression will be kept at runtime:  When generating intermediate code, this just refers to a symbol table entry for a variable or temporary that will hold that value;  The variable/temporary is mapped to an actual memory location when going from intermediate to final code.
  • 19. 19 Simple Expressions 1 Syntax tree node E Action during intermediate code generation codeGen_expr(E) { /* E.nodetype == INTCON; */ E.place = newtemp(E.type); E.code = ‘E.place = intcon.val’; } codeGen_expr(E) { /* E.nodetype == ID; */ /* E.place is just the location of id (nothing more to do) */ E.code = NULL; } id E intcon E
  • 20. 20 Simple Expressions 2 Syntax tree node E Action during intermediate code generation codeGen_expr(E) { /* E.nodetype == UNARY_MINUS */ codeGen_expr(E1); /* recursively traverse E1, generate code for it */ E.place = newtemp( E.type ); /* allocate space to hold E’s value */ E.code = E1.code  newinstr(UMINUS, E1.place, NULL, E.place); } codeGen_expr(E) { /* E.nodetype == ‘+’ … other binary operators are similar */ codeGen_expr(E1); codeGen_expr(E2); /* generate code for E1 and E2 */ E.place = newtemp( E.type ); /* allocate space to hold E’s value */ E.code = E1.code  E2.code  newinstr(PLUS, E1.place, E2.place, E.place ); } – E1 + E1 E2 E E
  • 21. Semantic Analysis From Code Form To Program Meaning Compiler or Interpreter Translation Execution Source Code Target Code Interpre- tation
  • 22. Specification of Programming Languages  PLs require precise definitions (i.e. no ambiguity)  Language form (Syntax)  Language meaning (Semantics)  Consequently, PLs are specified using formal notation:  Formal syntax  Tokens  Grammar  Formal semantics  Attribute Grammars (static semantics)  Dynamic Semantics
  • 23. The Semantic Analyzer  The principal job of the semantic analyzer is to enforce static semantic rules.  In general, anything that requires the compiler to compare things that are separate by a long distance or to count things ends up being a matter of semantics.  The semantic analyzer also commonly constructs a syntax tree (usually first), and much of the information it gathers is needed by the code generator.
  • 24. Attribute Grammars  Context-Free Grammars (CFGs) are used to specify the syntax of programming languages  E.g. arithmetic expressions • How do we tie these rules to mathematical concepts? • Attribute grammars are annotated CFGs in which annotations are used to establish meaning relationships among symbols – Annotations are also known as decorations
  • 25. Attribute Grammars Example  Each grammar symbols has a set of attributes  E.g. the value of E1 is the attribute E1.val  Each grammar rule has a set of rules over the symbol attributes.
  • 26. Attribute Flow  Context-free grammars are not tied to an specific parsing order  E.g. Recursive descent, LR parsing  Attribute grammars are not tied to an specific evaluation order  This evaluation is known as the annotation or decoration of the parse tree
  • 27. Attribute Flow Example  The figure shows the result of annotating the parse tree for (1+3)*2  Each symbols has at most one attribute shown in the corresponding box  Numerical value in this example  Operator symbols have no value  Arrows represent attribute flow
  • 29. Static and Dynamic Semantics  Attribute grammars add basic semantic rules to the specification of a language  They specify static semantics  But they are limited to the semantic form that can be checked at compile time  Other semantic properties cannot be checked at compile time  They are described using dynamic semantics
  • 30. Dynamic Semantics  Use to formally specify the behavior of a programming language  Semantic-based error detection  Correctness proofs  There is not a universally accepted notation  Operational semantics  Executing statements that represent changes in the state of a real or simulated machine  Axiomatic semantics  Using predicate calculus (pre and post-conditions)  Denotational semantics  Using recursive function theory