Compilers Welcome to a journey to CS419 Lecture15: Syntax Analysis:

Slides:



Advertisements
Similar presentations
Compiler Construction
Advertisements

Grammar and Algorithm }
Pertemuan 9, 10, 11 Top-Down Parsing
By Neng-Fa Zhou Syntax Analysis lexical analyzer syntax analyzer semantic analyzer source program tokens parse tree parser tree.
1 Predictive parsing Recall the main idea of top-down parsing: Start at the root, grow towards leaves Pick a production and try to match input May need.
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.
Top-Down Parsing.
– 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.
COP4020 Programming Languages Computing LL(1) parsing table Prof. Xin Yuan.
Top-Down Parsing - recursive descent - predictive parsing
Chapter 5 Top-Down Parsing.
-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.
1 Compiler Construction Syntax Analysis Top-down parsing.
Lesson 5 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
CSI 3120, Syntactic analysis, page 1 Syntactic Analysis and Parsing Based on A. V. Aho, R. Sethi and J. D. Ullman Compilers: Principles, Techniques and.
Pembangunan Kompilator.  The parse tree is created top to bottom.  Top-down parser  Recursive-Descent Parsing ▪ Backtracking is needed (If a choice.
BİL 744 Derleyici Gerçekleştirimi (Compiler Design)1 Top-Down Parsing The parse tree is created top to bottom. Top-down parser –Recursive-Descent Parsing.
Parsing Top-Down.
LL(1) Parser. What does LL signify ? The first L means that the scanning takes place from Left to right. The first L means that the scanning takes place.
1 Compiler Construction Syntax Analysis Top-down parsing.
TOP-DOWN PARSING Recursive-Descent, Predictive Parsing.
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)
1 Nonrecursive Predictive Parsing  It is possible to build a nonrecursive predictive parser  This is done by maintaining an explicit stack.
Top-Down Parsing.
Top-Down Predictive Parsing We will look at two different ways to implement a non- backtracking top-down parser called a predictive parser. A predictive.
Parsing methods: –Top-down parsing –Bottom-up parsing –Universal.
1 Topic #4: Syntactic Analysis (Parsing) CSC 338 – Compiler Design and implementation Dr. Mohamed Ben Othman ( )
COMP 3438 – Part II-Lecture 6 Syntax Analysis III Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
Parsing COMP 3002 School of Computer Science. 2 The Structure of a Compiler syntactic analyzer code generator program text interm. rep. machine code tokenizer.
WELCOME TO A JOURNEY TO CS419 Dr. Hussien Sharaf Dr. Mohammad Nassef Department of Computer Science, Faculty of Computers and Information, Cairo University.
WELCOME TO A JOURNEY TO CS419 Dr. Hussien Sharaf Dr. Mohammad Nassef Department of Computer Science, Faculty of Computers and Information, Cairo University.
Parsing — Part II (Top-down parsing, left-recursion removal)
Compiler Construction
Programming Languages Translator
Context free grammars Terminals Nonterminals Start symbol productions
Compilers Welcome to a journey to CS419 Lecture5: Lexical Analysis:
Parsing — Part II (Top-down parsing, left-recursion removal)
Syntactic Analysis and Parsing
Compiler Construction
Introduction to Top Down Parser
Top-down parsing cannot be performed on left recursive grammars.
SYNTAX ANALYSIS (PARSING).
FIRST and FOLLOW Lecture 8 Mon, Feb 7, 2005.
CS 404 Introduction to Compiler Design
UNIT 2 - SYNTAX ANALYSIS Role of the parser Writing grammars
Top-Down Parsing.
Parsing Techniques.
3.2 Language and Grammar Left Factoring Unclear productions
Top-Down Parsing CS 671 January 29, 2008.
Lecture 7 Predictive Parsing
CS 540 George Mason University
Syntax Analysis source program lexical analyzer tokens syntax analyzer
BOTTOM UP PARSING Lecture 16.
Top-Down Parsing Identify a leftmost derivation for an input string
Top-Down Parsing The parse tree is created top to bottom.
Chapter 4 Top Down Parser.
Bottom Up Parsing.
Predictive Parsing Lecture 9 Wed, Feb 9, 2005.
Parsing — Part II (Top-down parsing, left-recursion removal)
Computing Follow(A) : All Non-Terminals
Syntax Analysis - Parsing
Lecture 7 Predictive Parsing
BNF 9-Apr-19.
Nonrecursive Predictive Parsing
Context Free Grammar – Quick Review
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Predictive Parsing Program
Parsing CSCI 432 Computer Science Theory
Presentation transcript:

