8 January 2004 Department of Software & Media Technology 1 Top Down Parsing Recursive Descent Parsing Top-down parsing: –Build tree from root symbol –Each.

Slides:



Advertisements
Similar presentations
Compiler Construction
Advertisements

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.
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
LESSON 18.
Recursive Descent Parsing Top-down parsing: build tree from root symbol Each production corresponds to one recursive procedure Each procedure recognizes.
Top-Down Parsing.
1 Contents Introduction A Simple Compiler Scanning – Theory and Practice Grammars and Parsing LL(1) Parsing LR Parsing Lex and yacc Semantic Processing.
Parsing III (Eliminating left recursion, recursive descent parsing)
ISBN Chapter 4 Lexical and Syntax Analysis The Parsing Problem Recursive-Descent Parsing.
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.
Parsing — Part II (Ambiguity, Top-down parsing, Left-recursion Removal)
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 LL(1) parsing. Overview of Top-Down  There are only two actions 1.Replace 2.Match.
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.
Shift/Reduce and LR(1) Professor Yihjia Tsai Tamkang University.
Recursive Descent Parsing Top-down parsing: build tree from root symbol Each production corresponds to one recursive procedure Each procedure recognizes.
CPSC 388 – Compiler Design and Construction
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.
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.
# 1 CMPS 450 Parsing CMPS 450 J. Moloney. # 2 CMPS 450 Check that input is well-formed Build a parse tree or similar representation of input Recursive.
Parsing III (Top-down parsing: recursive descent & LL(1) )
Parsing Jaruloj Chongstitvatana Department of Mathematics and Computer Science Chulalongkorn University.
C Chuen-Liang Chen, NTUCS&IE / 77 TOP-DOWN PARSING Chuen-Liang Chen Department of Computer Science and Information Engineering National Taiwan 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.
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.
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.
Parsing Top-Down.
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 Recursive Descent & LL(1) Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412.
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.
TOP-DOWN PARSING Recursive-Descent, Predictive Parsing.
1 Nonrecursive Predictive Parsing  It is possible to build a nonrecursive predictive parser  This is done by maintaining an explicit stack.
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.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Top-Down Parsing.
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.
Chapter 2 (part) + Chapter 4: Syntax Analysis S. M. Farhad 1.
UMBC  CSEE   1 Chapter 4 Chapter 4 (b) parsing.
Parsing III (Top-down parsing: recursive descent & LL(1) )
COMP 3438 – Part II-Lecture 6 Syntax Analysis III Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
CMSC 330: Organization of Programming Languages Pushdown Automata Parsing.
Syntax and Semantics Structure of programming languages.
Programming Languages Translator
Table-driven parsing Parsing performed by a finite state machine.
Top-Down Parsing.
4 (c) parsing.
Programming Language Syntax 7
Top-Down Parsing CS 671 January 29, 2008.
Compiler Design 7. Top-Down Table-Driven Parsing
LL and Recursive-Descent Parsing
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Predictive Parsing Program
Presentation transcript:

8 January 2004 Department of Software & Media Technology 1 Top Down Parsing Recursive Descent Parsing Top-down parsing: –Build tree from root symbol –Each production corresponds to one recursive procedure –Each procedure recognizes an instance of a non-terminal, returns tree fragment for the non-terminal

8 January 2004 Department of Software & Media Technology 2 General model Each right-hand side of a production provides body for a function Each non-terminal on the right hand side is translated into a call to the function that recognizes that non-terminal Each terminal in the right hand side is translated into a call to the lexical scanner. If the resulting token is not the expected terminal error occurs. Each recognizing function returns a tree fragment.

8 January 2004 Department of Software & Media Technology 3 Example: parsing a declaration FULL_TYPE_DECLARATION ::= type DEFINING_IDENTIFIER is TYPE_DEFINITION; Translates into: –get token type –Find a defining_identifier -- function call –get token is –Recognize a type_definition -- function call –get token semicolon In practice, we already know that the first token is type, that’s why this routine was called in the first place! Predictive parsing is guided by the next token

