Chapter 2. Formal Languages Dept. of Computer Engineering, Hansung University, Sung-Dong Kim
Introduction Formal language —Well-defined/well-formed language —Set of sentences Formal definition of the languages Grammar —One of the language representation methods (2011-1) Compiler2
1. Languages (1) (2011-1) Compiler3 Alphabet Word / String Sentence / Statement Symbol Language
1. Languages (2) Definition 2.3: Length of string (length) —Example: w = a 1 a 2 a 3 …a k |w| = k (2011-1) Compiler4 # of symbols of the string: |w|
1. Languages (3) Definition 2.4: Concatenation —Example —Note –u v = uv (2011-1) Compiler5 Connection of the strings u = a 1 a 2 a 3 …a n, v = b 1 b 2 b 3 …b m u v = a 1 a 2 a 3 …a n b 1 b 2 b 3 …b m
1. Languages (4) Definition 2.5: Empty/Null string —Property —Others: reverse string (2011-1) Compiler6 String with length 0: , u = u = u u v = uv w = a 1 a 2 a 3 …a n w R = a n …a 2 a 1 a n = aa…a (n as) a 0 : empty string
1. Languages (5) Definition 2.6: T* —Property: universal set —T dagger: (2011-1) Compiler7 Set of all possible strings from T including empty string T + = T* - { } T = {0} T* = { , 0, 00, 000, … } T + = { 0, 00, 000, … } T = {a, b} T* = { , a, b, aa, ab, ba, bb, aaa, aab, … } T + = {a, b, aa, ab, ba, bb, aaa, aab, … } For u, v T*, uv T*
1. Languages (6) Definition 2.7: Language —Finite language —Infinite language Definition 2.8: Product of the languages (2011-1) Compiler8 Language L is a subset of T* LL’ = { uv | u L and v L’ }
1. Languages (7) Definition 2.9: Power of languages recursive definition Definition 2.10: Reflexive transitive closure (2011-1) Compiler9 L 0 = { } L n = LL n-1 for n 1
1. Languages (8) Definition 2.11: Transitive closure T* : set of all possible strings from the alphabet T Language: set of strings which satisfy some rules Sentence: string in the language (2011-1) Compiler10
2. Grammar (1) Language representation —Finite language: list all strings —Infinite language: finite representation Finite representation —Condition presentation: set representation —Grammar: language generation system —Recognizer (2011-1) Compiler11
2. Grammar (2) Grammar —V N : set of nonterminal symbol intermediate symbol —V T : set of terminal symbol alphabet —Grammar symbol = V N + V T = V (vocabulary) (2011-1) Compiler12
2. Grammar (3) Definition 2.12: Grammar —V N V T = , V N V T =V —P (production rule) —S (start/sentence symbol): start/sentence symbol, S V N (2011-1) Compiler13 G = ( V N, V T, P, S)
2. Grammar (4) Example Compact representation Convention —Nonterminal symbol: A, B, C, … —Terminal symbol: a, b, c, … —Starting symbol: S —V* string: , , , … —V T * string: (2011-1) Compiler14 G = ( {S, A}, {a, b}, P, S), P: S aASS a A SbAA baA SS
2. Grammar (5) Definition 2.13: Derivation — : direct derivation — : zero or more derivations — : one or more derivations (2011-1) Compiler15
2. Grammar (6) Definition 2.14: Sentential form — : sentence —Example 10: (2011-1) Compiler16
2. Grammar (7) Sentence: string —Consists of terminals —Derivable from the start symbol of the grammar G (2011-1) Compiler17
2. Grammar (8) Definition 2.15: Language L(G) —Example 11: (2011-1) Compiler18
2. Grammar (9) Relationship between grammar and language Language generation from grammar —Start symbol application of the generation rules sentence —Generated sentences fixed rules (2011-1) Compiler19 GrammarLanguage generation design
2. Grammar (10) Example 12: (2011-1) Compiler20
2. Grammar (11) Grammar of the programming languages (syntax) —Syntax: describe the language structure simply and definitely —Understanding of the grammar derive some strings from the start symbol —Appendix A.5: mini C’s syntax (2011-1) Compiler21
3. Types of Grammar (1) Grammar concept: Norm Chomsky Chomsky Hierarchy: —Type 0 grammar (unrestricted grammar, ug): —Type 1 grammar (context-sensitive grammar, csg): || || (2011-1) Compiler22
3. Types of Grammar (2) —Type 2 grammar (context-free grammar, cfg): A —Type 3 grammar (regular grammar, rg) –A tB or A t (A, B V N and t V T *): right-linear grammar –A Bt or A t (A, B V N and t V T *): left-linear grammar (2011-1) Compiler23
3. Types of Grammar (3) Definition 2.16: Language definition by grammar —Regular language —Context free language —Context sensitive language L Example 1 (2011-1) Compiler24
3. Types of Grammar (4) —Context-sensitive grammar —Derivation steps (2011-1) Compiler25
3. Types of Grammar (5) Example 2 —Context-free grammar —Derivation steps (2011-1) Compiler26
3. Types of Grammar (6) Regular grammar = left-linear + right-linear Example 3 —Regular grammar Derivation steps (2011-1) Compiler27
3. Types of Grammar (7) Chomsky hierarchy of languages (2011-1) Compiler28
3. Types of Grammar (8) Languages & Recognizers (2011-1) Compiler29