Compilers Welcome to a journey to CS419 Lecture15: Syntax Analysis: Cairo University FCI Welcome to a journey to Compilers CS419 Lecture15: Syntax Analysis: Top-Down Parsing (Prerequisites) (Cont’d) Dr. Hussien Sharaf Dr. Mohammad Nassef Department of Computer Science, Faculty of Computers and Information, Cairo University

designing a top-down parser: Dr. Mohammad Nassef designing a top-down parser: Steps: Elimination of Ambiguity Elimination of Left Recursion Left Factoring Drawing Transition Diagram (Optional) Applying First/Follow operators Building the Parsing Table Parse the given statements

Transition diagrams Transition diagrams can describe recursive parsers, just like they can describe lexical analyzers, but the diagrams are slightly different. Construction: Eliminate left recursion from grammar G Left factor grammar G For each non-terminal A, do: Create an initial and final (return) state For each production A -> X1 X2 … Xn, create a path from the initial to the final state with edges X1 X2 … Xn.

Example transition diagrams An expression grammar with left recursion and ambiguity removed: E -> T E’ E’ -> + T E’ | ε T -> F T’ T’ -> * F T’ | ε F -> ( E ) | id Example : parse the string “id + id * id” Corresponding transition diagrams:

Using transition diagrams Begin in the start state for the start symbol When we are in state s with edge labeled by terminal a to state t, if the next input symbol is a, move to state t and advance the input pointer. For an edge to state t labeled with non-terminal A, jump to the transition diagram for A, and when finished, return to state t For an edge labeled ε, move immediately to t.

Procedure Make a transition diagram( like DFA/NFA) for every rule of the grammar. Optimize the DFA by reducing the number of states, yielding the final transition diagram To parse a string, simulate the string on the transition diagram If after consuming the input the transition diagram reaches an accept state, it is parsed.

designing a top-down parser: Dr. Mohammad Nassef designing a top-down parser: Steps: Elimination of Ambiguity Elimination of Left Recursion Left Factoring Drawing Transition Diagram (Optional) Applying First/Follow operators Building the Parsing Table Parse the given statements

Predictive parsing Recall the main idea of top-down parsing: Start at the root, grow towards leaves Pick a production and try to match input May need to backtrack Can we avoid the backtracking? Given A   |  the parser should be able to choose between  and  How? What if we do some "preprocessing" to answer the question: Given a non-terminal A and look-ahead t, which (if any) production of A is guaranteed to start with a t?

Predictive parsing Armed with FIRST FOLLOW We can build a parser where no backtracking is required!

Example grammar for first/follow EE+E EE*E E(E) Eid Original grammar: This grammar is left-recursive, ambiguous and requires left-factoring. It needs to be modified before we build a predictive parser for it: Remove ambiguity: Remove left recursion: ETE' E'+TE'| TFT' T'*FT'| F(E) Fid EE+T TT*F F(E) Fid

Computing first: Compute FIRST(X) as follows: if X is a terminal, then FIRST(X)={X} if X is a production, then add  to FIRST(X) if X is a non-terminal and XY1Y2...Yn is a production, add FIRST(Y1) to FIRST(X) if X is a non-terminal and XY1Y2...Yn is a production, add FIRST(Yi) to FIRST(X) if the preceding Yj’s contain  in their FIRSTs Focus on L.H.S of productions

