Languages Grammars

Embed Size (px)

Citation preview

  • 8/11/2019 Languages Grammars

    1/24

    Languages and Grammars

    MSU CSE 260

  • 8/11/2019 Languages Grammars

    2/24

    Outline

    Introduction: Example

    Phrase-Structure Grammars: Terminology, Definition,

    Derivation, Language of a Grammar, Examples Exercise 10.1 (1)

    Types of Phrase-Structure Grammars

    Derivation Trees: Example,Parsing

    Exercise 10.1 (2, 3)

    Backus-Naur Form

  • 8/11/2019 Languages Grammars

    3/24

    Introduction

    In the English language, the grammar determineswhether a combination of words is a validsentence.

    Are the following valid sentences?

    The largerabbit hopsquickly. Yes

    The frog writes neatly. Yes

    Swims quickly mathematician. No Grammars are concerned with the syntax (form) of

    a sentence, and NOT its semantics (or meaning.)

  • 8/11/2019 Languages Grammars

    4/24

    English Grammar

    Sentence: noun phrasefollowed by verb phrase;

    Noun phrase: articleadjectivenoun,or article

    noun; Verb phrase: verbadverb, or verb;

    Article: a, or the;

    Adjective: large, or hungry;

    Noun: rabbit, or mathematician, orfrog;

    Verb: eats, or hops, orwrites, orswims;

    Adverb: quickly, or wildly, orneatly;

  • 8/11/2019 Languages Grammars

    5/24

    Example

    Sentence

    Noun phrase verb phrase

    Article adjective noun verb phrase Article adjective noun verb adverb

    the adjective noun verb adverb

    the largenoun verb adverb

    the largerabbit verb adverb

    the largerabbit hopsadverb

    the largerabbit hopsquickly

  • 8/11/2019 Languages Grammars

    6/24

    Grammars and Computation

    Grammars are used as a model of

    computation.

    Grammars are used to:

    generate the words of a language, and

    determine whether a word is in a language.

  • 8/11/2019 Languages Grammars

    7/24

    Phrase-Structure Grammars

    Terminology Definitions.A vocabulary(or alphabet) Vis a

    finite, nonempty set of elements calledsymbols.

    A word(orsentence) over Vis a string of finitelength of elements of V.

    The empty string(or null string,) denoted by , is

    the string containing no symbols.

    The set of all words over Vis denoted by V*.

    A language over Vis a subset of V*.

  • 8/11/2019 Languages Grammars

    8/24

    Phrase-Structure Grammars

    A language can be specified by:

    listing all the words in the language, or

    giving a set of criteria satisfied by its words, or using a grammar.

    A grammar provides:

    a set of symbols, and

    a set of rules, called productions, for producing words

    by replacing strings by other strings: w0w1.

  • 8/11/2019 Languages Grammars

    9/24

    Phrase-Structure Grammar

    DefinitionAphrase-structure grammarG = (V, T, S,P)consists of:

    a vocabularyV,a subset Tof Vconsisting of terminalelements,

    a start symbolSfrom V, and

    a setPofproductions.

    The setN = V-Tconsists of nonterminal symbols.

    Every production inPmust contain at least onenonterminal on its left side.

  • 8/11/2019 Languages Grammars

    10/24

    Phrase-structure Grammar

    Example G= {V, T, S,P}, where

    V = {a, b,A,B, S},

    T = {a, b},Sis the start symbol, and

    P= { SAba,

    ABB,

    Bab,

    ABb}.

  • 8/11/2019 Languages Grammars

    11/24

    Phrase-Structure Grammars

    Derivation Definition.

    Let G = (V, T, S,P) be a phrase-structure grammar.

    Let w0 = lz0rand w1 = lz1rbe strings over V. Ifz0z1is a production of G, we say that:

    w1is directly derivablefrom w0(denoted: w0w1.)

    If w0, w1, , wnare strings over Vsuch that:

    w0w1, w1w2, ,wn-1wn, we say that:wnis derivablefrom w0(denoted: w0* wn.)

    Note. * should be on top of.

    The sequence of all steps used to obtain wnfrom w0iscalled a derivation.

  • 8/11/2019 Languages Grammars

    12/24

    Example

    In the previous example grammar, theproduction:Babmakes the stringAaba

    directly derivable from stringABa.ABa Aaba

    AlsoAaba BBaba Bababa abababa

    using: ABB, Bab, andBab.

    So:ABa* abababa

    abababa is derivable fromABa.

  • 8/11/2019 Languages Grammars

    13/24

    Language of a Grammar

    Definition.

    Let G = (V, T, S,P) be a phrase-structure

    grammar.The language generatedby G(or thelanguageof G), denoted byL(G), is the set

    of all strings of terminals that are derivablefrom the start symbol S.

    L(G) = {wT* | S* w}.

  • 8/11/2019 Languages Grammars

    14/24

    Example

    Let G= {V, T, S,P} be the grammar where:

    V = {S, 0, 1},

    T = {0, 1},

    P= { S11S,

    S0}.

    What isL(G)?

    At any stage of the derivation we can either: add two 1s at the end of the string, or

    terminate the derivation by adding a 0 at the end of the string.

    L(G)={0, 110, 11110, 1111110, } = Set of all stringsthat begin with an even number of 1s and end with 0.

  • 8/11/2019 Languages Grammars

    15/24

    Exercise 10.1 (1)

  • 8/11/2019 Languages Grammars

    16/24

    Types of Grammars

    A type 0(phrase-structure) grammar has norestrictions on its productions.

    A type 1(or context-sensitive) grammar hasproductions only of forms:

    w1w2with length of w2length of w1, or

    w1.

    A type 2(or context-free) grammar hasproductions only of the formAw2, whereAis asingle nonterminal symbol.

  • 8/11/2019 Languages Grammars

    17/24

    Types of Grammarscont.

    A type 3(or regular) grammar has productionsonly of the form:

    AaB,orAa, where

    AandBare nonterminal symbols, and

    ais a terminal symbol, or

    S.

    Note.

    Every type 3 grammar is a type 2 grammar

    Every type 2 grammar is a type 1 grammar

    Every type 1 grammar is a type 0 grammar

  • 8/11/2019 Languages Grammars

    18/24

    Types of Grammars - Summary

    Type Restrictions on productions w1w2

    0 No restrictions

    1 l(w1) l(w2), or w2=

    2 w1=AwhereAN

    3 w1=A, and w2=aBor w2=a, whereAN,BN, aT, or

    w1=Sand w2=

  • 8/11/2019 Languages Grammars

    19/24

    Derivation Trees

    For type 2 (context-free) grammars:

    A derivation(orparse)tree, is an ordered rooted

    tree that represents a derivation in the languagegenerated by a context-free grammar, where:

    the root represents the starting symbol;

    the internal vertices represent nonterminal symbols;

    the leaves represent the terminal symbols;

    for a productionAw, the vertex representingAwill

    have children vertices that represent each symbol in w.

  • 8/11/2019 Languages Grammars

    20/24

    Example

    Derivation tree for:

    the hungry rabbit eats quickly

    sentence

    noun phrase verb phrase

    article adjective noun verb adverb

    the hungry rabbit eats quickly

  • 8/11/2019 Languages Grammars

    21/24

    Exercise 10.1 (2, 3)

  • 8/11/2019 Languages Grammars

    22/24

    Parsing

    To determine whether a string is in the

    language generated by a grammar, use:

    Top-down parsing: Begin with S and attempt to derive the word by

    successively applying productions, or

    Bottom-up parsing:

    Work backward: Begin by inspecting the word and

    apply productions backward.

  • 8/11/2019 Languages Grammars

    23/24

    Example

    Let G= {V, T, S,P} be the grammar where: V = {a, b, c,A,B, C, S}, T = {a, b, c},

    Productions: Determine whether cbabis inL(G)?

    SAB Top-down parsing:ACa SAB

    B Ba S AB CaB

    B Cb SAB CaB cbaB

    B b S AB CaB cbaBcbab

    C cb Bottom-up parsing:C b Cab cbab

    Ab Cabcbab

    AB AbCab cbab

    S AB AbCabcbab

  • 8/11/2019 Languages Grammars

    24/24

    Backus-Naur Form

    Used with type 2 (context-free) grammars; like forspecification of programming languages:

    Use ::= instead of

    Enclose nonterminal symbols within < >

    Group productions with same left side with symbol |

    Example.

    ::= ::=+ |-

    ::= |

    ::=0 |1 |2 |3 |4 |5 |6 |7 |8 |9