chapter 2 cf grammar and cf language-eng

Upload: nur-syahela

Post on 07-Apr-2018

249 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/3/2019 Chapter 2 Cf Grammar and Cf Language-Eng

    1/20

    CF GRAMMARCF GRAMMAR

    AND CFAND CFLANGUAGELANGUAGE

    Chapter 2Chapter 2

  • 8/3/2019 Chapter 2 Cf Grammar and Cf Language-Eng

    2/20

    2.12.1CF GRAMMAR AND CFCF GRAMMAR AND CFLANGUAGELANGUAGE

    Grammar can be divided into certaintype such :

    Context-sensitive Grammar(csg) Context-free Grammar(cfg)

    Right-linear Grammar

  • 8/3/2019 Chapter 2 Cf Grammar and Cf Language-Eng

    3/20

    Definition:Definition:

    CFG. G represent with 4 tuples G =(V, , P, S):V is a set of variables(finite) or non-

    terminal symbol is a set of terminal symbol (end),

    where V =

    S is a start symbol, where S V

    P is a set (finite) of production rules thatis in a form of A for A V, (v )*.

  • 8/3/2019 Chapter 2 Cf Grammar and Cf Language-Eng

    4/20

    Context-free DefinitionContext-free Definition

    Context-free Grammar (cfg) is asystem to generate a set of string oflanguage. This process is done by

    changing a string using productionrules. Grammar is called context-freebecause of the general applicability

    of the rules.

  • 8/3/2019 Chapter 2 Cf Grammar and Cf Language-Eng

    5/20

    Example:Examp

    le:P : S uAv

    A w

    String derivation is:S => uAv => uwv

    A string w can be derived from v if there are sequencial usedof rules that convert v to w:

    v => w1 => w2 => ... => wn => w

    Or writen as:*

    v => w

  • 8/3/2019 Chapter 2 Cf Grammar and Cf Language-Eng

    6/20

    Let G = (V, , P, S) is cfg:

    A string w(V )* is a sententialform of G if there is a derivation S=>*w in G.

    A string w* is a sentence of G ifthere is a derivation S =>* w inG.

    A language of G denoted L(G), is theset (w * S=>*w).

  • 8/3/2019 Chapter 2 Cf Grammar and Cf Language-Eng

    7/20

    Example:Example:

    G = ({S}, {a,b}, P, S)P: S aS bS

    String derivation :S => S => aS => a => aS => bS => b => bS => aS => aaS => aa => aaS => aS => abS => ab => ab

    S => bS => bbS => bb => bb.................S => ... => combination of a and b

  • 8/3/2019 Chapter 2 Cf Grammar and Cf Language-Eng

    8/20

    Language is formed from certainword in a or b or combination of aand b, defined by regular expression:

    L (G) = (a+b)*

  • 8/3/2019 Chapter 2 Cf Grammar and Cf Language-Eng

    9/20

    Derivation tree,DtDerivation tree,Dt

    If G = (V, , P,S) is a cfg and S=>*w is aderivation. Derivation tree Dt for S=>*wis a process of building D.t as follows: Dt begin with S as root.

    If A x1, x2, .... xn with xi(V) is the i rulefor string derivation uAv, then the addition x1,x2,..., xn is for the child of A tree.

    If A is the i derivation in a string derivation

    uAv, then the addition is a child for A tree.

  • 8/3/2019 Chapter 2 Cf Grammar and Cf Language-Eng

    10/20

    Contoh:Contoh:

    G = ({S,.A}, {a, b}, P, S)P: S AA

    A AAA bA Ab aThen string derivation for string formation ababaa is:S => AA => aA => aAAA => abAAA

    => abaAA => ababAA => ababaa

    Figure ofderivation tree

  • 8/3/2019 Chapter 2 Cf Grammar and Cf Language-Eng

    11/20

    Cfg G called ambiguous, if there aren L(G), where n have more thanone method or different tree

    derivation.

    Cf language L is a naturalambiguous if every cfg generate L

    is ambiguous.

  • 8/3/2019 Chapter 2 Cf Grammar and Cf Language-Eng

    12/20

    Example:Example:G = ({S}, {a,b}, P,S)

    P : S aS Sa b Two derivation method for string aba is:

    S =>aS > aSa => aba

    or

    S => Sa => aSa => aba

    Production rules can be modified so that everyword in a language can be derived with only onemethod.

  • 8/3/2019 Chapter 2 Cf Grammar and Cf Language-Eng

    13/20

    Example:Example:P :S aS bB

    B aB There only one way for string derivation for string formation aba:S => aS => abB => abaB => aba => aba

    If there are n times derivation on the S

    aS

    bB rules, it willproduce,S => anS and then S => anbB

    For derivation of m times doe on a rule of B aB it will produce:B => amB

    andB => am => am

    So,S => anbB => anbam {anbam n 0, m 0 } a*ba*

  • 8/3/2019 Chapter 2 Cf Grammar and Cf Language-Eng

    14/20

    Regular GrammarRegular GrammarDefinition:

    Regular language is a language that can be generate byregular grammar.

    Regular grammar is cfg. G consist of 4 tuples G = (V, , P,S), where every production rules in forms as below:

    A aA aBA for A,BV and a. Grammar can also be classified into regular grammar and

    non regular grammar.

  • 8/3/2019 Chapter 2 Cf Grammar and Cf Language-Eng

    15/20

    Example: RegularExample: RegularGrammarGrammar

    L = a+b*

    G1 = ({S,R}, {a,b}, P,S)

    P = S

    aS aRR bR L is a regular language because it

    can be generate by regular grammarproduction rules.

  • 8/3/2019 Chapter 2 Cf Grammar and Cf Language-Eng

    16/20

    Example:non regularExample:non regular

    L = a + b*

    G1 = ({S,A,B}, {a,b}, P,S)

    P : S AB

    A aA aB bB

    L is not regular language because itcannot be generate by regular grammarproduction rules.

  • 8/3/2019 Chapter 2 Cf Grammar and Cf Language-Eng

    17/20

    2.22.2Example of BNF inExample of BNF inPascal LanguagePascal Language

    Cfg is in Backus Naur Form (BNF) ifevery variable writen in whereproduction writen as:

    BNF usually being used to showsyntax for programming languagesuch as Pascal, C, ect.

  • 8/3/2019 Chapter 2 Cf Grammar and Cf Language-Eng

    18/20

    Example of BNF format forExample of BNF format fora simple language:a simple language:

    :=

    A B C

    +

    -

    () *

    where: var, exp, := A, B, C, +, *, -, ( , ) is asymbol used in grammar definition for a simple

    phrase assign. Symbol Meaning

    nonterminalor starting symbol

    or:=,+,-,*,A,B,C, (.) terminal

  • 8/3/2019 Chapter 2 Cf Grammar and Cf Language-Eng

    19/20

    Example of BNF UsageExample of BNF Usage(Pascal language):(Pascal language):

    A := B*(A+B) :=

    A :=

    A := *A := *()A := B* ()A := B*(+)

    A := B*(A+)A := B*(A+)A := B*(A+B)

  • 8/3/2019 Chapter 2 Cf Grammar and Cf Language-Eng

    20/20

    contcontA := B*C(A+C) also represented inparse tree

    form as below: