1 / 32

Lecture#14-15

Lecture#14-15. Grammar. A grammar G = (V, T, S, P) consists of the following quadruple: a set V of variables (non-terminal symbols), including a starting symbol S  NT a set T of terminals (same as an alphabet, ) A start symbol S  V a set P of production rules Example: S  aS | A

adair
Télécharger la présentation

Lecture#14-15

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#14-15 Unit-2

  2. Grammar • A grammar G = (V, T, S, P) consists of the following quadruple: • a set V of variables (non-terminal symbols), including a starting symbol S  NT • a set T of terminals (same as an alphabet, ) • A start symbol S  V • a set P of production rules • Example: S  aS | A A bA | λ Unit-2

  3. Derivation • Strings are “derived” from a grammar • Example of a derivation S  aS  aaS  aaA  aabA  aab • At each step, a nonterminal is replaced by the sentential form on the right-hand side of a rule (a sentential form can contain nonterminals and/or terminals) • Automata recognize languages; grammars generate languages Unit-2

  4. Regular grammar • A grammar is said to be right-linear if all productions are of the form AxB or Ax, where A and B are variables and x is a string of terminals. (This means that if there is a variable on the right side of the production rule, then it is the rightmost element in the rule.) • A grammar is said to be left-linear if all productions are of the form ABx or Ax • A regular grammar is either right-linear or left-linear. Unit-2

  5. Linear grammar • A grammar can be linear without being right- or left-linear. • A linear grammar is a grammar in which at most one variable can occur on the right side of any production rule, without any restriction on the position of the variable. • Example: S  aS | A A Ab | λ Unit-2

  6. Another formalism for regular languages • Every regular grammar generates a regular language, and every regular language can be generated by a regular grammar. • A regular grammar is a simpler, special-case of a context-free grammar • The regular languages are a proper subset of the context-free languages Unit-2

  7. Exercises • Find a regular grammar that generates the language on  = {a,b} consisting of all strings with no more than three a’s. Unit-2

  8. Exercises • Find a regular grammar that generates the language on  = {a,b} consisting of all strings with no more than three a’s S  bS | aA | λ A  bA | aB | λ B  bB | aC | λ C  bC | λ Unit-2

  9. Exercises • Find a regular grammar that generates the language consisting of even-length strings over {a,b}. Unit-2

  10. Exercises • Find a regular grammar that generates the language consisting of even-length strings over {a,b}. S  aaS | abS | baS | bbS | λ Unit-2

  11. Programming languages • Programming languages are context-free, but not regular • Programming languages have the following features that require infinite “stack memory” • matching parentheses in algebraic expressions • nested if .. then .. else statements, and nested loops • block structure Unit-2

  12. Exercise • Given a grammar, you should be able to say what language it generates • Use set notation to define the language generated by the following grammars 1) S  aaSB | λ B  bB | b 2) S  aSbb | A A  cA | c Unit-2

  13. Exercise S  aaSB | λ B  bB | b It helps to list some of the strings that can be formed: S  aaSB  aaB  aab S  aaSB  aaB  aabB  aabb S  aaSB  aaB  aabB  aabbB  aabbb S  aaSB  aaB  aabB  aabbB  aabbbB  aabbbb S  aaSB  aaaaSBB  aaaaBB  aaaaBb  aaaabb S  aaSB  aaaaSBB  aaaaBB  aaaaBbB  aaaaBbb  aaaabbb What is the pattern? L = {(aa)nbnb*} Unit-2

  14. Exercise • Given a language, you should be able give a grammar that generates it. • For example, give a regular (right-linear) grammar for the language consisting of all strings over {a, b, c} that begin with a, contain exactly two b’s, and end with cc. Unit-2

  15. Exercise • Give a regular (right-linear) grammar for the language consisting of all strings over {a, b, c} that begin with a, contain exactly two b’s, and end with cc: S  aA A  bB | aA | cA B  bC | aB | cB C  aC | cC | cD D  c Unit-2

  16. Derivation Given the grammar, S  aaSB | λ B  bB | b the string aab can be derived in two different ways: S  aaSB  aaB  aab S  aaSB  aaSb  aab Unit-2

  17. Leftmost (rightmost) derivation In a leftmost derivation, the leftmost nonterminal is replaced at each step. In a rightmost derivation, the rightmost nonterminal is replaced at each step. Many derivations are neither leftmost nor rightmost. Unit-2

  18. Theorem 3.3 • Every language generated by a right-linear grammar is regular. • Proof: • Specify a procedure for automatically constructing an NFA that mimics the derivations of a right-linear grammar. Unit-2

  19. Theorem 3.3 • Justification: The sentential forms produced by a right linear grammar have exactly one variable, which occurs as the rightmost symbol. Assume that our grammar has a production rule D dE and that, during the derivation of a string, there is a step wcD  wcdE We can construct an NFA which has states D and E, and an arc labeled d from D to E. NFAs can be converted to DFAs. All languages accepted by DFAs are regular. Unit-2

  20. Theorem 3.3 Construction: For each variable Vi in the grammar there will be a state in the automaton labeled Vi. The initial state of the automaton will be labeled V0 and will correspond to the S variable in the grammar. For each production rule Vi a1a2…amVj the automaton will have transitions such that δ*(Vi, a1a2…am) = Vj For each production rule Vi a1a2…am the automaton will have transitions such that δ*(Vi, a1a2…am) = Vfinal Unit-2

  21. Theorem 3.3 Construct an NFA that accepts the language generated by the grammar: S  aA convert to: V0 aV1 A abS | b V1 abV0 | b a b V0 V1 Vf b a Unit-2

  22. Theorem 3.4 • Every regular language can be generated by a right-linear grammar. • Proof: • Generate a DFA for the language. • Specify a procedure for automatically constructing a right-linear grammar from the DFA. Unit-2

  23. Theorem 3.4 • Given a regular language L, let M = (Q, , δ, q0, F) be a DFA that accepts L. Let Q = {q0, q1, …, qn} and  = {a1, a2, …, am}. • Construct the grammar G = (V, T, S, P) with: V = {q0, q1, …, qn} T = {a1, a2, …, am} S = q0. P = {} initially. • P, the set of production rules, is constructed as follows: Unit-2

  24. Theorem 3.4 • For each transition δ(qi, aj) = qk in the transition table of M, add to P the production: qi ajqk • If qk is in F, then add to P the production: qk λ Unit-2

  25. Example • Construct a right-linear grammar for the language L = L(aab*a) • First, build an NFA for L: a a a q0 q1 q2 qf b Unit-2

  26. Example, cont. a a a q0 q1 q2 qf b P = {} initially. Add to P a rule for each transition in the NFA: q0 aq1 q1  aq2 q2  bq2 q2  aqf Since qf is in F, add to P the production: qf  λ Unit-2

  27. Example a a a q0 q1 q2 qf b Now P = You can convert to normal grammar notation: {q0 aq1 S  aA q1  aq2 A  aB q2  bq2 B  bB q2  aqf B  aC qf  λ } C  λ Unit-2

  28. Theorem 3.5 A language L is regular if and only if there exists a left-linear grammar G such that L = L(G). Proof: The strategy here is a little tricky. We describe an algorithm to construct a right-linear grammar that generates the reverse of all the strings generated by the left-linear grammar. Unit-2

  29. Theorem 3.5 Given any left-linear grammar we can construct from it an right-linear grammar G’ by replacing productions of the form: A  Bv with A  vRB and A  v with A  vR Since L(G’) is generated by a right-linear grammar, it is regular. It can be demonstrated that L(G) = (L(G’))R. It can be proven that the reverse of any regular language is also regular (see exercise 12, section 2.3 in the Linz text). Hence, L is regular. Unit-2

  30. Theorem 3.6 A language L is regular if and only if there exists a regular grammar G such that L = L(G). Proof: Combine our definition of regular grammars, which includes the statement, “A regular grammar is either right-linear or left-linear”, with theorems 3.4 and 3.5 Unit-2

  31. 3 ways of specifying regular languages Regular expressions DFA NFA Regular grammars describe accept Regular languages generate Unit-2

  32. THANK YOU Unit-2

More Related