0% found this document useful (0 votes)
14 views35 pages

Finite Automata C FLV 2

The document discusses Context-Free Languages (CFLs) and their representation through Context-Free Grammars (CFGs) and Pushdown Automata (PDAs). It explains the structure of CFGs, the process of derivations, and the components of PDAs, highlighting their applications in programming languages and compilers. Additionally, it covers the properties of CFLs, including closure and non-closure properties, as well as the advantages and limitations of using CFLs.

Uploaded by

Radiel Kassa
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)
14 views35 pages

Finite Automata C FLV 2

The document discusses Context-Free Languages (CFLs) and their representation through Context-Free Grammars (CFGs) and Pushdown Automata (PDAs). It explains the structure of CFGs, the process of derivations, and the components of PDAs, highlighting their applications in programming languages and compilers. Additionally, it covers the properties of CFLs, including closure and non-closure properties, as well as the advantages and limitations of using CFLs.

Uploaded by

Radiel Kassa
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/ 35

MicroLink information Technology

College Department of Computer Science


CSSE-363 Formal Languages and Automata

Formal Languages and Automata


1
Context Free Language

2
Context Free Language

3
Context Free Language

 Context-Free Language (CFL) is a language which is generated


by a context-free grammar and gets accepted or recognized by
a Pushdown Automata.

4
Context-Free Languages

➢ A language that is defined by some CFG is called a context-free


language.
➢ There are CFL’s that are not regular languages, such as the example
just given.
➢ But not all languages are CFL’s.
➢ Intuitively: CFL’s can count two things, not three.

5
Context-Free Grammars

6
Informal Comments

➢ A context-free grammar is a notation for describing


languages.
➢ It is more powerful than finite automata or RE’s, but
still cannot define all possible languages.
➢ Useful for nested structures, e.g., parentheses in
programming languages.

7
CFG
 A context-free grammar (CFG) G is a quadruple (V, Σ, R, S) where
 V: a set of non-terminal symbols
 Σ: a set of terminals (V ∩ Σ = Ǿ)
 R: a set of rules (R: V → (V U Σ)*)
 S: a start symbol.
➢ Terminals = symbols of the alphabet of the language being defined.
➢ Variables = nonterminals = a finite set of other symbols, each of
which represents a language.
➢ Start symbol = the variable whose language is the one being
defined.
➢ A production has the form variable -> string of variables and
terminals.

8
Informal Comments

➢ Basic idea is to use “variables” to stand for sets of strings (i.e.,


languages).
➢ These variables are defined recursively, in terms of one another.
➢ Recursive rules (“productions”) involve only concatenation.
➢ Alternative rules for a variable allow union.

9
Example: CFG for { 0n1n | n > 1}

➢ Productions:
➢ S -> 01
➢ S -> 0S1
➢ Basis: 01 is in the language.
➢ Induction: if w is in the language, then so is 0w1.

10
Example: Formal CFG

➢ Here is a formal CFG for { 0n1n | n > 1}.


➢ Terminals = {0, 1}.
➢ Variables = {S}.
➢ Start symbol = S.
➢ Productions =
➢ S -> 01
➢ S -> 0S1

11
Derivations – Formalism

➢ We say A =>  if A ->  is a production.


➢ Example: Rule 1=S → 0S.
Rule 2 = S → 01.
Rule 2 = ε.

➢ To generate 000111
➢ 0S1 ------Rule 1
➢ 00S11 ------Rule 1
➢ 000111 ------Rule 2

12
Convention

➢ Convention:
➢ A, B, C,… are variables.
➢ a, b, c,… are terminals.
➢ …, X, Y, Z are either terminals or variables.
➢ …, w, x, y, z are strings of terminals only.
➢ , , ,… are strings of terminals and/or variables.

13
Derivations – Intuition

➢ We derive strings in the language of a CFG by starting


with the start symbol, and repeatedly replacing some
variable A by the right side of one of its productions.
 That is, the “productions for A” are those that have A on
the left side of the ->.

14
Example: Kleene Closure

➢ Grammar for unsigned integers can be replaced by:


U -> UD | D
D -> 0|1|2|3|4|5|6|7|8|9

15
Leftmost and Rightmost Derivations

➢ Derivations allow us to replace any of the variables in a string.


➢ Leads to many different derivations of the same string.
➢ By forcing the leftmost variable (or alternatively, the rightmost
variable) to be replaced, we avoid these “distinctions without a
difference.”

16
Leftmost Derivations

➢ Say wA =>lm w if w is a string of terminals only and A


->  is a production.
➢ Also,  =>*lm  if  becomes  by a sequence of 0 or
more =>lm steps.

17
Example: Leftmost
Derivations
➢ Balanced-parentheses grammmar:
➢ S -> SS | (S) | ()
➢ S =>lm SS =>lm (S)S =>lm (())S =>lm (())()
➢ Thus, S =>*lm (())()
➢ S => SS => S() => (S)() => (())() is a derivation, but not a leftmost
derivation.

