06L6

download 06L6

of 20

Transcript of 06L6

  • 8/13/2019 06L6

    1/20

  • 8/13/2019 06L6

    2/20

    2014/2/3 CSC 3130 Formal Languages and Automata Theory 2

    Minimizing a DFA

    Two steps:

    Removing the states that are inaccessiblefrom the start state.

    Combining states that are equivalent.

    Two states p and q are equivalent iffFor any string , if p r and q s, r is

    final iff s is final.

  • 8/13/2019 06L6

    3/20

    2014/2/3 CSC 3130 Formal Languages and Automata Theory 3

    Example of FA Minimization

    aStart

    e

    fb

    c g

    d h

    1 1

    1

    1

    11

    1

    1

    0 0

    0

    0

    0

    00 0

    a b c d e f g

    h

    g

    f

    e

    d

    c

    b

  • 8/13/2019 06L6

    4/20

    2014/2/3 CSC 3130 Formal Languages and Automata Theory 4

    Example of FA Minimization

    We finally get: Start

    1

    1

    1 1

    1

    0

    0

    0

    00

    [c] [d,f]

    [b,h]

    [a,e]

    [g]

  • 8/13/2019 06L6

    5/20

    2014/2/3 CSC 3130 Formal Languages and Automata Theory 5

    Class Discussion

    Minimize the following DFA:

    0

    00

    1

    11

    a

    bc

    d e f g01 0

    10

    0

    1

    1

    Start

  • 8/13/2019 06L6

    6/20

    2014/2/3 CSC 3130 Formal Languages and Automata Theory 6

    Context Free Languages (CFL)

    Many languages are not regular, e.g.,

    balanced parentheses, begin-end matches,

    {0n1n| n>0}, {1n2| n>0}

    In this second part, we will study another

    class of languages higher in the hierarchythat can describe a larger set of languages.

  • 8/13/2019 06L6

    7/20

    2014/2/3 CSC 3130 Formal Languages and Automata Theory 7

    Context Free Grammar (CFG)

    CFG is invented originally for describing naturallanguages:

    boy

    little

    , , , and are called non-terminalsor variables. boy

    and little are called terminals. The rules are called

    productions.

  • 8/13/2019 06L6

    8/20

    2014/2/3 CSC 3130 Formal Languages and Automata Theory 8

    Example of CFG

    +

    *

    ()

    id

    Variables: Terminals: +, *, (, ), id

  • 8/13/2019 06L6

    9/20

    2014/2/3 CSC 3130 Formal Languages and Automata Theory 9

    Formal Definition of CFG

    A CFG is denoted by a 4-tuple:

    (V, T, P, S)

    where V is a set of variables (non-terminals)T is a set of terminals

    P is a set of productions (rules) of the form:

    A where A is a variable and (VT)*

    S is a special variable called start symbol

  • 8/13/2019 06L6

    10/20

    2014/2/3 CSC 3130 Formal Languages and Automata Theory 10

    Production Rules

    A set of productions:

    A 1

    A 2

    ..

    A k

    The above can be written as A 1|2|.. |k|

    The previous example for can be

    written as E E+E | E*E | (E) | id

  • 8/13/2019 06L6

    11/20

    2014/2/3 CSC 3130 Formal Languages and Automata Theory 11

    DerivationLets look the CFG G: E E+E | E*E | (E) | id

    E * E

    (E) * E

    (E) * id

    ( E + E ) * id

    ( E + id ) * id

    ( id + id ) * id

    We say if can be obtained from by applying aproduction once. We say if can be obtained from

    by applying the productions zero or more times. We use

    to specify clearly which grammar we are using.

    E

    Derivation

    *

    G

  • 8/13/2019 06L6

    12/20

    2014/2/3 CSC 3130 Formal Languages and Automata Theory 12

    Context Free Languages (CFL)

    The language generated by a CFG G, denoted by

    L(G), is :

    L(G) = {| T* and S }Therefore,

    every string consists solely of terminals, and

    every string can be derived from S

    It is called a context free language (CFL).

    *G

  • 8/13/2019 06L6

    13/20

    2014/2/3 CSC 3130 Formal Languages and Automata Theory 13

    CFL

    Notice that:

    We must start with the start symbol

    We can use any production any number oftimes.

    The final string can only contain terminals.

  • 8/13/2019 06L6

    14/20

    2014/2/3 CSC 3130 Formal Languages and Automata Theory 14

    Examples of CFG

    Consider the following CFG G1:

    S aSb

    S ab

    How to generate aabb?S aSb (use Rule 1)

    aabb (use Rule 2)

    How to generate aaabbb?

    S aSb (use Rule 1)

    aaSbb (use Rule 1)

    aaabbb (use Rule 2)

  • 8/13/2019 06L6

    15/20

    2014/2/3 CSC 3130 Formal Languages and Automata Theory 15

    Examples of CFG

    Consider the following CFG G2:

    S SS

    S (S)

    S

    In this example, variables are {S}, terminals are

    {(,)}, start symbol is S and productions are the

    above three rules.

    What language does this grammar generate?

  • 8/13/2019 06L6

    16/20

    2014/2/3 CSC 3130 Formal Languages and Automata Theory 16

    Examples of CFG

    How to generate () with G2?S (S) (use Rule 2)

    () (use Rule 3)

    How to generate (()()) with G2?S (S) (use Rule 2)

    (SS) (use Rule 1)

    ((S)S) (use Rule 2)

    ((S)(S)) (use Rule 2)(()(S)) (use Rule 3)

    (()()) (use Rule 3)

    G2:

    S SSS (S)

    S

  • 8/13/2019 06L6

    17/20

    2014/2/3 CSC 3130 Formal Languages and Automata Theory 17

    Examples of CFG

    How can we represent all the arithmetic expressionswith plus, minus, parentheses and variables x,

    y and z, i.e., {x, y, z, x+y, x-y, y+x, x+(y-z), .. }?

    Consider the following CFG G3:E E + E E (E) V x V z

    E E - E E V V y

    In this example, variables are {E, V}, terminals are{+, -, (, ), x, y, z}, start symbol is S and productions

    are the above seven rules.

  • 8/13/2019 06L6

    18/20

    2014/2/3 CSC 3130 Formal Languages and Automata Theory 18

    Examples of CFG

    How to generate x+(y-z)?

    E E + E (use Rule 1)

    V + E (use Rule 4)

    x + E (use Rule 5)x + (E) (use Rule 3)

    x + (E - E) (use Rule 2)

    x + (V - E) (use Rule 4)

    x + (y - E) (use Rule 6)x + (y - V) (use Rule 4)

    x + (y - z) (use Rule 7)

    G3:E E + E | E - E | (E) | V

    Vx | y | z

  • 8/13/2019 06L6

    19/20

    2014/2/3 CSC 3130 Formal Languages and Automata Theory 19

    Class Discussion

    Write a CFG for each of the following

    languages:

    The set of all palindromes over the alphabet={a,b}

    {anbncmdm| n 1, m 1}

    {anbmcmdn| n 1, m 1}

  • 8/13/2019 06L6

    20/20

    2014/2/3 CSC 3130 Formal Languages and Automata Theory 20

    Class Discussion

    Consider the grammar:

    S aB | bA

    A a | aS | bAAB b | bS | aBB

    Generate 4 strings from this grammar.

    Do you know what language does this CFGrepresent?