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
This is an example where English needs a context sensitive grammar. 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 |
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 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 |
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: |
| 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 If B is a final state, we write If B is a null state, we write |
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 Below are examples of each and a full example showing all the rules. USES: tell if an input string 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. |
![]() |
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. |
![]() |
When two states are parallel to eachother, we can use the OR operator (somtimes writebn as + or |). |
![]() |
Whe we have a repeating loop, we can replace it with r* since the definition of r* is zero or more repetitions. |
![]() |
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.