0% found this document useful (0 votes)
25 views24 pages

3B-Formal Languages

Uploaded by

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

3B-Formal Languages

Uploaded by

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

Formal Language

Dr. Farooq Ahmad

16:09 1
Formal Language
• A language which has a well-defined
grammar.
• The grammar can be verified through
algebraic systems
• Usually called Automata
• Ref: Geeks for geeks
• Natural Language Processing - Overview –
GeeksforGeeks
• Introduction to Natural Language Processin
g-
GeeksforGeeks
16:09 2/24
Automata
The process of making a language
amenable through automatic
processor/machines/systems
• Regular Expression
• Finite Automata
• Context Free Grammar

16:09 3/24
Properties of RE
• It is a way of representing regular
languages.
• The algebraic description for regular
languages is done using regular
expressions.
• They can define the same language that
various forms of finite automata can
describe.
• Regular expression is a declarative way
to express the strings that we want to
accept. They act as input for many
systems. They are used for string
16:09 4/24
Properties of RE
• Union of two RE is RE
• Intersection of two RE is RE
• Complement of an RE is RE
• Difference of two RE is RE
• Reversal of an RE is RE
• Closure of two RE is RE
• Concatenation of two RE is RE

16:09 5/24
Regular Expression (RE)
• Algebraic notation characterizing a set
of strings
• Consisting of Regular set is a
• A Pattern to search set of outputs of RE
• A Corpus to search from e.g.
• Defined as RE = a* + ba
• ε in a RE is empty string
L = {∈, a, aa, aaa,…… , ba}
• φ in a RE is empty language
• If X and Y are RE, then
• X.Y Concatenation of XY
• X+Y Union of X and Y
• X*, Y* Kleene Closure of X and Y 6/24
Rules for Regular
Expressions
• Operators
• Concatenation(.)
• Join the LHS + RHS
• Union(+)
• Combine LHS, or RHS, or both
• Kleene closure(∗)
• Can repeat many times on LHS
• Every letter of input set ∑ can be made into a
regular expression, null string (∈), ∈ itself is a
regular expression.
• If r1 and r2 are regular expressions, then (r1),
r1.r2, r1+r2, r1*, r1+ are also regular
expressions.
16:09 7/24
Regular Expression &
Regular Set
• Same thing two different
Regular
Regular set
language
representation
∅ {}
• Example:
• ∑ = {a, b} and r is a regular ∈ {∈}
expression of language
{∈, a, aa, aaa
using these symbols a*
…..}

a+ b {a, b}
a.b {ab}
a* + ba {∈, a, aa, aaa,
…… , ba}

16:09 8/24
Operations on RE – Union,
+
The union of two regular languages, L1
and L2, which are represented using L1
∪ L2, is also regular and which
represents the set of strings that are
either in L1 or L2 or both.
Example:
L1 = (1+0).(1+0) = {00 , 10, 11, 01} and
L2 = {∈ , 100}
then L1 ∪ L2 = {∈, 00, 10, 11, 01, 100}.

16:09 9/24
Operations on RE –
Concatenation, .
• The concatenation of two regular
languages, L1 and L2, which are
represented using L1.L2 is also regular
and which represents the set of strings
that are formed by taking any string in
L1 concatenating it with any string in
L2.
• Example:
• L1 = { 0,1 } and L2 = { 00, 11} then
• L1.L2 = {000, 011, 100, 111}
16:09 10/24
Operations on RE – Kleene
Closure, *
• If L1 is a regular language, then the Kleene
closure i.e. L1* of L1 is also regular and
represents the set of those strings which are
formed by taking a number of strings from L1
and the same string can be repeated any
number of times and concatenating those
strings.
• Example:
• L1 = { 0,1}
• L1*= {∈, 0, 1, 00, 01, 10, 11 …….}
• L1* is all strings possible with symbols 0 and 1
including a null string
16:09 11/24
Algebraic Properties of RE
If r1, r2 and r3 are regular expressions(RE), then
1. Closure::
r1* is a RE, r1+r2 is a RE, r1.r2 is a RE
2. Closure laws –
• (r*)* = r*,
closing an expression that is already closed does not
change the language.
• ∅* = ∈,
a string formed by concatenating any number of copies of an
empty string is empty itself.
• r+ = r.r* = r*r, as r* = ∈ + r + rr+ rrr … and r.r* = r+ rr +
rrr …
• r* = r*+ ∈
3.
16:09
Associativity – 12/24
i.) r1+ (r2+r3) = (r1+r2) +r3
Finite State Automata-
FSA
• Self-propelled computing device with
Finite States
• Mathematically
• Q finite set of states
• Σ finite set of symbols, the alphabets
• δ set of transition
• q0 initial state (q0 Є Q)
• F set of final state/s of Q (F

16:09 13/24
Regular Language, FSA &
RE
• FSA is theoretical foundation of
computational work
• RE can be implemented as FSA
• Regular Language can be described by
RE
• Regular Grammar Characterize Regular
language

16:09 14/24
Types of FSA
1. Deterministic Finite Automata (DFA)
• Transition to only one state through same
input
• No input no transition
• Therefore, output deterministic

2. Non-deterministic Finite Automata


(NDFA)
• Multiple inputs can have same transition
• Can change state by itself
• Therefore, output non-deterministic
16:09 15/24
Deterministic Finite
Automata (DFA)
DFA consists of 5 tuples {Q, Σ, q0, F, δ}
Q : set of all states.
Σ : set of input symbols.
(Symbols which machine takes as
input )
q0 : Initial state.
( Starting state of a machine )
F : set of final state.
δ : Transition Function,
defined as δ : Q x Σ → Q.
One
16:09
input can take to one state only 16/24
DFA Example
• DFA which accept a language of all strings ending with ‘a’.
• Given: Σ = {a,b}, Q = {q0, q1}, q = {q0}, F={q1}

• RE=(a+b)*.a

• DFA

• Subset of acceptable strings of language


• L = {a, aa, aaa, aaaa, aaaaa, ba, bba, bbbaa, aba, abba, aaba, abaa,
…}
• Not acceptable
• ab, bb, aab, abbb, …

16:09 17/24
DFA - Even numbers of ‘a’s
• RE = (b|ab*ab*)*.

• DFA

• L = {b, aba, abba, …}

16:09 18/24
DFA - String with ‘ab’ as
substring
• RE = (a|b)*ab(a|b)*

• DFA

• L ={ab, bab, aab, abb, ...}

16:09 19/24
DFA-String with count of ‘a’
divisible by 3
• {a3n | n >= 0}

• DFA

• L = {aaa, aaaaaa, …}

16:09 20/24
DFA-Binary numbers
divisible by 3
• RE = (0|1(01*0)*1)*

• DFA

• L = {110, 1001, 10101, …}

16:09 21/24
Non-deterministic Finite Automata
(NDFA)
• Same as DFA
• Null or ε is allowed in input
• Can move without input
• Can transmit to any number of sates for
a particular input
• Hence
δ: Transition Function defined as
δ: Q X (Σ U ε ) →

16:09 22/24
NDFA Example

DFA is a subset of NDFA, NDFA isa theoretical


concept

DFA - δ : Q x Σ → Q.

Can go to two states on same input


NDFA - δ: Q X (Σ U ε ) →

DFA used in Lexical Analysis


16:09 23/24
4 Types of Grammar

0 Unrestricted
Grammar
1 Context
Sensitive
Grammar
2 Context Free
Grammar
3 Regular
Grammar

16:09 24/24

You might also like