0% found this document useful (0 votes)
19 views7 pages

Compact L5 Final 1

Uploaded by

Bhanu Priya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views7 pages

Compact L5 Final 1

Uploaded by

Bhanu Priya
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Grammar Definition Grammars

A grammar G is defined as G = (V, T, P, S) where:


Grammar z V : Finite set of variables/non-terminals
(We use capital letters A,B,C,… for variables)
z T : Alphabet/Finite set of terminals
(We use small letters a,b,c,… for terminals)
Context-sensitive Grammar z P : Finite set of rules/productions
Regular Grammar
z S : Start symbol
S ∈ V
Context-free Grammar V ∩ T = φ
Rule : α → β
+
α ∈ ( V ∪ T) , β ∈ (V ∪ T) *
Left-linear Grammar right-linear Grammar
Each grammar G defines a language L(G), which is the
set of strings in T* (=S*) that G can generate from S.
It is all about the production rules.
1

Grammars Grammars
Definition Example

Given a grammar G = (V, T, P, S) Given a grammar G = (V, T ,P,S)


For a string w=uxv we can apply the production rule V={A, B, C}
xày to w so we get a string z=uyv. T={a, b, x}
In this case we write w z, which reads w drives z. S= A
And P is:
If w1 w2 … wn ,
we say that w1 drives wn and we write w1 * wn AaBxàaBAaBb
CaBxàaBAaCb
ABCà?

Grammars Grammars
Definition

Let G=(V, Τ, P, S) be a grammar.


• w ∈ (V ∪ T) * is a sentential form, if Some Remarks
S ⇒*G w

• w ∈ T * is a sentence, if S ⇒*G w The language L(G) = { w∈T* : S ⇒* w } contains


only strings of terminals, not variables.
• The language of G,
Notation: We summarize several rules for one variable:
L(G) = {w ∈ T* | S ⇒ G w}
*
A→ B
A → 01 by A → B | 01 | AA
A → AA

5 6

1
Grammars Right-Linear Grammars
Example
Definition
Given the grammar:
A Grammar G= (V, T ,P,S) is called right-
linear grammar if every production is of
G = {{S },{a , b},{S → λ , S → aSb}, S ) the form A à xB, or A à x where
A,B ∈ V, x ∈ T*
The language generated by this grammar is: Example: The grammar
x à 0x | 1y
y à 0x | 1z
L(G ) = {a n b n | n ≥ 0} z à 0x | 1z | ?
Is a right-linear grammar.
7

Left-Linear Grammars Regular Grammars

Definition Definition

A Grammar G= (V, T ,P,S) is called left- A Grammar G= (V, T ,P,S) is called regular
linear grammar if every production is of grammar if its is left- or right-linear
the form A à Bx, or A à x where
A,B ∈ V, x ∈ T*
Example: The grammar Example: The grammar
x à x0 | y1 x à 0x | 1y
Example: The grammar
y à x0 | z1 y à 0x | 1z
x à x0 | y1
z à x0 | z1 | ? z à 0x | 1z | ?
y à x0 | z1
Is a left-linear grammar, Is a right-linear grammar,
z à x0 | z1 | ?
hence is Regular Grammar Hence is Regular Grammar
Is a left-linear grammar.

Grammars Grammars
Example 1

Write a grammar that generate the language: Example 3

