Download presentation
Presentation is loading. Please wait.
1
Formal Language & Automata Theory
Shyamanta M Hazarika Computer Sc. & Engineering Tezpur University
2
Introduction Finite Automata accept all regular languages and only regular languages Even very simple languages are non regular ( = {a,b}): - {anbn : n = 0, 1, 2, …} - {w : w is palindrome word} We are going to define a new class of languages, called context-free languages that contain all regular languages and many more (including the 2 above)
3
Context-Free Grammar A context-free grammar is a kind of program
Languages that are generated by context-free grammars are called context-free languages Context-free grammars are more expressive than finite automata: if a language L is accepted by a finite automata then L can be generated by a context-free grammar
4
Context-Free Grammar (CFG)
Definition. A context-free grammar (CFG) is a 4-tuple (V, , R, S), where: is an alphabet (characters are called terminals) V is a set (elements in NT are called variables) R is a subset of NT ( NT)* S, the start variable, is one of the variables in NT V = If (,) R, we write is called a rule
5
Derivations Definition. u yields v in one-step, written u v, if: for some u,v in (V )* the following 3 conditions hold: u = xz v = xz in R Definition. u derives v, written u * v, if: There is a chain of one-step yields of the form: u u1 u2 … v
6
Example = {a,b} V = {S} R = { S aSb, S e }
7
Context-Free Languages
Definition. Given a context-free grammar G = (V, , R, S), the language generated or derived from G is the set: L(G) = {w *: } S * w Definition. A language L is context-free if there is a context-free grammar G = (, NT, R, S), such that L is generated from G
8
Example = {a,b} NT = {S} R = { S aS, S Sb, S e}
9
Example = {a,b} NT = {S} R = { S aSa, S bSb, S e}
10
Parse Tree A parse tree of a derivation u u1 u2 … v
is a tree in which: Each internal node is labeled with a variable If a rule A A1A2…An occurs in the derivation then A is a parent node of nodes labeled A1, A2, …, An S a e b
11
Leftmost, Rightmost Derivations
Definition. A leftmost derivation of a sentential form is one in which rules transforming the left-most nonterminal are always applied Definition. A rightmost derivation of a sentential form is one in which rules transforming the right-most nonterminal are always applied
12
Ambiguous Grammar Definition. A grammar G is ambiguous if there is a word w L(G) having are least two different leftmost derivations S A S B S AB A aA B bB A e B e Notice that the word a has at least two left-most derivations Some ambiguous grammars G can be disambiguated: find an unambiguous grammar G’ such that L(G) = L(G’) Some languages cannot be disambiguated
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.