6/4/2016IT 3271 The most practical Parsers: Predictive parser: 1.input (token string) 2.Stacks, parsing table 3.output (syntax tree, intermediate codes)

Slides:



Advertisements
Similar presentations
Compiler Construction
Advertisements

A question from last class: construct the predictive parsing table for this grammar: S->i E t S e S | i E t S | a E -> B.
1 Parsing The scanner recognizes words The parser recognizes syntactic units Parser operations: Check and verify syntax based on specified syntax rules.
Chap. 5, Top-Down Parsing J. H. Wang Mar. 29, 2011.
Lecture # 11 Grammar Problems.
YANGYANG 1 Chap 5 LL(1) Parsing LL(1) left-to-right scanning leftmost derivation 1-token lookahead parser generator: Parsing becomes the easiest! Modifying.
Mooly Sagiv and Roman Manevich School of Computer Science
Top-Down Parsing.
By Neng-Fa Zhou Syntax Analysis lexical analyzer syntax analyzer semantic analyzer source program tokens parse tree parser tree.
CS Summer 2005 Top-down and Bottom-up Parsing - a whirlwind tour June 20, 2005 Slide acknowledgment: Radu Rugina, CS 412.
Context-Free Grammars Lecture 7
ISBN Chapter 4 Lexical and Syntax Analysis The Parsing Problem Recursive-Descent Parsing.
1 The Parser Its job: –Check and verify syntax based on specified syntax rules –Report errors –Build IR Good news –the process can be automated.
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.
Professor Yihjia Tsai Tamkang University
COS 320 Compilers David Walker. last time context free grammars (Appel 3.1) –terminals, non-terminals, rules –derivations & parse trees –ambiguous grammars.
Chapter 6: Top-Down Parser1 Compiler Designs and Constructions Chapter 6: Top-Down Parser Objectives: Types of Parsers Backtracking Vs. Non-backtracking.
1 Bottom-up parsing Goal of parser : build a derivation –top-down parser : build a derivation by working from the start symbol towards the input. builds.
Chapter 3 Chang Chi-Chung Parse tree intermediate representation The Role of the Parser Lexical Analyzer Parser Source Program Token Symbol.
CPSC 388 – Compiler Design and Construction
CSC3315 (Spring 2009)1 CSC 3315 Lexical and Syntax Analysis Hamid Harroud School of Science and Engineering, Akhawayn University
Syntax and Semantics Structure of programming languages.
Parsing Chapter 4 Parsing2 Outline Top-down v.s. Bottom-up Top-down parsing Recursive-descent parsing LL(1) parsing LL(1) parsing algorithm First.
Review: –How do we define a grammar (what are the components in a grammar)? –What is a context free grammar? –What is the language defined by a grammar?
Top-Down Parsing - recursive descent - predictive parsing
4 4 (c) parsing. Parsing A grammar describes the strings of tokens that are syntactically legal in a PL A recogniser simply accepts or rejects strings.
1 Chapter 5 LL (1) Grammars and Parsers. 2 Naming of parsing techniques The way to parse token sequence L: Leftmost R: Righmost Top-down  LL Bottom-up.
Chapter 5 Top-Down Parsing.
BİL 744 Derleyici Gerçekleştirimi (Compiler Design)1 Syntax Analyzer Syntax Analyzer creates the syntactic structure of the given source program. This.
4 4 (c) parsing. Parsing A grammar describes syntactically legal strings in a language A recogniser simply accepts or rejects strings A generator produces.
10/13/2015IT 3271 Tow kinds of predictive parsers: Bottom-Up: The syntax tree is built up from the leaves Example: LR(1) parser Top-Down The syntax tree.
Profs. Necula CS 164 Lecture Top-Down Parsing ICOM 4036 Lecture 5.
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.
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Syntax and Semantics Structure of programming languages.
4 4 (c) parsing. Parsing A grammar describes syntactically legal strings in a language A recogniser simply accepts or rejects strings A generator produces.
1 Section 12.3 Context-Free Parsing We know (via a theorem) that the context-free languages are exactly those languages that are accepted by PDAs. When.
COP4020 Programming Languages Parsing Prof. Xin Yuan.
Chapter 4 Top-Down Parsing Recursive-Descent Gang S. Liu College of Computer Science & Technology Harbin Engineering University.
Parsing Top-Down.
Bottom-Up Parsing David Woolbright. The Parsing Problem Produce a parse tree starting at the leaves The order will be that of a rightmost derivation The.
Top-Down Parsing CS 671 January 29, CS 671 – Spring Where Are We? Source code: if (b==0) a = “Hi”; Token Stream: if (b == 0) a = “Hi”; Abstract.
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)
Top-down Parsing. 2 Parsing Techniques Top-down parsers (LL(1), recursive descent) Start at the root of the parse tree and grow toward leaves Pick a production.
Unit-3 Parsing Theory (Syntax Analyzer) PREPARED BY: PROF. HARISH I RATHOD COMPUTER ENGINEERING DEPARTMENT GUJARAT POWER ENGINEERING & RESEARCH INSTITUTE.
Top-Down Parsing.
Syntax Analyzer (Parser)
1 Pertemuan 7 & 8 Syntax Analysis (Parsing) Matakuliah: T0174 / Teknik Kompilasi Tahun: 2005 Versi: 1/6.
Parsing methods: –Top-down parsing –Bottom-up parsing –Universal.
COMP 3438 – Part II-Lecture 5 Syntax Analysis II Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
UMBC  CSEE   1 Chapter 4 Chapter 4 (b) parsing.
COMP 3438 – Part II-Lecture 6 Syntax Analysis III Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
Lecture # 10 Grammar Problems. Problems with grammar Ambiguity Left Recursion Left Factoring Removal of Useless Symbols These can create problems for.
Spring 16 CSCI 4430, A Milanova 1 Announcements HW1 due on Monday February 8 th Name and date your submission Submit electronically in Homework Server.
Syntax Analysis By Noor Dhia Syntax analysis:- Syntax analysis or parsing is the most important phase of a compiler. The syntax analyzer considers.
Syntax Analysis By Noor Dhia Left Recursion: Example1: S → S0s1s | 01 The grammar after eliminate left recursion is: S → 01 S’ S' → 0s1sS’
Syntax and Semantics Structure of programming languages.
Parsing COMP 3002 School of Computer Science. 2 The Structure of a Compiler syntactic analyzer code generator program text interm. rep. machine code tokenizer.
lec02-parserCFG May 8, 2018 Syntax Analyzer
Chapter 4 - Parsing CSCE 343.
Programming Languages Translator
Parsing and Parser Parsing methods: top-down & bottom-up
Lecture #12 Parsing Types.
Table-driven parsing Parsing performed by a finite state machine.
Compiler Construction
Compiler Construction
Top-Down Parsing.
4 (c) parsing.
Compiler Design 7. Top-Down Table-Driven Parsing
lec02-parserCFG May 27, 2019 Syntax Analyzer
Presentation transcript:

