C Chuen-Liang Chen, NTUCS&IE / 77 TOP-DOWN PARSING Chuen-Liang Chen Department of Computer Science and Information Engineering National Taiwan University.

Slides:



Advertisements
Similar presentations
Parsing 4 Dr William Harrison Fall 2008
Advertisements

Compiler Construction
Joey Paquet, 2000, 2002, 2008, Lecture 7 Bottom-Up Parsing II.
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
Lexical and Syntactic Analysis Here, we look at two of the tasks involved in the compilation process –Given source code, we need to first break it into.
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.
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.
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 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
COS 320 Compilers David Walker. last time context free grammars (Appel 3.1) –terminals, non-terminals, rules –derivations & parse trees –ambiguous grammars.
Top-Down Parsing.
Compiler construction in4020 – lecture 3 Koen Langendoen Delft University of Technology The Netherlands.
(2.1) Grammars  Definitions  Grammars  Backus-Naur Form  Derivation – terminology – trees  Grammars and ambiguity  Simple example  Grammar hierarchies.
CSC3315 (Spring 2009)1 CSC 3315 Lexical and Syntax Analysis Hamid Harroud School of Science and Engineering, Akhawayn University
Parsing. Goals of Parsing Check the input for syntactic accuracy Return appropriate error messages Recover if possible Produce, or at least traverse,
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.
1 Compiler Construction Syntax Analysis Top-down parsing.
C Chuen-Liang Chen, NTUCS&IE / 51 CONTEXT-FREE GRAMMARS Chuen-Liang Chen Department of Computer Science and Information Engineering National Taiwan University.
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.
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.
More Parsing CPSC 388 Ellen Walker Hiram College.
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 Nonrecursive Predictive Parsing  It is possible to build a nonrecursive predictive parser  This is done by maintaining an explicit stack.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Top-Down Parsing.
CSE 5317/4305 L3: Parsing #11 Parsing #1 Leonidas Fegaras.
Parsing methods: –Top-down parsing –Bottom-up parsing –Universal.
CS 330 Programming Languages 09 / 25 / 2007 Instructor: Michael Eckmann.
8 January 2004 Department of Software & Media Technology 1 Top Down Parsing Recursive Descent Parsing Top-down parsing: –Build tree from root symbol –Each.
1 Topic #4: Syntactic Analysis (Parsing) CSC 338 – Compiler Design and implementation Dr. Mohamed Ben Othman ( )
Chapter 2 (part) + Chapter 4: Syntax Analysis S. M. Farhad 1.
Bernd Fischer RW713: Compiler and Software Language Engineering.
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.
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.
WELCOME TO A JOURNEY TO CS419 Dr. Hussien Sharaf Dr. Mohammad Nassef Department of Computer Science, Faculty of Computers and Information, Cairo University.
Programming Languages Translator
Lecture #12 Parsing Types.
Table-driven parsing Parsing performed by a finite state machine.
Compiler design Bottom-up parsing: Canonical LR and LALR
4 (c) parsing.
Top-Down Parsing CS 671 January 29, 2008.
Compiler Design 7. Top-Down Table-Driven Parsing
R.Rajkumar Asst.Professor CSE
Predictive Parsing Lecture 9 Wed, Feb 9, 2005.
LL and Recursive-Descent Parsing Hal Perkins Autumn 2011
Chapter 5 Grammars and Parsers
LL and Recursive-Descent Parsing
Syntax Analysis - Parsing
Nonrecursive Predictive Parsing
LL and Recursive-Descent Parsing Hal Perkins Autumn 2009
Predictive Parsing Program
LL and Recursive-Descent Parsing Hal Perkins Winter 2008
Compiler design Bottom-up parsing: Canonical LR and LALR
Presentation transcript:

c Chuen-Liang Chen, NTUCS&IE / 77 TOP-DOWN PARSING Chuen-Liang Chen Department of Computer Science and Information Engineering National Taiwan University Taipei, TAIWAN