8 January 2004 Department of Software & Media Technology 4 Example: parsing a loop FOR_STATEMENT ::= ITERATION_SCHEME loop STATEMENTS end loop; Node1 := find_iteration_scheme; -- call function get token loop List1 := Sequence of statements -- call function get token end get token loop get token semicolon; Result := build loop_node with Node1 and List1 return Result

8 January 2004 Department of Software & Media Technology 5 Problem: If there are multiple productions for a non-terminal, mechanism is required to determine which production to use: IF_STAT ::= if COND then Stats end if; IF_STAT ::= if COND then Stats ELSIF_PART end if; When next token is if, so which production to use ?

8 January 2004 Department of Software & Media Technology 6 One Solution: factorize grammar If several productions have the same prefix, rewrite as single production: IF_STAT ::= if COND then STATS [ELSIF_PART] end if; –Problem now reduces to recognizing whether an optional –Component (ELSIF_PART) is present

8 January 2004 Department of Software & Media Technology 7 Second Problem of Recursion Grammar should not be left-recursive: E ::= E + T | T Problem: to find an E, start by finding an E… –Original scheme leads to infinite loop –Grammar is inappropriate for recursive-descent

8 January 2004 Department of Software & Media Technology 8 Solution to left-recursion E ::= E + T | T means that eventually E expands into T + T + T …. Rewrite as: –E ::= TE’ –E’ ::= + TE’ | epsilon Informally: E’ is a possibly empty sequence of terms separated by an operator

8 January 2004 Department of Software & Media Technology 9 Recursion can involve multiple productions A ::= B C | D B ::= A E | F –Can be rewritten as: A ::= A E C | F C | D –Now apply previous method –General algorithm to detect and remove left-recursion

8 January 2004 Department of Software & Media Technology 10 Further Problem Transformation does not preserve associativity : – E ::= E + T | T – Parses a + b + c as (a + b) + c – E ::= TE’, E’ ::= + TE’ | epsilon – Parses a + b +c as a + (b + c) –Incorrect for a - b – c : must rewrite tree

8 January 2004 Department of Software & Media Technology 11 In practice: use loop to find sequence of terms Node1 := P_Term; -- call function that recognizes a term loop exit when Token not in Token_Class_Binary_Addop; Node2 := New_Node (P_Binary_Adding_Operator); Scan; -- past operator Set_Left_Opnd (Node2, Node1); Set_Right_Opnd (Node2, P_Term); -- find next term Set_Op_Name (Node2); Node1 := Node2; -- operand for next operation end loop;

8 January 2004 Department of Software & Media Technology 12 LL (1) Parsing LL (1) grammars If table construction is successful, grammar is LL (1): left-to right, leftmost derivation with one-token lookahead. If construction fails, can conceive of LL (2), etc. Ambiguous grammars are never LL (k) If a terminal is in First for two different productions of A, the grammar cannot be LL (1). Grammars with left-recursion are never LL (k) Some useful constructs are not LL (k)

8 January 2004 Department of Software & Media Technology 13 Building LL (1) parse tables Table indexed by non-terminal and token. Table entry is a production: for each production P: A  loop for each terminal a in First (  ) loop T (A, a) := P; end loop; if  in First (  ), then for each terminal b in Follow (  ) loop T (A, b) := P; end loop; end if; end loop; All other entries are errors. If two assignments conflict, parse table cannot be built.

8 January 2004 Department of Software & Media Technology 14 Left Recursion Removal & Left Factoring Left Recursion Removal: Left Factoring:

8 January 2004 Department of Software & Media Technology 15 Synatx Tree Construction in LL(1) First and Follow Sets LL(k) Parsers (Extending the Lookahead Error Recovery in Top Down Parsers Error Recovery in LL(1) Parsers