Download presentation
Presentation is loading. Please wait.
1
Derivations and Languages
Cpt S 317: Spring 2009 Derivations and Languages Sampath Kumar S, AP/CSE, SECE School of EECS, WSU
2
Derivations and Languages
2/22/2019 Derivations and Languages While inferring whether the given input strings belongs to the given CFG, we can have 2 approaches: Using the rules from body to head (Recursive Inference) Using the rules from head to body (Derivation) Derivation using Grammar Recursive Inference Derivation Left Most Derivation Right Most Derivation Sampath Kumar S, AP/CSE, SECE
3
2/22/2019 Recursive Inference: Here we take string from each variables, concatenate them in proper order and infer that the resulting string is in the language of the variable in the head Sampath Kumar S, AP/CSE, SECE
4
2/22/2019 Derivation: Here we use the production from head to body i.e., from start symbol expanding till reaches the given string. This use of grammar is called derivation. Sampath Kumar S, AP/CSE, SECE
5
2/22/2019 Problems to discuss: For the language L = {wcwR | w (0+1)*} check whether the string 01c10 belongs to the language L or not. Solution: Step 1: For the language L, the productions are S → 0S0 | 1S1 | C Step 2: Derivation S → 0S0 [∵ S → 0S0] → 01E10 [∵ S → 1S1] → 01c10 [∵ S → c] Sampath Kumar S, AP/CSE, SECE
6
Understanding the Language defined by CFG:
2/22/2019 Understanding the Language defined by CFG: The only way to recognize the language, is to try out various strings from the given production rules. Then by observing the derived strings, one can find out the language generated from the given CFG. Sampath Kumar S, AP/CSE, SECE
7
2/22/2019 Problems to discuss: Derive ‘a4’ from the grammar G=({S}, {a},P,S) where P: S → aS|ε. Find L(G) and derive the string ‘abbab’ for the grammar G=({S}, {a},P,S) where P: S → aS|bS|a|b. Find L(G) and derive the string ‘abbaaba’ for the grammar G=({S,X}, {a},P,S) where P: S → XaaX, X → aX|bX|ε. Find L(G) for the grammar G=({S,C}, {a,b},P,S) where P is given by S → aCa, C → aCa|b. Sampath Kumar S, AP/CSE, SECE
8
Leftmost and Rightmost Derivation:
2/22/2019 Leftmost and Rightmost Derivation: If a word w is generated by a CFG by a certain derivation and at each step in the derivation, a rule of production is applied to the leftmost non terminal in the working string, then this derivation is called leftmost derivation (LMD). If a word w is generated by a CFG by a certain derivation and at each step in the derivation, a rule of production is applied to the rightmost non terminal in the working string, then this derivation is called rightmost derivation (RMD). Sampath Kumar S, AP/CSE, SECE
9
2/22/2019 Problems to discuss: Consider the CFG ({S,X}, {a,b}, P, S} where productions are S → baXaS|ab, X →Xab|aa. Find the LMD and RMD for the string w=baaaababaab. Consider the CFG S → aB|bA A → a | aS | bAA B → b | bS | aBB Find the LMD and RMD for the string W=aabbabba. Sampath Kumar S, AP/CSE, SECE
10
2/22/2019 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. Sampath Kumar S, AP/CSE, SECE
11
Representation Technique
2/22/2019 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 − Sampath Kumar S, AP/CSE, SECE
12
Approaches for constructing Derivation Tree:
2/22/2019 Approaches for constructing Derivation Tree: There are two different approaches to draw a derivation tree − Top-down Approach − Starts with the starting symbol S Goes down to tree leaves using productions Bottom-up Approach − Starts from tree leaves Proceeds upward to the root which is the starting symbol S Sampath Kumar S, AP/CSE, SECE
13
Derivation or Yield of a Tree:
2/22/2019 Derivation or Yield of a Tree: The derivation or the yield of a parse tree is the final string obtained by concatenating the labels of the leaves of the tree from left to right, ignoring the Nulls. However, if all the leaves are Null, derivation is Null. Sampath Kumar S, AP/CSE, SECE
14
Sentential Form and Partial Derivation Tree:
2/22/2019 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. Sampath Kumar S, AP/CSE, SECE
15
Leftmost and Rightmost Derivation of a String
2/22/2019 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. Sampath Kumar S, AP/CSE, SECE
16
Left and Right Recursive Grammars
2/22/2019 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. 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. Sampath Kumar S, AP/CSE, SECE
17
Problems to discuss: 68. Let a CFG G= { {S}, {a, b}, P, S} Where
2/22/2019 Problems to discuss: 68. Let a CFG G= { {S}, {a, b}, P, S} Where P = S → SS | aSb | ε. Draw derivation tree for the string “abaabb” One derivation form of the above CFG is S → SS → aSbS → abS → abaSb → abaaSbb → abaabb Sampath Kumar S, AP/CSE, SECE
18
2/22/2019 Problems to discuss: 69. Let any set of production rules in a CFG be X → X+X | X*X |X| a over an alphabet {a}. Find LMD and RMD for string "a+a*a“. 70. Construct Parse tree for the string a*(a+b00) for the following CFG using left and right most derivation. E → A | E + E | E * E | (E) A → a | b |Aa | Ab | A0 | A1 71. Construct Parse tree for the string a*(a+a) for the following CFG using left and right most derivation. E → E + E | E * E | (E) | a Sampath Kumar S, AP/CSE, SECE
19
2/22/2019 Problems to discuss: 72. Generate parse tree for the string using left and right most derivation for following CFG S → A1B, A → 0A|ε | B → 0B | 1B | ε 73. Generate parse tree for the string babbab using left and right most derivation for following CFG S → aSa | bSb | a |b | ε Sampath Kumar S, AP/CSE, SECE
20
2/22/2019 Sampath Kumar S, AP/CSE, SECE
21
2/22/2019 நன்றி Sampath Kumar S, AP/CSE, SECE
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.