c Chuen-Liang Chen, NTUCS&IE / 78 Parser (2/5) void system_goal(void) { /* ::= SCANEOF */ program(); match(SCANEOF); } void program(void) { /* ::= BEGIN END */ match(BEGIN) statement_list(); match(END); } void statement_list(void) { /* ::= { } */ statement(); while (TRUE) { switch (next_token()) { case ID: case READ: case WRITE: statement(); break; default: return; } QUIZ: Why ID, READ, WRITE ?

c Chuen-Liang Chen, NTUCS&IE / 79 trace of top-down parsing (left-most derivation)  orange : just derived (predicted)blue : just read (matched) black : derived or readgreen : un-processed (parse stack) Example of top-down parsing Tail E Prefix(E) FVTail +E V E Prefix(E) E (E) FVTail E Prefix(E) FVTail +E E Prefix(E) FVTail +E V EE Prefix(E) F Why this production rule? Why this production rule?

c Chuen-Liang Chen, NTUCS&IE / 80 Predict set (1/2) top-down parsing predicts the production that is to be matched before matching actually begins predict set is used to indicate the applied production rule, according to encountered nonterminal & lookahead symbol(s) Predict k ( A  X 1  X m ) = if  First k (X 1  X m ) thenFirst k (X 1  X m ) else( First k (X 1  X m ) - { } )  Follow k (A)  for LL(k) parsing usage -- e.g., to construct recursive descent parsing void non_term(void) { token tok = next_token(); switch (tok) { case Predict_set: parsing_actions(); break;  default: syntax_error(tok); break; } QUIZ: how to construct, automatically?  QUIZ: how to construct, automatically?

c Chuen-Liang Chen, NTUCS&IE / 81 Predict set (2/2) example for LL(1) Predict set 1.  begin end begin 2.  First(rhs) =ID read write 3.  First(rhs) =ID read write 4.  Follow(lhs) =end 5.  ID := ;ID 6.  read ( ) ;read 7.  write ( ) ;write 8.  ID ID 9. , ID, 10.  Follow(lhs) =) 11.  First(rhs) =ID INTLIT ( 12. ,, 13.  Follow(lhs) =) 14.  First(rhs) =ID INTLIT ( 15.  First(rhs) =  Follow(lhs) =, ; ) 17.  ( )( 18.  IDID 19.  INTLITINTLIT 20.    $First(rhs) =begin  extended from BNF to CFG

c Chuen-Liang Chen, NTUCS&IE / 82 LL(k) parse table example for LL(1)  blank entity -- syntax error QUIZ: parse table size for LL(k) parsing? QUIZ: Is LL(k), k  2, practical?

c Chuen-Liang Chen, NTUCS&IE / 83 LL(k) grammar the grammar -- unique prediction for each combination of nonterminal and lookahead symbol(s) (entry of parse table) unambiguous grammar it is usually (but not always) to create an LL(1) grammar for programming language predict conflicts  common prefix  left recursion  and so on

c Chuen-Liang Chen, NTUCS&IE / 84 Common prefix elimination  if then endif ;  if then else endif ;   if then ;  endif ;  else endif ; QUIZ: how, systematically?

c Chuen-Liang Chen, NTUCS&IE / 85 Left recursion elimination E  E + TE  E1 EtailE  T Etail E  TE1  TEtail  + T Etail T  T * PEtail  + T EtailEtail  T  PEtail  T  P Ttail P  IDT  T1 TtailTtail  * P Ttail T1  PTtail  Ttail  * P TtailP  ID Ttail  P  ID QUIZ: why conflict? QUIZ: how, systematically? 

c Chuen-Liang Chen, NTUCS&IE / 86 Other predict conflict eliminations   ID :   ID := ;   ID  :  := ;  ID := ; ..  ID   .. 

c Chuen-Liang Chen, NTUCS&IE / 87 LL(1) parser driver using a parse stack to keep predicted but unprocessed symbols QUIZ: time complexity? QUIZ: space complexity? QUIZ: recursive descent v.s. LL(1)? void lldriver(void) { /* Push the Start Symbol onto an empty stack */ push(S); while ( ! stack_empty() ) { /* Let X be the top stack symbol; */ /* let a be the current input token */ if (is_nonterminal(X) && T[X][a] == X  Y 1  Y m ) { /* Expand nonterminal */ Replace X with Y 1  Y m on the stack; } else if (is_terminal(X) && X == a) { pop(l);/* Match of X worked */ scanner(&a);/* Get next token */ } else if (is_action_symbol(X)) { pop(l); Call Semantic Routine corresponding to X; } else /* Process syntax error */ }

c Chuen-Liang Chen, NTUCS&IE / 88 Tracing example (1/2) StepRemaining InputParse StackAction (1)begin A:=BB-314+A; end $ Predict 22 (2)begin A:=BB-314+A; end $ $Predict 1 (3)begin A:=BB-314+A; end $begin end $Match (4)A:=BB-314+A; end $ end $Predict 2 (5)A:=BB-314+A; end $ end $Predict 5 (6)A:=BB-314+A; end $ID := ; end $Match (7):=BB-314+A; end $:= ; end $Match (8)BB-314+A; end $ ; end $Predict 14 (9)BB-314+A; end $ ; end $Predict 18 (10)BB-314+A; end $ID ; end $Match (11)-314+A; end $ ; end $Predict 15 (12)-314+A; end $ ; end $Predict 21 (13)-314+A; end $- ; end $Match

c Chuen-Liang Chen, NTUCS&IE / 89 Tracing example (2/2) StepRemaining InputParse StackAction (14)314+A; end $ ; end $Predict 19 (15)314+A; end $IntL ; end $Match (16)+A; end $ ; end $Predict 15 (17)+A; end $ ; end $Predict 20 (18)+A; end $+ ; end $Match (19)A; end $ ; end $Predict 18 (20)A; end $ID ; end $Match (21); end $ ; end $Predict 16 (22); end $ ; end $Match (23) end $ end $Predict 4 (24)end $ end $Match (25)$$Match

c Chuen-Liang Chen, NTUCS&IE / 90 Dangling else problem (1/2) a notable exception of LL(1) even LL(k), but can be solved by bottom- up parsing QUIZ: why?  QUIZ: why? if if else ifelse if ifelse if

c Chuen-Liang Chen, NTUCS&IE / 91 Dangling else problem (2/2) solution 1 -- ambiguous grammar + special handling ( else associates with nearest if )  1.G  S ; 2.S  if S E 3.S  Other 4.E  else S 5.E  solution 2 -- change language structure  G  S ; S  if S E S  Other E  else S endif E  endif 