1 / 10

Discrete Structure

Discrete Structure. Li Tak Sing( 李德成 ). From grammar to inductive definition. Consider the following grammar: S |aB B b|bB Basis: , abL(G) Induction: if ay  L(G), then aby  L(G). Constructing grammars. {a n |n N} S|aS {a n b n |n N} S |aSb {(ab) n |nN} S |abS.

ronald
Download Presentation

Discrete Structure

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Discrete Structure Li Tak Sing(李德成)

  2. From grammar to inductive definition • Consider the following grammar: • S|aB • B b|bB • Basis: , abL(G) • Induction: if ay  L(G), then aby  L(G).

  3. Constructing grammars • {an|nN} • S|aS • {anbn|n N} • S |aSb • {(ab)n|nN} • S |abS

  4. Combining grammars • Suppose M and N are languages whose grammars have disjoint sets of non-terminals. Suppose also that the start symbols for the grammars of M and N are A and B respectively. Then we have the following new languages and grammars: • Union rule: the language MN starts with the two productions: SA|B • Product Rule: the language MN starts with the productions: S AB • Closure Rule: The language M* starts with the productions: S AS|

  5. Examples • Write the grammar for each of the following languages. • {anbcm|m,nN} • {, a, ab, aab, aaab, ....} • {a, ba, bba, bbba, ....} • {anbn|n N}* • {anbn|n N}{cn|n1}

  6. Meaning and ambiguity • A grammar is said to be ambiguous if its language contains some string that has two different parse trees. This is equivalent to saying that some string has two distinct leftmost derivations or that some string has two distinct rightmost derivations.

  7. Ambiguity • Consider the grammar. • Ea|b|E-E • The string a-b-a has the following distinct derivations. • EE-E a-E a-E-E a-b-E a-b-a • E E-E E-E-E a-E-E a-b-E a-b-a • The former one has the meaning of a-(b-a) • The latter one has the meaning of (a-b)-a

  8. Removing the ambiguity • If we change the grammar as this: • E E-T|T • T a|b • In the above grammar, T can only be replaced by a or b. So only the right operand can be further expands to another expression that have -. So a-b-a will only be expanded to have the meaning of (a-b)-a

  9. Ambiguity • Show that each of the following grammers is ambiguous. In other words, find a string that has two different parse trees (equivalently, two different leftmost derivations or two different rightmost derivations) • Sa|SbS. • S abB|AB and A |Aa and B |bB. • S aS|Sa|a • S aS|Sa|b • S Ab|A and A b|bA

  10. More Examples • Find grammars for the following languages: • {anbmcmdn| m,n N} • {anbmcdn| m,n N} • {anbmcmd| m,n N} • {abmcmd| m,n N} • abmcdm| m N}

More Related