1 / 9

Context-Free Grammars

Context-Free Grammars. Sipser 2.1 (pages 99 – 109). What are we missing?. So far: We know how to recognize languages With finite state automata As people… We know how to generate languages With regular expressions As people…

mignon
Download Presentation

Context-Free 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. Context-Free Grammars Sipser 2.1 (pages 99 – 109)

  2. What are we missing? • So far: • We know how to recognize languages • With finite state automata • As people… • We know how to generate languages • With regular expressions • As people… • Finite state automata and regular expressions are limited, though!

  3. Bring back memories? • In English, a grammar tells us whether a particular sentence is well formed or not • For instance, “a sentence can consist of a noun phrase followed by a verb phrase” • More concisely, we could write <sentence> →G <noun_phrase><verb_phrase>

  4. Great, but what’s a noun phrase? • A sentence is • <sentence> →G <noun_phrase><verb_phrase> • We need to provide definitions for the newly introduced constructs <noun_phrase> and <verb_phrase> • <noun_phrase> →G <article><noun> • <verb_phrase> →G <verb>

  5. Generating well-formed sentences • Grammar rules so far: • <sentence> →G <noun_phrase><verb_phrase> • <noun_phrase> →G <article><noun> • <verb_phrase> →G <verb> • To complete our simple grammar, we associate actual words with the terms <article>, <noun>, and <verb> • <article> →G a • <article> →G the • <noun> →G student • <verb> →G relaxes • <verb> →G studies.

  6. Context-free grammars • A context-free grammar G is a quadruple (V, Σ, R, S), where • V is a finite set called the variables • Σis a finite set, disjoint fromV, called the terminals • Ris a finite subset ofV × (V∪Σ)* called the rules • S∈Vis called the start symbol • For any A∈V and u∈(V∪Σ)*, we write A→Gu whenever (A, u)∈R

  7. The language of a grammar • If • u, v, w∈ (V∪Σ)* • A →Gw is a rule then • We say uAvyields uwv • Write uAv⇒Guwv • If • u⇒G u1 ⇒G u2 ⇒G ... ⇒G uk⇒G v then • We write u ⇒*Gv • The language of the grammar G is L(G) = {w∈Σ* | S ⇒*G w}

  8. For example… • ConsiderG = (V, Σ, R, S), where • V={S} • Σ={a,b} • R ={ S →GaSa | bSb | aSb | bSa | ε} • Is there a grammar whose language is PAL = {w∈ Σ* | w = reverse(w)}?

  9. Arithmetic expressionsand parse trees • ConsiderG = (V, Σ, R, S), where • V ={<EXPR>, <TERM>, <FACTOR>} • Σ ={a, +, ×, (, )} • R ={ <EXPR> →G <EXPR>+<TERM> | <TERM>, <TERM> →G <TERM>×<FACTOR> | <FACTOR>, <FACTOR> →G (<EXPR>) | a } • S = <EXPR> • What about a × a +a?

More Related