6/4/2016IT 3271 The most practical Parsers: Predictive parser: 1.input (token string) 2.Stacks, parsing table 3.output (syntax tree, intermediate codes) No back tracking.

6/4/2016IT 3272 Tow kinds of predictive parsers: Bottom-Up: The syntax tree is built up from the leaves Example: LR(1) parser Left to right scanning Rightmost derivations 1 symbol look-ahead Left to right scanning Leftmost derivations 1 symbol look-ahead Top-Down The syntax tree is built up from the root Example: LL(1) parser

6/4/2016IT 3273 LL(1) Grammar 1.S  A S b 2.S  C 3.A  a 4.C  c C 5.C  abc$ S1222 A3 C545 LL(1) Parsing Table aaaccbbb S  ASb  aSb  aASbb  aaSbb  aaASbbb  aaaSbbb  aaaCbbb  aaacCbbb  aaaccCbbb  aaaccbbb A left-most derivation end-of-file symbol

6/4/2016IT 3274 Recursive-descent Parser 1.S  A S b 2.S  C 3.A  a 4.C  c C 5.C  abc$ S1222 A3 C545 LL(1) Parsing Table S():Switch(token) { case a: A();S();get(b); build S  ASb; break; case b: C(); build S  C; break; case c: C(); built S  C; break; case $: C(); built S  C; break; } all possible terminal and end-of-file symbols

6/4/2016IT 3275 Recursive-descent Parser A():Switch(token) { case a: get(a); build A  a; break; case b: error; break; case c: error; break; case $: error; break; } 1.S  A S b 2.S  C 3.A  a 4.C  c C 5.C  abc$ S1222 A3 C545 LL(1) Parsing Table

