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?

Slides:



Advertisements
Similar presentations
Lecture # 11 Grammar Problems.
Advertisements

Top-Down Parsing.
By Neng-Fa Zhou Syntax Analysis lexical analyzer syntax analyzer semantic analyzer source program tokens parse tree parser tree.
Parsing III (Eliminating left recursion, recursive descent parsing)
ISBN Chapter 4 Lexical and Syntax Analysis The Parsing Problem Recursive-Descent Parsing.
CS 310 – Fall 2006 Pacific University CS310 Parsing with Context Free Grammars Today’s reference: Compilers: Principles, Techniques, and Tools by: Aho,
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
Table-driven parsing Parsing performed by a finite state machine. Parsing algorithm is language-independent. FSM driven by table (s) generated automatically.
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.
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.
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.
BİL 744 Derleyici Gerçekleştirimi (Compiler Design)1 Syntax Analyzer Syntax Analyzer creates the syntactic structure of the given source program. This.
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.
Parsing III (Top-down parsing: recursive descent & LL(1) )
Parsing Jaruloj Chongstitvatana Department of Mathematics and Computer Science Chulalongkorn University.
-Mandakinee Singh (11CS10026).  What is parsing? ◦ Discovering the derivation of a string: If one exists. ◦ Harder than generating strings.  Two major.
Profs. Necula CS 164 Lecture Top-Down Parsing ICOM 4036 Lecture 5.
Syntactic Analysis Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.
Syntax and Semantics Structure of programming languages.
COP4020 Programming Languages Syntax Prof. Robert van Engelen (modified by Prof. Em. Chris Lacher)
4 4 (c) parsing. Parsing A grammar describes syntactically legal strings in a language A recogniser simply accepts or rejects strings A generator produces.
Top Down Parsing - Part I Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
6/4/2016IT 3271 The most practical Parsers: Predictive parser: 1.input (token string) 2.Stacks, parsing table 3.output (syntax tree, intermediate codes)
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.
Muhammad Idrees, Lecturer University of Lahore 1 Top-Down Parsing Top down parsing can be viewed as an attempt to find a leftmost derivation for an input.
Parsing — Part II (Top-down parsing, left-recursion removal) Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students.
Top-down Parsing lecture slides from C OMP 412 Rice University Houston, Texas, Fall 2001.
Parsing — Part II (Top-down parsing, left-recursion removal) Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students.
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.
Top-Down Parsing.
1 Pertemuan 7 & 8 Syntax Analysis (Parsing) Matakuliah: T0174 / Teknik Kompilasi Tahun: 2005 Versi: 1/6.
CSE 5317/4305 L3: Parsing #11 Parsing #1 Leonidas Fegaras.
Parsing methods: –Top-down parsing –Bottom-up parsing –Universal.
Lesson 4 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
COMP 3438 – Part II-Lecture 5 Syntax Analysis II Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
1 Topic #4: Syntactic Analysis (Parsing) CSC 338 – Compiler Design and implementation Dr. Mohamed Ben Othman ( )
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.
Syntax and Semantics Structure of programming languages.
Parsing #1 Leonidas Fegaras.
lec02-parserCFG May 8, 2018 Syntax Analyzer
Parsing — Part II (Top-down parsing, left-recursion removal)
Programming Languages Translator
Compiler design Bottom-up parsing Concepts
Context free grammars Terminals Nonterminals Start symbol productions
Lecture #12 Parsing Types.
Parsing IV Bottom-up Parsing
Table-driven parsing Parsing performed by a finite state machine.
Parsing — Part II (Top-down parsing, left-recursion removal)
Compiler Construction
Parsing with Context Free Grammars
Top-Down Parsing.
Parsing Techniques.
3.2 Language and Grammar Left Factoring Unclear productions
Top-Down Parsing CS 671 January 29, 2008.
Syntax-Directed Definition
Recursive decent parsing
Syntax Analysis source program lexical analyzer tokens syntax analyzer
COP4020 Programming Languages
Lecture 8: Top-Down Parsing
Bottom Up Parsing.
Parsing — Part II (Top-down parsing, left-recursion removal)
lec02-parserCFG May 27, 2019 Syntax Analyzer
Presentation transcript:

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? –What is an ambiguous grammar? –Why we care about left or right derivation?

