Download presentation
Presentation is loading. Please wait.
1
1 Semantic Processing
2
2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice Grammars and Parsing Grammars and Parsing LL(1) Parsing LL(1) Parsing Lex and yacc Lex and yacc LR Parsing LR Parsing Semantic Processing Semantic Processing Symbol Tables Symbol Tables Run-time Storage Organization Run-time Storage Organization Code Generation and Local Code Optimization Code Generation and Local Code Optimization Global Optimization Global Optimization
3
3 Syntax-Directed Translation Semantic processing Semantic processing To take the sequence of parse actions and use them to construct a syntax tree representation of the input program. Such a tree is usually referred to as an abstract syntax tree, indicating its relationship to the actual (concrete) syntax and parse tree. One or more traversals of the tree. The two semantic processing tasks The two semantic processing tasks Static semantic checking IR or code generation can be accomplished using semantic attributes attached to the nodes of the syntax tree. can be accomplished using semantic attributes attached to the nodes of the syntax tree.
4
4 Syntax-Directed Translation (Cont’d.) A tree traversal that visits nodes using a post-order traversal A tree traversal that visits nodes using a post-order traversal Propagate semantic attributes throughout the tree and do static semantic checking at the same time. Propagation of semantic attributes includes Propagation of semantic attributes includes Actions as processing declarations to build a symbol table. Looking up identifiers in the symbol table to attach associated attribute information to appropriate nodes of the syntax tree. Examining the types of arguments (type checking).
5
5
6
6 Syntax-Directed Translation (Cont’d.) Semantic routines traverse the AST, computing attributes of the nodes of AST. Semantic routines traverse the AST, computing attributes of the nodes of AST. Initially, only leaves (i.e. terminals, e.g. const, id) have attributes. Initially, only leaves (i.e. terminals, e.g. const, id) have attributes.
7
7 Syntax-Directed Translation (Cont’d.) Top-down propagation Top-down propagation Information form declarations (the symbol table) is propagated top-down through subtree for statements and expressions. Bottom-up propagation Bottom-up propagation Expression-type information for semantic checking and code generation generally propagates bottom-up. After all the attribute propagation is done, the tree is said to be decorated. After all the attribute propagation is done, the tree is said to be decorated. Ready for code generation phase
8
8 Syntax-Directed Translation (Cont’d.) Two kinds of static semantic checks Two kinds of static semantic checks Some checks depend entirely on propagated semantic attributes. The check for type compatibility across an assignment The check for type compatibility across an assignment Others combine structural information from the tree with semantic information. A comparison of the number of actual parameters with the number of formal parameters. A comparison of the number of actual parameters with the number of formal parameters. Attribute grammar Attribute grammar Attributes are associated with each grammar symbol in a production. Rules are attached to each production to compute attribute values.
9
9 Syntax-Directed Translation (Cont’d.) Compiler organization alternatives Compiler organization alternatives Analysis Analysis Scanner Parser Static semantic checking Synthesis Synthesis Synthesis task of Semantic Routines IR Code optimization Code generation
10
10 Syntax-Directed Translation (Cont’d.) Compiler organization alternatives Compiler organization alternatives A single pass for analysis and synthesis (Chap 2) One-pass compiler plus peephole optimization (Chap 15) One-pass analysis and IR synthesis plus a code generation pass (Chap 10-13) Multipass analysis Multipass synthesis Multilanguage and multitargeted compilers The GNU C compiler, uses two intermediate forms The 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), which is more machine-oriented. The second is called RTL (Register Transfer Language), which is more machine-oriented.
11
11 Syntax-Directed Translation (Cont’d.) Parsing, checking, and translation in a single pass Parsing, checking, and translation in a single pass There are two principal advantages of such an approach There are two principal advantages of such an approach The front-end of the compiler is simpler because no tree building or tree traversal code is required. Much less storage space is required to process a program if an entire syntax tree is not explicitly built.
12
12 Attribute Grammars Please refer Chap 14 Please refer Chap 14 If only synthesized attributes are used, the SDT is said to be S- attributed. In practice, this means that a parse tree may be “decorated” as it is built bottom-up.
13
13 Attribute Grammars (Cont’d.) Annotated parse tree for 3*5+4n Annotated parse tree for 3*5+4n
14
14
15
15 Semantic Processing Techniques LL parsers and action symbols LL parsers and action symbols LR parsers and action symbols LR parsers and action symbols
16
16 Semantic Processing Techniques (Cont’d.) Semantic record representations Semantic record representations
17
17 Semantic Processing Techniques (Cont’d.) Implementation action-controlled semantic stacks Implementation action-controlled semantic stacks
18
18 Semantic Processing Techniques (Cont’d.) LL parser-controlled semantic stacks LL parser-controlled semantic stacks
19
19 Semantic Processing Techniques (Cont’d.)
20
20 Semantic Processing Techniques (Cont’d.)
21
21 Semantic Processing Techniques (Cont’d.)
22
22 Semantic Processing Techniques (Cont’d.)
23
23
24
24 Semantic Processing Techniques (Cont’d.)
25
25 Semantic Processing Techniques (Cont’d.)
26
26 Semantic Processing Techniques (Cont’d.)
27
27
28
28
29
29 Intermediate Representation and Code generation
30
30 Intermediate Representation and Code generation (Cont’d.)
31
31 Intermediate Representation and Code generation (Cont’d.)
32
32 Intermediate Representation and Code generation (Cont’d.)
33
33 Intermediate Representation and Code generation (Cont’d.)
34
34 Intermediate Representation and Code generation (Cont’d.)
35
35
36
36 Intermediate Representation and Code generation (Cont’d.)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.