Download presentation
Presentation is loading. Please wait.
1
Compiler Construction
Chapter 3 Compiler Construction Dr K. V. N. Sunitha
2
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
3
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
4
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
5
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
6
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
7
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
8
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
9
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
10
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
11
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
12
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
13
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
14
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
15
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
16
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
17
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
18
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
19
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
20
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
21
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
22
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
23
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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.