Flat CH 3
Flat CH 3
Flat CH 3
• Grammar:
-Sentence structure is a lot of rules which check whether a string have a
place with a specific language or not. This defined Sentence structure is
called Grammar
• Top-down Approach
- Starts with the starting symbol S.
- Goes down to tree leaves using productions.
• Bottom-up Approach
- Starts from tree leaves.
- Proceeds upward to the root which is the starting symbol S.
.
Derivation of a Tree
• The induction or the yield of a parse tree is the last string acquired by
connecting the marks of the leaves of the tree from left to right, overlooking
the Nulls. In any case, if all the leaves are Null, deduction is Null
Derivation of a Tree : Example
• Let a CFG {N,T,P,S} be N = {S}, T = {a, b}, Starting symbol = S, P = S → SS | aSb
| ε One derivation from the above CFG is “abaabb”
Answer:-
S → SS
S → aSbS (replace S →aSb)
S →abS (replace S →ε)
S → abaSb (replace S →aSb)
S → abaaSbb (replace S →aSb)
•Example
In the event that in any CFG the creations/production are:
S → AB, A → aaA | ε, B →Bb| ε
•The fractional deduction tree can be the accompanying:
-In the event that an incomplete determination tree contains the root S, it is
known as a sentential form.
-The above example is likewise in sentential form.
Types of Derivation Tree
• Leftmost and Rightmost Derivation of a String
- Leftmost derivation: A furthest left induction is acquired by applying
creation to the furthest left factor in each progression
• On the off chance that a setting free sentence structure G has more than
one induction tree for some string w ∈ L(G), it is called an ambiguous
grammar. There exist various right-most or left-most determinations for some
string produced from that language structure.
CFG Simplification
• In a CFG, it might happen that all the production rules and symbol are not
required for the determination of strings. Furthermore, there might be some
invalid production and unit creations. End of these production and symbols is
called disentanglement of CFGs.
Steps to simply
•Reduction of CFG
•Removal of Unit Productions
•Removal of Null Productions
Reduction of CFG
Derivation of an equivalent grammar, G’, from the CFG, G, such that each
variable derives some terminal string.
Removal Procedure:-
Step 1: To remove A→B, add production A → x to the grammar rule whenever
B → x occurs in the grammar. [x ∈ Terminal, x can be Null]
Step 2: Delete A→B from the grammar.
Step 3: Repeat from step 1 until all unit productions are removed.
Removal of Null Productions
In a CFG, a non-terminal symbol ‘A’ is a nullable variable if there is a production
A → ϵ or there is a derivation that starts at A and finally ends up with
ϵ: A → .......… → ϵ
Removal Procedure:
Step1 Find out nullable non-terminal variables which derive ϵ.
Step2 For each production A → a, construct all productions A → x where x is
obtained from ‘a’ by removing one or multiple non-terminals from Step 1.
Step3 Combine the original productions with the result of step 2 and remove ϵ-
productions.
Chomsky Normal Form
In formal language theory, a CFG ”G ” is said to be in Chomsky normal form if all
of its production rules are of the form:
Q → RE,
or Q → f,
or S → ε,
Here Q,R,E are Non terminals and S is starting symbol. f and ε(epsilon) are
terminals where ε represent NULL
Greibach Normal Form
In formal language theory, a CFG is in Greibach normal form (GNF) if the right-
hand sides of all production rules start with a terminal symbol, optionally
followed by some variables.
Here P,Q,W,E,R,T,Y are non terminals and S is start symbol. ε and a are
terminals
Non Deterministic Push down Automata
•A nondeterministic pushdown automaton (npda) is basically an nfa with a stack
added to it.
•A nondeterministic pushdown automaton or npda is a 7-tuple
M = (Q,∑, Γ, δ,q0, z, F)
•Q is a finite set of states,
∑ is a the input alphabet,
Γ is the stack alphabet,
δ is a transition function,
q0 ∈ Q is the initial state,
z ∈ Γ is the stack start symbol, and
F ⊆ Q is a set of final states.
Block diagram of PDA
Example of NPDA
[1]https://www.google.co.in/imgres?imgurl=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fmedia.geeksforgeeks.org%2Fwp-
content%2Fuploads%2F33333.jpg&imgrefurl=https%3A%2F%2Ffanyv88.com%3A443%2Fhttps%2Fwww.geeksforgeeks.org%2Fnpda-for-l-0i1j2k-ij-or-jk-i-j-k-
1%2F&tbnid=7m8Az1XEry4VSM&vet=12ahUKEwjS3IKF-tbpAhVQRSsKHa-1DH
PDA equivalence to CFG
•Algorithm to find PDA corresponding to a given CFG:
• In a parse tree, the points are called nodes. Each node has a label on it.
• The topmost node is called the root. The bottom nodes are called leaves.
• In a parse tree for a grammar G, the leaves must be labelled with terminal
symbols from G, or with ǫ. The root is often labeled with the start symbol of G,
but not always.
• If a node N labeled with A has children N1, N2, . . . , Nk from left to right,
labeled with A1, A2, . . . , Ak, respectively, then A → A1A2, . . . Ak must be a
production in the grammar G.
● For every CFL L there is a constant k ≥ 0 such that for any word z in
L of length at least k, there are strings u, v,w, x, y such that
● z = uvwxy,
● Vx ≠ ϵ
● |vwx| ≤ k,
● and for each i ≥ 0, the string uviwxiy belongs to L.
Parse trees for CFG’s
Parse trees for CFG’s
• Here Language L1 generates all strings that contains occurrence of a’s equals
to occurrence of b’s and L2 generates all strings that contains occurrence of b’s
equals to the occurrence of c’s.