Compiler Construction

Slides:



Advertisements
Similar presentations
lec02-parserCFG March 27, 2017 Syntax Analyzer
Advertisements

Lecture # 8 Chapter # 4: Syntax Analysis. Practice Context Free Grammars a) CFG generating alternating sequence of 0’s and 1’s b) CFG in which no consecutive.
1 Parsing The scanner recognizes words The parser recognizes syntactic units Parser operations: Check and verify syntax based on specified syntax rules.
Grammars, Languages and Parse Trees. Language Let V be an alphabet or vocabulary V* is set of all strings over V A language L is a subset of V*, i.e.,
By Neng-Fa Zhou Syntax Analysis lexical analyzer syntax analyzer semantic analyzer source program tokens parse tree parser tree.
Slide 1 Chapter 2-b Syntax, Semantics. Slide 2 Syntax, Semantics - Definition The syntax of a programming language is the form of its expressions, statements.
COP4020 Programming Languages
(2.1) Grammars  Definitions  Grammars  Backus-Naur Form  Derivation – terminology – trees  Grammars and ambiguity  Simple example  Grammar hierarchies.
The College of Saint Rose CIS 433 – Programming Languages David Goldschmidt, Ph.D. from Concepts of Programming Languages, 9th edition by Robert W. Sebesta,
CPSC 388 – Compiler Design and Construction Parsers – Context Free Grammars.
BİL 744 Derleyici Gerçekleştirimi (Compiler Design)1 Syntax Analyzer Syntax Analyzer creates the syntactic structure of the given source program. This.
1 Chapter 3 Describing Syntax and Semantics. 3.1 Introduction Providing a concise yet understandable description of a programming language is difficult.
Context-Free Grammars
Chapter 5 Context-Free Grammars
PART I: overview material
Lecture # 9 Chap 4: Ambiguous Grammar. 2 Chomsky Hierarchy: Language Classification A grammar G is said to be – Regular if it is right linear where each.
CFG1 CSC 4181Compiler Construction Context-Free Grammars Using grammars in parsers.
LESSON 04.
Syntax Analysis - Parsing Compiler Design Lecture (01/28/98) Computer Science Rensselaer Polytechnic.
Unit-3 Parsing Theory (Syntax Analyzer) PREPARED BY: PROF. HARISH I RATHOD COMPUTER ENGINEERING DEPARTMENT GUJARAT POWER ENGINEERING & RESEARCH INSTITUTE.
Syntax Analyzer (Parser)
1 Pertemuan 7 & 8 Syntax Analysis (Parsing) Matakuliah: T0174 / Teknik Kompilasi Tahun: 2005 Versi: 1/6.
Overview of Previous Lesson(s) Over View 3 Model of a Compiler Front End.
Chapter 4: Syntax analysis Syntax analysis is done by the parser. –Detects whether the program is written following the grammar rules and reports syntax.
Parser: CFG, BNF Backus-Naur Form is notational variant of Context Free Grammar. Invented to specify syntax of ALGOL in late 1950’s Uses ::= to indicate.
1 Topic #4: Syntactic Analysis (Parsing) CSC 338 – Compiler Design and implementation Dr. Mohamed Ben Othman ( )
Compiler Construction Lecture Five: Parsing - Part Two CSC 2103: Compiler Construction Lecture Five: Parsing - Part Two Joyce Nakatumba-Nabende 1.
COMP 3438 – Part II - Lecture 4 Syntax Analysis I Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
Syntax Analysis By Noor Dhia Syntax analysis:- Syntax analysis or parsing is the most important phase of a compiler. The syntax analyzer considers.
Compiler Chapter 5. Context-free Grammar Dept. of Computer Engineering, Hansung University, Sung-Dong Kim.
Last Chapter Review Source code characters combination lexemes tokens pattern Non-Formalization Description Formalization Description Regular Expression.
Introduction to Parsing
Chapter 3 – Describing Syntax
PROGRAMMING LANGUAGES
lec02-parserCFG May 8, 2018 Syntax Analyzer
CS510 Compiler Lecture 4.
Chapter 3 Context-Free Grammar and Parsing
Introduction to Parsing (adapted from CS 164 at Berkeley)
Chapter 3 – Describing Syntax
Syntax Specification and Analysis
What does it mean? Notes from Robert Sebesta Programming Languages
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Context-Free Grammars
CS 363 Comparative Programming Languages
CSE 3302 Programming Languages
Compiler Design 4. Language Grammars
Context-Free Grammars
Context-Free Grammars
Syntax Analysis source program lexical analyzer tokens syntax analyzer
Programming Language Syntax 2
Chapter 2: A Simple One Pass Compiler
Lecture 7: Introduction to Parsing (Syntax Analysis)
CSC 4181Compiler Construction Context-Free Grammars
R.Rajkumar Asst.Professor CSE
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
CSC 4181 Compiler Construction Context-Free Grammars
Theory of Computation Lecture #
Context-Free Grammars
Chapter 3 Describing Syntax and Semantics.
BNF 9-Apr-19.
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Discrete Maths 13. Grammars Objectives
lec02-parserCFG May 27, 2019 Syntax Analyzer
Context-Free Grammars
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Faculty of Computer Science and Information System
Presentation transcript:

