The document discusses symbol tables and their role in compiler design. Symbol tables store information about tokens and symbols defined in a source program. This information is used and updated across different compiler phases like lexical analysis, syntax analysis, semantic analysis, code generation, and optimization. Error handling is also discussed where compilers should try to continue after detecting errors instead of stopping.
The document discusses symbol tables and their role in compiler design. Symbol tables store information about tokens and symbols defined in a source program. This information is used and updated across different compiler phases like lexical analysis, syntax analysis, semantic analysis, code generation, and optimization. Error handling is also discussed where compilers should try to continue after detecting errors instead of stopping.
The document discusses symbol tables and their role in compiler design. Symbol tables store information about tokens and symbols defined in a source program. This information is used and updated across different compiler phases like lexical analysis, syntax analysis, semantic analysis, code generation, and optimization. Error handling is also discussed where compilers should try to continue after detecting errors instead of stopping.
The document discusses symbol tables and their role in compiler design. Symbol tables store information about tokens and symbols defined in a source program. This information is used and updated across different compiler phases like lexical analysis, syntax analysis, semantic analysis, code generation, and optimization. Error handling is also discussed where compilers should try to continue after detecting errors instead of stopping.
Lexical analysis • Symbol table stores information about all Tokens the ‘symbols’ defined in the source program. Syntax analysis • Symbols generally mean recognized tokens. Parse tree • Information/attributes about a symbol? • Storage allocation, Type, Scope in case Semantic analysis of variables Symbol table Semantic correctness Error handling & • Number and types of arguments, management recovery return type in case of Intermediate code functions/procedures generation
• An entry for a symbol is usually made in the Intermediate code
lexical analysis phase Optimization • Additional Information about different attributes may be added in the subsequent Optimized intermediate code phases Target code • Information about a symbol will be used in generation the subsequent phases Target code Optimization
Optimized target code
Source program
Error handling & recovery Lexical analysis
• Each phase of compilation can have error. Tokens
• A compiler should not stop at the first error! Syntax analysis
• A compiler should deal with the errors and Parse tree
continue as far as possible. • Most error detection happens in lexical, Semantic analysis syntax and semantic analysis phases Symbol table Semantic correctness Error handling & • Example error in lexical analysis phase management recovery Intermediate code • ‘sequence of characters’ do not match generation any token->invalid keywords, invalid identifiers, etc. Intermediate code • Example error in syntax analysis phase Optimization • violation of program structure rules (grammar)->wrong if-else Optimized intermediate code combination, invalid expressions Target code • Example error in semantic analysis phase generation • wrong operand type of operators-> Target code addition of array identifier and Optimization function identifier Optimized target code Compiler writing/construction tools • The first compiler for a language (e.g., C) can be written in some other language. The first Targe language/ Source language compiler constructed for a language is usually Machine code referred to as the bootstrap compiler. Compiler • Based on the bootstrap compiler for the language (e.g., C), further developments can be done for the language. • The process of creating a compiler from scratch is This is also a program referred to as bootstrapping or bootstrap written in some process. programming language that was converted/compiled to a machine code
X compiler written in Y X+1 compiler written in X
• But what if there are no other languages yet? Or
How is the first ever compiler created? Compiler writing/construction tools • There are specialized tools (programs) developed to implement different phases of a compiler. • Today, we don’t have to write/implement from scratch. We can specify the ‘requirements/behaviour’ as input to the specialized tools that generates the compiler components for us. • Scanner (Lexical analyzer) generators • Generates lexical analyzer (lexical analyzer program) from a specification based on regular expressions. • The generated lexical analyzer is basically a finite automaton. • Examples: LEX, FLEX • Input? • Regular expression that describes the token of a language. • Output • Lexical analyzer. • Parser (Syntax analyzer) generators. • Generates syntax analyzer (parser) from a specification based on context-free grammar. • Parser generators utilize powerful parsing algorithms that are too complex to be carried out by hand. • Examples: YACC, BISON • Input? • Grammar of the language. • Output? • Syntax analyzer. Compiler writing/construction tools • Syntax-directed translation engines. • Produce collection of routines (program) that traverse the parse tree generated by the parser. • Input? • Parse tree • Output? • Intermediate Code • Automatic code generators • Translate each operation of intermediate code to machine language based on collection of rules. • Most common rule/technique used is ‘template matching’. Intermediate code statements are replace by machine instruction templates. • Input? • Intermediate code • Output? • Machine code
• Data-flow engines • Performs data-flow analysis. • Essential for code optimization. Example compilation