6/4/2016IT 3276 Recursive-descent Parser C():Switch(token) { case a: error; break; case b: build C  ; break; case c: get(c);C(); built C  cC; break; case $: build C  ; break; } 1.S  A S b 2.S  C 3.A  a 4.C  c C 5.C  abc$ S1222 A3 C545 LL(1) Parsing Table

IT 3277 LL(1) Parsing 1.S  A S b 2.S  C 3.A  a 4.C  c C 5.C  abc$ S1222 A3 C545 S(); A();S();get(b); get(a);S();get(b); S();get(b); A();S();get(b);get(b); get(a);S();get(b);get(b); S();get(b);get(b); A();S();get(b);get(b);get(b); get(a);S();get(b);get(b);get(b); S();get(b);get(b);get(b); C();get(b);get(b);get(b); get(c);C();get(b);get(b);get(b); C();get(b);get(b);get(b); get(c);C();get(b);get(b);get(b); C();get(b);get(b);get(b); get(b);get(b);get(b); get(b);get(b); get(b); aaaccbbb S  ASb  aSb  aASbb  aaSbb  aaASbbb  aaaSbbb  aaaCbbb  aaacCbbb  aaaccCbbb  aaaccbbb

6/4/2016IT 3278 LL(1) Grammar A grammar having an LL(1) parsing table. i.e., There is no conflict in the parsing table LL(1) Grammars allow -production. 1.S  A S b 2.S  C 3.A  a 4.C  c C 5.C  abc$ S1222 A3 C545 LL(1) Parsing Table

6/4/2016IT 3279 Not every CFG is an LL(1) grammar ::= | s1 | s2 ::= if then else | if then ::= e1 | e2 if e1 then if e2 then s1 else s2 if (a > 2) if (b > 1) b++; else a++; if (a > 2) if (b > 1) b++; else a++;

6/4/2016IT The recursive-descent parser does not work for every CFG 1.E  E + T 2.E  T 3.T  T * F 4.T  F 5.F  ( E ) 6.F  id E():Switch(token) { case id: E();... } Left-recursions id+id*id

6/4/2016IT Left-recursions 1.A  A  2.A   A  A  A  A   A A’    1.A   A’ 2.A’   A’ 3.A’  A left-recursive grammarRemove left-recursion 

6/4/2016IT Eliminating left-recursions 1.E  E + T 2.E  T 3.T  T * F 4.T  F 5.F  ( E ) 6.F  id 1.E  T E’ 2.E’  + T E’ 3.E’  4.T  F T’ 5.T’  * F T’ 6.T’  7.F  ( E ) 8.F  id    

6/4/2016IT An Algorithm for Eliminating immediate left-recursions Given a CFG G, let A be one of its non-terminal symbols such that 1. Add a new non-terminal symbol A’ to G ; 2. For each production A   such that A is not the 1 st symbol in  add A   A’ to G ; 3. For each production A  A  replace it by A   A’ ; 4. Add A’  to G ; A  A  A   1.A   A’ 2.A’   A’ 3.A’ 

6/4/2016IT Indirect left-recursions 1.S  A a 2.S  b 3.A  S d 4.A  e S a A d S a A d S b bdada

6/4/2016IT Indirect left-recursions 1.S  A a 2.S  b 3.A  SdA’ 4.A  eA’ 5.A’  cA’ 6.A’  find all immediate left recursions A  A  A   A   A’ A’   A’ A’  1.S  A a 2.S  b 3.A  A c 4.A  S d 5.A  e   1.S  SdA’ a 2.S  eA’a 3.S  b 4.A’  cA’ 5.A’  if any, remove the last non-terminal symbol Z with rule Z  X… find all immediate left recursions   1.S  eA’aS’ 2.S  bS’ 3.S’  dA’aS’ 4.S’  5.A’  cA’ 6.A’  repeat 

