90 likes | 108 Views
Explore formal language concepts like syntax, semantics, and phrase-structure grammar. Understand types of grammars and finite-state machines with practical examples and exercises.
E N D
CMSC 203 / 0201Fall 2002 Week #14 – 25/27 November 2002 Prof. Marie desJardins clip art courtesy of www.dumpty.com
Concepts/Vocabulary • Formal language, syntax, semantics • Vocabulary (alphabet) V, word (sentence) V*, language V* • Phrase-structure grammar G=(V,T,S,P) • Alphabet V; terminal symbols TV; nonterminal elements N=V-T; start symbol SN; productions P: {xy: x, y V*} • Derivation * (sequence of productions) • Derivation tree / parse tree • L(G): {wT*: S* w}
Concepts/Vocabulary cont. • Types of grammars: • Type 0: no restrictions • Type 1 (context-sensitive): productions must be w1 or w1w2 where w2 has length w1 • Type 2 (context-free): All productions must have w1N (single symbol) • Type 3 (regular): All productions must have w1N and w2N or w2=aB where B N • (Top-down parsing, bottom-up parsing) • (Backus-Naur form)
Examples • Find a phrase-structure grammar for each of the following languages: • (a) the set of all bit strings containing an even number of 0s and no 1s • (e) the set of all strings containing more 0s than 1s • (g) the set of all strings containing an unequal number of 0s and 1s • Show a derivation tree for the string 00110010 from the grammar given in (g)
Examples II • Exercise 23(a): Construct a phrase-structure grammar that generates all signed decimal numbers, consisting of a sign, either + or -; a nonnegative integer; and a decimal fraction that is either the empty string or a decimal point followed by a positive integer, where initial zeros in an integer are allowed. • Consider the 4 PSGs we’ve constructed. What type is each?
Concepts/Vocabulary • Finite-state machine M=(S,I,O,f,g,s0): • States S, input alphabet I, output alphabet O, transition function f: SIS, output function g SIg, initial state s0S • State table, state diagram • (Mealy machines, Moore machines)
Examples • Vending machine model (Table 1) • Draw a state diagram for the vending machine • Draw a state diagram to recognize the signed decimal integer grammar from Exercise 23(a) • Exercise 10.3.7: Construct a FSM that delays an input string two bits, giving 00 as the first two bits of output.