FIRST Example E  TE’ E’  +TE’ |  T  FT’ T’  *FT’ |  F  (E) | id CS416 Compiler Design Fall 2003 FIRST Example E  TE’ E’  +TE’ |  T  FT’ T’  *FT’ |  F  (E) | id FIRST(F) = {(,id} FIRST(T’) = {*, } FIRST(T) = FIRST(F) = {(,id} FIRST(E’) = {+, } FIRST(E) = FIRST(T) = {(,id} FIRST(TE’) = {(,id} FIRST(+TE’ ) = {+} FIRST() = {} FIRST(FT’) = {(,id} FIRST(*FT’) = {*} FIRST((E)) = {(} FIRST(id) = {id}

Computing follow Compute FOLLOW as follows: FOLLOW(S) contains EOF (or $) For productions AB, everything in FIRST() except  goes into FOLLOW(B) For productions AB or AB where FIRST() contains , FOLLOW(B) contains everything that is in FOLLOW(A) Focus on R.H.S of productions

FOLLOW Example E  TE’ E’  +TE’ |  T  FT’ T’  *FT’ |  CS416 Compiler Design Fall 2003 FOLLOW Example E  TE’ E’  +TE’ |  T  FT’ T’  *FT’ |  F  (E) | id FOLLOW(E) = { $, ) } FOLLOW(E’) = Follow(E) = { $, ) } FOLLOW(T) = FIRST(E’) + FOLLOW(E’) = { +, ), $ } FOLLOW(T’) = FOLLOW(T) = { +, ), $ } FOLLOW(F) = FIRST(T’) + FOLLOW(T’) = {+, *, ), $ }

designing a top-down parser: Dr. Mohammad Nassef designing a top-down parser: Steps: Elimination of Ambiguity Elimination of Left Recursion Left Factoring Drawing Transition Diagram (Optional) Applying First/Follow operators Building the Parsing Table Parse the given statements

Predictive parsing (w/table) For each production A do: For each terminal a  FIRST() add A to entry M[A,a] If FIRST(), add A to entry M[A,b] for each terminal b  FOLLOW(A). If FIRST() and EOFFOLLOW(A), add A to M[A,EOF] Use table and stack to simulate recursion.

LL(1) Parsing table E  TE’ E’  +TE’ |  T  FT’ T’  *FT’ |  CS416 Compiler Design Fall 2003 LL(1) Parsing table E  TE’ E’  +TE’ |  T  FT’ T’  *FT’ |  F  (E) | id FIRST(E) = FIRST(T) = FIRST(F) = {(, id} FIRST(E') = {+, } FIRST(T') = {*, } FOLLOW(E) = FOLLOW(E') = {$, )} FOLLOW(T) = FOLLOW(T') = {+, $, )} FOLLOW(F) = {*, +, $, )} id + * ( ) $ E E  TE’ E’ E’  +TE’ E’   T T  FT’ T’ T’   T’  *FT’ F F  id F  (E) Is this grammar LL(1)? Yes, because each cell has only one production!

designing a top-down parser: Dr. Mohammad Nassef designing a top-down parser: Steps: Elimination of Ambiguity Elimination of Left Recursion Left Factoring Drawing Transition Diagram (Optional) Applying First/Follow operators Building the Parsing Table Parse the given statements

LL(1) Parser – Example id + * ( ) $ E E  TE’ E’ E’  +TE’ E’   T CS416 Compiler Design Fall 2003 LL(1) Parser – Example id + * ( ) $ E E  TE’ E’ E’  +TE’ E’   T T  FT’ T’ T’   T’  *FT’ F F  id F  (E) stack input output $E id+id$ E  TE’ $E’T id+id$ T  FT’ $E’ T’F id+id$ F  id $ E’ T’id id+id$ $ E’ T’ +id$ T’   $ E’ +id$ E’  +TE’ $ E’ T+ +id$ $ E’ T id$ T  FT’

LL(1) Parser – Example (Cont’d) CS416 Compiler Design Fall 2003 LL(1) Parser – Example (Cont’d) id + * ( ) $ E E  TE’ E’ E’  +TE’ E’   T T  FT’ T’ T’   T’  *FT’ F F  id F  (E) stack input output $ E’ T id$ T  FT’ $ E’ T’ F id$ F  id $ E’ T’id id$ $ E’ T’ $ T’   $ E’ $ E’   $ $ accept