Example: ->’program’ id ‘begin’ ‘end’ -> ‘;’ | -> id ‘=‘ -> | id -> ‘+’ | ‘-’ | ‘*’ | ‘/’ program test begin t0 = t1 + t2; t3 = t0 * t4 end program test begin t0 = t1+t2; t3 = t0*t4 end ==> *

Parsing: –The process to determine whether the start symbol can derive the program. If successful, the program is a valid program. If failed, the program is invalid. –Two approaches in general. Expanding from the start symbol to the whole program (top down) Reduction from the whole program to start symbol (bottom up).

Parsing methods: –universal: There exists algorithms that can parse any context free grammar. These algorithms are too inefficient to be used anywhere. What is considered efficient? Scan the program (from left to right) once. –Top-down parsing build the parse tree from root to leave (using leftmost derivation, why?). Recursive descent, and LL parser –Bottom-up parsing build the parse tree from leaves to root. Operator precedence parsing, LR (SLR, canonical LR, LALR).

–Recursive descent parsing associates a procedure with each nonterminal in the grammar, it may require backtracking of the input string. –Example: -> | ^ id | array [ ] of ->integer | char | num dotdot num void type() { if (lookahead == INTEGER || lookahead == CHAR || lookahead==NUM) simple(); else if (lookahead == ‘^’) { match (‘^’); match(ID); } else if (lookahead == ARRAY) { match (ARRAY); match(‘[‘); simple(); match (‘]’); match (OF); type(); } else error(); }

–Example: -> | ^ id | array [ ] of ->integer | char | num dotdot num void simple() { if (lookahead == INTEGER) match (INTEGER); else if (lookahead == CHAR) match (CHAR); else if (lookahead == NUM) { match(NUM); match(DOTDOT); match(NUM); } else error(); } void match(token t) { if (lookahead == t) {lookahead = nexttoken();} else error(); }

–Recursive descent parsing may require backtracking of the input string try out all productions, backtrack if necessary. E.g S->cAd, A->ab | a input string cad –A special case of recursive-descent parser that needs no backtracking is called a predictive parser. Look at the input string, must predict the right production every time to avoid backtracking. Needs to know what first symbols can be generated by the right side of a production only lookahead for one token)

–First(a) - the set of tokens that can appear as the first symbols of one or more strings generated from a. If a is empty string or can generate empty string, then empty string is also in First(a). –Given productions A ->a | b, predictive (by looking at 1 token ahead) parsing requires First(a) and First(b) to be disjoint. –Predictive parsing won’t work on some type of grammars: Left recursion: A->Aw (expanding A results in an infinite loop). Have common left factor: A->aB | aC (First(aB) and First(aC) is not disjoint).

–Eliminating Left Recursion Immediate Left Recursion –Replace A->Aa | b with A->bA’ and A’->aA’ | e –Example: E->E+T | T T->T*F | F F->(E) | id –In general, Can be replaced by

Algorithm 4.1. Eliminating left recursion: Arrange the nonterminals in some order A1, A2, …, An for i = 1 to n do begin for j = 1 to I-1 do begin expand production of the form Ai ->Aj w end for eliminate the immediate left recursion among Ai productions. End for (the algorithm can fail if the grammar has a cycle (A==> A), or A->e)

Example 1: S->Aa | b A->Ac | Sd | e Example 2: X->YZ | a Y->ZX |Xb Z->XY | ZZ | a

–Left factoring (to produce a grammar suitable for predictive parsing) replace productions by Example: S->iEtS | iEtSeS|a E->b