0% found this document useful (0 votes)
16 views17 pages

Languages, Grammar and Recognizers

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)
16 views17 pages

Languages, Grammar and Recognizers

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/ 17

LANGUAGES,GRAMMAR

AND RECOGNIZERS
GROUP 1
YUSSUF MARIAM AGBEKE
ANIFOWESHE CLINTON
OGUNTIMOJU SARAH
ADEGBOYEGA OLAYINKA
Introduction to Formal Languages
Definition: A formal language is a precisely defined set of strings over an alphabet. Unlike
natural languages (like English) which can be ambiguous, formal languages follow
strict mathematical rules that eliminate ambiguity.

Mathematical Definition: - Let Σ be an alphabet (finite set of symbols) - Σ*


represents all possible strings over Σ (including empty string ε) - A formal language L
is simply a subset of Σ* (L ⊆ Σ*)

Importance: Eliminates ambiguity, enables automated processing, defines programming


languages

Key Difference: Unlike natural languages, formal languages follow strict mathematical rules
KEY COMPONENTS
•Alphabet (Σ): Finite set of symbols

•String (w): Sequence of symbols from Σ

•Empty string (ε): String with no symbols

•Language (L): Set of valid strings over Σ

•Grammar (G): Rules defining structure of strings in a


language
Operations on Languages
•Union (L₁ ∪ L₂): Strings in either L₁ or L₂

•Intersection (L₁ ∩ L₂): Strings in both L₁ and L₂

•Concatenation (L₁·L₂): All possible combinations of strings from


L₁ followed by strings from L₂

•Kleene Star (L*): Zero or more concatenations of L (includes ε)

•Kleene Plus (L⁺): One or more concatenations of L

•Complement (L̄ ): All strings not in L


What is a Grammar?
Definition: A grammar is a formal system that specifies the syntactic structure of a language
through production rules. It defines how strings in a language are constructed.

Formal Definition: A grammar G is defined as a 4-tuple G = (N, T, P, S) where: - N = Set of


non-terminal symbols (variables like E, S, etc.) - T = Set of terminal symbols (elements of
the alphabet) - P = Set of production rules (α → β, where α contains at least one non-
terminal) - S = Start symbol (S ∈ N, the initial non-terminal)

Formal Notation: G = (N, T, P, S)


N: Non-terminal symbols
T: Terminal symbols
P: Production rules
S: Start symbol

EXAMPLE:
E→E+T|T
T→T*F|F
F → (E) | id
Derivations and Parse Tree
Derivation: Sequence of rule applications transforming start symbol into terminal string

Example: S ⇒ (S) ⇒ ((S)) ⇒ (())

Types:
Leftmost derivation: Always replace leftmost non-terminal first
Rightmost derivation: Always replace rightmost non-terminal first

Parse Tree: a graphical representation of the derivation process of an input program, showing each
grammar symbol used in the derivation
The Chomsky Hierarchy
Type 0 (Unrestricted):
•Rules: α → β (no restrictions)
•Recognizer: Turing Machine
•Example: Valid programs that halt

Type 1 (Context-Sensitive):
Rules: αAβ → αγβ (|γ| ≥ 1)
Recognizer: Linear Bounded Automaton
Example: a^n b^n c^n

Type 2 (Context-Free):
Rules: A → γ (A is a single non-terminal)
Recognizer: Pushdown Automaton
Example: Balanced parentheses, a^n b^n

Type 3 (Regular):
Rules: A → aB or A → a
Recognizer: Finite Automaton
Example: (ab), ab*
Ambiguity in Grammars
Definition: A grammar is ambiguous if there exists at least one string with multiple valid
leftmost derivations or parse trees.

Example: E → E + E | E * E | (E) | id
String "a + b * c" can be parsed two different ways

Problems: Leads to unpredictable behavior in compilers

Resolving Ambiguity:

1. Rewrite the grammar to be unambiguous

2. Define operator precedence rules

3. Specify associativity (left/right)


Regular Languages
Regular Languages:

