Introduction to Compiling CS308 Compiler Theory
Introduction to Compilin g CS308 Compiler Theory 1
COMPILERS A compiler is a program takes a program written in a source language and translates it into an equivalent program in a target language. source program COMPILER target program Normally a program written in Normally the equivalent program in a high-level programming language) machine code-relocatable object file) error messages A compiler is a bridge. CS308 Compiler Theory 2
COMPILERS • A compiler is a program takes a program written in a source language and l ii i l i l d translates it into an equivalent program in a target language. source program COMPILER target program ( Normally the equivalent program in ( Normally a program written in a high-level programming language) ( Normally the equivalent program in machine code – relocatable object file) error messages • A compiler is a bridge. CS308 Compiler Theory 2
Compiler Architecture In more detail: Intermediate Language Source Front End- Back End- 一Target Language Language language specific machine specific Analysis Synthesis .Separation of Concerns .Retargeting CS308 Compiler Theory 3
Compiler Architecture In more detail: Intermediate Language Front End – language specific Back End – machine specific Source Language Target Language Analysis Synthesis •Separation of Concerns •Retar getin g 3 g g CS308 Compiler Theory
Phases of A Compiler Source LexicalSyntax Semantic Intermediate Code Code Target Program AnalyzerAnalyzer Generator Optimizer GeneratorProgram Each phase transforms the source program from one representation into another representation. They communicate with error handlers. They communicate with the symbol table. CS308 Compiler Theory 4
Phases of A Compiler Lexical Analyzer Semantic Analyzer Syntax Analyzer Intermediate Code Generator Code Optimizer Code Generator Target Program Source Program • Each phase transforms the source program from one representation into another re presentation. • They communicate with error handlers. • They communicate with the symbol table. CS308 Compiler Theory 4
Lexical Analyzer Lexical Analyzer reads the source program character by character and returns the tokens of the source program. A token describes a pattern of characters having same meaning in the source program.(such as identifiers,operators,keywords,numbers, delimeters and so on) Ex: newval :oldval 12 => tokens: newval identifier = assignment operator oldval identifier + add operator 12 a number Puts information about identifiers into the symbol table. Regular expressions are used to describe tokens(lexical constructs). A (Deterministic)Finite State Automaton can be used in the implementation of a lexical analyzer. CS308 Compiler Theory 5
Lexical Analyzer • Lexical Analyzer reads the source program character by character and ret rns the returns the t ko ens of the so rce program of the source program. • A token describes a pattern of characters having same meaning in the source program. (such as identifiers, operators, keywords, numbers, source program. (such as identifiers, operators, keywords, numbers, delimeters and so on) Ex: newval := oldval + 12 => tokens: newval identifier := assignment operator oldval identifier + add operator 12 a number • Puts information about identifiers into the symbol table. • R l i d t d ib t k (l i l t t ) Regular expressions are used to describe tokens (lexical constructs). • A (Deterministic) Finite State Automaton can be used in the implementation of a lexical analyzer. 5 implementation of a lexical analyzer. CS308 Compiler Theory
Syntax Analyzer A Syntax Analyzer creates the syntactic structure(generally a parse tree)of the given program. A syntax analyzer is also called as a parser. .A parse tree describes a syntactic structure. assgstmt identifier expression In a parse tree,all terminals are at leaves. newval expression expression All inner nodes are non-terminals in a context free grammar. identifier number oldval 12 CS308 Compiler Theory 6
Syntax Analyzer • A Syntax Analyzer creates the syntactic structure (generally a parse tree) fh i o t e given program. • A syntax analyzer is also called as a parser. • A parse tree describes a syntactic structure. assgstmt identifier := expression • In a parse tree, all terminals are at leaves. newval expression + expression All i d t i li identifier number • All inner nodes are non-terminals in a context free grammar. oldval 12 CS308 Compiler Theory 6
Syntax Analyzer (CFG) The syntax of a language is specified by a context free grammar (CFG). The rules in a CFG are mostly recursive. A syntax analyzer checks whether a given program satisfies the rules implied by a CFG or not. If it satisfies,the syntax analyzer creates a parse tree for the given program. Ex:We use BNF(Backus Naur Form)to specify a CFG assgstmt -identifier :=expression expression -identifier expression -number expression -expression expression CS308 Compiler Theory 7
Syntax Analyzer (CFG) • The syntax of a language is specified by a context free grammar ( ) CFG ). • The rules in a CFG are mostly recursive. • A syntax analyzer checks whether a given program satisfies the rules implied by a CFG or not. – If it satisfies the syntax analyzer creates a parse tree for the given program If it satisfies, the syntax analyzer creates a parse tree for the given program. • Ex: We use BNF (Backus Naur Form) to specify a CFG We use BNF (Backus Naur Form) to specify a CFG assgstmt -> identifier := expression ex pression -> identifie r expression -> number expression -> expression + expression CS308 Compiler Theory 7
Syntax Analyzer versus Lexical Analyzer Which constructs of a program should be recognized by the lexical analyzer,and which ones by the syntax analyzer? Both of them do similar things;But the lexical analyzer deals with simple non-recursive constructs of the language. The syntax analyzer deals with recursive constructs of the language. The lexical analyzer simplifies the job of the syntax analyzer. The lexical analyzer recognizes the smallest meaningful units(tokens)in a source program. -The syntax analyzer works on the smallest meaningful units(tokens)in a source program to recognize meaningful structures in our programming language CS308 Compiler Theory 8
Syntax Analyzer versus Lexical Analyzer • Which constructs of a program should be recognized by the lexical anal d hi h b h l lyzer, an d whi c h ones by t he syntax ana lyzer? – Both of them do similar things; But the lexical analyzer deals with simple non-recursive constructs of the lan g g ua ge. – The syntax analyzer deals with recursive constructs of the language. – The lexical analyzer simplifies the job of the syntax analyzer. – The le ical anal er recogni es the smallest meaningf l nits (tokens) in a so rce program The le xical analyzer recogni zes the smallest meaningful units (tokens) in a so urce program. – The syntax analyzer works on the smallest meaningful units (tokens) in a source program to recognize meaningful structures in our programming language. CS308 Compiler Theory 8
Parsing Techniques Depending on how the parse tree is created,there are different parsing techniques. These parsing techniques are categorized into two groups: Top-Down Parsing, Bottom-Up Parsing ● Top-Down Parsing: Construction of the parse tree starts at the root,and proceeds towards the leaves -Efficient top-down parsers can be easily constructed by hand. Recursive Predictive Parsing,Non-Recursive Predictive Parsing(LL Parsing). ·Bottom-Up Parsing: -Construction of the parse tree starts at the leaves,and proceeds towards the root. -Normally efficient bottom-up parsers are created with the help of some software tools. Bottom-up parsing is also known as shift-reduce parsing. Operator-Precedence Parsing-simple,restrictive,easy to implement LR Parsing-much general form of shift-reduce parsing,LR,SLR,LALR CS308 Compiler Theory 9
Parsing Techniques • Depending on how the parse tree is created, there are different parsing techni ques. • These parsing techniques are categorized into two groups: – Top-Down Parsing, – Bottom-Up Parsing • Top-Down Parsing: – Construction of the parse tree starts at the root, and proceeds towards the leaves. – Efficient top-down parsers can be easily constructed by hand. – Recursive Predictive Parsing, Non-Recursive Predictive Parsing (LL Parsing). • Bottom-Up Parsing: – Construction of the parse tree starts at the leaves, and proceeds towards the root. – Normally efficient bottom Normally efficient bottom -up parsers are created with the help of some software tools up parsers are created with the help of some software tools. – Bottom-up parsing is also known as shift-reduce parsing. – Operator-Precedence Parsing – simple, restrictive, easy to implement LR P i h l f f hif d i LR SLR LALR 9 – LR Parsing – muc h general form o f shift-re duce parsing, LR, SLR, LALR CS308 Compiler Theory
Semantic Analyzer A semantic analyzer checks the source program for semantic errors and collects the type information for the code generation. Type-checking is an important part of semantic analyzer. 。 Normally semantic information cannot be represented by a context-free language used in syntax analyzers. Context-free grammars used in the syntax analysis are integrated with attributes (semantic rules) the result is a syntax-directed translation, -Attribute grammars ·EX: newval :oldval 12 The type of the identifier newval must match with type of the expression (oldval+12) CS308 Compiler Theory 10
Semantic Analyzer • A semantic analyzer checks the source program for semantic errors and coll h i f i f h d i llects the type information for the code generation. • Type-checking is an important part of semantic analyzer. • Normally semantic information cannot be represented by a context-free language used in syntax analyzers. • Ctt on ex -f d i th t l i i t t d ith free grammars used in the syntax analysis are integrated with attributes (semantic rules) – the result is a syntax the result is a syntax-directed translation directed translation, – Attribute grammars • Ex: newval := oldval + 12 • The type of the identifier newval must match with type of the expression (oldval+12) 10 The type of the identifier newval must match with type of the expression (oldval 12) CS308 Compiler Theory