1 / 8

Useful modifications to grammars

Useful modifications to grammars. Ambiguity elimination undecidable in general case whether a grammar is ambiguous not always possible to remove ambiguity – some inherently ambiguous grammars no algorithm exists for always removing ambiguity Left recursion elimination

leia
Télécharger la présentation

Useful modifications to grammars

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. Useful modifications to grammars • Ambiguity elimination • undecidable in general case whether a grammar is ambiguous • not always possible to remove ambiguity – some inherently ambiguous grammars • no algorithm exists for always removing ambiguity • Left recursion elimination • can result in empty productions – which can be eliminated too • Left factoring • combine productions with same initial RHS subsequence • by introducing new nonterminals • Add sentence-final sentinel ‘$’ to inputs, add production S’ ::= S $ http://csiweb.ucd.ie/staff/acater/comp30330.html Compiler Construction

  2. Recursive Descent parsers • A simple top-down parsing technique that requires backtracking • Constructs a leftmost derivation • Decide which production to choose, then match its RHS Proc A () choose the correct production A ::= X1 X2 … Xk for i from 1 to k if Xi is a terminal then if nextToken()=Xi then join it to the parse tree else error() else join subtree for Xi to the parse tree, and use proc Xi http://csiweb.ucd.ie/staff/acater/comp30330.html Compiler Construction

  3. Predictive parser • A predictive parser does not need to backtrack • It chooses correct production by lookahead of k symbols • Grammars for which it is possible to construct such parsers are LL(k) grammars. • LL(1) is the important case. • They depend on knowledge of FIRST and FOLLOW sets • which are also useful for bottom-up parsers later • FOLLOW sets are also useful for error recovery • Productions are stored in a predictive parsing table indexed by • the nonterminal to be recognised • the terminal found in next position in input stream http://csiweb.ucd.ie/staff/acater/comp30330.html Compiler Construction

  4. FIRST sets • Meaningful for a particular grammar • FIRST( β ) = the set of terminals that may begin strings derived from β, • may include e iff be • Motivation: If there are some productions A ::= γ | δ, and FIRST( γ ) and FIRST( δ ) are disjoint, the next symbol can distinguish between these productions • A simple algorithm computes FIRST http://csiweb.ucd.ie/staff/acater/comp30330.html Compiler Construction

  5. FOLLOW sets • Meaningful for the nonterminals of a particular grammar • FOLLOW( A) = set of terminals that can follow A in some sentential form • i.e. the set of d such that S aAdg • includes sentinel $ iff S aA • FOLLOW sets are computed by a simple algorithm http://csiweb.ucd.ie/staff/acater/comp30330.html Compiler Construction

  6. Computing FIRST and FOLLOW For grammar symbol X, If X is a terminal, FIRST(X) = {X} If X ::= Y1 Y2 … Yk is a production, k>0 add all of FIRST(Y1) except e into FIRST(X), then if FIRST(Y1) contains e add all of FIRST(Y2) except e, and so on If X ::= e is a production, add e to FIRST(X) For string of symbols X1X2…Xn, Add non-e symbols of FIRST(X1), then non-e symbols of each FIRST(Xk) if e is in all FIRST(Xj) for j<k, then e if e is in all FIRST(Xj), 1<=j<=n Place $ in FOLLOW(S) If A ::= aBb is a production, put everything in FIRST(b) except e into both FOLLOW(B) … and anything that copied (in)directly from FOLLOW(B) If A ::= aB is a production, or A ::= aBb and be then add everything in FOLLOW(B) into FOLLOW(A) etc http://csiweb.ucd.ie/staff/acater/comp30330.html Compiler Construction

  7. LL(1) grammar properties • LL(1) grammars are never left-recursive • LL(1) grammars are never ambiguous • Necessary & Sufficient Condition: • For any two productions A ::= a | b • FIRST( a ) and FIRST ( b ) are disjoint • If be then FOLLOW(A) and FIRST( a ) are disjoint • &vv – although it is unnecessary to say so since a and b are any two http://csiweb.ucd.ie/staff/acater/comp30330.html Compiler Construction

  8. Predictive Parsing Table • Indexed by (1) Nonterminal and (2) next input token • Empty cells correspond to error • Multiply-filled cells occur when the grammar is ambiguous http://csiweb.ucd.ie/staff/acater/comp30330.html Compiler Construction

More Related