Chapter 10 Discrete Structures Notes

Table of Contents

Tree-Grammar Comparison Types of Grammar
Examples of CFGs Regular Expression, FSA, and Grammar Production Rules
An example of a product that uses regular expressions  
Trees Grammars (type-2/context-free)
internal nodes non-terminal symbol (N) also VN usually capital letters are used
leaves terminal symbol (T) also VT
usually lower case letters are used
root starting symbol (S)
node symbol (V) V= VT U VN
edge production (N-> V)
adjancecy E->E+T
E->T
Can be combined as: E-> E+T | T

(A space indicates AND, | indicates OR.)

Type of Grammar
Type Name Recognized By Generated By Analyzed By Use Comments Memory
Type 0 phrase structure
PSG
Turing Machine any string with nonterminals ->
any string
NtN->Nt
use an infinite tape (in both directions) and is able to do read/write.This allows the code to modify itself. Can be used to model any computer.   Unlimited.
Type 1 context-sensitive
CSG
linear bound automata any string with nonterminals
->
any string as long as or longer than the input
NNt->NNNt
    English tends to have context-sensitive constructions in which word choice is determined by the words before and after.

 

Base->cowardly

Ball->dance


BaseBall->cowardly dance

This is an example where English needs a context sensitive grammar.

For example,
Base Line-> starting point

Notice that we need to have several symbols on the left hand side to define the context. A grammar checker that is to work well needs to work on a context sensitive language. By their nature, these are harder to deal with than context free languages.

 
Type 2 context-free
CFG
Trees one nonterminal
->
any string

N->t
N->N
N->tN
N->Nt

N->NNNtt
N-> null

top down parsing, bottom up parsing, pre/post order Used to parse expression or code When we scan a tree inorder, we frequently need to put items on a stack (memory) so that we can use them later on. A finite state machine can not do this. A stack is also used for Backtracking.

Many rules in English are context text free.

S-> NP VP
NP-> the good book
VP-> was easy to read
S-> the good book was easy to read

See CSG above for counterexamples.

 

Makes use of a push-down stack as   memory. Item can put push/pulled on a stack, but once used, there is no memory of the item.  
Type 3 regular
FA
Finite State Machines one nonterminal
->
tN or t
where t is ternimal and N is nonterminal

N->t
N->tN
N-> null

Finite state machine with or without output   Finite State machines with output produce and output string given an input string  

Have limited form of memory as they can remember past states visted.

Finite state machines without output are used to recognize a grammar

Example of an SQL expression stated in gramatical terms. ([]=optional)

SELECT [predicate]
{ * | table.* | [table.]field1 [AS alias1] [, [table.]field2 [AS alias2] [, ...]]}
FROM tableexpression [, ...]
[IN externaldatabase]
[WHERE... ]
[GROUP BY... ]
[HAVING... ]
[ORDER BY... ]
[WITH OWNERACCESS OPTION]
Context-free definition of a fixed point number with sign (| = OR)
V-> N | +N | - N
N-> 0 |1|2|3|4|5|6|7|8|9|     (can also be written an N-> <digit>
N->0N | 1N | 2N | 3N | 4N | 5N | 6N | 7N | 8N | 9N (i.e. N-> digit number)

The production to get +134 is as follows:
V -> +N -> +1N -> +13N -> +134
V->+N
N -> 1N
N->  3N
N->  4
derivation_tree.gif (16308 bytes)

 

Regular Expression, FSA, and Grammar Production Rules Comments
Production Rules:

In going from state A to state B with an input of r, we can write
A->rB

If B is a final state, we write
A->r

If B is a null state, we write
A-> null
cs_reg5.gif (25976 bytes)

All of these are type 3 (regular grammar)

The digram at the left shows how we can go from FSA from production rules and back again.

Regular expression are built up from the following rules:

r1, r2 are regular expresions
r1+r2 is a regular expression (in your book r1Ur2)
r1r2 is a regular expession
r* is a regular expression ({null,r, rr, rrr, rrrr...}

Below are examples of each and a full example showing all the rules.
It can be show that any FSA can be writen as a regular expression and any regular expression can be writen as a FSA.  What you get when you do the transformation is a start state, and end state and a regular expression.

USES:

tell if an input string is a signed integer:
If in matches the pattern
(+|-|null)dd*
it is a signed integer.

Maskes: In the print statement, you provide a template for your output. Does the template reflect you actual output? Are you outputing charactors but formating for numbers?

When reading in a string in C, determine valid strings.

 

These, as we show below are also regular grammars.
cs_reg1.gif (26406 bytes) If two states are in series, we can eliminate one stae and concatinate.

The grammar rules to the left show that this is a regular grammar.

cs_reg2.gif (16673 bytes) When two states are parallel to eachother, we can use the OR operator (somtimes writebn as + or |).
cs_reg3.gif (15799 bytes) Whe we have a repeating loop, we can replace it with r* since the definition of r* is zero or more repetitions.
cs_reg4.gif (33833 bytes) In this complete example, r1 and r4 are replaced by r1+r4, the loop r3 is designated by r3* and the path from S to A to F is given by (r1+r4)r3*r2

You are the visitor to this site.

Return to Main Page