Advertisement
Guest User

11

a guest
Apr 25th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. Recursive descent parsing associates a procedure with each nonterminal in the grammar, it may require backtracking of the input string.
  2.  
  3. The simplest parsing method, for a large subset of context free grammars, is called recursive descent. It is simple because the algorithm closely follows the production rules of nonterminal symbols.
  4. - Write 1 procedure per nonterminal rule
  5.  
  6.  
  7. - Within each procedure,
  8.  
  9. a) match terminals at appropriate positions, and
  10.  
  11. b) call procedures for non-terminals.
  12.  
  13.  
  14. - Pitfalls: left recursion is FATAL; must distinguish between several production rules, or potentially, one has to try all of them via backtracking. In particular provide a high-level general algorithm for how the terminal & nonterminal symbols are handled.
  15.  
  16. EXAMPLE:
  17.  
  18. The grammar
  19.  
  20. S -> A B C
  21. A -> a A
  22. A -> epsilon
  23. B -> b
  24. C -> c
  25. maps to code like
  26.  
  27. procedure S()
  28. if A() & B() & C() then succeed
  29. end
  30.  
  31. procedure A()
  32. if currtoken == a then # use production 2
  33. currtoken = scan()
  34. return A()
  35. else
  36. succeed # production rule 3, match epsilon
  37. end
  38.  
  39. procedure B()
  40. if currtoken == b then
  41. currtoken = scan()
  42. succeed
  43. else fail
  44. end
  45.  
  46. procedure C()
  47. if currtoken == c then
  48. currtoken = scan()
  49. succeed
  50. else fail
  51. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement