 Hello everyone, I am Mrs. Sunita Do working as an assistant professor in computer science and engineering department of Valshan Institute of Technology, Sulapur. Topic covered here is writing a grammar topic of syntax analysis. Grammars are capable of describing most but not all of the syntax of programming language. For example, the requirement that the identifiers be declared before they are used cannot be described by context programmer. Therefore, the sequence of token accepted by a parser form a superset of programming language and subsequent phases of a compiler must analyze the output of the parser to ensure the compliance with the rules that are not checked by the parser. Here we consider the transformation that are useful for rewriting the grammar so that they become suitable for top-down passing. In previous video I covered the two topics that is the role of the parser and the context free grammar. Now, in this video I am going to cover the topic writing a grammar. Learning outcome. At the end of this session, students will be able to write a grammar for a given non-deterministic finite automata, eliminate the ambiguity from the given grammar and identify the non-context free languages. Regular expression versus context free grammar. Every regular expression that can be described by regular expression can also be described by a grammar. We can convert a non-deterministic finite automata into a grammar that generates the same language as recognized by the non-deterministic finite automata. Now consider the procedure to convert a non-deterministic finite automata into a grammar. Step 1. For E state i of the NFA, create a non-terminal symbol capital E i. Step 2. If state i has a transition to state j on input symbol E, then introduce the production capital E i derives E capital E j. Step 3. If state i goes to state j on the input symbol epsilon, then introduce the production capital E i derives E capital E j. Step 4. If state i is an accepting state, introduce capital E i derives epsilon. Consider the non-deterministic finite automata given below. There are four states in the given non-deterministic finite automata. So create the four non-terminal symbol A0, A1, A2 and A3 corresponding to Q0, Q1, Q2, Q3. State Q0 has transition to state Q0 on symbol A and B and has a transition to state Q1 on symbol A. Hence introduce the production A0 derives small a capital A0 or small b capital A0 or small a capital A1. State Q1 has a transition to state Q2 on symbol B, hence introduce the production capital E i derives B capital A2. State Q2 has a transition to state Q3 on the symbol B, hence introduce the production capital A2 derives B capital A3. State Q3 is an accepting state, so introduce capital A3 derives epsilon. Till now we consider how to convert a non-deterministic finite automata into a grammar. Now pause this video and reflect on this question for a minute or two minute and write your response. Once you return the answer to this question, then you can restart playing this video. The question is write a grammar for the following non-deterministic finite automata. I hope all of you have completed this activity. So the question was write a grammar for the following non-deterministic finite automata. Since there are three states in the given non-deterministic finite automata, so create the three non-terminal symbol A0, E1 and E2 corresponding to Q0, Q1 and Q2. State Q0 has transition to state Q1 on symbol A, hence introduce the production capital A0 derives A capital E1. State Q1 has a transition to state Q2 on symbol B, hence introduce the production capital E1 derives B capital E2. State Q2 has transition to state Q2 on symbol A and B, hence introduce the production capital E2 derives A capital E2 or B capital E2. State Q2 is an accepting state, so introduce the production capital E2 derives epsilon. Eliminating ambiguity, a grammar that produce more than one pastry for some sentence is said to be ambiguous. For the most passive, the grammar must be unambiguous. Unambiguous grammar means unique selection of the pastry for a sentence. Sometimes an ambiguous grammar can be re-returned to eliminate the ambiguity. As an example, we shall eliminate the ambiguity from the following dangling as grammar. Statement derives if expression then statement or if expression then statement as statement or other statement. Here other statement stands for any other statement. According to this grammar, the compound conditional statement if E1 then S1 else if E2 then S2 else S3 has the pastry as shown in figure. The grammar is ambiguous since the string if E1 then if E2 then S1 else S2 has the two pastry as shown in figure. In all programming languages with the conditional statement of this form, the first pastry is preferred. The general rule is, match each else with the closest previous unmasked then. We can rewrite this grammar as the following unambiguous grammar. The idea is that the statement appearing between a then and an else must be matched i.e. it must not end with the unmasked then followed by any statement. For the else would then be forced to match this unmasked then. A match statement is either if then else statement containing no unmasked statement or it is any other kind of unconditional statement. Grammar after eliminating the ambiguity is statement derives match statement or the unmasked statement. Match statement derives if expression then match statement as match statement or other statement. Unmasked statement derives if expression then statement or if expression then match statement as unmasked statement. This grammar generate the same set of strings but it allows only one passing for the string namely the one that associate each else with the closest previous unmasked then. This is the second example of eliminating the ambiguity from the grammar. So this grammar represent the simple arithmetic expression. Non-context free language constructs. There are some language construction in the programming language which are not context free. This means that we cannot write a context free grammar for this constructions. Example l1 equals to wcw such that w is in a or b star. This language l1 consists of all the words composed of a repeated string of a's and p's separated by a c. This language is not context free. This language abstract the problem of checking that the identifiers are declared before their use in a program. That is the first w in wcw represent the declaration of an identifier w and the second w represent its c use. l2 equals to a raise to n, b raise to m, c raise to n, d raise to m such that n and m is greater than equals to 1. This language is not context free language. l2 consists of strings in the language generated by the regular expression a star, b star, c star, d star such that the number of a's and c's are equal and the number of b's and d's are equal. l2 abstract the problem of checking that the number of formal parameters in the declaration of the procedure agrees with the number of actual parameter in a use of procedure. l3 equals to a raise to n, b raise to n, c raise to n such that n is greater than equals to 0. That is strings in language of a star, b star, c star with equal number of a's, b's and c's is not context free language. But the language similar to l1, l2 and l3 are context free. Example l1 dash equals to wc, w raise to r such that w is in a or b star where w raise to r stands for the reverse of w is a context free and the grammar for the same is given below. l2 dash equals to a raise to n, b raise to m, c raise to m, d raise to n such that n and m is greater than equals to 1, it is a context free language and the grammar for the same is given here. l2 dash equals to a raise to n, b raise to n, c raise to m, d raise to m such that n and m is greater than equals to 1 is context free and the grammar for the same is given below. L3 dash equals to a raise to n, b raise to n such that n is greater than equals to 1 is a context free and the grammar for the this language is s derives a capital as b or a b. This is the reference. Thank you.