EKSPRESI REGULER
DEFINISI • Suatu cara untuk merepresentasikan bahasa regular [4] • Pola ( pattern) atau template untuk string dari suatu bahasa [3] • Cara lain untuk mendeskripsikan bahasa reguler [5] • Penyederhanaan dari bahasa-bahasa reguler[7]
NOTASI EKSPRESI REGULAR • Closure, terdiri dari – * (star closure) artinya suatu simbol jumlah kemunculannya adalah 0 s/d n kali – + ( plus closure) artinya suatu simbol jumlah kemunculannya adalah 1 s/d n kali • Concatenation , menggunakan simbol (biasanya bisa dihilangkan), misal a . b bisa ditulis ab
• Union atau gabungan, menggunakan simbol • Alternation, menggunakan simbol + atau |, artinya salah satu simbol bisa muncul
CONTOH EKSPRESI REGULER[2] Bahasa Reguler
Ekspresi Reguler
{a}
a
{b}
b
{ab}
ab
{a,b}= {a} υ {b}
a υ b
{a}*
a*
{a}+
a+
∅
∅
{ε}
{ε}
CONTOH EKSPRESI REGULER[2] •
•
• • • • • •
ER = 010* String yang dibangkitkan / muncul = 01, 010, 0100, 01000 (jumlah nol di ujung kanan bisa tidak muncul, bisa juga muncul berhingga kali) ER = ab*cc String yang dibangkitkan / muncul = abcc,abbcc, abbbcc, abbbbcc, acc (jumlah b bisa tidak muncul, bisa juga muncul berhingga kali) ER = a+d String yang dibangkitkan / muncul = ad, aad, aaad ER = a* b* atau bisa juga ditulis a* + b* ( /+ berarti atau) String yang dibangkitkan / muncul = ε, a, b, aa, bb, aaa, bbb ER = a b atau bisa juga ditulis a + b String yang dibangkitkan / muncul = a, b ER = (a b)* atau bisa juga ditulis (a+b)* String yang dibangkitkan / muncul = ε, a, b, aa, bb, aaa, bbb, ab, abb, aab, ba ER = 01* + 0 ? ER = a*d ?
Commutative & Associative Rules [1] Let L, M and N be regular expressions, which of the followings are correct?
– L + M = M + L – LM = ML
Commutative Rules
– (L + M) + N = L + (M + N) – (LM)N = L(MN)
Associative Rules
Distributive Rules [1] Which of the followings are correct?
– L(M + N) = LM + LN – L + (MN) = (L + M)(L + N) – (M + N)L = ML + NL – (MN) + L = (M + L)(N + L)
Left Distributive Rules
Right Distributive Rules
Identities [1] • What is the identity for union? ?+L=L+?=L
• What is the identity for concatenation? ?L = L? = L
Other Rules for Kleene Closure [1] • (L*)* = L* • L+ = LL* = L*L • L* = L+ +
• * = • * =
Applications of RE [1] Two common applications of RE: – Lexical analysis in compiler – Finding patterns in text
Lexical Analyzer [1] • Recognize “tokens” in a program source code. • The tokens can be variable names, reserved words, operators, numbers, … etc. • Each kind of token can be specified as an RE, e.g., a variable name is of the form [A-Za-z][A-Za-z0-9]*. We can then construct an -NFA to recognize it automatically. • By putting all these -NFA’s together, we obtain one that can recognize different kinds of tokens in the input string. • We can convert this -NFA to NFA and then to DFA, and implement this DFA as a deterministic program - the lexical analyzer.
Text Search
[1]
grep” in Unix stands for “Global (search for) Regular Expression and Print”. • Unix has its own notations for regular expressions: – Dot “.” stands for “any character”. – [a1a2…ak] stands for {a1, a2,…,ak}, e.g., [bcd12] stands for the set {b, c, d, 1, 2}. – [x-y] stands for all characters from x to y in the ASCII sequence. – | means “or”, i.e., + in our normal notation. – * means “Kleene star”, as in our normal notation. – ? means “zero or one”, e.g., R? is + R
•
“
Text Search [1] – + means “one or more”, e.g., R+ is RR* – {n} means “n copies of ”, e.g., R{5} is RRRRR (You can find out more by “man grep”, “man regex”)
• We can use these notations to search for string patterns in text. – For example, credit card numbers: [0-9]{16} | [0-9]{4}-[0-9]{4}-[0-9]{4}-[0-9]{4}
– For example, phone numbers: [0-9]{8} | [0-9]{3}-[0-9]{5} | 852-[0-9]{8} | 852-[0-9]{3}-[0-9]{5}
• MasterCard: ^5[1-5][0-9]{14}$ All MasterCard numbers start with the numbers 51 through 55. All have 16 digits. • American Express: ^3[47][0-9]{13}$ American Express card numbers start with 34 or 37 and have 15 digits.
STUDI KASUS
[3] & [4]
1. Dari Regular Expression berikut ini, string apa yang dapat dibangkitkan?
a. [0-9]{4}U[0-9]{3} b. K.[0-9]{3} | B.[0-9]{3} | C.[0-9]{3} c. [A-Z]{1}[a-z]{15} [A-Z]*[a-z]*
2. Buat Regular Expression untuk
a. Nomor Handphone b. Nomor kendaraan bermotor di Indonesia c. Alamat Rumah
3. Dari Expression Regular berikut ini, string apa yang dapat dibangkitkan?
a. 0 (1 U 0)* atau 0 (1 + 0)* b. 01*0 c. ab U c atau ab + c d. a (b U c) atau a(b + c) e. c*(a U bc)* atau c*(a + bc) f. a*b U (c U d) atau a*b + (c + d) g. (11+0)*(00+1)* h. 10+(0+11)0*1
STUDI KASUS
[3] & [4]
4. Buat Expression Regular yang menghasilkan semua string a, b, c, dan b, serta digit 0 dan 1 dimana : a. Stringnya dimulai dengan karakter b dan diakhiri dengan aba b. Stringnya dimulai dengan digit 0 dan setiap kemunculan digit 0 harus diikuti dengan dua atau lebih digit 101 c. Stringnya dimulai dengan karakter b dan setiap kemunculan karakter b harus diikuti dengan karakter a yang jumlahnya ganjil d. Stringnya dimulai dengan karakter a dan setiap kemunculan karakter a harus diikuti dengan string aba yang jumlahnya genap e. Stringnya harus dimulai dengan digit 01 yang jumlahnya genap dan setiap kemunculan digit 01 diikuti karakter baca dan diakhiri oleh digit 10 yang jumlahnya ganjil
REFERANSI 1. 2. 3. 4.
http://www.cse.cuhk.edu.hk/~csc3130/ http://evidianti.ppkia.ac.id/wpcontent/uploads/2008/05/part5-tbo.ppt, Tanggal Akses : 13 Februari 2009 15:38 Swinglly Purba, “Otomata dan Bahasa Formal”, Graha Ilmu,Yogyakarta, 2008 Firrar Utdirartatmo, “Teori Bahasa dan Otomata”,
Graha Ilmu, Yogyakarta, 2005 5.
Roni Djuliawan, M.T., “Diktat & Handout Kuliah Teori Bahasa & Otomata”, Teknik Informatika –
Universitas Widyatama, 2003