Sie sind auf Seite 1von 19

Simple and Operator Grammar

Grammar can be classified as simple grammar and operator grammar based on the restriction on the occurrence of terminal symbols Operator Grammar is one that does not permit two adjacent non-terminals on nonits RHS of the production rules. i.e non terminals must be separated by at least rules. one terminal symbols. Terminal symbols in this grammar are called operators. SO Invalid Operator Grammar is .N1N2. Valid Operator Grammar is E E+T | T T T*F | F Simple Grammar on the other hand does not have such kind of restriction. A restriction. valid simple grammar for all its symbols X and Y ( including terminal and nonnonterminals) must have a unique relation between X and Y. Z bMb M (L L Ma)

Derivation and Derivation Tree


Derivation is a sequence of applications of the rules of grammar that produces a finished strings of terminals. A derivation is also called a parse. In a simple words, Grammar helps to derive string beginning with the start symbol and repeatedly replaces a non terminal in the sentential form by the body of a production for that non-terminal. nonThe terminal strings that can be derived from the start symbol forms the language defined by the grammar, therefore language is all possible strings that are valid under the given grammar. Equivalent Grammar:: The two grammars are equivalent if they generate the same languages. Derivation Tree: An effective way of representing the structure of a sentence in a language is through a derivation tree.

Derivation Tree: An effective way of representing the structure of a sentence in a language is through a derivation tree. Derivation Tree is also called parse tree. A derivation tree for a CFG grammar G={ N,T,P,S) is a tree satisfying the following conditions 1) 2) 3) 4) Every Vertex has a label which is a variable or terminal or null. The root has label S The label of internal vertex is a variable. If the vertices n1,, n2 , n3.. nk written with the label x1,, x2 , x3.. Xk are the son of vertex n written with the label A then A x1,, x2 , x3.. Xk is a production in P. A vertex n is a leaf if its label is null . i.e n is the only son of its father.

Derivation Tree

5)

Example : Construct a derivation tree for the string w=aabbaa from the following grammar S aAS | a, A SbA | SS | ba| Solution: S =>aAS=>aSbAS=>aabAS=>aabbaS=>aabbaa S a A S a b A b a S a

Derivation Tree

Leftmost and Rightmost Derivation Leftmost derivation or simply left derivation always replaces the leftmost non terminal first in the sentential form during the derivation. Right Most derivation always replaces the rightmost non-terminal first. The rightmost nonderivation is also called canonical derivations Example of Leftmost and Rightmost Derivation X=Y+Z*X <assign> <id>=<expr> <id> X | Y | Z <expr> <expr> + <term> | <term> <term> <term>*<factor> | <factor> <factor> <expr> |<id>

Derivation

Derivation
Example of Leftmost and Rightmost Derivation X=Y+Z*X Left Most Derivation <assign> <id>=<expr> =>X=<expr> =>X=<expr>+<term> =>X=<term>+<term> =>X=<factor>+<term> =>X=<id>+<term> =>X=Y+<term>*<factor> =>X=Y+<factor>*<factor> =>X=Y+<id>*<factor> =>X=Y+Z*<factor> =>X=Y+Z*<id> =>X=Y+Z*X

Derivation
Example of Leftmost and Rightmost Derivation X=Y+Z*X Right Most Derivation <assign> <id>=<expr> =>X=<expr>+<term> =>X=<expr>+<term>*<factor> =>X=<expr>+<term>*<id> =>X=<expr>+<term>*X =>X=<expr>+<factor>*X =>X=<expr>+<id>*X =>X =<expr>+Z*X =>X=<term>+Z*X =>X =<factor>+Z*X =>X =<id>+Z*X =>X =Y+Z*X

Derivation
Find the (a) left most derivation, (b) rightmost derivation, and (c) derivation for the string 00110101, and grammar S 0B | 1A, A 0 | 0S | 1AA, B 1 | 1S | 0BB a) S=>0B=>00BB=>001B=>0011 =>00110B=>001101S=>0011010B=>00110101 S=>0B=>00BB=>001B=>0011S=>00110B=>001101S=>0011010B=>00110101 b) S=>0B=>00BB=>00B1S=>00B10B=>00B101S=>00B1010B=>00B10101=>00110101