6/4/2016IT An Algorithm for Eliminating left-recursions Given a CFG G, let A 1, A 2,..... A n, be its nonterminal symbols for i:= n down to 1 do { for j := 1 to i-1 do { // find one level of indiretion For each production A i  A j ω do { For each production A j  , add A i   ω to the grammar; Remove A i  A j ω by } } // end for j Eliminate the immediate left-recursion caused by A i } // end for i

6/4/2016IT A Grammar for if statements 1.S  iCtSE 2.S  a 3.E  eS 4.E  5.C  b abeit$ S21 E3,44 C5 Is it an LL(1) grammar? Is there an LL(1) parsing table for it? No!

6/4/2016 IT A Grammar for if statements 1.S  iCtSE 2.S  a 3.E  eS 4.E  5.C  b abeit$ S 21 E 3,44 C 5 Why there is a conflict? S ...  i b t S E…...  i b t ibtSE E…  i b t ibtaE E…  i b t ibta E…  i b t ibta eS… S ...  i b t S E…...  i b t ibtSE E…  i b t ibtaE E…  i b t ibtaeS E… 4: 3: ibtibtae……

6/4/2016IT A Grammar for if statements 1.S  iCtSE 2.S  a 3.E  eS 4.E  5.C  b abeit$ S21 E3,44 C5 Can we have an unambiguous equivalent grammar for this grammar? Yes! No! In general, No! Some inherently ambiguous languages exist. Can we write a program to get an unambiguous equivalent grammar from any grammar of a language that is known to be not inherently ambiguous? Can we write a program to test whether a given grammar is ambiguous? No!

6/4/2016IT Is there an LL(2) Grammar ? Yes! 1.S  A B 2.A  a A 3.A  a 4.B  b B 5.B  c abc S1 Aabc 233 B45 LL(2) Parsing Table We need to look two symbols ahead in order to determine which rule should be used. { a m b n c | m ≥ 1 and n ≥ 0 } a a a a a b b b b c

6/4/2016IT LL(2) Parsing 1.S  A B 2.A  a A 3.A  a 4.B  b B 5.B  c LL(2) Parsing Table S();a a a b c A();B();a a a b c get(a);A();B();a a a b c A();B(); a a b c get(a);A();B(); a a b c A();B(); a b c get(a);B() a b c B();b c get(b);B();b c B(); c get(c); c abc S1 Aabc 233 B45

6/4/2016IT Is there an LL(1) grammar equivalent to the following LL(2) grammar? 1.S  a A B 2.A  a A 3.A  4.B  b B 5.B  c { a m b n c | m ≥ 1 and n ≥ 0 } a a a a a b b b b c 1.S  A B 2.A  a A 3.A  a 4.B  b B 5.B  c Yes

6/4/2016IT Every left-recursive grammar is not an LL(k) grammar 1.S  a S’ 2.S’  AS’ 3.S’  4.A  b 1.S  S A 2.S  a 3.A  b S  SA  SAA  SAAA  SAAAA  a AAAAA ....  abbbbbb 1.E  E + T 2.E  T 3.T  T * F 4.T  F 5.F  ( E ) 6.F  id 1.E  T E’ 2.E’  + T E’ 3.E’  4.T  F T’ 5.T’  * F T’ 6.T’  7.F  ( E ) 8.F  id But we can effectively find an equivalent one Are we happy with this?

6/4/2016IT Does any LL(2) grammar always has an equivalent LL(1) grammar? 1.S  a S A 2.S  3.A  abS 4.A  c No no equivalent LL(1) grammar 1.S  a S A 2.S  3.A  a k-1 bS 4.A  c no equivalent LL(k-1) grammar LL(2) grammar LL(k) grammar, k  2 LL(1)  LL(2)  LL(3) ..... LL(k)  LL(k+1) ... KuriKi-Sunoi [1969]

6/4/2016IT There exists DCFL that is not LL(k) -- Stearns [1970] { a n | n ≥ 0 }  { a n b n | n ≥ 0 } 1.S  a S A 2.S  3.A  a k-1 bS 4.A  c LL(k) grammar, k  2 This grammar is inherently ambiguous. (KuriKi-Sunoi [1969 ]) Is there an unambiguous CFG that is not an LL(k) grammar? Yes

6/4/2016IT LL(1) Parser Implementation 1.E  T E’ 2.E’  + T E’ 3.E’  4.T  F T’ 5.T’  * F T’ 6.T’  7.F  ( E ) 8.F  n p.s. Let n be any positive integer less than n+*()$ E11 E’233 T44 T’6566 F87 Programming Assignment Details will be announced later.