Compiler Construction Chapter 3 Compiler Construction Dr K. V. N. Sunitha

Compiler Construction Syntax Analysis Syntax analysis is done by the parser. Detects and reports any syntax errors. Produces a parse tree from which intermediate code can be generated. Source program Lexical analyzer Token Parser Rest of front end Symbol table Int. code Parse tree Compiler Construction Dr K. V. N. Sunitha

Compiler Construction The syntax of a programming language is described by a context-free grammar (Backus-Naur Form (BNF)). Context-free Grammars Precise and easy way to specify the syntactical structure of a programming language Efficient recognition methods exist Natural specification of many “recursive” constructs: expr -> expr + expr | term Compiler Construction Dr K. V. N. Sunitha

Compiler Construction A grammar G = (V, T, P, S) is said to be context free if all production in P have the form A → x where A € V and x € (V ∪ T)*. A CFG consists of the following components: (V,T,P,S) V: Nonterminals The set of variables, also known as nonterminals. Each variable represents a set of strings. It is simply a language. T: Terminals The set of terminals, which are a set of symbols that form the strings of the language. It is also known as terminal symbols. e.g. if, else, id Compiler Construction Dr K. V. N. Sunitha

Compiler Construction P : Productions Rules that determine how strings are formed V -> (V|T)* S: Start symbol (£V) Denotes the set of strings of L(G) Example: Terminals T: {id, +, -, *, /} Nonterminals {e} Start symbol e e-> i e -> e + e e -> e – e e -> e * e e -> e / e Grammar Compiler Construction Dr K. V. N. Sunitha

Compiler Construction Shorthands and Derivations E -> E + E | E * E | (E) | - E | <id> E => - E “E derives -E” => derives in 1 step ex: aAb=>awb , if A->w => derive in n (0..) steps a1=>a2=>…=>am, written as a1=>am * Compiler Construction Dr K. V. N. Sunitha

Compiler Construction Derivation It is a process of defining a string out of a grammar by application of the rules begins from the starting symbol is called derivation. The start-symbol defines a language – The result contains no non-terminals Example (e is a non-terminal symbol; number, +, -, *, / are terminal symbols) e -> i This defines strings e -> e + e i+i e -> e – e i*i-i+i e -> e * e i-i-i e -> e / e i-i*i+i/i*i-i……. Compiler Construction Dr K. V. N. Sunitha

Compiler Construction Derivation LMD Left-most derivation Every step left-most non-terminal is replaced first RMD Right-most derivation Every step right-most non-terminal is replaced first Compiler Construction Dr K. V. N. Sunitha

Example: Derive id + id * id from the Grammar: e => e + e => id + e LMD RMD e => e + e => id + e =>id +e * e =>id + id * e => id +id *id e-> i d e -> e + e e -> e – e e -> e * e e -> e / e e => e * e => e* id =>e + e * id =>e + id * id => id +id *id Compiler Construction Dr K. V. N. Sunitha