Ambiguity of Grammar
A CFG grammar is ambiguous if and only if there is a string w L(G) such that w is the frontier of more than one derivation tree for a G. In other words, a grammar is ambiguous if and only if there is at least one string that can be generated by more than one distinct derivation tree. Since each tree corresponds to a unique leftmost or rightmost derivation, an ambiguous grammar is one for which there exists more than one leftmost or rightmost derivation of the given string. Example : Consider the following grammar that defines standard arithmetic addition and multiplications E E + E | E * E | ( E ) | id

Ambiguity of Grammar
Example : Consider the following grammar that defines standard arithmetic addition and multiplications E E + E | E * E | ( E ) | id E E+E => id + E => id + E * E => id + Id * E => Id + Id * Id E E+E =>E + E * E => id + E * E => id + Id * E => Id + Id * Id

For an unambiguous grammar, there is exactly one parse tree for a specific sentence; else, it is said to be ambiguous. Natural languages in a daily usage can tolerate some kind of ambiguity, but is not acceptable when applied to computer languages because ambiguous grammar generally hard to parse since the parser may not be able to decide which grammar rule to apply.

Removing Ambiguity
For Programming Languages one can either construct an unambiguous grammar or introduce additional disambiguating rules on it to remove the undesirable extra parse trees. One Way to overcome ambiguity is to embed into the parser the knowledge of precedence and associativity and force the parser to construct the tree on the right instead of left. We then add intermediate non-terminals to enforce precedence between the nonoperators and make everything left associative.

Removing Ambiguity
There are 3 steps to perform it..

1. Introduce and add new nonterminal for each precedence level. 2. Isolate the corresponding part of the grammar 3. Modify and force the parser to recognize the high precedence sub expressions first. Precedence is the priority for grouping different types of operators with their operands. Associativity is the left-to-right or right-to-left order left-toright-tofor grouping operands to operators that have the same precedence. An operator's precedence is meaningful only if other operators with higher or lower precedence are present. Expressions with higherhigherprecedence operators are evaluated first. The grouping of operands can be forced by using parentheses.

Removing Ambiguity
Consider the following grammar that is highly ambiguous E E + E | E-E | E * E | E/E |( E ) | id EBy enforcing precedence between operators binds * and / more tightly than + and - Therefore new unambiguous grammar will have the following productions E E + T | E-T | T ET T * F | T/F | F F ( E ) | var

Removing Ambiguity
Consider the following grammar that is highly ambiguous E E+E E E*E E D This is ambiguous grammar generates two derivation tree. By enforcing precedence by introducing new non terminals, we have a new grammar: E E T | E+T T F | T*F E T F D T T F F + F * D D D

Introduction to Finite Automata


An automata is defined as a system where energy, material & information are transformed, transmitted & used for performing some functions without the direct participation of human being. e.G Automatic Washing Machine Automactic photoprinting machine In Computer Science , The Term automata means discrete automata Automata Theory:: Automata theory is the study of abstract machines (or more appropriately, abstract 'mathematical' machines or systems) and the computational problems that can be solved using these machines. These abstract machines are called automata.

Introduction to Finite Automata


An automata is defined as a system where energy, material & information are transformed, transmitted & used for performing some functions without the direct participation of human being. e.G Automatic Washing Machine Automactic photoprinting machine In Computer Science , The Term automata means discrete automata Automata Theory:: Automata theory is the study of abstract machines (or more appropriately, abstract 'mathematical' machines or systems) and the computational problems that can be solved using these machines. These abstract machines are called automata.

Introduction to Finite Automata


A Modal of Discrete Automata

I1 I2 : .: : Ip

Automata

O1 O2

q,q1,q2.qn Oq

Introduction to Finite Automata


An Input values I1,I2, Ip each of which can take a finite no of fixed values from the input alphabet ={I1,I2, Ip) The output values O1,O2, Oq each of which can take a finite no of fixed values from the output symbol O O= {O1,O2, Oq } At any instant of the time, the automata can be in one of the following states : q, q1,q2, qn

Introduction to Finite Automata


States Relation: The next state of an automata at any instant of time is determined by the present state & the present output. Output Relation: Output is related to the state only or to both input & state.

Points to Be Remember 1) An automata in which output depends only on the input is called an automata without the memory 2) An automata in which output depends on the states as well is called automata with a finite memory 3) An automata in which output depends only on the states of machine is called moore machine. 4) An automata in which output depends on the states as well as on the input at any instant of time is called a Mealy machine.