270 likes | 529 Views
CSCI 115. Chapter 10 Languages and Finite State Machines. CSCI 115. §10 .1 Languages. §10 .1 – Languages. From section 1.3: S = Set of “words” S* = collection of sentences formed from S concatenation. §10 .1 – Languages. Languages Proper construction Syntax
E N D
CSCI 115 Chapter 10 Languages and Finite State Machines
CSCI 115 §10.1 Languages
§10.1 – Languages • From section 1.3: • S = Set of “words”S* = collection of sentences formed from S • concatenation
§10.1 – Languages • Languages • Proper construction • Syntax • An dog talked pink martians • Meaningful • Semantics • The boy ate red candy • Programming Languages
§10.1 – Languages • Grammar • A phrase structure grammar G is a 4-tuple(V, S, v0, ) s.t.:V is some finite setS Vv0 V – S is a finite relation on V* • production relation
§10.1 – Languages • The relation and related concepts • w w’ (w, w’ V*) is called a production on V • No production ever has on the left side • is a new relation (the substitution relation) • x y means y results from x via an allowable production (i.e. xy) • x y means y results from x via a finite number of allowable relations • w S* is syntactically correct iff v0 w
§10.1 – Languages • Other components of the 4-tuple • S is sometimes called the set of terminal symbols • N = V – S is the set of non-terminal symbols • V = N S • There is a language corresponding to G • L(G) • Always a subset of S* • L(G) is the set of all properly constructed sentences
§10.1 – Languages • Classifying languages and elements • Derivation Trees • Determine if an arbitrary element is a valid element of the language • Description of languages • Finding a template for the valid elements
§10.1 – Languages • Consider a grammar where is given by: v0 aw aw c • Sentences cannot be verified by a derivation tree • Grammar typing
§10.1 – Languages • Let G = (V, S, v0, ) be a phrase structure grammar. G is: • Type 0 if no restrictions are placed on productions of G • Type 1 If for all productions w1w2, the length of w1 length of w2 • Type 2 If for all productions the left hand side is a single non-terminal symbol and the right hand side is 1 or more symbols • Type 3 if for all productions the left hand side is a single non-terminal symbol and the right hand side is 1 or more symbols with at most 1 non terminal symbol. If it has a non terminal symbol, it appears on the extreme right
§10.1 – Languages • Programming language compilers are of type 2 or 3 • We will focus on these types • Type 3 grammars are called regular grammars
§10.1 – Languages • Parsing a sentence • Verifying a sentence is syntactically correct by finding a derivation tree (parse tree) • Compiling • Parse sentence into parse tree • Search parse tree, at each step converting the corresponding sentence into another language • Ultimately need machine language
CSCI 115 §10.2 Representations of Special Grammars & Languages
§10.2 – Representations of Special Grammars & Languages • Representations of languages • (as in section 10.1) • Backus-Naur form (BNF notations) • Syntax Diagrams
§10.2 – Representations of Special Grammars & Languages • BNF Notation • Place left hand side of production on left • Place all possible productions on the right, separated by: | • is replaced by ::= • Non terminal symbols are placed in < > • Used for clarification • Allows spaces
§10.2 – Representations of Special Grammars & Languages • Syntax Diagrams • Arrows – direction to travel to complete a substitution • Symbols in boxes – non terminal symbols • Symbols in circles – terminal symbols • Components of diagrams • Sequence, selection, iteration
§10.2 – Representations of Special Grammars & Languages • Syntax Diagrams • Sequence • w::= <w1><w2><w3> (BNF Notation) has the following syntax diagram:
§10.2 – Representations of Special Grammars & Languages • Syntax Diagrams • Selectionw ::= <w1> | a (BNF Notation) has the following syntax diagram
§10.2 – Representations of Special Grammars & Languages • Syntax Diagrams • Iterationw ::= a<w> | a (BNF Notation) has the following syntax diagram
§10.2 – Representations of Special Grammars & Languages • Master Diagrams • Combine the various syntax diagrams of a language into one comprehensive diagram • Involve only v0 and terminal symbols • Are usually too cumbersome to be useful
§10.2 – Representations of Special Grammars & Languages • There is the following correlation between regular expressions (section 1.3) and the components of a regular grammar • Discussion starts on page 399 of section 10.2 • We will not cover in this class
CSCI 115 §10.3 Finite State Machines
§10.3 – Finite State Machines • Machine • A machine is a system that accepts input, produces output, and has memory to keep track of previous inputs (which many be manipulated) • State • A state is the complete internal condition of the machine (i.e. the memory) • Affects reaction to subsequent I/O
§10.3 – Finite State Machines • Finite State Machine (FSM) • A machine that has a finite number of states • Example: computer
§10.3 – Finite State Machines • Assume we have the following: • S = {s0, s1, …, sn} is a set with n elements • A finite set I • xI, fx:SS. • Let F = {fx | x I} • Then (S, I, F) is a finite state machine
§10.3 – Finite State Machines • Given (S, I, F) is a FSM, we have the following: • S is the state set • si is a state si S • I is the input set • x I, fx describes the effect the input x has on the state of the machine • State transition function • If the machine is in state si and the input x occurs, the next state is fx(si) • State transition table – matrix showing the effects of the various inputs on the various states
§10.3 – Finite State Machines • Relation arising from finite state machines • If M is the FSM (S, I, F) then define RM as follows: • RM is a relation on S • si RMsjiff x I s.t. fx(si) = sj • Edges in the digraph of RM are labeled with all inputs that create that state transition • We will not be covering Moore Machines, Machine Congruences, or Quotient Machines