Docsity

Introduction to Generating Formal Languages using Grammars, Study notes of Computer Science

Toggle sidebar
bg1
Grammars and Languages
Languages are associated with two mechanisms is formal language theory
1. The finite automata that recognize the language.
2. The grammars that generate the language.
This lecture presents an introduction to the use of grammars to generate a formal
language. Note that we don’t think of grammars generating natural languages.
In a natural language, such as English or Spanish, grammar is often considered
a codification of correct usage. Ungrammatical constructs are still recognized.
In a formal language, the grammar generates the language. Any string that has
not been generated by the grammar will not be recognized.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15

Partial preview of the text

Download Introduction to Generating Formal Languages using Grammars and more Study notes Computer Science in PDF only on Docsity!

Grammars and Languages

Languages are associated with two mechanisms is formal language theory

  1. The finite automata that recognize the language.
  2. The grammars that generate the language. This lecture presents an introduction to the use of grammars to generate a formal language. Note that we don’t think of grammars generating natural languages. In a natural language , such as English or Spanish, grammar is often considered a codification of correct usage. Ungrammatical constructs are still recognized. In a formal language , the grammar generates the language. Any string that has not been generated by the grammar will not be recognized.

Alphabets

We begin by defining these basic terms, which also appear in the finite automata approach to the subject. Fortunately, the definitions are synonymous. The most basic construct is a symbol , which can be considered the equivalent of an ASCII or UNICODE character. There is no better definition of the term.

An alphabet is a finite non–empty set of symbols. We denote this by .

Some common alphabets seen in introductory courses are:

Binary digits:  = {0, 1}

Small letter set:  = {a, b}

Small number set  = {0, 1, 2, 3}

Another alphabet that I like to use for illustration is the hexadecimal digit set

 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F}

More on Strings and Concatenation

Example: Let  = {0, 1}. This is an alphabet with only two symbols.

Given this, we can define the following sets:

S1 = { 0, 1 } the set of all strings of length 1 over .

S2 = { 00, 01, 10, 11 } the set of all strings of length 2 over .

Consider two strings s 1 = 011 and s 2 = 0110 with symbols from  = {0, 1}.

  1. |s 1 | = 3 and |s 2 | = 4
  2. s 1 s 2 = 0110110 and s 2 s 1 = 0110011.
  3. |s 1 s 2 | = 7 and |s 2 s 1 | = 7 NOTE: The string concatenation operator is not commutative. Specifically s 1 s 2  s 2 s 1.

Concatenation (Part 2)

The string concatenation is associative. More precisely, we say

Let s 1 , s 2 , and s 3 each be a string over a given alphabet .

Then (s 1 s 2 )s 3 = s 1 (s 2 s 3 ), so that the expression (s 1 s 2 s 3 ) is not ambiguous. For example, let s 1 = 11 s 2 = 2222, and s 3 = 333333. Then s 1 s 2 = 112222 and (s 1 s 2 )s 3 = (112222)333333 = 112222333333, while s 2 s 3 = 2222333333 and s 1 (s 2 s 3 ) = 11(2222333333) = 112222333333. Note again that s 1 s 2 = 112222 while s 2 s 1 = 222211 Yet again, we have another reminder that the operator does not commute.

Strings and Languages

Again, we start with , an alphabet of symbols.

We define  * as the set of all strings based on the alphabet .

More properly, it is the set of strings obtained by concatenating

zero or more symbols from the alphabet .

We define 

+

as the set of all non–empty strings based on the alphabet .

We can also say 

+

A language L is defined as a subset of  * ; L   *.

Example:  = {a, b}

 * = { , a, b, aa, ab, ba, bb, aaa, aab, aba, abb, … }

+ = { a, b, aa, ab, ba, bb, aaa, aab, aba, abb, … }

L = { a, ab, aba, abb } is a finite language over . Note that L   *.

Strings and Languages (Part 2)

Again, consider  = {a, b}

with  * = { , a, b, aa, ab, ba, bb, aaa, aab, aba, abb, … }

Consider the set defined over  by L = { a

n b n | n  0 }. Notation: The “exponent” refers here to concatenation. Let w be a string. Then: w 2 = ww is the concatenation of w with itself. w n+ = w n w = ww n , for n  1 (an inductive definition).

So we now have L = { , ab, aabb, aaabbb, aaaabbbb, …. }.

Obviously L   * , so that L is a language over  = {a, b}.

