honors compilers semantic analysis and attribute grammars mar 5th 2002
Post on 20-Dec-2015
213 views
TRANSCRIPT
Honors CompilersHonors Compilers
Semantic Analysis and Semantic Analysis and Attribute GrammarsAttribute Grammars
Mar 5th 2002Mar 5th 2002
Semantic analysisSemantic analysis
Enforce context-dependent language rules Enforce context-dependent language rules that are not reflected in the BNF, e.g.that are not reflected in the BNF, e.g.a a function must have a return statement.function must have a return statement.
Decorate AST with semantic information for Decorate AST with semantic information for subsequent code generation, e.g. subsequent code generation, e.g. determinedetermine types of all expressionstypes of all expressions
Expand complex constructs in preparation for Expand complex constructs in preparation for code generation, e.g. code generation, e.g. aggregates into loopsaggregates into loops..
General framework: General framework: compute attributescompute attributes
Attributes and attribute grammarsAttributes and attribute grammars
Syntax-directed framework:Syntax-directed framework: For every symbol in the grammar we define some For every symbol in the grammar we define some
computable properties computable properties (e.g. the value of a constant (e.g. the value of a constant expression)expression)
For every production in the grammar we give computation For every production in the grammar we give computation rules for the properties of all symbols on both sides of the rules for the properties of all symbols on both sides of the production production (e.g. the value of a sum is the sum of the (e.g. the value of a sum is the sum of the values of the operands)values of the operands)
The rule is The rule is locallocal: it only refers to other symbols in the : it only refers to other symbols in the same productionsame production
The evaluation of the attributes can require an arbitrary The evaluation of the attributes can require an arbitrary number of traversals of the AST: arbitrary context number of traversals of the AST: arbitrary context dependence (.dependence (.e.g. the value of a declared constant is e.g. the value of a declared constant is found in the constant declarationfound in the constant declaration))
Example: binary stringsExample: binary strings String ::= Digit | String DigitString ::= Digit | String Digit Digit ::= 0 | 1Digit ::= 0 | 1 Attribute: the numeric value of a stringAttribute: the numeric value of a string Digit ::= ‘0’Digit ::= ‘0’
Val Val digitdigit := 0; := 0; Digit ::= ‘1’Digit ::= ‘1’
Val Val digitdigit := 1; := 1; String ::= DigitString ::= Digit
Val Val StringString ::= Val ::= Val digitdigit String ::= String DigitString ::= String Digit
Val Val String1String1 := 2 * Val := 2 * Val String2String2 + Val + Val digitdigit
Example : type imposed by contextExample : type imposed by context
Assignment ::= Name := ExpressionAssignment ::= Name := Expression
AttributeAttribute : if expression is overloaded, : if expression is overloaded, its type is determined by the type of its type is determined by the type of the name:the name:
Context_Type Context_Type expressionexpression := Type := Type name name
Two distinct attributes: type of name is Two distinct attributes: type of name is determined from context and/or determined from context and/or other rulesother rules
Inherited and synthesized Inherited and synthesized attributesattributes
If attribute of left-hand side is computed from If attribute of left-hand side is computed from attributes in the right-hand side, attribute is attributes in the right-hand side, attribute is synthesizedsynthesized: bottom-up propagation: bottom-up propagation
If attribute of symbol on right-hand is computed from If attribute of symbol on right-hand is computed from attributes of left-hand side, or from attributes of other attributes of left-hand side, or from attributes of other symbols on right-hand side, attribute is symbols on right-hand side, attribute is inheritedinherited: top-: top-down propagation of informationdown propagation of information
String Val
Name Context typeexpression
General resultsGeneral results Attribute grammars have the power of Turing machinesAttribute grammars have the power of Turing machines
Attributes are computed by repeated passes over the ASTAttributes are computed by repeated passes over the AST
Attribute definitions may be cyclic; checking whether an Attribute definitions may be cyclic; checking whether an attribute grammar has cycles is decidable but potentially attribute grammar has cycles is decidable but potentially expensiveexpensive
In practice inherited attributes are handled by means of In practice inherited attributes are handled by means of global data structures (global data structures (symbol tablesymbol table))
Useful subsets: L-attributed and S-attributed grammarsUseful subsets: L-attributed and S-attributed grammars
L-attributed grammars and S-attributed L-attributed grammars and S-attributed grammarsgrammars
In an L-attributed grammar, inherited attributes can In an L-attributed grammar, inherited attributes can be computed left-to-right:be computed left-to-right:
N ::= S1 S2 S3N ::= S1 S2 S3 attributes of S1 cannot depend on attributes of S2 or attributes of S1 cannot depend on attributes of S2 or
S3S3 Easy to implement in a Easy to implement in a top-downtop-down parser: when parser: when
building a node, nodes on which its attributes building a node, nodes on which its attributes depend have been seen and processed.depend have been seen and processed.
An S-attributed grammar has only synthesized An S-attributed grammar has only synthesized attributesattributes
Usable withUsable with bottom-up bottom-up parser parser Can convert L- to S- but result is awkwardCan convert L- to S- but result is awkward..
Some important attributesSome important attributes
For expressions: For expressions: typetype For overloaded calls: For overloaded calls: candidate candidate
interpretationsinterpretations For identifiers: For identifiers: entityentity (defining_occurrence) (defining_occurrence) For definitions: For definitions: scopescope For data/function members: For data/function members: visibility visibility (public, (public,
protected, private)protected, private) For function: For function: virtual functionsvirtual functions (primitive (primitive
operations)operations) Etc, etc.Etc, etc.
Attribute computation and tree Attribute computation and tree traversalstraversals
In gnat (and others) mostly In gnat (and others) mostly left-to-right, top-downleft-to-right, top-down traversal, with localized multiple traversalstraversal, with localized multiple traversals
Inherited attributes computed during declaration Inherited attributes computed during declaration processing, symbol table carries inherited processing, symbol table carries inherited information as one global data structureinformation as one global data structure
Synthesized attributes on terminals: Synthesized attributes on terminals: names, literal names, literal valuesvalues
In the presence of overloading, type is both inherited In the presence of overloading, type is both inherited and synthesized: and synthesized: two passes required over two passes required over expressionsexpressions..
Generated code can be treated as synthesized Generated code can be treated as synthesized attributeattribute
Name ResolutionName Resolution
Compute attribute Compute attribute entityentity: associate every : associate every identifier (identifier (use occurrenceuse occurrence) with the ) with the
corresponding corresponding defining occurrencedefining occurrence.. If entity is If entity is overloadedoverloaded, associate entity with , associate entity with
set of set of candidate entitiescandidate entities, to be resolved by , to be resolved by types and context.types and context.
Complications:Complications:Block structure and hiding rulesBlock structure and hiding rulesContext and import rulesContext and import rules
Name resolution and block Name resolution and block structurestructure
Basic rule: Basic rule: inner definition hides outer one inner definition hides outer one with same namewith same name
Data structures reflect scope nestingData structures reflect scope nesting A stack of scopesA stack of scopes: defining occurrences of functions, : defining occurrences of functions,
packages, blocks, loops, recordspackages, blocks, loops, records A A list of local entitieslist of local entities declared in each scope declared in each scope A A names tablenames table
Entry in names table points to innermost Entry in names table points to innermost occurrence of entity with given nameoccurrence of entity with given name
All identifiers with given name point to same All identifiers with given name point to same names table entry names table entry (handled by scanner)(handled by scanner)
Name resolution does not require any Name resolution does not require any hashinghashing
Data structures for name resolutionData structures for name resolution
Entity chainEntity chain, homonym chain, , homonym chain, charschars
Outerscope
var1 count y
Parentscope
var1 Z2 yzz
Currentscope
var1 count y1
var1 count y
var1
Names table
Semantic actions for visibility Semantic actions for visibility processingprocessing
A A scopescope is any entity that has local declarations: is any entity that has local declarations: package, function, record type, task, block.package, function, record type, task, block.
On scope entryOn scope entry: place new scope on stack, initialize list : place new scope on stack, initialize list of local entitiesof local entities
For every declarationFor every declaration: chain name entry to local entity, : chain name entry to local entity, set homonym of local entity to outer entity with same set homonym of local entity to outer entity with same namename
On scope exit:On scope exit: chain name entry to homonym of local chain name entry to homonym of local entity. Local entity becomes invisible.entity. Local entity becomes invisible.
On package exitOn package exit: leave entries chained, because : leave entries chained, because entities remain visible through qualified notation.entities remain visible through qualified notation.
Full information remains in the tree for subsequent Full information remains in the tree for subsequent passes.passes.
Resolving qualified namesResolving qualified names
To resolve A.B, first resolve A (direct name).To resolve A.B, first resolve A (direct name). If A is enclosing scope, follow If A is enclosing scope, follow homonym chainhomonym chain for for
B until we find a variable whose scope is AB until we find a variable whose scope is A If A is a variable, find its typeIf A is a variable, find its type
If If recordrecord or struct, find component of type named B or struct, find component of type named B If If pointerpointer, apply rule to designated type (implicit , apply rule to designated type (implicit
dereference)dereference) If If tasktask, find entry named B, find entry named B
To resolve A.B.C, To resolve A.B.C, recurserecurse: resolve : resolve prefixprefix A.B, then A.B, then apply previous rulesapply previous rules
To resolve A->B (C++): type of A must be of the To resolve A->B (C++): type of A must be of the form *T, proceed as aboveform *T, proceed as above
Packages and import rulesPackages and import rules
PackagesPackages (namespaces) are (namespaces) are scopesscopes, have chain of , have chain of local entities, but are not in scope stack.local entities, but are not in scope stack.
Package entities also linked to names tablePackage entities also linked to names table To resolve P.A, if P is a package in context To resolve P.A, if P is a package in context
((with_clausewith_clause) use same algorithm as for an enclosing ) use same algorithm as for an enclosing scopescope
If If use_clauseuse_clause is active for P, then A is a legal use of is active for P, then A is a legal use of P.A Treat as a direct name if there is no A in an P.A Treat as a direct name if there is no A in an enclosing scope that hides it: enclosing scope that hides it: entities in enclosing entities in enclosing scopes hide imported entitiesscopes hide imported entities..
If multiple entities are use-visible, reference is If multiple entities are use-visible, reference is ambiguousambiguous
Detailed algorithms in gnat sourcesDetailed algorithms in gnat sources
Visibility : sem_ch8Visibility : sem_ch8 New_ScopeNew_Scope: push current scope on scope_stack: push current scope on scope_stack Find_Direct_NameFind_Direct_Name: resolve identifier: resolve identifier Find_Selected_ComponentFind_Selected_Component: resolve A.B, where A can be an : resolve A.B, where A can be an
expressionexpression Find_Qualified_NameFind_Qualified_Name: resolve A.B, where A is a scope: resolve A.B, where A is a scope Use_One_Package: Use_One_Package: make entities that are local to a make entities that are local to a
package use-visible in current contextpackage use-visible in current context End_Use: End_Use: when use clause goes out of scope, the conversewhen use clause goes out of scope, the converse End_Scope: End_Scope: remove local entities from visibility, pop remove local entities from visibility, pop
scope_stackscope_stack Declarations: sem_ch3, sem_utilDeclarations: sem_ch3, sem_util
Enter_NameEnter_Name: enter new entity into local chain, diagnose : enter new entity into local chain, diagnose duplicatesduplicates
Top-down processing: all but Top-down processing: all but expressionsexpressions
Semantic analysis of package declarationSemantic analysis of package declaration:: Enter new scopeEnter new scope Process visible declarationsProcess visible declarations Process private declarationsProcess private declarations Exit scopeExit scope
Semantic analysis of while statementSemantic analysis of while statement Process (analyze and resolve) conditionProcess (analyze and resolve) condition Process list of statementsProcess list of statements
Semantic analysis of object declarationSemantic analysis of object declaration Enter new entity into current scopeEnter new entity into current scope Resolve type definitionResolve type definition Analyze and resolve expressionAnalyze and resolve expression