L = { w ∈ { a , b }* | length ( w ) is EVEN }
Write a grammar that generate the language:
E → λ E → λ | aO | bO L = {w ∈{a , b, c}* | w does not contain abc}
| aaE | abE
| baE | bbE O→ aE | bE reset → b reset | c reset
| a seenA |λ
Example 2
seenA → a seenA | c reset
Write a grammar that generate the language:
| b seenAB |λ
L = {w ∈ {a , b}* | w has EVEN number of b' s}
seenAB → a seenA
E → λ | aE | bO
| b reset |λ
O → aO | bE

2
Context-free Grammars
Definition CFG = (V, T, P, S)

zV : Finite set of variables/non-terminals


zT : Alphabet/Finite set of terminals
Context-free Grammars zP : Finite set of rules/productions
zS : Start symbol
S∈ V
V∩T =φ
Rule : A → ω
A ∈ V ω ∈ (V ∪ T) *
13

Context-free Grammars Context-free Grammars


Definition Derivation

zOne-step Derivation

zContext
Context--freeness: An A-rule can be
uAv A ⇒
→ω
uωv
applied whenever A occurs in a string,
zw is derivable from v in CFG, if there is a
irrespective of the context (that is, non-
finite sequence of rule applications such
terminals and terminals around A).
that:
v ⇒ w1 ⇒ ... ⇒ wn = w
In this case we can write this derivation as v *w

Context-free Grammars Context-free Grammars


Derivation Example 1
Let G = ({S}, {a,b},S,P) with for P:
The derivation as v * w is called: zS? aSa, and S? bSb, and S? ?.
zSome derivations from this grammar:
Leftmost derivation: if in every step the leftmost z S ⇒ aSa ⇒ aaSaa ⇒ aabSbaa ⇒ aabbaa
variable is selected for reduction z S ⇒ bSb ⇒ baSab ⇒ baab, and so on.

Rightmost derivation: if in every step the rightmost zIn general S ⇒….⇒ wwR for
variable is selected for reduction
w∈{a,b}*.

L(G)={wwR : w∈{a,b}*}

3
Context-free Grammars Context-free Grammars
Example 3
Example 2 G = ({S , A, B},{a , b},
Take the CFG S → 0 | 1 | ¬(S) | (S)∨(S) | (S)∧(S), which
{S → AB , A → aA | λ , generates all proper Boolean formulas that use
B → Bb | λ}, “0”, “1”, “¬”, “∨”, “∧”, “(“ and “)”.

S) Then “(0)∨((0)∧(1))” can be derived in the following


ways…
L(G ) = L(a * b*) [leftmost] S ⇒ (S)∨(S) ⇒ (0)∨(S) ⇒ (0)∨((S)∧(S))
Leftmost D erivation : ⇒ (0)∨((0)∧(S)) ⇒ (0)∨((0)∧(1))
[rightmost] S ⇒ (S)∨(S) ⇒ (S)∨((S)∧(S)) ⇒ (S)∨((S)∧(1))
S ⇒ AB ⇒ aAB ⇒ aB ⇒ aBb ⇒ ab ⇒ (S)∨((0)∧(1)) ⇒ (0)∨((0)∧(1))
Rightmost Derivation : [something else] S ⇒ (S)∨(S) ⇒ (0)∨(S) ⇒ (0)∨((S)∧(S))
⇒ (0)∨((S)∧(1)) ⇒ (0)∨((0)∧(0))
S ⇒ AB ⇒ ABb ⇒ Ab ⇒ aAb ⇒ ab

Context-free Grammars Context-free Grammars

Example 5
Example 4
Consider the CFG G:

S → aSa | aBa
Consider the CFG:

G = {{ S }, { a , b }, { S → λ , S → aSb }, S )
B → bB | b
zDerivation of aabb is
L( B) = {b m | m > 0}
S ⇒ aSb ⇒ aaSbb ⇒ aabb
L( S ) = {a n bm a n | n > 0 ∧ m > 0}
L(G)= L(S)

Context-free Grammars Context-free Grammars

Example 6 Example 7 Consider the CFGs G1 and G2 :


S → aSa | B
Consider the CFG G1: S → AB
B → bB | λ G1 :
S → aS | aB
A → aA | a G2 :

B → bB | λ
The language generated by G1 is:
B → bB | λ
L (G1) = {a nb m a n | n ≥ 0 ∧ m ≥ 0}
The language generated by G1 and G 2 is:
Consider the CFG G2: S → abSc | λ L(G1) = L(G 2) = L(S)

The language generated by G2 is: L ( S ) = { a n b m | m ≥ 0 ∧ n > 0}


L (G2 ) = {( ab) n c n | n ≥ 0} L( S ) = L (a +b* )

4
Context-free Grammars Context-free Grammars

Example 8 Exercise

Write a CFG to generate the language: a * ba * ba *


WRITE A CFG FOR THE EMPTY SET
S → aS | B
S → AbAbA G = { {S}, S, ∅, S }
B → bA
A → aA | λ A → aA | bC
C → aC | λ
26
Left to right generation of string.

Context-free Grammars Context-free Grammars

Exercise 3 Example
Consider the CFG G=({S,Z},{0,1},S,P) with
P: S → 0S1 | 0Z1
Z → 0Z | ?
What is the CFG ({S},{(,)},S,P) that produces What is the language generated by G?
the language of correct parentheses like (), (()), or ()(())?
Answer: L(G) = {0i 1j | i≥j }

S? (S)|SS|? Specifically, S yields the 0j+k1j according to:


S ⇒ 0S1 ⇒ … ⇒ 0j S1j ⇒
0j Z1j ⇒ 0j 0Z1j ⇒ … ⇒ 0j+kZ1j ⇒ 0j+ke1j = 0j+k1j

27 28

Context-free Grammars Context-free Grammars


Exercise Derivation Tree

n Can you make Context Free Grammars for the following? For a CFG G=(V,T,S,P) a derivation
tree has the following properties: Example
a) { 0n1n : n=
n=0}
b) { 0n1m : n,m=
n,m =0} 1) The root is labeled S S
2) Each leaf is from T∪{?}
c) Arithmetic a,b,c formulas like a+b×c+a (without ())
a+b× 3) Each interior node is from V
4) If node has label A∈V and ( S ) ∨ ( S )
n Answers: its children a1…an (from L to R),
a) S ? 0S1 | ? then P must have the rule
b) S ? 0S | R and R ? 1R | ? A? a1…an (with aj∈V∪T∪{?}) 0 ( S ) ∧ ( S )
5) A leaf labeled ? is a single
c) S×S
S ? a | b | c | S+S | S× child (has no siblings). 1
0
For partial derivation trees we have:
29 2a) Each leaf is from V∪T∪{?} 30