Is a formal language that can be recognized by a finite automation or defined by a regular expression

Recognized by: Finite Automata (FA)

Defined by: Regular Expressions or Type 3 Grammars

Properties:
Closed under union, intersection, concatenation, complement, Kleene star
Cannot count or match balanced structures
Cannot recognize nested structures

Regular Expressions: - Basic elements: ε (empty string), a (terminal symbols) - Operations: Concatenation
(rs), Alternation (r|s), Kleene Star (r*), Grouping ((r)), Optional (r?), One or more (r+)

Examples of Regular Languages: -

Binary strings ending in 1: (0|1)1

Strings with even number of 0s: (1(0101))

Strings with no consecutive 1s: (0|10)1?


Context-Free Languages
CONTEXT FREE LANGUAGE:
Is a language generated by a context-free by a context-free grammar(CFG),which is a type of grammar that
uses rules to produce strings without considering the surrounding context of symbols

Defined by: Context-Free Grammars (CFGs)

Recognized by: Pushdown Automata (PDAs)

Properties:
Closed under union, concatenation, Kleene star (NOT intersection or complement)
Can recognize balanced structures
Examples of Context-Free Languages: - Balanced parentheses: S → (S)S

ε - Palindromes: S → aSa | bSb | a | b | ε - Equal a’s and b’s: S → aSbS | bSaS | ε

Applications:

Programming language syntax

XML/HTML validation

Simple natural language parsing


Finite Automata
Definition: mathematical models of computation that consist of a finite number of states and
transitions, used to recognize patterns and process regular languages
Types:
 DFA: Deterministic (exactly one transition per input)
 NFA: Non-deterministic (multiple possible transitions)

Components:

States

Input alphabet

Transition function

Start state

Accepting states

Applications:

Lexical analysis

Pattern matching
Pushdown Automata
Definition: A Pushdown Automata (PDA) is a type of finite automata with an added stack
memory, enabling it to recognize Context-Free Languages. PDAs are used in computation
theory and are more capable than finite-state machines

Components: States, input alphabet, stack alphabet, transition function, start state, initial stack
symbol, accepting states

Example:

PDA for {a^n b^n | n ≥ 1}


Push X for each 'a'
Pop X for each 'b'
Accept if stack empty after input processed

Power: Can recognize all context-free languages


Compiler Construction Applications
• Lexical Analysis:
 Uses regular expressions and finite automata
 Converts source code to token stream
 Tools: Lex/Flex

• Syntax Analysis:
 Uses context-free grammars
 Builds parse trees
 Techniques: LL, LR parsing
 Tools: Yacc/Bison, ANTLR

• Semantic Analysis:
 Type checking
 Symbol table management
Regular Expressions
Basic Elements

ε (empty string)
a (for any a ∈ Σ)

Operations:

Concatenation: rs
Alternation: r|s
Kleene Star: r*
Grouping: (r)
Optional: r?
One or more: r+
Rules for Regular Expressions

The set of regular expressions is defined by the following rules:

1.Every letter of ∑ can be made into a regular expression, null string, ∈ itself is a regular expression.
2..If r1 and r2 are regular expressions, then (r1), r1.r2, r1+r2, r1*, r1 + are also regular expressions.

Applications: Pattern matching, lexical analysis, data validation


Parsing Techniques
Top-Down Parsing:

•Recursive Descent
•LL(1), LL(k) Parsing
•Starts with start symbol, expands downward

Bottom-Up Parsing:

•Shift-Reduce
•LR(0), SLR(1), LALR(1), LR(1)
•Starts with input, reduces to start symbol

Parsing Challenges:

•Handling left recursion


•Resolving conflicts (shift-reduce, reduce-reduce)
Practical Applications
Programming Language Design:
 Syntax definition
 Parser implementation
 Type systems

Compilers and Interpreters:


 Front-end (lexing, parsing, semantic analysis)
 Back-end (code generation, optimization)

• Other Applications:
 Natural language processing
THANK YOU

GROUP 1

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