Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

context free grammar, Slides of Theory of Automata

cfg, ambiguity, derivation and parse trees etc

Typology: Slides

2019/2020

Uploaded on 04/23/2020

aditi-sharma-kec-cse
aditi-sharma-kec-cse ๐Ÿ‡ฎ๐Ÿ‡ณ

1 document

1 / 37

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Todayโ€™s Topic
Context Free grammar (CFG)
UNIT โ€“ 3
Regular and Non regular Grammar
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

Partial preview of the text

Download context free grammar and more Slides Theory of Automata in PDF only on Docsity!

Todayโ€™s Topic

Context Free grammar (CFG)

UNIT โ€“ 3

Regular and Non regular Grammar

Definition โˆ’ A context-free grammar (CFG) consisting of a

finite set of grammar rules is a quadruple (N, T, P, S) where

N is a set of non-terminal symbols.

T is a set of terminals where N โˆฉ T = NULL.

P is a set of rules, P: N โ†’ (N โˆช T), i.e., the left-hand side of T), i.e., the left-hand side of

the production rule P does have any right context or left

context.

S is the start symbol.

Generation of Derivation Tree

A derivation tree or parse tree is an ordered rooted tree that

graphically represents the semantic information a string

derived from a context-free grammar.

Representation Technique

Root vertex โˆ’ Must be labeled by the start symbol.

Vertex โˆ’ Labeled by a non-terminal symbol.

Leaves โˆ’ Labeled by a terminal symbol or ฮต.

If S โ†’ x1x2 โ€ฆโ€ฆ xn is a production rule in a CFG, then the parse tree / derivation tree will be as follows โˆ’

Derivation Tree

There are two different approaches to draw a derivation tree โˆ’ Top-down Approach โˆ’

  1. Starts with the starting symbol S
  2. Goes down to tree leaves using productions

Sentential Form and Partial Derivation Tree

A partial derivation tree is a sub-tree of a derivation

tree/parse tree such that either all of its children are in the

sub-tree or none of them are in the sub-tree.

Example:

If in any CFG the productions are โˆ’

S โ†’ AB, A โ†’ aaA | ฮต, B โ†’ Bb| ฮต

the partial derivation tree can be the following โˆ’

Leftmost and Rightmost Derivation of a String Leftmost derivation โˆ’ A leftmost derivation is obtained by applying production to the leftmost variable in each step. Rightmost derivation โˆ’ A rightmost derivation is obtained by applying production to the rightmost variable in each step. Example Let any set of production rules in a CFG be X โ†’ X+X | XX |X| a over an alphabet {a}. The leftmost derivation for the string "a+aa" may be โˆ’ X โ†’ X+X โ†’ a+X โ†’ a + XX โ†’ a+aX โ†’ a+a*a The stepwise derivation of the above string is shown as-

Left and Right Recursive Grammars In a context-free grammar G, if there is a production in the form X โ†’ Xa where X is a non-terminal and โ€˜aโ€™ is a string of terminals, it is called a left recursive production. The grammar having a left recursive production is called a left recursive grammar.

And if in a context-free grammar G, if there is a production is in the form X โ†’ aX where X is a non-terminal and โ€˜aโ€™ is a string of terminals, it is called a right recursive production. The grammar having a right recursive production is called a right recursive grammar.

UNIT โ€“ 3

Regular and Non regular Grammar

Todayโ€™s Topic

AMBUIGUITY, CFG for regular

language and expressions

Ambiguity in Grammar A grammar is said to be ambiguous if there exists more than one leftmost derivation or more than one rightmost derivation or more than one parse tree for the given input string. If the grammar is not ambiguous, then it is called unambiguous. If the grammar has ambiguity, then it is not good for compiler construction. No method can automatically detect and remove the ambiguity, but we can remove ambiguity by re-writing the whole grammar without ambiguity. Example 1: Let us consider a grammar G with the production rule

Since there are two parse trees for a single string "3 * 2 + 5", the grammar G is ambiguous. QUES 2: Check whether the given grammar G is ambiguous or not. E โ†’ E + E E โ†’ E - E E โ†’ id Solution: From the above grammar String "id + id - id" can be derived in 2 ways: First Leftmost derivation E โ†’ E + E โ†’ id + E โ†’ id + E - E โ†’ id + id - E โ†’ id + id- id

Second Leftmost derivation E โ†’ E - E โ†’ E + E - E โ†’ id + E - E โ†’ id + id - E โ†’ id + id - id Since there are two leftmost derivation for a single string "id + id - id", the grammar G is ambiguous.

3. Check whether the given grammar G is ambiguous or not. S โ†’ aSb | SS S โ†’ ฮต FOR INPUT STRING โ€œaabbโ€