Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "C Chuen-Liang Chen, NTUCS&IE / 77 TOP-DOWN PARSING Chuen-Liang Chen Department of Computer Science and Information Engineering National Taiwan University."— Presentation transcript:

1 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

2 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 ?

3 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?

4 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?

5 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) =+ - 16.  Follow(lhs) =, ; ) 17.  ( )( 18.  IDID 19.  INTLITINTLIT 20.  ++ 21.  -- 22.  $First(rhs) =begin  extended from BNF to CFG

6 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?

7 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

8 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?

9 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? 

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

11 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 */ }

12 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

13 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

14 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

15 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 


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

Similar presentations


Ads by Google