5
Context-free Grammars Context-free Grammars
Derivation Tree: Example Derivation Tree: Notes
Take the CFG S → 0 | 1 | ¬(S) | (S)∨(S) | (S)∧(S), which Application of a production rule A ? x is
generates all proper Boolean formulas that use “0”, “1”, represented by node A with children x.
“¬”, “∨”, “∧”, “(“ and “)”. (Note that the tree is ordered:
the ordering of the nodes matters.)
S
The derivation S ⇒* (0)∨((0)∧(1)) can be expressed by
the following derivation tree: S The root has variable S.

The yield of S (is S ) ∨ ( S )


( S ) ∨ ( S ) expressed by the
leaves of the tree.
0 ( S ) ∧ ( S )
0 ( S ) ∧ ( S )

0 1 31
0 1 32

Context-free Grammars Context-free Grammars


Derivation Tree: Notes Example 1 Consider the CFG G:

G = {{ S }, { a , b }, { S → λ , S → aSb }, S )
n Looking
at a tree you see the derivation without the
unnecessary information about its order.
zThe derivation of aabb is:
n Theorem
Theorem:: Let G be a CFG. We have w∈L(G L(G)) if and S ⇒ aSb ⇒ aaSbb ⇒ aabb
only if there exists a derivation tree of G with yield w.
S
n Also, y is a sentential form of G if and only if there exists
a partial derivation tree for G. zDerivation tree is S
n Remember: the root always has to be S. a b

a λ b
33

Context-free Grammars Context-free Grammars


Example 3
<EXPR> ? <EXPR> + <EXPR>
Example 2
<EXPR> ? <EXPR> * <EXPR>
A ⇒ 0A1 ⇒ 00A11 ⇒ 00B11 ⇒ 00#11 <EXPR> ? ( <EXPR> )
<EXPR> ? a
A Build a parse tree for a + a * a
A <EXPR>

A
<EXPR>
B <EXPR>
<EXPR> <EXPR>
0 0 # 1 1
35 36
a + a * a

6
Context-free Grammars

Exercise

WRITE A CFG FOR EVEN-LENGTH


PALINDROMES over S={a,b,c,d} ?
S ? aSa for all a ∈ S
S? ?

37

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy