1 semantic processing. 2 contents introduction introduction a simple compiler a simple compiler...

36
1 Semantic Processing Semantic Processing

Post on 20-Dec-2015

226 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: 1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice

1

Semantic ProcessingSemantic Processing

Page 2: 1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice

2

ContentsContents IntroductionIntroduction A Simple CompilerA Simple Compiler Scanning – Theory and PracticeScanning – Theory and Practice Grammars and ParsingGrammars and Parsing LL(1) ParsingLL(1) Parsing Lex and yaccLex and yacc LR ParsingLR Parsing Semantic ProcessingSemantic Processing Symbol TablesSymbol Tables Run-time Storage OrganizationRun-time Storage Organization Code Generation and Local Code OptimizationCode Generation and Local Code Optimization Global OptimizationGlobal Optimization

Page 3: 1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice

3

Syntax-Directed TranslationSyntax-Directed Translation Semantic processingSemantic processing

To take the To take the sequence of parse actionssequence of parse actions and and use them use them to construct a syntax tree representationto construct a syntax tree representation of the of the input program.input program.

Such a tree is usually referred to as an Such a tree is usually referred to as an abstract abstract syntax treesyntax tree, indicating its relationship to the actual , indicating its relationship to the actual (concrete) syntax and parse tree.(concrete) syntax and parse tree.

One or more traversals of the tree.One or more traversals of the tree. The two semantic processing tasksThe two semantic processing tasks

Static semantic checkingStatic semantic checking IR or code generationIR or code generation

can be accomplished using semantic attributes attached to can be accomplished using semantic attributes attached to the nodes of the syntax tree.the nodes of the syntax tree.

Page 4: 1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice

4

Syntax-Directed Translation Syntax-Directed Translation (Cont’d.)(Cont’d.)

A tree traversal that visits nodes using a A tree traversal that visits nodes using a post-order traversalpost-order traversal Propagate semantic attributes throughout the tree Propagate semantic attributes throughout the tree

and do static semantic checking at the same time.and do static semantic checking at the same time. Propagation of semantic attributes includesPropagation of semantic attributes includes

Actions as processing declarations to Actions as processing declarations to build a build a symbol tablesymbol table..

Looking up identifiers in the symbol table Looking up identifiers in the symbol table to to attach associated attribute information to attach associated attribute information to appropriate nodesappropriate nodes of the syntax tree. of the syntax tree.

Examining the types of arguments (type Examining the types of arguments (type checking).checking).

Page 5: 1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice

5

Page 6: 1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice

6

Syntax-Directed Translation Syntax-Directed Translation (Cont’d.)(Cont’d.)

Semantic routines traverse the AST, Semantic routines traverse the AST, computing computing attributesattributes of the nodes of AST.of the nodes of AST.

Initially, only leaves (i.e. terminals, e.g. const, Initially, only leaves (i.e. terminals, e.g. const, id) have attributes.id) have attributes.

Page 7: 1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice

7

Syntax-Directed Translation Syntax-Directed Translation (Cont’d.)(Cont’d.)

Top-down propagationTop-down propagation Information form declarationsInformation form declarations (the symbol table) is (the symbol table) is

propagated top-down through subtree for propagated top-down through subtree for statements and expressions. statements and expressions.

Bottom-up propagationBottom-up propagation Expression-type informationExpression-type information for semantic for semantic

checking and code generation generally checking and code generation generally propagates bottom-up.propagates bottom-up.

After all the attribute propagation is done, the After all the attribute propagation is done, the tree is said to be tree is said to be decorateddecorated.. Ready for code generation phaseReady for code generation phase

Page 8: 1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice

8

Syntax-Directed Translation Syntax-Directed Translation (Cont’d.)(Cont’d.)

Two kinds of static semantic checksTwo kinds of static semantic checks Some checks depend entirely on propagated Some checks depend entirely on propagated

semantic attributes.semantic attributes. The check for type compatibility across an assignmentThe check for type compatibility across an assignment

Others combine structural information from the Others combine structural information from the tree with semantic information.tree with semantic information. A comparison of the number of actual parameters with A comparison of the number of actual parameters with

the number of formal parameters.the number of formal parameters.

Attribute grammarAttribute grammar Attributes are associated with each grammar Attributes are associated with each grammar

symbol in a production.symbol in a production. Rules are attached to each production to compute Rules are attached to each production to compute

attribute values.attribute values.

Page 9: 1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice

9

Syntax-Directed Translation Syntax-Directed Translation (Cont’d.)(Cont’d.)

Compiler organization Compiler organization alternativesalternatives

AnalysisAnalysis ScannerScanner ParserParser Static semantic checkingStatic semantic checking

SynthesisSynthesis Synthesis task of Synthesis task of

Semantic RoutinesSemantic Routines IRIR Code optimizationCode optimization Code generationCode generation

Page 10: 1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice

10

Syntax-Directed Translation Syntax-Directed Translation (Cont’d.)(Cont’d.)

Compiler organization alternativesCompiler organization alternatives A single pass for analysis and synthesis (Chap 2)A single pass for analysis and synthesis (Chap 2) One-pass compiler plus peephole optimization One-pass compiler plus peephole optimization

(Chap 15)(Chap 15) One-pass analysis and IR synthesis plus a code One-pass analysis and IR synthesis plus a code

generation pass (Chap 10-13)generation pass (Chap 10-13) Multipass analysisMultipass analysis Multipass synthesisMultipass synthesis Multilanguage and multitargeted compilersMultilanguage and multitargeted compilers

The GNU C compiler, uses two intermediate formsThe GNU C compiler, uses two intermediate forms The first is a high-level, tree-oriented intermediate form.The first is a high-level, tree-oriented intermediate form. The second is called RTL (Register Transfer Language), The second is called RTL (Register Transfer Language),

which is more machine-oriented.which is more machine-oriented.

Page 11: 1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice

11

Syntax-Directed Translation Syntax-Directed Translation (Cont’d.)(Cont’d.)

Parsing, checking, and translation in a single Parsing, checking, and translation in a single passpass

There are two principal advantages of such There are two principal advantages of such an approachan approach The front-end of the compiler is simpler because The front-end of the compiler is simpler because

no tree building or tree traversal code is required.no tree building or tree traversal code is required. Much less storage space is required to process a Much less storage space is required to process a

program if an entire syntax tree is not explicitly program if an entire syntax tree is not explicitly built.built.

Page 12: 1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice

12

Attribute GrammarsAttribute Grammars Please refer Chap 14Please refer Chap 14

If only synthesized If only synthesized attributes are used, the attributes are used, the SDT is said to be SDT is said to be S-S-attributedattributed..

In practice, this means In practice, this means that a parse tree may that a parse tree may be “decorated” as it is be “decorated” as it is built bottom-up.built bottom-up.

Production Semantic Rules

L En Print(E.val)

E E1 + T E.val := E1.val + T.val

E T E.val := T.val

T T1 * F T.val := T1.val * F.val

T F T.val := F.val

F (E) F.val := E.val

F digit F.val := digit.lexval

Page 13: 1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice

13

Attribute Grammars Attribute Grammars (Cont’d.)(Cont’d.)

Annotated parse tree for Annotated parse tree for 3*5+4n3*5+4nL

E.val = 19

T.val = 4T.val = 15

T.val = 3 F.val = 5

F.val = 3

digit

digit

*

+

F.val = 4

digit

n

3

5 4

Page 14: 1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice

14

Page 15: 1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice

15

Semantic Processing TechniquesSemantic Processing Techniques LL parsers and action symbolsLL parsers and action symbols LR parsers and action symbolsLR parsers and action symbols

Page 16: 1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice

16

Semantic Processing Techniques Semantic Processing Techniques (Cont’d.)(Cont’d.)

Semantic record Semantic record representationsrepresentations

Page 17: 1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice

17

Semantic Processing Techniques Semantic Processing Techniques (Cont’d.)(Cont’d.)

Implementation action-controlled semantic stacksImplementation action-controlled semantic stacks

Page 18: 1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice

18

Semantic Processing Techniques Semantic Processing Techniques (Cont’d.)(Cont’d.)

LL parser-controlled semantic stacksLL parser-controlled semantic stacks

Page 19: 1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice

19

Semantic Processing Techniques Semantic Processing Techniques (Cont’d.)(Cont’d.)

Page 20: 1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice

20

Semantic Processing Techniques Semantic Processing Techniques (Cont’d.)(Cont’d.)

Page 21: 1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice

21

Semantic Processing Techniques Semantic Processing Techniques (Cont’d.)(Cont’d.)

Page 22: 1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice

22

Semantic Processing Techniques Semantic Processing Techniques (Cont’d.)(Cont’d.)

Page 23: 1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice

23

Page 24: 1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice

24

Semantic Processing Techniques Semantic Processing Techniques (Cont’d.)(Cont’d.)

Page 25: 1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice

25

Semantic Processing Techniques Semantic Processing Techniques (Cont’d.)(Cont’d.)

Page 26: 1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice

26

Semantic Processing Techniques Semantic Processing Techniques (Cont’d.)(Cont’d.)

Page 27: 1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice

27

Page 28: 1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice

28

Page 29: 1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice

29

Intermediate Representation and Code Intermediate Representation and Code generationgeneration

Page 30: 1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice

30

Intermediate Representation and Code Intermediate Representation and Code generation generation (Cont’d.)(Cont’d.)

Page 31: 1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice

31

Intermediate Representation and Code Intermediate Representation and Code generation generation (Cont’d.)(Cont’d.)

Page 32: 1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice

32

Intermediate Representation and Code Intermediate Representation and Code generation generation (Cont’d.)(Cont’d.)

Page 33: 1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice

33

Intermediate Representation and Code Intermediate Representation and Code generation generation (Cont’d.)(Cont’d.)

Page 34: 1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice

34

Intermediate Representation and Code Intermediate Representation and Code generation generation (Cont’d.)(Cont’d.)

Page 35: 1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice

35

Page 36: 1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice

36

Intermediate Representation and Code Intermediate Representation and Code generation generation (Cont’d.)(Cont’d.)