Finite Automata - Context Free Grammar - Notes, Study notes of Electrical and Electronics Engineering

Description about Context Free Grammars, Reading assignment, Non-Determinism, PDA Push Down Automata, Eliminating non-generative symbols, Modifying productions.

Typology: Study notes

2010/2011

Uploaded on 09/03/2011

krithika
krithika 🇮🇳

4.4

(58)

96 documents

1 / 58

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Context Free Grammars
Grammar: Grammar is a recursive definition of
Language
(Natural or Programming)
Formally: Grammar G = {V,T,P,S}
Terminals: Basically T = ∑
Variables: Non terminal symbols that represent
sets of strings being defined recursively
Start Symbols S: S belongs to V and is a special
symbol that generates the desired language
Production rules P: Recursive definitions
Note: T, V and P are always finite sets.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a

Partial preview of the text

Download Finite Automata - Context Free Grammar - Notes and more Study notes Electrical and Electronics Engineering in PDF only on Docsity!

Grammar : Grammar is a recursive definition of Language (Natural or Programming) Formally : Grammar G = {V,T,P,S} Terminals : Basically T = ∑ Variables : Non terminal symbols that represent sets of strings being defined recursively Start Symbols S : S belongs to V and is a special symbol that generates the desired language Production rules P : Recursive definitions Note : T, V and P are always finite sets.

L

eq Example : The grammar: G eq

= (V,T,P,S)

T = {0,1}

V = {S, A, B}

P = { S  ε|oA|1B A  1S|0AA B  0S|1BB} Notations : For set of rules A  α 1 , A α 2 , A  α 3 Short cut: A  α 1 | α 2 | α 3

Language of CFG G = {V, T, P, S} is defined as Context Free Language is any language which has a Context Free Grammar G Terminology: Sentence : Any w Є T* such that S => w Sentential form : any α Є (V U T)* such that S => α Terminals : a, b, c… Variables : A, B, C, … Terminal Strings : …, u, v, w, x, y, z V U T : …, X, Y, T Sentential Form : α, β, γ, …

L ( G )  { w  T *| S  w }

Reading assignment: From Textbook, ( nd edition,) Theorem 5.7 (which talks about language of palindromes) Example : Arithmetic Expressions G = {V, T, P, S} V = {E, I} S = E; T = { x, y, z, +, , (, )} P = { E  I|(E)|E+E|EE I  x|y|z } Apply : E => E+E => E+EE => I+EE => x+E*E

Left-most derivation : Always substitute the leftmost variable with a production rule in sentential form arising in course of a derivation. Notation : Example : E => E+E => I+E => x+E…. Similarly : We can define right-most derivations Example : E => E+E => E=EE => E=EI…. Now we can talk about canonical derivation sequence and

Derivation Trees : Also called as parse trees in compilers Idea is to represent the derivation pictorially This tree represents the derivation x+y*z In General to represent A => α  (^) Root labeled A  (^) Leaves in left to right order gives α  (^) Internal nodes are labeled with variables and their children specify the production rule

Leftmost derivation(lm) is obtained by traversing the tree in depth-first order always going into left subtrees before right one’s Similarly , Rightmost derivation(rm) comes from depth-first traversal going into right subtrees first.

Claim : Following are all equivalent statements. For CFG G = (V, T, P, S) and string w Є T* a)w Є L(G) b)S =(LM)=> w c)S =(RM)=> w d)There exists an S-tree which yields w Of course : we could always use leftmost derivations to specify a simplest way to derive any w belonging to L(G) or convert parse tree to unique derivations – Thus simplifying the task of parsers. But , what if some w belonging to L(G) has two

Definition : A CFG is ambiguous if for some w Є L(G), there exists more than one distinct parse tree. In compilers parse trees determine interpretation and we cannot allow ambiguity. Of course we can force use of parenthesis, but we should really redesign the grammar to be unambiguous by encoding precedence of operators. (See textbook for redesigning of grammars)

Definition : A CFG is called Inherently Ambiguous if all its grammars are ambiguous. Example : L = {a n b n c m d m | n,m≥1} U {a n b m c m d n | n,m≥1} Consider the strings of the form a k b k c k d k ,  (^) We can never tell whether this string came from first or second type of strings in L and any CFG must allow both of these possibilities.

Push Down Automata(PDA)

Setup : A PDA on transition 1.Consumes an input symbol 2.Goes to a new state(or stays in the old) 3.Replaces top of the stack by any string (does nothing, pops the stack or pushes a string onto the stack) Push Down Automata (PDA) is essentially an ε-NFA

Push Down Automata(PDA)

Stack Notation Content : (top)ABBAC(bottom) Pop : returns A; new content BBAC Push(XYZ) : new content XYZBBAC Transitions : Determined by : Input or ε-move Current state Stack top Effect : New state Pop Push new string

Push Down Automata(PDA)

Transition function : δ takes as argument a triple given as Suppose we have δ(q, a, X) then

  1. q is a state in Q
  2. a is either an input symbol in Σ or a = ε, the empty string which is not to be assumed an input symbol.
  3. X is the stack symbol in Γ Output of δ is finite set of pairs (p i ,α i ) where p i is the new state and α i is the string of stack symbol that replaces X. δ(q,a,X) = {(p , α ), (p , α ), …}

: { } 2      QQ  

Push Down Automata(PDA)

Action : First PDA pops stack top to determine X, reads input to determine a (unless it is an ε-transition) then knowing q, a, X it selects non-deterministically one of the possibilities of (p i , α i ) Finally : State : goes from q to p i Input : scans past a (unless a = ε) Stack : Loses old top symbol X but gets α i pushed onto it. Note : We thus need Z 0 on stack initially to allow the first transition to pop the stack Convention : String in Σ

: … x, y, z