Compiler Construction More Definitions L(G) language generated by G = set of strings derived from S S => +w : w sentence of G (w string of terminals) S =>+ α : α sentential form of G (string cannot contain nonterminals) G and G’ are equivalent : L(G) = L(G’) A language generated by a grammar (of the form shown) is called a context-free language Compiler Construction Dr K. V. N. Sunitha

Compiler Construction Parse tree: Derivation tree shows how a word is derived from a Context Free grammar ‘G’. These trees are called syntax trees, parse trees, derivation The root is the start symbol The leaf node is terminal/ token / the empty string Each interior node is a nonterminal If S is a non-terminal and xyz are the children of that node from left to right, then S->xyz is a production of the grammar. Compiler Construction Dr K. V. N. Sunitha

Parse Tree/ Derivation Tree: e-> i d e -> e + e e -> e – e e -> e * e e -> e / e LMD RMD e e + e e e e * e e id e * id + e id id id id Compiler Construction Dr K. V. N. Sunitha

Compiler Construction Grammars Ambiguous Grammars More than one LMD/RMD for a given string LMD/RMD represents different parse trees More than one parse tree Unambiguous Grammars One LMD/RMD for a given string LMD/RMD represents the same parse trees One parse tree Usually want to have unambiguous grammars E.g. want to have just one evaluation order: Compiler Construction Dr K. V. N. Sunitha

Compiler Construction S →Sa | aS | a , w=aaa How many parse trees we get? Ans: 4 S → aSbS | bSaS | ε , w=abab Ans: 2 Compiler Construction Dr K. V. N. Sunitha

Another Classification of Grammars Left Recursive i.e., A → A α |β • L(G)= β α * Danger of going into infinite loop Certain parsers does not use left Recursive grammars Right Recursive A → a A | β L(G)= α * β No such danger Compiler Construction Dr K. V. N. Sunitha

Eliminating Left Recursion A → A α | β rewrite as A → β A’ A’ → aA’ | ε Eliminate left recursion in the following grammars 1. S → ( L ) | a L → L , S | S Ans: S → ( L ) | a L → S L’ L’ → ,SL’ | ε Compiler Construction Dr K. V. N. Sunitha

Compiler Construction 2. S → A, A → Ad | Ae |aB|aC, B → bBC|f, C → g 3. S → SSS | a 4. S → S0S1S | 01 5. S → Aa | b , A → Ac | S d |ε Compiler Construction Dr K. V. N. Sunitha

Eliminating Common Prefixes Problem: Needs backtracking To avoid back tracking, left factor the grammar. Left factoring: A → α A’ A’ → β1| β2 | β3 Compiler Construction Dr K. V. N. Sunitha

Compiler Construction Left Factoring S → iEtS | iEtSeS | a E → b After left factoring S → iEtS S’ | a S’ → ε | eS Compiler Construction Dr K. V. N. Sunitha

More Examples on Left Factoring S → abc | abd |f Ans: S → abS’ | f S’→ c | d S → abc | abd |ae |f Ans: S → abS’ | ae |f But this is not a solution as still ‘a’ is common prefix in ‘S’. S → aS’’ | f S’’→ bS’ | e Compiler Construction Dr K. V. N. Sunitha

Compiler Construction Rewriting Ambiguous Expression Grammar to Equivalent Unambiguous Grammar Try to understand what is lacking in the ambiguous grammar, i.e. why grammar is ambiguous. In the example expression grammar, precedence and associativity are not taken care of. To get equivalent unambiguous grammar , rewrite the grammar by imposing precedence and associativity rules. Compiler Construction Dr K. V. N. Sunitha

Compiler Construction To take care of precedence, define the productions starting with operator with lowest precedence to highest precedence E → E + T | T E → E + E T → T * T T → T * F | F F → id F → F ↑ P | F P → id | E-T | T/F E -> E + E |E * E | id Compiler Construction Dr K. V. N. Sunitha

Compiler Construction To take care of associativity, write the production as left recursive (A → A α | β) if operator is left associative. write the production as right recursive (A → α A | β ) if operator is right associative. E → E+T | T T → i E E + T i E + T T i E → T+E | T T → i E T + E i E + T T i string :i+i+i Compiler Construction Dr K. V. N. Sunitha