1 / 17

Lecture # 11

Lecture # 11. Grammar Problems . Left Recursion. A grammar is left recursive if for a non-terminal A, there is a derivation A + A There are three types of left recursion: direct (A  A x) indirect (A  B C, B  A ) hidden (A  B A, B  ). How to eliminate Left recursion?.

asha
Download Presentation

Lecture # 11

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture # 11 Grammar Problems

  2. Left Recursion • A grammar is left recursive if for a non-terminal A, there is a derivation A+ A • There are three types of left recursion: • direct (A  A x) • indirect (A  B C, B  A ) • hidden (A  B A, B  )

  3. How to eliminate Left recursion? • To eliminate direct left recursion replace A  A1 | A2 | ... | Am| 1 | 2 | ... | n with A  1B | 2B | ... | nB B  1B | 2B | ... | mB | 

  4. Left recursion • How about this: S  EE  E+T E  T T  E-T T  F F  E*F F  id There is direct recursion: EE+TThere is indirect recursion: TE+T, ET Algorithm for eliminating indirect recursion List the nonterminals in some order A1, A2, ...,An for i=1 to n for j=1 to i-1 if there is a production AiAj, replace Ajwith its rhs eliminate any direct left recursion on Ai

  5. Eliminating indirect left recursion ordering: S, E, T, F i=S i=E i=T, j=E S  EE  E+T E  T T  E-T T  F F  E*F F  id S  EE  E+T E  T T  E-T T  F F  E*F F  id S  EE  TE' E'+TE'| T  E-T T  F F  E*F F  id S  EE  TE' E'+TE'| T  TE'-T T  F F  E*F F  id S  EE  TE' E'+TE'| T  FT' T'  E'-TT'| F  E*F F  id

  6. Eliminating indirect left recursion i=F, j=E i=F, j=T S  EE  TE' E'+TE'| T  FT' T'  E'-TT'| F  TE'*F F  id S  EE  TE' E'+TE'| T  FT' T'  E'-TT'| F  FT'E'*F F  id S  EE  TE' E'+TE'| T  FT' T'  E'-TT'| F  idF' F'  T'E'*FF'|

  7. Example • Eliminate Left Recursion from the following grammar: • S Aa | b • A Ac | Sd • The algorithm is guaranteed to work if the grammar has no cycle and null productions

  8. Example Left Recursion Elim. A BC | aB C A | AbC A B | C C | a Choose arrangement: A, B, C i = 1: nothing to doi = 2, j = 1: BC A | AbB C A | B Cb | a b(imm)B C A BR | a bBRBR C bBR |  i = 3, j = 1: C A B | C C | a C B C B | aB | C C | ai = 3, j = 2: C B C B | aB | C C | a C C A BR C B | a b BRC B | aB | C C | a(imm)C a b BR C B CR | aBCR | a CRCR A BR C B CR | C CR | 

  9. Grammar problems • Consider S  if E then S else S | if E then S • Which of the two productions should we use to expand non-terminal S when the next token is if? • We can solve this problem by factoring out the common part in these rules. This way, we are postponing the decision about which rule to choose until we have more information (namely, whether there is an else or not). • This is called left factoring

  10. Left factoring A  1 | 2 |...| n |  becomes A  B| B 1 | 2 |...| n

  11. Example • Left factor the following grammar: • S iEtS | iEtSeS |a • E b

  12. Grammar problems • A symbol XV is useless if • there is no derivation from X to any string in the language (non-terminating) • there is no derivation from S that reaches a sentential form containing X (non-reachable) • Reduced grammar = a grammar that does not contain any useless symbols.

  13. Useless symbols • In order to remove useless symbols, apply two algorithms: • First, remove all non-terminating symbols • Then, remove all non-reachable symbols. • The order is important! • For example, consider S+ X where  contains a non-terminating symbol. What will happen if we apply the algorithms in the wrong order? • Concrete example: S  AB | a, A a

  14. Useless symbols • Example Initial grammar: S AB | CA A a B CB | AB C cB | b D aD | d Algorithm 1 (terminating symbols): A is in because of A a C is in because of C b D is in because of D d S is in because A, C are in and S AC

  15. Useless symbols • Example continued After algorithm 1: S CA A a C b D aD | d Algorithm 2 (reachable symbols): S is in because it is the start symbol C and A are in because S is in and S CA Final grammar: S CA A a C b

  16. Parsing • Parsing = process of determining if a string of tokens can be generated by a grammar • For any CF grammar there is a parser that takes at most O(n3) time to parse a string of n tokens • Linear algorithms suffice for parsing programming language source code • Top-down parsing “constructs” a parse tree from root to leaves • Bottom-up parsing “constructs” a parse tree from leaves to root

  17. Predictive Parsing • Recursive descent parsing is a top-down parsing method • Every nonterminal has one (recursive) procedure responsible for parsing the nonterminal’s syntactic category of input tokens • When a nonterminal has multiple productions, each production is implemented in a branch of a selection statement based on input look-ahead information • Predictive parsing is a special form of recursive descent parsing where we use one lookahead token to unambiguously determine the parse operations

More Related