1 / 26

Languages & Grammars

Languages & Grammars. Languages. FORMAL languages, not natural languages (not even machine languages) Alphabet is all symbols allowed. Σ is alphabet, λ is null letter String variables: w = abba Concatenation, reverse, length Σ * is 0 or more letters, Σ + is 1 or more letters.

luisa
Download Presentation

Languages & 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. Languages & Grammars

  2. Languages • FORMAL languages, not natural languages (not even machine languages) • Alphabet is all symbols allowed. Σ is alphabet, λ is null letter String variables: w = abba Concatenation, reverse, length Σ* is 0 or more letters, Σ+ is 1 or more letters

  3. Set Notation for Languages • Σ = {a,b} • Σ* = {λ,a,b,aa,ab,ba,bb,aaa,aab,…} • Σ+ = Σ* - {λ} • L = {anbn : n >0} • Which are in L? • λ • ab • aaa • aba • aaaaaabbb

  4. More Language Notation Σ = {a,b,c}, L1 = {ab, bc, aba}, L2 = {c,bc,bcc} • L1 U L2 = • L1 ∩ L2 = • L1 = • L1 ∩ L2 = • L1L2 = • L22 = • L1* = • L2R =

  5. Grammars • A set of rules which govern the structure of a language <sentence> <nounPhrase><predicate> <nounPhrase> Fritz <nounPhrase> The dog <predicate> ate <predicate> left

  6. Formal Grammar Notation • G = {V,T,S,P} • V are variables (<predicate>) • T are terminals (Fritz) • S is the start variable (<sentence>) • P are the production rules • Let W be a string of variables and terminals • W Y means that W can be transformed into Y using the production rules

  7. Languages + Grammars • A grammar defines a language • Many grammars can define the same language • Grammars that generate the same language are equivalent.

  8. Formal languages • Forget words; letters only • S is usually start symbol • What language? • S -> aA |  • A -> bS

  9. Exercises • If L = {anbn ,n>0} what is L2? • Write a grammar that generates:L={anbm, n<2, m<=2}

  10. ... or Puzzles? • Write a grammar that generates:L={anbm, n>0, m>=0} • Write a grammar that generates all strings on {a,b}* with exactly 2 a’s • Write a grammar that generates:L={anbn, n>0} • Write a grammar that generates: • L = {w  {a}*: |w| mod 4 = 0}

  11. Mathy Questions • Suppose a grammar for L1 has start symbol S1 and a grammar for L2 has start symbol S2. What grammar describes • L1 U L2? • L1L2? • L1*? • Can you prove that your answer is correct? • Can you prove your neighbor’s answer is wrong?

  12. Recursion • Generate a+ • S -> aS | a • Generate a* • S -> aS | 

  13. Impose Order • Language: a+b+ • S -> AB • A -> aA | a • B -> bB | b

  14. Relationship between symbols • Language: anbn n>0 • S -> aSb | ab

  15. Context-Free Grammars • A grammar is context-free if all production rules have only one non-terminal on the left-hand side A -> aSa A -> AB A -> a • Not context-free: ABB -> aaSB

  16. Members of CFLs • To see if a string is a member of a CFL, replace every non-terminal with the right side of one of its production rules. • S -> ABA -> aaA | λB -> Bb | λ • Derivation of string aab: S -> AB -> aaAB -> aaB -> aaBb -> aab • So, aab is a sentence in the language, and aaAB is a sentential form.

  17. Theory Not all Context-free languages are regular. Example: anbn Can you write a CF grammar? Can you write an automaton?

  18. Theory CF Reg BUT: All regular languages are also context-free. How could we prove this?

  19. Automata to Grammar States are non-terminals Alphabet letters are the terminals Start state corresponds to Start Symbol Final States go to lambda

  20. S -> aA A -> aA | aS | bB | C B -> bC C -> λ

  21. Derivations • Leftmost derivation always replaces the leftmost variable in the sentential form next. • Rightmost derivation always replaces the one on the right. • Derivations can be shown using a derivation tree.

  22. Example S -> aAB A -> bBb B -> A | λ Derive the string abbbb Leftmost: S-> a A B -> a bBb B -> a bAb B -> a b bBb b B -> a b b b b B -> a b b b b Rightmost:

  23. Derivation Trees • Ordered tree in which: • Interior nodes are left-hand sides of rules (variables) • Children of a node are right-hand sides • Root is start symbol • Leaves are terminals • Reading the leaves from left to right is the yield of the tree (a sentence in the language)

  24. Example: S • S -> aABA -> bBbB -> A | λ • Show derivation tree for derivation of abbbb a A B b B b λ A b B b λ

  25. Ambiguity • For any string in a context-free language, there may be more than one derivation tree that produces it. This is ambiguity. • Programming Languages cannot have ambiguity. • Try to find equivalent, unambiguous grammars, but it can’t always be done. • If there is any unambiguous grammar for a language, it is unambiguous. • If the language has no unambiguous grammar, the language is inherently ambiguous.

  26. Exercises • Grammar: • S -> Sa | SSb | b |  • Give rightmost derivation of string bbaaba • Draw derivation tree • Show that the grammar is ambiguous

More Related