principles of programming languages - wordpress.com...principles of programming languages ting zhang...
TRANSCRIPT
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
Principles of Programming Languages
Ting Zhang
Iowa State UniversityComputer Science Department
Lecture Note 6September 15, 2009Semantics Analysis
1 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
Outline
1 Preliminary
2 Attribute Grammars for Parse Trees
3 Attribute Grammars for Syntax Trees
4 Action Routines
2 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
Outline
1 Preliminary
2 Attribute Grammars for Parse Trees
3 Attribute Grammars for Syntax Trees
4 Action Routines
3 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
Syntax vs. Semantics
+ Syntax concerns the form of a valid program, while semanticsconcerns its meaning, which cannot be described by context-freegrammars. For example,
variable declaration before variable usethe number of arguments match the number of formal parameters
Syntax is the portion of the language that can be convenientlydescribed by a CFG, while the semantics is not.
4 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
Syntax vs. Semantics
Syntax concerns the form of a valid program, while semanticsconcerns its meaning, which cannot be described by context-freegrammars. For example,
variable declaration before variable use
the number of arguments match the number of formal parameters
Syntax is the portion of the language that can be convenientlydescribed by a CFG, while the semantics is not.
4 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
Syntax vs. Semantics
Syntax concerns the form of a valid program, while semanticsconcerns its meaning, which cannot be described by context-freegrammars. For example,
variable declaration before variable usethe number of arguments match the number of formal parameters
Syntax is the portion of the language that can be convenientlydescribed by a CFG, while the semantics is not.
4 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
Syntax vs. Semantics
Syntax concerns the form of a valid program, while semanticsconcerns its meaning, which cannot be described by context-freegrammars. For example,
variable declaration before variable usethe number of arguments match the number of formal parameters
+ Syntax is the portion of the language that can be convenientlydescribed by a CFG, while the semantics is not.
4 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
Syntax vs. Semantics
+ The boundary is not clear-cut; some properties can be expressed incontext-free grammar, but that requires high complexity
Convention: the following are conventionally treated in semanticsanalysis.
rules that compare things that are separated by long distances,rules that count things that are not properly nested,...
5 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
Syntax vs. Semantics
The boundary is not clear-cut; some properties can be expressed incontext-free grammar, but that requires high complexity
+ Convention: the following are conventionally treated in semanticsanalysis.
rules that compare things that are separated by long distances,rules that count things that are not properly nested,...
5 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
Syntax vs. Semantics
The boundary is not clear-cut; some properties can be expressed incontext-free grammar, but that requires high complexity
Convention: the following are conventionally treated in semanticsanalysis.
rules that compare things that are separated by long distances,
rules that count things that are not properly nested,...
5 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
Syntax vs. Semantics
The boundary is not clear-cut; some properties can be expressed incontext-free grammar, but that requires high complexity
Convention: the following are conventionally treated in semanticsanalysis.
rules that compare things that are separated by long distances,rules that count things that are not properly nested,
...
5 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
Syntax vs. Semantics
The boundary is not clear-cut; some properties can be expressed incontext-free grammar, but that requires high complexity
Convention: the following are conventionally treated in semanticsanalysis.
rules that compare things that are separated by long distances,rules that count things that are not properly nested,...
5 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
Static Semantics vs. Dynamic Semantics
+ Static semantic rules are enforced by a compiler at the phase ofsemantic analysis. For example,
Type checkingIdentifiers are used in appropriate contextCheck subroutine call argumentsCheck labels
Dynamic semantics rules are checked at run time by the codegenerated by a compiler. For example,
Arithmetic errorArray index values are without boundsPointers are not dereferenced unless pointing to valid objectVariables are initialized before use
6 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
Static Semantics vs. Dynamic Semantics
Static semantic rules are enforced by a compiler at the phase ofsemantic analysis. For example,
Type checking
Identifiers are used in appropriate contextCheck subroutine call argumentsCheck labels
Dynamic semantics rules are checked at run time by the codegenerated by a compiler. For example,
Arithmetic errorArray index values are without boundsPointers are not dereferenced unless pointing to valid objectVariables are initialized before use
6 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
Static Semantics vs. Dynamic Semantics
Static semantic rules are enforced by a compiler at the phase ofsemantic analysis. For example,
Type checkingIdentifiers are used in appropriate context
Check subroutine call argumentsCheck labels
Dynamic semantics rules are checked at run time by the codegenerated by a compiler. For example,
Arithmetic errorArray index values are without boundsPointers are not dereferenced unless pointing to valid objectVariables are initialized before use
6 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
Static Semantics vs. Dynamic Semantics
Static semantic rules are enforced by a compiler at the phase ofsemantic analysis. For example,
Type checkingIdentifiers are used in appropriate contextCheck subroutine call arguments
Check labels
Dynamic semantics rules are checked at run time by the codegenerated by a compiler. For example,
Arithmetic errorArray index values are without boundsPointers are not dereferenced unless pointing to valid objectVariables are initialized before use
6 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
Static Semantics vs. Dynamic Semantics
Static semantic rules are enforced by a compiler at the phase ofsemantic analysis. For example,
Type checkingIdentifiers are used in appropriate contextCheck subroutine call argumentsCheck labels
Dynamic semantics rules are checked at run time by the codegenerated by a compiler. For example,
Arithmetic errorArray index values are without boundsPointers are not dereferenced unless pointing to valid objectVariables are initialized before use
6 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
Static Semantics vs. Dynamic Semantics
Static semantic rules are enforced by a compiler at the phase ofsemantic analysis. For example,
Type checkingIdentifiers are used in appropriate contextCheck subroutine call argumentsCheck labels
+ Dynamic semantics rules are checked at run time by the codegenerated by a compiler. For example,
Arithmetic errorArray index values are without boundsPointers are not dereferenced unless pointing to valid objectVariables are initialized before use
6 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
Static Semantics vs. Dynamic Semantics
Static semantic rules are enforced by a compiler at the phase ofsemantic analysis. For example,
Type checkingIdentifiers are used in appropriate contextCheck subroutine call argumentsCheck labels
Dynamic semantics rules are checked at run time by the codegenerated by a compiler. For example,
Arithmetic error
Array index values are without boundsPointers are not dereferenced unless pointing to valid objectVariables are initialized before use
6 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
Static Semantics vs. Dynamic Semantics
Static semantic rules are enforced by a compiler at the phase ofsemantic analysis. For example,
Type checkingIdentifiers are used in appropriate contextCheck subroutine call argumentsCheck labels
Dynamic semantics rules are checked at run time by the codegenerated by a compiler. For example,
Arithmetic errorArray index values are without bounds
Pointers are not dereferenced unless pointing to valid objectVariables are initialized before use
6 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
Static Semantics vs. Dynamic Semantics
Static semantic rules are enforced by a compiler at the phase ofsemantic analysis. For example,
Type checkingIdentifiers are used in appropriate contextCheck subroutine call argumentsCheck labels
Dynamic semantics rules are checked at run time by the codegenerated by a compiler. For example,
Arithmetic errorArray index values are without boundsPointers are not dereferenced unless pointing to valid object
Variables are initialized before use
6 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
Static Semantics vs. Dynamic Semantics
Static semantic rules are enforced by a compiler at the phase ofsemantic analysis. For example,
Type checkingIdentifiers are used in appropriate contextCheck subroutine call argumentsCheck labels
Dynamic semantics rules are checked at run time by the codegenerated by a compiler. For example,
Arithmetic errorArray index values are without boundsPointers are not dereferenced unless pointing to valid objectVariables are initialized before use
6 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
Role of Semantic Analysis
+ Following parsing, the next two phases of the ”typical” compiler are
semantic analysis(intermediate) code generation
The principal job of the semantic analyzer is to enforce staticsemantic rules
constructs a syntax tree (usually first)information gathered is needed by the code generator
7 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
Role of Semantic Analysis
Following parsing, the next two phases of the ”typical” compiler are
semantic analysis
(intermediate) code generation
The principal job of the semantic analyzer is to enforce staticsemantic rules
constructs a syntax tree (usually first)information gathered is needed by the code generator
7 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
Role of Semantic Analysis
Following parsing, the next two phases of the ”typical” compiler are
semantic analysis(intermediate) code generation
The principal job of the semantic analyzer is to enforce staticsemantic rules
constructs a syntax tree (usually first)information gathered is needed by the code generator
7 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
Role of Semantic Analysis
Following parsing, the next two phases of the ”typical” compiler are
semantic analysis(intermediate) code generation
+ The principal job of the semantic analyzer is to enforce staticsemantic rules
constructs a syntax tree (usually first)information gathered is needed by the code generator
7 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
Role of Semantic Analysis
Following parsing, the next two phases of the ”typical” compiler are
semantic analysis(intermediate) code generation
The principal job of the semantic analyzer is to enforce staticsemantic rules
constructs a syntax tree (usually first)
information gathered is needed by the code generator
7 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
Role of Semantic Analysis
Following parsing, the next two phases of the ”typical” compiler are
semantic analysis(intermediate) code generation
The principal job of the semantic analyzer is to enforce staticsemantic rules
constructs a syntax tree (usually first)information gathered is needed by the code generator
7 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
Role of Semantic Analysis
+ Considerable variety in the extent to which parsing, semanticanalysis, and intermediate code generation are interleaved
Common approach interleaves construction of a syntax tree withparsing (no explicit parse tree), and then follows with separate,sequential phases for semantic analysis and code generation
8 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
Role of Semantic Analysis
Considerable variety in the extent to which parsing, semanticanalysis, and intermediate code generation are interleaved
+ Common approach interleaves construction of a syntax tree withparsing (no explicit parse tree), and then follows with separate,sequential phases for semantic analysis and code generation
8 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
Parse Tree vs. Syntax Tree
+ A parse tree is known as a concrete syntax tree
It demonstrates completely and concretely how a particular sequenceof tokens can be derived under the rules of the contex-free grammarHowever, once we know that a token sequence is valid, much of theinformation in the partree is irrelevant to further phases of compilation.
An (abstract) syntax tree (or simply AST) is obtained by removingmost of the “artificial” nodes in the parse tree’s interior
The semantic analyzer annotates the nodes in an AST.The annotations attached to a particular node are know as itsattributes
9 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
Parse Tree vs. Syntax Tree
A parse tree is known as a concrete syntax tree
It demonstrates completely and concretely how a particular sequenceof tokens can be derived under the rules of the contex-free grammar
However, once we know that a token sequence is valid, much of theinformation in the partree is irrelevant to further phases of compilation.
An (abstract) syntax tree (or simply AST) is obtained by removingmost of the “artificial” nodes in the parse tree’s interior
The semantic analyzer annotates the nodes in an AST.The annotations attached to a particular node are know as itsattributes
9 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
Parse Tree vs. Syntax Tree
A parse tree is known as a concrete syntax tree
It demonstrates completely and concretely how a particular sequenceof tokens can be derived under the rules of the contex-free grammarHowever, once we know that a token sequence is valid, much of theinformation in the partree is irrelevant to further phases of compilation.
An (abstract) syntax tree (or simply AST) is obtained by removingmost of the “artificial” nodes in the parse tree’s interior
The semantic analyzer annotates the nodes in an AST.The annotations attached to a particular node are know as itsattributes
9 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
Parse Tree vs. Syntax Tree
A parse tree is known as a concrete syntax tree
It demonstrates completely and concretely how a particular sequenceof tokens can be derived under the rules of the contex-free grammarHowever, once we know that a token sequence is valid, much of theinformation in the partree is irrelevant to further phases of compilation.
+ An (abstract) syntax tree (or simply AST) is obtained by removingmost of the “artificial” nodes in the parse tree’s interior
The semantic analyzer annotates the nodes in an AST.The annotations attached to a particular node are know as itsattributes
9 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
Parse Tree vs. Syntax Tree
A parse tree is known as a concrete syntax tree
It demonstrates completely and concretely how a particular sequenceof tokens can be derived under the rules of the contex-free grammarHowever, once we know that a token sequence is valid, much of theinformation in the partree is irrelevant to further phases of compilation.
An (abstract) syntax tree (or simply AST) is obtained by removingmost of the “artificial” nodes in the parse tree’s interior
The semantic analyzer annotates the nodes in an AST.
The annotations attached to a particular node are know as itsattributes
9 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
Parse Tree vs. Syntax Tree
A parse tree is known as a concrete syntax tree
It demonstrates completely and concretely how a particular sequenceof tokens can be derived under the rules of the contex-free grammarHowever, once we know that a token sequence is valid, much of theinformation in the partree is irrelevant to further phases of compilation.
An (abstract) syntax tree (or simply AST) is obtained by removingmost of the “artificial” nodes in the parse tree’s interior
The semantic analyzer annotates the nodes in an AST.The annotations attached to a particular node are know as itsattributes
9 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
Outline
1 Preliminary
2 Attribute Grammars for Parse Trees
3 Attribute Grammars for Syntax Trees
4 Action Routines
10 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
Attribute Grammars
+ An attribute grammar “connects” syntax with semantics
Attributes have values that hold information related to the(non)terminal
A (non)terminal may have any number of attributes
Each grammar production has semantic rules that modify values ofattributes of (non)terminals
Semantic rules are used by a compiler to enforce static semanticsand/or to produce an abstract syntax tree while parsing tokens
Multiple occurrences of a nonterminal are indexed in semantic rules
11 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
Attribute Grammars
An attribute grammar “connects” syntax with semantics
+ Attributes have values that hold information related to the(non)terminal
A (non)terminal may have any number of attributes
Each grammar production has semantic rules that modify values ofattributes of (non)terminals
Semantic rules are used by a compiler to enforce static semanticsand/or to produce an abstract syntax tree while parsing tokens
Multiple occurrences of a nonterminal are indexed in semantic rules
11 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
Attribute Grammars
An attribute grammar “connects” syntax with semantics
Attributes have values that hold information related to the(non)terminal
+ A (non)terminal may have any number of attributes
Each grammar production has semantic rules that modify values ofattributes of (non)terminals
Semantic rules are used by a compiler to enforce static semanticsand/or to produce an abstract syntax tree while parsing tokens
Multiple occurrences of a nonterminal are indexed in semantic rules
11 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
Attribute Grammars
An attribute grammar “connects” syntax with semantics
Attributes have values that hold information related to the(non)terminal
A (non)terminal may have any number of attributes
+ Each grammar production has semantic rules that modify values ofattributes of (non)terminals
Semantic rules are used by a compiler to enforce static semanticsand/or to produce an abstract syntax tree while parsing tokens
Multiple occurrences of a nonterminal are indexed in semantic rules
11 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
Attribute Grammars
An attribute grammar “connects” syntax with semantics
Attributes have values that hold information related to the(non)terminal
A (non)terminal may have any number of attributes
Each grammar production has semantic rules that modify values ofattributes of (non)terminals
+ Semantic rules are used by a compiler to enforce static semanticsand/or to produce an abstract syntax tree while parsing tokens
Multiple occurrences of a nonterminal are indexed in semantic rules
11 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
Attribute Grammars
An attribute grammar “connects” syntax with semantics
Attributes have values that hold information related to the(non)terminal
A (non)terminal may have any number of attributes
Each grammar production has semantic rules that modify values ofattributes of (non)terminals
Semantic rules are used by a compiler to enforce static semanticsand/or to produce an abstract syntax tree while parsing tokens
+ Multiple occurrences of a nonterminal are indexed in semantic rules
11 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
Examples: Attribute Grammars
E1 → E2 + T E1.val = E2.val + T.valE1 → E2 - T E1.val = E2.val - T.valE → T E.val = T.valT1 → T2 * F T1.val = T2.val * F.valT1 → T2 / F T1.val = T2.val / F.valT → F T.val = F.valF1 → - F2 F1.val = - F2.valF → (E) F.val = E.valF → const F.val = C.val
12 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
Decorated Parse Trees (LR)
Decorated parse tree of (1 + 3) ∗ 2 under LR(1) grammar
13 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
Synthesized Attributes vs. Inherited attributes
+ Synthesized attributes of a node are computed from attribute of thechild nodes.
The information flows upwards
Inherited attributes of child nodes are computed from attributes ofthe sibling nodes and/or the parent node
The information flows downwards
14 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
Synthesized Attributes vs. Inherited attributes
Synthesized attributes of a node are computed from attribute of thechild nodes.
The information flows upwards
Inherited attributes of child nodes are computed from attributes ofthe sibling nodes and/or the parent node
The information flows downwards
14 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
Synthesized Attributes vs. Inherited attributes
Synthesized attributes of a node are computed from attribute of thechild nodes.
The information flows upwards
+ Inherited attributes of child nodes are computed from attributes ofthe sibling nodes and/or the parent node
The information flows downwards
14 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
Synthesized Attributes vs. Inherited attributes
Synthesized attributes of a node are computed from attribute of thechild nodes.
The information flows upwards
Inherited attributes of child nodes are computed from attributes ofthe sibling nodes and/or the parent node
The information flows downwards
14 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
S-Attributed Grammars vs. L-AttributedGrammars
+ A grammar is called S-attributed if all attributes are synthesized
A grammar is called L-attributed if the parse tree traversal to updateattribute values is always left-to-right and depth-first
An S-attributed grammar is a special case of an L-attributed grammarValues of inherited attributes must be passed down to children fromleft to rightSemantic rules can be applied immediately during parsing and parsetrees do not need to be kept in memoryThis is an essential grammar property for a one-pass compiler
15 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
S-Attributed Grammars vs. L-AttributedGrammars
A grammar is called S-attributed if all attributes are synthesized
+ A grammar is called L-attributed if the parse tree traversal to updateattribute values is always left-to-right and depth-first
An S-attributed grammar is a special case of an L-attributed grammarValues of inherited attributes must be passed down to children fromleft to rightSemantic rules can be applied immediately during parsing and parsetrees do not need to be kept in memoryThis is an essential grammar property for a one-pass compiler
15 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
S-Attributed Grammars vs. L-AttributedGrammars
A grammar is called S-attributed if all attributes are synthesized
A grammar is called L-attributed if the parse tree traversal to updateattribute values is always left-to-right and depth-first
An S-attributed grammar is a special case of an L-attributed grammar
Values of inherited attributes must be passed down to children fromleft to rightSemantic rules can be applied immediately during parsing and parsetrees do not need to be kept in memoryThis is an essential grammar property for a one-pass compiler
15 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
S-Attributed Grammars vs. L-AttributedGrammars
A grammar is called S-attributed if all attributes are synthesized
A grammar is called L-attributed if the parse tree traversal to updateattribute values is always left-to-right and depth-first
An S-attributed grammar is a special case of an L-attributed grammarValues of inherited attributes must be passed down to children fromleft to right
Semantic rules can be applied immediately during parsing and parsetrees do not need to be kept in memoryThis is an essential grammar property for a one-pass compiler
15 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
S-Attributed Grammars vs. L-AttributedGrammars
A grammar is called S-attributed if all attributes are synthesized
A grammar is called L-attributed if the parse tree traversal to updateattribute values is always left-to-right and depth-first
An S-attributed grammar is a special case of an L-attributed grammarValues of inherited attributes must be passed down to children fromleft to rightSemantic rules can be applied immediately during parsing and parsetrees do not need to be kept in memory
This is an essential grammar property for a one-pass compiler
15 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
S-Attributed Grammars vs. L-AttributedGrammars
A grammar is called S-attributed if all attributes are synthesized
A grammar is called L-attributed if the parse tree traversal to updateattribute values is always left-to-right and depth-first
An S-attributed grammar is a special case of an L-attributed grammarValues of inherited attributes must be passed down to children fromleft to rightSemantic rules can be applied immediately during parsing and parsetrees do not need to be kept in memoryThis is an essential grammar property for a one-pass compiler
15 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
Example: Attribute Grammars (LL)
E → T TT E.val = TT.valTT.st = T.val
TT1 → + T TT2 TT1.val = TT2.valTT2.st = TT1.st + T.val
TT1 → - T TT1 TT1.val = TT2.valTT2.st = TT1.st - T.val
TT → � TT.val = TT.st
16 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
Example: Attribute Grammars (LL)
(Cont’d)T → F FT T.val = FT.val
FT.st = F.valFT1 → * F FT2 FT1.val = FT2.val
FT2.st = FT1.st * F.valFT1 → / F FT2 FT1.val = FT2.val
FT2.st = FT1.st / F.valFT → � FT.val = FT.stF1 → - F2 F1.val = - F2.valF → ( E ) F.val = E.valF → const F.val = C.val
17 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
Decorated Parse Trees (LL)
18 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
Outline
1 Preliminary
2 Attribute Grammars for Parse Trees
3 Attribute Grammars for Syntax Trees
4 Action Routines
19 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
Attribute Grammars for Syntax TreeConstruction (LR)
Bottom-up attribute grammar to construct a syntax tree:
E1 → E2 + T E1.ptr := make bin op(“+”, E2.ptr, T.ptr)E1 → E2 - T E1.ptr := make bin op(“-”, E2.ptr, T.ptr)E → T E.ptr := T.ptrT1 → T2 * F T1.ptr := make bin op(“*”, T2.ptr, F.ptr)T1 → T2 / F T1.ptr := make bin op(“/”, T2.ptr, F.ptr)T → F T.ptr := F.ptrF1 → - F2 F1.ptr := make un op(“-”, F2.ptr)F → ( E ) F.ptr := E.ptrF → const F.ptr := make leaf(const.val)
20 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
Attribute Grammars for Syntax TreeConstruction (LR)
(a) Two leaves are created to hold 1and 3, respectively. The pointers tothem propagate to E and T ,respectively.
(b) The pointers to the two leavesbecome children of node +.
(c) The pointer to this nodepropagates to T . A new leaf iscreated to hold 2.
(d) The pointers to T and F becomechildren of node ×, a pointer towhich propagates to the root.
21 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
Attribute Grammars for Syntax TreeConstruction (LR)
(a) Two leaves are created to hold 1and 3, respectively. The pointers tothem propagate to E and T ,respectively.
(b) The pointers to the two leavesbecome children of node +.
(c) The pointer to this nodepropagates to T . A new leaf iscreated to hold 2.
(d) The pointers to T and F becomechildren of node ×, a pointer towhich propagates to the root.
21 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
Attribute Grammars for Syntax TreeConstruction (LR)
(a) Two leaves are created to hold 1and 3, respectively. The pointers tothem propagate to E and T ,respectively.
(b) The pointers to the two leavesbecome children of node +.
(c) The pointer to this nodepropagates to T . A new leaf iscreated to hold 2.
(d) The pointers to T and F becomechildren of node ×, a pointer towhich propagates to the root.
21 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
Attribute Grammars for Syntax TreeConstruction (LR)
(a) Two leaves are created to hold 1and 3, respectively. The pointers tothem propagate to E and T ,respectively.
(b) The pointers to the two leavesbecome children of node +.
(c) The pointer to this nodepropagates to T . A new leaf iscreated to hold 2.
(d) The pointers to T and F becomechildren of node ×, a pointer towhich propagates to the root.
21 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
Attribute Grammars for Syntax TreeConstruction (LR)
(a) Two leaves are created to hold 1and 3, respectively. The pointers tothem propagate to E and T ,respectively.
(b) The pointers to the two leavesbecome children of node +.
(c) The pointer to this nodepropagates to T . A new leaf iscreated to hold 2.
(d) The pointers to T and F becomechildren of node ×, a pointer towhich propagates to the root.
21 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
Attribute Grammars for Syntax TreeConstruction (LL)
Top-down attribute grammar to construct a syntax tree:
E → T TT TT.st := T.ptrE.ptr := TT.ptr
TT1 → + T TT2 TT2.st := make bin op(“+”, TT1.st, T.ptr)TT1.ptr := TT2.ptr
TT1 → - T TT1 TT2.st := make bin op(“-”, TT1.st, T.ptr)TT1.ptr := TT2.ptr
TT → � TT.ptr := TT.st
22 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
Attribute Grammars for Syntax TreeConstruction (LL)
Top-down attribute grammar to construct a syntax tree (cont’d):
T → F FT FT.st := F.ptrT.ptr := FT.ptr
FT1 → * F FT2 FT2.st := make bin op(“*”, FT1.st, F.ptr)FT1.ptr := FT2.ptr
FT1 → / F FT2 FT2.st := make bin op(“/”, FT1.st, F.ptr)FT1.ptr := FT2.ptr
FT → � FT.ptr := FT.stF1 → - F2 F1.ptr := make un op(“-”, F2.ptr)F → ( E ) F.ptr := E.ptrF → const F.ptr := make leaf(const.val)
23 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
Attribute Grammars for Syntax TreeConstruction (LL)
(a) A leaf is created to hold1. A pointer to itpropagates to TT .
(b) The 2nd leaf is createdto hold 3. The pointers tothe two leaves becomechildren of node +, thepointer to whichpropagates to FT .
(c) The third leaf is createdto hold 2. The pointers toit and to the node +become children of node×, the pointer to whichpropagates to the root.
24 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
Attribute Grammars for Syntax TreeConstruction (LL)
(a) A leaf is created to hold1. A pointer to itpropagates to TT .
(b) The 2nd leaf is createdto hold 3. The pointers tothe two leaves becomechildren of node +, thepointer to whichpropagates to FT .
(c) The third leaf is createdto hold 2. The pointers toit and to the node +become children of node×, the pointer to whichpropagates to the root.
24 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
Attribute Grammars for Syntax TreeConstruction (LL)
(a) A leaf is created to hold1. A pointer to itpropagates to TT .
(b) The 2nd leaf is createdto hold 3. The pointers tothe two leaves becomechildren of node +, thepointer to whichpropagates to FT .
(c) The third leaf is createdto hold 2. The pointers toit and to the node +become children of node×, the pointer to whichpropagates to the root.
24 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
Attribute Grammars for Syntax TreeConstruction (LL)
(a) A leaf is created to hold1. A pointer to itpropagates to TT .
(b) The 2nd leaf is createdto hold 3. The pointers tothe two leaves becomechildren of node +, thepointer to whichpropagates to FT .
(c) The third leaf is createdto hold 2. The pointers toit and to the node +become children of node×, the pointer to whichpropagates to the root.
24 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
Outline
1 Preliminary
2 Attribute Grammars for Parse Trees
3 Attribute Grammars for Syntax Trees
4 Action Routines
25 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
Action Routines
An action routine is a semantic function that we tell the compiler toexecute at a particular point in the parse
+ An ad-hoc translation scheme that is interleaved with parsing takesthe form of a set of action routines.
If semantic analysis and code generation are interleaved withparsing, then action routines can be used to perform semanticchecks and generate code
If semantic analysis and code generation are broken out asseparate phases, then action routines can be used to build a syntaxtree
26 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
Action Routines
An action routine is a semantic function that we tell the compiler toexecute at a particular point in the parse
An ad-hoc translation scheme that is interleaved with parsing takesthe form of a set of action routines.
+ If semantic analysis and code generation are interleaved withparsing, then action routines can be used to perform semanticchecks and generate code
If semantic analysis and code generation are broken out asseparate phases, then action routines can be used to build a syntaxtree
26 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
Action Routines
An action routine is a semantic function that we tell the compiler toexecute at a particular point in the parse
An ad-hoc translation scheme that is interleaved with parsing takesthe form of a set of action routines.
If semantic analysis and code generation are interleaved withparsing, then action routines can be used to perform semanticchecks and generate code
+ If semantic analysis and code generation are broken out asseparate phases, then action routines can be used to build a syntaxtree
26 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
Action Routines for Syntax Tree Construction(LL)
LL(1) grammar with action routines to build a syntax tree:
E → T { TT.st := T.ptr } TT { E.ptr := TT.ptr }TT1 → + T { TT2.st := make bin op(“+”, TT1.st, T.ptr) }
TT2 {TT1.ptr := TT2.ptr }TT1 → - T { TT2.st := make bin op(“-”, TT1.st, T.ptr) }
TT2 {TT1.ptr := TT2.ptr }TT → � { TT.ptr := TT.st }T → F { FT.st := F.ptr } FT { T.ptr := FT.ptr }
27 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
Action Routines for Syntax Tree Construction(LL)
LL(1) grammar with action routines to build a syntax tree (cont’d):
FT1 → * F { FT2.st := make bin op(“*”, FT1.st, F.ptr) }FT2 {FT1.ptr := FT2.ptr }
TT1 → / F { FT2.st := make bin op(“/”, FT1.st, F.ptr) }FT2 {FT1.ptr := FT2.ptr }
FT → � { FT.ptr := FT.ptr }F1 → - F2 { F1.ptr := make un op(“-”, F2.ptr)F → ( E ) { F.ptr := E.ptr }F → const { F.ptr := make leaf(const.ptr) }
28 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
Action Routines for Syntax Tree Construction(LL)
(a) A leaf is created to hold1. A pointer to itpropagates to TT .
(b) The 2nd leaf is createdto hold 3. The pointers tothe two leaves becomechildren of node +, thepointer to whichpropagates to FT .
(c) The third leaf is createdto hold 2. The pointers toit and to the node +become children of node×, the pointer to whichpropagates to the root.
29 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
Action Routines for Syntax Tree Construction(LL)
(a) A leaf is created to hold1. A pointer to itpropagates to TT .
(b) The 2nd leaf is createdto hold 3. The pointers tothe two leaves becomechildren of node +, thepointer to whichpropagates to FT .
(c) The third leaf is createdto hold 2. The pointers toit and to the node +become children of node×, the pointer to whichpropagates to the root.
29 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
Action Routines for Syntax Tree Construction(LL)
(a) A leaf is created to hold1. A pointer to itpropagates to TT .
(b) The 2nd leaf is createdto hold 3. The pointers tothe two leaves becomechildren of node +, thepointer to whichpropagates to FT .
(c) The third leaf is createdto hold 2. The pointers toit and to the node +become children of node×, the pointer to whichpropagates to the root.
29 / 29Principles of Programming Languages
-
Preliminary Attribute Grammars for Parse Trees Attribute Grammars for Syntax Trees Action Routines
Action Routines for Syntax Tree Construction(LL)
(a) A leaf is created to hold1. A pointer to itpropagates to TT .
(b) The 2nd leaf is createdto hold 3. The pointers tothe two leaves becomechildren of node +, thepointer to whichpropagates to FT .
(c) The third leaf is createdto hold 2. The pointers toit and to the node +become children of node×, the pointer to whichpropagates to the root.
29 / 29Principles of Programming Languages
PreliminaryAttribute Grammars for Parse TreesAttribute Grammars for Syntax TreesAction Routines