simple and operator grammar

Upload: dhirajkapila

Post on 06-Apr-2018

245 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/3/2019 Simple and Operator Grammar

    1/19

    Simple and Operator GrammarSimple and Operator Grammar

    Grammar can be classified asGrammar can be classified as simple grammarsimple grammarandand operator grammaroperator grammarbased onbased onthe restriction on the occurrence of terminal symbolsthe restriction on the occurrence of terminal symbols

    Operator Grammar is one thatOperator Grammar is one that does not permit two adjacent nondoes not permit two adjacent non--terminals onterminals on

    its RHS of the production rulesits RHS of the production rules. i.e non terminals must be separated by at least. i.e non terminals must be separated by at least

    one terminal symbols. Terminal symbols in this grammar are called operators.one terminal symbols. Terminal symbols in this grammar are called operators.

    SO Invalid Operator Grammar is .N1N2.SO Invalid Operator Grammar is .N1N2.

    Valid Operator Grammar is EValid Operator Grammar is EE+T | TE+T | T

    TTT*F | FT*F | F

    Simple Grammar on theSimple Grammar on the other hand does not have such kind of restrictionother hand does not have such kind of restriction. A. A

    valid simple grammar for all its symbols X and Y ( including terminal and nonvalid simple grammar for all its symbols X and Y ( including terminal and non--terminals) must have a unique relation between X and Y.terminals) must have a unique relation between X and Y.

    ZZbMbbMb

    MM(L(L

    LLMa)Ma)

  • 8/3/2019 Simple and Operator Grammar

    2/19

    Derivation and Derivation TreeDerivation and Derivation TreeDerivation is a sequence of applications of the rules of grammar that produces a finishedDerivation 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 tostrings 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 thederive string beginning with the start symbol and repeatedly replaces a non terminal in the

    sentential form by the body of a production for that nonsentential form by the body of a production for that non--terminal.terminal.

    The terminal strings that can be derived from the start symbol forms the language defined byThe terminal strings that can be derived from the start symbol forms the language defined bythe grammar, therefore language is all possible strings that are valid under the giventhe grammar, therefore language is all possible strings that are valid under the given

    grammar.grammar.

    Equivalent Grammar:: The two grammars are equivalent if they generate the sameEquivalent Grammar:: The two grammars are equivalent if they generate the same

    languages.languages.

    Derivation Tree: An effective way of representing the structure of a sentence in a languageDerivation Tree: An effective way of representing the structure of a sentence in a language

    is through a derivation tree.is through a derivation tree.

  • 8/3/2019 Simple and Operator Grammar

    3/19

    Derivation TreeDerivation TreeDerivation Tree: An effective way of representing the structure of a sentence in a languageDerivation Tree: An effective way of representing the structure of a sentence in a language

    is through a derivation tree.is through a derivation tree.

    Derivation Tree is also called parse tree. A derivation tree for a CFG grammar G={ N,T,P,S)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 conditionsis a tree satisfying the following conditions

    1)1) Every Vertex has a label which is a variable or terminal or null.Every Vertex has a label which is a variable or terminal or null.2)2) The root has label SThe root has label S

    3)3) The label of internal vertex is a variable.The label of internal vertex is a variable.

    4)4) If the vertices nIf the vertices n1,1,, n, n22 ,, nn3..3.. nnkk written with the label xwritten with the label x1,1,, x, x22 ,, xx3..3.. XXkkare the son of vertex n written with the label A then Aare the son of vertex n written with the label A then A xx1,1,, x, x22 ,, xx3..3.. XXkk is ais a

    production in P.production in P.

    5)5) A vertex n is a leaf if its label is null . i.e n is the only son of its father.A vertex n is a leaf if its label is null . i.e n is the only son of its father.

  • 8/3/2019 Simple and Operator Grammar

    4/19

    Derivation TreeDerivation TreeExample : Construct a derivation tree for the stringExample : Construct a derivation tree for the string ww==aabbaa from the following grammaraabbaa from the following grammar

    SSaAS | a,aAS | a,

    AASbA | SS | ba|SbA | SS | ba|

    Solution: S =>aAS=>aSbAS=>aabAS=>aabbaS=>aabbaaSolution: S =>aAS=>aSbAS=>aabAS=>aabbaS=>aabbaa

    SS

    aa

    A SA S

    S b A aS b A a

    a b aa b a

  • 8/3/2019 Simple and Operator Grammar

    5/19

    DerivationDerivationLeftmost and Rightmost DerivationLeftmost and Rightmost DerivationLeftmost derivation or simply left derivation always replaces the leftmost non terminal first inLeftmost derivation or simply left derivation always replaces the leftmost non terminal first in

    the sentential form during the derivation.the sentential form during the derivation.

    Right Most derivation always replaces the rightmost nonRight Most derivation always replaces the rightmost non--terminal first. The rightmostterminal first. The rightmost

    derivation is also called canonical derivationsderivation is also called canonical derivations

    Example of Leftmost and Rightmost DerivationExample of Leftmost and Rightmost Derivation

    X=Y+Z*XX=Y+Z*X

    ==

    X | Y | ZX | Y | Z

    + | + |

    * | * |

    | |

  • 8/3/2019 Simple and Operator Grammar

    6/19

    DerivationDerivationExample of Leftmost and Rightmost DerivationExample of Leftmost and Rightmost Derivation

    X=Y+Z*XX=Y+Z*X

    Left Most DerivationLeft Most Derivation

    ==

    =>X==>X=

    =>X=+=>X=+

    =>X=+=>X=+

    =>X=+=>X=+

    =>X=+=>X=+

    =>X=Y+*=>X=Y+*

    =>X=Y+*=>X=Y+*

    =>X=Y+*=>X=Y+*

    =>X=Y+Z*=>X=Y+Z*=>X=Y+Z*=>X=Y+Z*

    =>X=Y+Z*X=>X=Y+Z*X

  • 8/3/2019 Simple and Operator Grammar

    7/19

    DerivationDerivationExample of Leftmost and Rightmost DerivationExample of Leftmost and Rightmost Derivation

    X=Y+Z*XX=Y+Z*X

    Right Most DerivationRight Most Derivation

    ==

    =>X=+=>X=+

    =>X=+*=>X=+*

    =>X=+*=>X=+*

    =>X=+*X=>X=+*X

    =>X=+*X=>X=+*X

    =>X=+*X=>X=+*X

    =>X =+Z*X=>X =+Z*X

    =>X=+Z*X=>X=+Z*X

    =>X =+Z*X=>X =+Z*X=>X =+Z*X=>X =+Z*X

    =>X =Y+Z*X=>X =Y+Z*X

  • 8/3/2019 Simple and Operator Grammar

    8/19

    DerivationDerivationFind the (a) left most derivation, (b) rightmost derivation, and (c) derivation for the stringFind the (a) left most derivation, (b) rightmost derivation, and (c) derivation for the string

    00110101, and grammar00110101, and grammar

    SS 0B | 1A, A0B | 1A, A 0 | 0S | 1AA, B0 | 0S | 1AA, B 1 | 1S | 0BB1 | 1S | 0BB

    a) S=>0a) S=>0BB=>00=>00BBB=>001B=>001BB=>0011=>0011S=>00110B=>001101S=>0011010B=>00110101=>00110B=>001101S=>0011010B=>00110101

    b) S=>0B=>00BB=>00B1S=>00B10B=>00B101S=>00B1010B=>00B10101=>00110101b) S=>0B=>00BB=>00B1S=>00B10B=>00B101S=>00B1010B=>00B10101=>00110101

  • 8/3/2019 Simple and Operator Grammar

    9/19

    Ambiguity of GrammarAmbiguity of GrammarA CFG grammar is ambiguous if and only if there is a string wA CFG grammar is ambiguous if and only if there is a string w L(G) such that w is theL(G) such that w is the

    frontier of more than one derivation tree for a G.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 beIn 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.generated by more than one distinct derivation tree.

    Since each tree corresponds to a unique leftmost or rightmost derivation, an ambiguousSince 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 derivationgrammar is one for which there exists more than one leftmost or rightmost derivationof the given string.of the given string.

    Example : Consider the following grammar that defines standard arithmetic addition andExample : Consider the following grammar that defines standard arithmetic addition and

    multiplicationsmultiplications

    EE E + E | E * E | ( E ) | idE + E | E * E | ( E ) | id

  • 8/3/2019 Simple and Operator Grammar

    10/19

    Ambiguity of GrammarAmbiguity of GrammarExample : Consider the following grammar that defines standard arithmetic addition andExample : Consider the following grammar that defines standard arithmetic addition and

    multiplicationsmultiplications

    EE E + E | E * E | ( E ) | idE + E | E * E | ( E ) | id

    EE E + EE + E EE E + EE + E

    => id + E =>E + E * E=> id + E =>E + E * E=> id + E * E => id + E * E=> id + E * E => id + E * E

    => id + Id * E => id + Id * E=> id + Id * E => id + Id * E

    => Id + Id * Id => Id + Id * Id=> Id + Id * Id => Id + Id * Id

    For an unambiguous grammar, there is exactly one parse tree for a specificFor 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 cansentence; else, it is said to be ambiguous. Natural languages in a daily usage cantolerate some kind of ambiguity, but is not acceptable when applied to computertolerate some kind of ambiguity, but is not acceptable when applied to computer

    languages because ambiguous grammar generally hard to parse since the parserlanguages because ambiguous grammar generally hard to parse since the parser

    may not be able to decide which grammar rule to apply.may not be able to decide which grammar rule to apply.

  • 8/3/2019 Simple and Operator Grammar

    11/19

    Removing AmbiguityRemoving Ambiguity

    For Programming Languages one can either construct an unambiguous grammarFor Programming Languages one can either construct an unambiguous grammar

    or introduce additional disambiguating rules on it to remove the undesirableor introduce additional disambiguating rules on it to remove the undesirable

    extra parse trees.extra parse trees.

    One Way to overcome ambiguity is to embed into the parser the knowledge ofOne Way to overcome ambiguity is to embed into the parser the knowledge ofprecedence and associativity and force the parser to construct the tree onprecedence and associativity and force the parser to construct the tree on

    the right instead of left.the right instead of left.

    We then add intermediate nonWe then add intermediate non--terminals to enforce precedence between theterminals to enforce precedence between the

    operators and make everything left associative.operators and make everything left associative.

  • 8/3/2019 Simple and Operator Grammar

    12/19

    Removing AmbiguityRemoving Ambiguity

    There are 3 steps to perform it..There are 3 steps to perform it..

    1. Introduce and add new nonterminal for each precedence level.1. Introduce and add new nonterminal for each precedence level.

    2. Isolate the corresponding part of the grammar2. Isolate the corresponding part of the grammar

    3. Modify and force the parser to recognize the high precedence sub3. Modify and force the parser to recognize the high precedence subexpressions first.expressions first.

    Precedence is the priority for grouping different types of operators withPrecedence is the priority for grouping different types of operators with

    their operands. Associativity is the lefttheir operands. Associativity is the left--toto--right or rightright or right--toto--left orderleft order

    for grouping operands to operators that have the same precedence.for grouping operands to operators that have the same precedence.

    An operator's precedence is meaningful only if other operators withAn operator's precedence is meaningful only if other operators with

    higher or lower precedence are present. Expressions with higherhigher or lower precedence are present. Expressions with higher--

    precedence operators are evaluated first. The grouping of operandsprecedence operators are evaluated first. The grouping of operands

    can be forced by using parentheses.can be forced by using parentheses.

  • 8/3/2019 Simple and Operator Grammar

    13/19

    Removing AmbiguityRemoving Ambiguity

    Consider the following grammar that is highly ambiguousConsider the following grammar that is highly ambiguous

    EE E + E | EE + E | E--E | E * E | E/E |( E ) | idE | E * E | E/E |( E ) | id

    By enforcing precedence between operators binds * and / more tightlyBy enforcing precedence between operators binds * and / more tightlythan + and than + and --

    Therefore new unambiguous grammar will have the following productionsTherefore new unambiguous grammar will have the following productions

    EE E + T | EE + T | E--T | TT | T

    TT T * F | T/F | FT * F | T/F | FFF ( E ) | var( E ) | var

  • 8/3/2019 Simple and Operator Grammar

    14/19

    Removing AmbiguityRemoving AmbiguityConsider the following grammar that is highly ambiguousConsider the following grammar that is highly ambiguous

    EE E + EE + E

    EEE * EE * E

    EEDD

    This is ambiguous grammar generates two derivation tree.This is ambiguous grammar generates two derivation tree.By enforcing precedence by introducing new non terminals, we have a newBy enforcing precedence by introducing new non terminals, we have a new

    grammar:grammar:

    EET | E+TT | E+T

    TTF | T*FF | T*F

    FFDD

    EE

    TT

    EE TT

    FF++

    TT FF

    DD**DD

    FF

    DD

  • 8/3/2019 Simple and Operator Grammar

    15/19

    Introduction to Finite AutomataIntroduction to Finite Automata

    An automata is defined as a system where energy, material & informationAn automata is defined as a system where energy, material & information

    are transformed, transmitted & used for performing some functions withoutare transformed, transmitted & used for performing some functions without

    the direct participation of human being.the direct participation of human being.

    e.G Automatic Washing Machinee.G Automatic Washing Machine

    Automactic photoprinting machineAutomactic photoprinting machine

    In Computer Science , The Term automata means discrete automataIn Computer Science , The Term automata means discrete automata

    Automata Theory:: Automata theory is the study of abstract machines (orAutomata Theory:: Automata theory is the study of abstract machines (or

    more appropriately, abstract 'mathematical' machines or systems) and themore appropriately, abstract 'mathematical' machines or systems) and the

    computational problems that can be solved using these machines. Thesecomputational problems that can be solved using these machines. Theseabstract machines are called automata.abstract machines are called automata.

  • 8/3/2019 Simple and Operator Grammar

    16/19

    Introduction to Finite AutomataIntroduction to Finite Automata

    An automata is defined as a system where energy, material & informationAn automata is defined as a system where energy, material & information

    are transformed, transmitted & used for performing some functions withoutare transformed, transmitted & used for performing some functions without

    the direct participation of human being.the direct participation of human being.

    e.G Automatic Washing Machinee.G Automatic Washing Machine

    Automactic photoprinting machineAutomactic photoprinting machine

    In Computer Science , The Term automata means discrete automataIn Computer Science , The Term automata means discrete automata

    Automata Theory:: Automata theory is the study of abstract machines (orAutomata Theory:: Automata theory is the study of abstract machines (or

    more appropriately, abstract 'mathematical' machines or systems) and themore appropriately, abstract 'mathematical' machines or systems) and the

    computational problems that can be solved using these machines. Thesecomputational problems that can be solved using these machines. Theseabstract machines are called automata.abstract machines are called automata.

  • 8/3/2019 Simple and Operator Grammar

    17/19

    Introduction to Finite AutomataIntroduction to Finite Automata

    A Modal ofDiscrete AutomataA Modal ofDiscrete Automata

    II11 OO11

    II22 OO22::

    .:.:

    ::

    IIpp OOqq

    Automata

    q,q1,q2.qn

  • 8/3/2019 Simple and Operator Grammar

    18/19

    Introduction to Finite AutomataIntroduction to Finite Automata

    An Input values IAn Input values I1,1,II2,2, IIpp each of which can take a finite no of fixedeach of which can take a finite no of fixed

    values from the input alphabet values from the input alphabet

    ={I={I1,1,II2,2, IIp)p)

    The output values OThe output values O1,1,OO

    2,2,OO

    qqeach of which can take a finite no of fixedeach of which can take a finite no of fixed

    values from the output symbol Ovalues from the output symbol O

    O=O={O{O1,1,OO2,2, OOqq }}

    At any instant of the time, the automata can be in one of the followingAt any instant of the time, the automata can be in one of the following

    states : q, qstates : q, q1,1,

    qq2,2,

    qqnn

  • 8/3/2019 Simple and Operator Grammar

    19/19

    Introduction to Finite AutomataIntroduction to Finite Automata

    States Relation: The next state of an automata at any instant of time isStates Relation: The next state of an automata at any instant of time is

    determined by the present state & the present output.determined by the present state & the present output.

    Output Relation: Output is related to the state only or to both input & state.Output Relation: Output is related to the state only or to both input & state.

    Points to Be RememberPoints to Be Remember1)1) An automata in which output depends only on theAn automata in which output depends only on the

    input is called an automata without the memoryinput is called an automata without the memory

    2)2) An automata in which output depends on the statesAn automata in which output depends on the states

    as well is called automata with a finite memoryas well is called automata with a finite memory

    3)3) An automata in which output depends only on theAn automata in which output depends only on thestates of machine is called moore machine.states of machine is called moore machine.

    4)4) An automata in which output depends on the statesAn automata in which output depends on the states

    as well as on the input at any instant of time is calledas well as on the input at any instant of time is called

    a Mealy machine.a Mealy machine.