Context-free languages I In Chapter 1 we consider two ways to - - PowerPoint PPT Presentation

context free languages i
SMART_READER_LITE
LIVE PREVIEW

Context-free languages I In Chapter 1 we consider two ways to - - PowerPoint PPT Presentation

Context-free languages I In Chapter 1 we consider two ways to describe languages automata & regular expressions Context-free grammars (CFG) More powerful than automata CFG is used in compilers and interpreters for parsers to read programs


slide-1
SLIDE 1

Context-free languages I

In Chapter 1 we consider two ways to describe languages automata & regular expressions Context-free grammars (CFG) More powerful than automata CFG is used in compilers and interpreters for parsers to read programs

September 26, 2020 1 / 12

slide-2
SLIDE 2

Context-free grammars I

A grammar G1: A → 0A1 A → B B → # Each one is called a substitution rule Variables: A, B (capital letters) Terminals: 0, 1, # (lowercase letters, number, special symbols) Start variable: A

September 26, 2020 2 / 12

slide-3
SLIDE 3

Context-free grammars II

A grammar: a collection of substitution rules Derivation: G1 generates 000#111 A ⇒ 0A1 ⇒ 00A11 ⇒ 000A111 ⇒ 000B111 ⇒ 000#111 Parse tree Fig 2.1

September 26, 2020 3 / 12

slide-4
SLIDE 4

Context-free grammars III

A A A A B # 1 1 1

September 26, 2020 4 / 12

slide-5
SLIDE 5

Context-free grammars IV

L(G): language of grammar For the CFG example we just discussed, L(G1) = {0n#1n | n ≥ 0} CFG is more powerful than regular expressions because we showed earlier that this language is not regular Representation of rules: A → 0A1 and A → B is often simplified to

September 26, 2020 5 / 12

slide-6
SLIDE 6

Context-free grammars V

A → 0A1 | B Example S ⇒ Noun-PhraseVerb-Phrase ⇒ Complex-NounVerb-Phrase ⇒ ArticleNounVerb-Phrase ⇒ aNounVerb-Phrase ⇒ a boyVerb-Phrase ⇒ a boyComplex-Verb ⇒ a boyVerb ⇒ a boy sees

September 26, 2020 6 / 12

slide-7
SLIDE 7

Context-free grammars VI

Why called “context-free” ? Rules independent of context

September 26, 2020 7 / 12

slide-8
SLIDE 8

Formal definition of a context-free grammar I

(V , Σ, R, S) V : variables, finite set Σ: terminals, finite set R: rules variable → strings of variables and terminals (including ǫ) S ∈ V , start variable

September 26, 2020 8 / 12

slide-9
SLIDE 9

Formal definition of a context-free grammar II

For the example G1: A → 0A1 A → B B → # V = {A, B}, Σ = {0, 1, #}, S = A, R : the above three rules

September 26, 2020 9 / 12

slide-10
SLIDE 10

Derivation of strings I

If u, v, w are strings and a rule A → w is applied, then we say uAv yields uwv and this is denoted as uAv ⇒ uwv if u = v or u ⇒ u1 ⇒ · · · ⇒ uk ⇒ v then we say u

⇒ v

September 26, 2020 10 / 12

slide-11
SLIDE 11

Derivation of strings II

Language of a CFG {w ∈ Σ∗ | S

⇒ w}

September 26, 2020 11 / 12

slide-12
SLIDE 12

Example 2.3 I

G3 = ({S}, {a, b}, R, S) R: S → aSb | SS | ǫ What is the language? If we treat a, b respectively as ( and ), then we have all valid nested parentheses

September 26, 2020 12 / 12