Note that L = { a n b n | n  0 } is defined as a string of the symbol “a” followed by a string of an equal number of the symbol “b”. L 2 = { a m b n | m  0, n  0 } is defined as a string of the symbol “a” followed by a string of symbol “b”, of the same or different size.

How Do Grammars Generate Languages?

A Small Language

Here is a grammar for a subset of English.

  1. A sentence comprises a noun_phrase followed by a verb_phrase.
  2. A noun_phrase is either a) an article followed by an adjective followed by a noun , or b) an article followed by a noun.
  3. A verb_phrase is either a) a verb followed by an adverb , or b) a verb.
  4. article = {a, the} -- this is a list of “terminal symbols”
  5. adjective = {silly, clever}
  6. noun = {frog, cow, lawyer}
  7. verb = {writes, argues, eats}
  8. adverb = {well, convincingly}

Producing a Sentence in the Language

In this language, the token sentence is the start symbol. sentence noun_phrase verb_phrase article adjective noun verb adverb At this point, we start to see terminals introduced. the adjective noun verb adverb the silly noun verb adverb the silly cow verb adverb the silly cow writes adverb the silly cow writes convincingly At the end, we have a sequence of terminal symbols Another sentence in the language: The clever lawyer argues convincingly

Recognizing the Language

For those who miss high–school English class, here I show the standard way taught then for recognizing a valid sentence in the language. It was taught to me as “diagramming a sentence”. Notice that this does not indicate how to write a sentence, but only how to show that it was constructed according to the “rules of grammar”.

Definitions (Part 1)

  1. A vocabulary V is a finite, non-empty, set of elements called symbols.
  2. A sentence over V is a sequence of a finite number of elements of V.

3. The empty sentence  (also called the empty string) is the sentence

containing no symbols.

  1. The set of all sentences over V is denoted by V*.
  2. A language over V is a subset of V*. NOTE: The textbook calls V a set of objects called variables. This is really the same as a vocabulary.

Analysis of Our Sample Grammar

  1. A sentence comprises a noun_phrase followed by a verb_phrase. Here the symbol sentence is clearly a non-terminal as it is replaced by the symbol noun_phrase followed by the symbol verb_phrase. This is the first rule in the grammar, so the symbol sentence is the start symbol.
  2. A noun_phrase is either a) an article followed by an adjective followed by a noun , or b) an article followed by a noun. Here the symbol noun_phrase is clearly a non-terminal as it can be replaced by either one or two other symbols.
  3. A verb_phrase is either a) a verb followed by an adverb , or b) a verb. The symbol verb_phrase is also clearly a non–terminal.

Analysis of the Grammar (Part 2)

We now move to a listing of the terminal symbols.

  1. article = {a, the}
  2. adjective = {silly, clever}
  3. noun = {frog, cow, lawyer}
  4. verb = {writes, argues, eats}
  5. adverb = {well, convincingly} The symbols article, adjective, noun, verb, adverb are clearly non-terminals. What we have left over are the symbols for which no substitution rules are specified. This is the set of terminal symbols, denoted by T. V = {sentence, noun_phrase, verb_phrase, article, adjective, noun, verb, adverb} T = { a, the, silly, clever, frog, cow, lawyer, writes, argues, eats, well, convincingly } Conventionally, the start symbol is listed first in the set V.

Our Silly Grammar as a Set of Productions

Here is the grammar, written in one style of production rules.

  1. sentencenoun_phrase verb_phrase
  2. noun_phrasearticle adjective noun | article noun
  3. verb_phraseverb adverb | verb
  4. article  a | the
  5. adjective  silly | clever
  6. noun  frog | cow | lawyer
  7. verb  writes | argues | eats
  8. adverb  well | convincingly But what is (V  T)

? (V  T) is the set of all objects, both terminals and non–terminals used in these rules. (V  T)

is the set of all sequences of one or more of these symbols; verb adverb  (V  T)

Just What is (V  T)

+

We first define (V  T). We present the sets V and T, but in another ordering. V = {adjective, adverb, article, noun, noun_phrase, sentence, verb, verb_phrase} T = { a, argues, clever, convincingly, cow, eats, frog, lawyer, silly, the, well, writes } (V  T) = { a, adjective, adverb, argues, article, clever, convincingly, cow, eats, frog, lawyer, noun, noun_phrase, sentence, silly, the, verb, verb_phrase, well, writes }. This set has 20 symbols. We could then consider the set that might be called (V  T) 2 , the set of 400 strings formed with two of the symbols above. It might contain strings such as a a a adjective article noun verb adverb (V  T)

is the set of all combinations of one or more of the symbols in (V  T)