Syntax Analysis Sections 4.1-4.4:.

Slides:



Advertisements
Similar presentations
Parsing II : Top-down Parsing
Advertisements

lec02-parserCFG March 27, 2017 Syntax Analyzer
Lecture # 11 Grammar Problems.
Parsing — Part II (Ambiguity, Top-down parsing, Left-recursion Removal)
Compiler Constreuction 1 Chapter 4 Syntax Analysis Topics to cover: Context-Free Grammars: Concepts and Notation Writing and rewriting a grammar Syntax.
1 Chapter 4: Top-Down Parsing. 2 Objectives of Top-Down Parsing an attempt to find a leftmost derivation for an input string. an attempt to construct.
Parsing II : Top-down Parsing Lecture 7 CS 4318/5331 Apan Qasem Texas State University Spring 2015 *some slides adopted from Cooper and Torczon.
– 1 – CSCE 531 Spring 2006 Lecture 7 Predictive Parsing Topics Review Top Down Parsing First Follow LL (1) Table construction Readings: 4.4 Homework: Program.
Lecture 21: Languages and Grammars. Natural Language vs. Formal Language.
BİL 744 Derleyici Gerçekleştirimi (Compiler Design)1 Syntax Analyzer Syntax Analyzer creates the syntactic structure of the given source program. This.
Context Free Grammar. Introduction Why do we want to learn about Context Free Grammars?  Used in many parsers in compilers  Yet another compiler-compiler,
Chapter 4. Syntax Analysis (1). 2 Application of a production  A  in a derivation step  i   i+1.
1 COMP313A Programming Languages Syntax Analysis (2)
1 Problems with Top Down Parsing  Left Recursion in CFG May Cause Parser to Loop Forever.  Indeed:  In the production A  A  we write the program procedure.
Section 12.4 Context-Free Language Topics
1 Context free grammars  Terminals  Nonterminals  Start symbol  productions E --> E + T E --> E – T E --> T T --> T * F T --> T / F T --> F F --> (F)
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.
1 Topic #4: Syntactic Analysis (Parsing) CSC 338 – Compiler Design and implementation Dr. Mohamed Ben Othman ( )
Exercises on Chomsky Normal Form and CYK parsing
Compiler Construction Lecture Five: Parsing - Part Two CSC 2103: Compiler Construction Lecture Five: Parsing - Part Two Joyce Nakatumba-Nabende 1.
Syntax Analysis Or Parsing. A.K.A. Syntax Analysis –Recognize sentences in a language. –Discover the structure of a document/program. –Construct (implicitly.
Last Chapter Review Source code characters combination lexemes tokens pattern Non-Formalization Description Formalization Description Regular Expression.
Introduction to Parsing
Problems with Top Down Parsing
lec02-parserCFG May 8, 2018 Syntax Analyzer
Parsing — Part II (Top-down parsing, left-recursion removal)
CS510 Compiler Lecture 4.
Context free grammars Terminals Nonterminals Start symbol productions
Syntax Specification and Analysis
Introduction to Parsing (adapted from CS 164 at Berkeley)
Syntax Specification and Analysis
Parsing — Part II (Top-down parsing, left-recursion removal)
lec02-parserCFG July 30, 2018 Syntax Analyzer
Simplifications of Context-Free Grammars
Midterm Content – Excepted from PPTs
Compiler Construction
Syntax versus Semantics
Top-Down Parsing.
Syntax Analysis Sections :.
Parsing Techniques.
3.2 Language and Grammar Left Factoring Unclear productions
CSE 3302 Programming Languages
Lecture 3: Introduction to Syntax (Cont’)
Syntax Analysis Sections :.
Parsing & Context-Free Grammars Hal Perkins Autumn 2011
(Slides copied liberally from Ruth Anderson, Hal Perkins and others)
Top-Down Parsing CS 671 January 29, 2008.
Lecture 7 Predictive Parsing
Some CFL Practicalities
Syntax Analysis source program lexical analyzer tokens syntax analyzer
Chapter 4: Syntax Analysis
5. Context-Free Grammars and Languages
Chapter 2: A Simple One Pass Compiler
Lecture 6 Grammar Modifications
Lecture 7: Introduction to Parsing (Syntax Analysis)
Lecture 8: Top-Down Parsing
R.Rajkumar Asst.Professor CSE
Programming Language Syntax 5
Introduction to Parsing
Introduction to Parsing
Parsing — Part II (Top-down parsing, left-recursion removal)
Lecture 7 Predictive Parsing
BNF 9-Apr-19.
LEFT RECURSION.
Recursive descent parsing
lec02-parserCFG May 27, 2019 Syntax Analyzer
Recursive descent parsing
Parsing CSCI 432 Computer Science Theory
Presentation transcript:

Syntax Analysis Sections 4.1-4.4:

Removing Ambiguity Take Original Grammar: S  i E t S | i E t S e S stmt  if expr then stmt | if expr then stmt else stmt | other (any other statement) Or to write more simply: S  i E t S | i E t S e S | s E  a The problem string: i a t i a t s e s

Revise to remove ambiguity: S  i E t S | i E t S e S | s E  a S  M | U M  i E t M e M | s U  i E t S | i E t M e U E  a Try the above on i a t i a t s e s stmt  matched_stmt | unmatched_stmt matched_stmt  if expr then matched_stmt else matched_stmt | other unmatched_stmt  if expr then stmt | if expr then matched_stmt else unmatched_stmt

Resolving Difficulties : Left Recursion A left recursive grammar has rules that support the derivation : A  A, for some . + Top-Down parsing can’t reconcile this type of grammar, since it could consistently make choice which wouldn’t allow termination. A  A  A  A … etc. A A |  Take left recursive grammar: A  A |  To the following: A  A’ A’  A’ | 

Why is Left Recursion a Problem ? Derive : id + id + id E  E + T  Consider: E  E + T | T T  T * F | F F  ( E ) | id How can left recursion be removed ? E  E + T | T What does this generate? E  E + T  T + T E  E + T  E + T + T  T + T + T  How does this build strings ? What does each string have to start with ?

Resolving Difficulties : Left Recursion (2) Informal Discussion: Take all productions for A and order as: A  A1 | A2 | … | Am | 1 | 2 | … | n Where no i begins with A. Now apply concepts of previous slide: A  1A’ | 2A’ | … | nA’ A’  1A’ | 2A’ | … | m A’ |  For our example: E  E + T | T T  T * F | F F  ( E ) | id E  TE’ E’  + TE’ |  T  FT’ T’  * FT’ | 

Resolving Difficulties : Left Recursion (3) Problem: If left recursion is two-or-more levels deep, this isn’t enough S  Aa | b A  Ac | Sd |  S  Aa  Sda Algorithm: Input: Grammar G with ordered Non-Terminals A1, ..., An Output: An equivalent grammar with no left recursion Arrange the non-terminals in some order A1=start NT,A2,…An for i := 1 to n do begin for j := 1 to i – 1 do begin replace each production of the form Ai  Aj by the productions Ai  1 | 2 | … | k where Aj  1|2|…|k are all current Aj productions; end eliminate the immediate left recursion among Ai productions

Using the Algorithm Apply the algorithm to: A1  A2a | b|  A2  A2c | A1d i = 1 For A1 there is no left recursion i = 2 for j=1 to 1 do Take productions: A2  A1 and replace with A2  1  | 2  | … | k | where A1 1 | 2 | … | k are A1 productions in our case A2  A1d becomes A2  A2ad | bd | d What’s left: A1 A2a | b |  A2  A2 c | A2 ad | bd | d Are we done ?

Using the Algorithm (2) No ! We must still remove A2 left recursion ! A1 A2a | b |  A2  A2 c | A2 ad | bd | d Recall: A  A1 | A2 | … | Am | 1 | 2 | … | n A  1A’ | 2A’ | … | nA’ A’  1A’ | 2A’ | … | m A’ |  Apply to above case. What do you get ?

Removing Difficulties : -Moves Transformation: In order to remove A  find all rules of the form B uAv and add the rule B uv to the grammar G. Why does this work ? Examples: E  TE’ E’  + TE’ |  T  FT’ T’  * FT’ |  F  ( E ) | id A1  A2 a | b A2  bd A2’ | A2’ A2’  c A2’ | bd A2’ | 

Removing Difficulties : Cycles How would cycles be removed ? Make sure every production is adding some terminal(s) (except a single  -production in the start NT)… e.g. S  SS | ( S ) |  Has a cycle: S  SS  S Transformation: substitute A uBv by the productions A ua1v| ... | uanv assuming that all the productions for B are B a1| ... | an S   Transform to: S  S ( S ) | ( S ) | 

Removing Difficulties : Left Factoring Problem : Uncertain which of 2 rules to choose: stmt  if expr then stmt else stmt | if expr then stmt When do you know which one is valid ? What’s the general form of stmt ? A  1 | 2  : if expr then stmt 1: else stmt 2 :  Transform to: A   A’ A’  1 | 2 EXAMPLE: stmt  if expr then stmt rest rest  else stmt | 

Resolving Grammar Problems Note: Not all aspects of a programming language can be represented by context free grammars / languages. Examples: 1. Declaring ID before its use 2. Valid typing within expressions 3. Parameters in definition vs. in call These features are call context-sensitive and define yet another language class, CSL. Reg. Lang. CFLs CSLs

Context-Sensitive Languages - Examples L1 = { wcw | w is in (a | b)* } : Declare before use L2 = { an bm cn dm | n  1, m  1 } an bm : formal parameter cn dm : actual parameter What does it mean when L is context-sensitive ?

How do you show a Language is a CFL? L3 = { w c wR | w is in (a | b)* } L4 = { an bm cm dn | n  1, m  1 } L5 = { an bn cm dm | n  1, m  1 } L6 = { an bn | n  1 }

Solutions L3 = { w c wR | w is in (a | b)* } L4 = { an bm cm dn | n  1, m  1 } L5 = { an bn cm dm | n  1, m  1 } L6 = { an bn | n  1 } S  a S a | b S b | c S  a S d | a A d A  b A c | bc S  XY X  a X b | ab Y  c Y d | cd S  a S b | ab

Example of CS Grammar L2 = { an bm cn dm | n  1, m  1 } S  a A c H A  a A c | B B  b B D | b D D c  c D D D H  D H d c D H  c d