18
Rightmost Derivations

➢ Say Aw =>rm w if w is a string of terminals only and A


->  is a production.
➢ Also,  =>*rm  if  becomes  by a sequence of 0 or
more =>rm steps.

19
Example: Rightmost Derivations

➢ Balanced-parentheses grammmar:
S -> SS | (S) | ()
➢ S =>rm SS =>rm S() =>rm (S)() =>rm (())()
➢ Thus, S =>*rm (())()
➢ S => SS => SSS => S()S => ()()S => ()()() is neither a rightmost nor a
leftmost derivation.

20
Example b + b x b
1. E→ E + E
2. E→ E x E
3. E→ b

4. E→ ∈
1. Derivation 2. Parsing tree
E➔E+E
E
➔ id + E
➔ id + E x E
E + E
➔ id + id x E
➔ id + id x id
b E x E

b
Note: rules to be followed when constructing parsing tree b
1. Root vertex = start symbol/non-terminal
2. Leaf vertex = terminals
3. intermediate vertex =non-terminal 21
Summary

22
Pushdown Automata and CFL

 A pushdown automaton is a type of computational


model that uses a stack to hold additional information.
 Unlike finite automata, which have limited memory,
PDAs can use the stack to handle infinite input
possibilities.
 A PDA consists of states, transitions, an input alphabet,
a stack alphabet, and a transition function that
specifies how the automaton moves between states
based on the input and stack operations (push, pop, or
no operation).
 PDAs are used to recognize context-free languages, such
as those generated by context-free grammars.
23
Cont…
 A pushdown Automata(PDA) is a way to implement a
context free grammar in a similar way we design Finite
Automata for regular language.
 It is more powerful than FSM(Finite State Machine)
 FSM has a very limited memory, but PDA has memory
 PDA = FSM(Finite State Machine) + A stack
 A stack is a way we arrange elements one on top of
another.
 A stack does two basic operations:
 PUSH: A new element is added at the top of the stack
 POP: The top element of the stack is read and removed

24
Components of PDA
 A Pushdown Automata(PDA) has 3 components:
1) An input tape
2) A Finite Control Unit
3) A Stack with infinite size

25
Formal Definition of PDA
 A Pushdown Automata (PDA) can be defined as -
M = (Q, Σ, Γ, δ, q0, Ζ, F) where
• Q is a finite set of states
• ∑ is a finite set of input symbols
• Γ is a finite stack alphabet (which can be pushed and
popped from stack)
• q0 is the start state
• Z is the start stack symbol
• F is the set of final/accepting states
• δ is a transition function which maps Q x {Σ ∪ ∈} x Γ into
Q x Γ*. In a given state, PDA will read input symbol and
stack symbol (top of the stack) and move to a new state
and change the symbol of stack.
26
Cont…
 δ takes as argument a triple δ (q, a, X) where:
 (i) q is a State in Q
 (ii) a is either an Input Symbol in Σ or a= ∈
 (iii)X is a Stack Symbol, that is a member of Γ
 The output of δ is finite set of pairs (p, y)
 where: p is a new state
 y is a string of stack symbols that replaces X at the top of
the stack
 Eg. If y= ∈ then the stack is popped
 If y=X then the stack is unchanged
 If y = YZ then X is replaced by Z and Y is pushed onto the
stack

27
Transition Operation

28
Example: L={0n1n|n>=0}

 Construct a PDA that accepts L={0n1n|n>=0}


0, ∈→0 1, 0→ ∈

Start ∈, ∈→Z 1, 0→ ∈ ∈, Z→ ∈
q0 q1 q2 q3

State(Q)={q0, q1, q2, q3}


Initial state =q0
Accept state= q3
stack symbol = Z

29
Instantaneous Descriptions

 We can formalize the pictures just seen with an


instantaneous description (ID).
 A ID is a triple (q, w, ), where:
1. q is the current state.
2. w is the remaining input.
3.  is the stack contents, top at the left.

30
Application of Pushdown
Automata(PDA)

 Pushdown Automata are essential in the design of


parsers for programming languages, compilers, and
other systems that need to process complex hierarchical
structures.

31
Properties of CFLs

 Closure Properties
 Union
 Concatenation
 Kleene Star
 Intersection with Regular Languages

 Non-Closure Properties
 Intersection with other CFLs
 Complement

32
Applications of CFLs and PDAs

 Programming Languages: Parsing and syntax analysis.

 Compilers: Translation of source code into machine


code.
 Natural Language Processing: Understanding and
generating human languages.

33
Advantages and Limitations

 Advantages:
 More expressive than regular languages.
 Suitable for describing nested structures.
 Limitations:
 Cannot handle all possible languages (e.g., context-sensitive
languages).

34
35

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