SlideShare a Scribd company logo
AUTOMATA AND
COMPILER
‫محور‬‫االوتومات‬‫و‬‫المترجمات‬
By Eng. Joud Khattab
Content
■ Overview.
■ Automate.
■ Regular Expression.
■ Context Free Grammars.
■ Compiler.
■ MCQ.
2
AUTOMATA
3
Automata
■‫عن‬ ‫أمثلة‬‫األوتومات‬:
–‫القهوة‬ ‫بيع‬ ‫آلة‬ ‫في‬ ‫الموجود‬ ‫الحاسب‬.
–‫اآللي‬ ‫الصراف‬ ‫في‬ ‫الموجود‬ ‫الحاسب‬ATM.
■‫ع‬ ‫لالستعالم‬ ‫المفاتيح‬ ‫لوحة‬ ‫نستخدم‬ ‫ثم‬ ‫ومن‬ ً‫أوال‬ ‫البطاقة‬ ‫نضع‬ ‫اآللي‬ ‫الصراف‬ ‫في‬‫ن‬
‫أو‬ ‫معين‬ ‫مبلغ‬ ‫لسحب‬ ‫أو‬ ‫الرصيد‬...‫إلخ‬.
■‫تسلسالت‬ ‫في‬ ‫اختالف‬ ‫يوجد‬ ‫ولكن‬ ‫معين‬ ‫بتسلسل‬ ‫األحداث‬ ‫من‬ ‫عدد‬ ‫لدينا‬ ‫يكون‬ ‫أي‬
‫عل‬ ‫أو‬ ‫المعلومات‬ ‫على‬ ‫يتم‬ ‫الذي‬ ‫هذا‬ ‫األحداث‬ ‫وتسلسل‬ ‫ثابت‬ ‫غير‬ ‫فالتسلسل‬ ‫األحداث‬‫ى‬
‫عليها‬ ‫يتعرف‬ ‫التي‬ ‫واللغة‬ ‫الحسابات‬ ‫هو‬ ‫المعلومات‬ ‫حركة‬‫األتومات‬،‫النظام‬ ‫بهذا‬ ‫الخاص‬
■‫ادخال‬ ‫بعد‬ ً‫فمثال‬ ‫مرة‬ ‫كل‬ ‫في‬ ‫التسلسل‬ ‫بنفس‬ ‫السير‬ ‫الضروري‬ ‫من‬ ‫ليس‬ ‫بالتالي‬‫البطاقة‬
‫الت‬ ‫حساب‬ ‫اختيار‬ ‫ممكن‬ ‫بل‬ ‫الجاري‬ ‫الحساب‬ ‫اختيار‬ ً‫دائما‬ ‫المفروض‬ ‫من‬ ‫ليس‬ ‫للصراف‬‫وفير‬
‫مختلف‬ ‫بفرعين‬ ‫الذهاب‬ ‫يمكن‬ ‫وبالتالي‬ ‫لنا‬ ‫بالنسبة‬ ‫مقبولتان‬ ‫العمليتين‬ ‫فكال‬‫في‬ ‫ين‬
‫األتومات‬.
4
Automata
■‫الرياضي‬ ‫التعريف‬‫لألوتومات‬:
■ M = ( Q , Σ , δ , q0 , F) were
– Q = { q0 , q1 , q2 , q3 }
– Σ = { 0 , 1 }
– q0 = q0 initial state
– F = { q0 }
■‫االصفار‬ ‫عدد‬ ‫فيها‬ ‫يكون‬ ‫التي‬ ‫السالسل‬ ‫جميع‬ ‫يقبل‬ ‫المثال‬‫والواحدات‬‫زوجي‬.
5
Different Kinds of Automata (Chomsky)
(‫القواعد‬ ‫لتوصيف‬ ‫وهرميتها‬ ‫اللغات‬ ‫أنواع‬)
1. Regular Languages:
– Definition Tool: Finite Automata ( ‫األوتومات‬‫المنتهي‬ )
– Memory Type: no temporary memory
– Ex: ATM, Search.
2. Context-Free Languages:
– Definition Tool: Push down Automata (PDA) ( ‫األوتومات‬‫المكدس‬ ‫ذات‬ )
– Memory Type: stack
– Ex: Syntax recognition of a specific language, Identify the conditional (IF)
3. Recursive Languages:
– Definition Tool: Turing Machines
– Memory Type: random access memory (RAM)
– Ex: Recognize all languages regardless their form
6
Finite Automata
■‫أنواع‬‫االوتومات‬‫المنتهي‬:Finite Automata
–‫االوتومات‬‫الحتمي‬ ‫المنتهي‬( :Deterministic finite automata DFA)
■‫هذا‬ ‫يحقق‬ ‫حيث‬‫االوتومات‬‫الى‬ ‫يكون‬ ‫معين‬ ‫رمز‬ ‫اجل‬ ‫ومن‬ ‫وحيدة‬ ‫حالة‬ ‫من‬ ‫االنتقال‬ ‫ان‬
‫محددة‬ ‫وحيدة‬ ‫حالة‬.
–‫االوتومات‬‫المنتهي‬‫الالحتمي‬( :Non deterministic finite automata NFA)
■‫شرط‬ ‫يحقق‬ ‫ال‬ ‫الذي‬ ‫وهو‬‫االوتومات‬‫السابق‬.
■‫الرمز‬ ‫بنفس‬ ‫حالة‬ ‫من‬ ‫اكثر‬ ‫الى‬ ‫االنتقال‬ ‫نستطيع‬ ‫أي‬.
7
Finite Automata
■‫مالحظات‬:
–‫اجل‬ ‫من‬‫اوتومات‬‫نوع‬ ‫من‬DFA‫ما‬ ‫لسلسلة‬ ‫القبول‬ ‫خوارزمية‬ ‫بناء‬ ‫يمكن‬.
■‫تنتمي‬ ‫ما‬ ‫سلسلة‬ ‫كانت‬ ‫ما‬ ‫اذا‬ ‫معرفة‬ ‫خاللها‬ ‫من‬ ‫نستطيع‬ ‫التي‬ ‫الخوارزمية‬ ‫أي‬‫لالوتو‬‫مات‬
‫ال‬ ‫ام‬.
–‫حالة‬ ‫في‬ ‫اما‬NFA‫قبول‬ ‫خوارزمية‬ ‫بناء‬ ‫يمكن‬ ‫فال‬.
■‫جدا‬ ‫مكلفة‬ ‫ولكنها‬ ‫تراجعية‬ ‫قبول‬ ‫خوارزمية‬ ‫بناء‬ ‫يمكن‬.
–‫على‬ ‫يبرهن‬NFA‫ل‬ ‫بتحويلها‬DFA‫لها‬ ‫مكافئ‬.
–‫لكل‬NFA‫هنالك‬DFA‫له‬ ‫مكافئ‬.
8
Finite Automata
From NFA To DFA
9
Finite Automata
From NFA To DFA
10
Finite Automata
From NFA To DFA
11
Finite Automata
ε-NFA
■‫االوتومات‬‫الالحتمي‬‫االنتقال‬ ‫مع‬‫ابسيلون‬
–‫مثال‬:
–‫هي‬ ‫االبجدية‬ ‫ان‬ ‫نعتبر‬:
■0 1 2 ε
12
Question 1
■‫أعط‬‫األوتومات‬‫المكافئ‬ ‫الحتمي‬ ‫المنتهي‬‫لألوتومات‬‫المنتهي‬‫الالحتمي‬‫التالي‬:
13
Question 1
■‫الجواب‬:
14
REGULAR LANGUAGE
‫المنتظمة‬ ‫اللغات‬
15
Regular Language
■‫النظامي‬ ‫التعبير‬:
–‫لتمثيل‬ ‫مختصرة‬ ‫مبسطة‬ ‫طريقة‬ ‫عن‬ ‫عبارة‬ ‫هو‬‫االوتومات‬‫معينة‬ ‫لغة‬ ‫يوصف‬ ‫الذي‬
‫من‬ ‫مجموعة‬ ‫على‬ ‫ويعتمد‬‫التراميز‬.
■‫المنتظمة‬ ‫اللغة‬:
–‫ب‬ ‫تمثيلها‬ ‫يمكن‬ ‫لغة‬ ‫كل‬ ‫هي‬RE‫ب‬ ‫ممثلة‬ ‫تكون‬ ‫عمليا‬ ‫أي‬DFA.
16
Regular Language
■‫امثلة‬:
–‫االبجدية‬ ‫لدينا‬ ‫كانت‬ ‫اذا‬σ = {𝑎}‫النظامية‬ ‫التعابير‬ ‫من‬ ‫العديد‬ ‫بناء‬ ‫نستطيع‬:
■a:‫واحد‬ ‫حرف‬ ‫من‬ ‫المؤلفة‬ ‫السلسلة‬ ‫يكافئ‬.
■ε:‫الخالية‬ ‫السلسلة‬ ‫يكافئ‬.
■a*:‫تكرار‬ ‫من‬ ‫المؤلفة‬ ‫السلسلة‬ ‫او‬ ‫الخالية‬ ‫السلسلة‬ ‫يكافئ‬a‫أو‬ ‫أكبر‬ ‫المرات‬ ‫من‬ ‫عدد‬
‫الواحد‬ ‫يساوي‬.
■a+:‫تكرار‬ ‫من‬ ‫المؤلفة‬ ‫السلسلة‬ ‫يكافئ‬a‫الواحد‬ ‫يساوي‬ ‫أو‬ ‫أكبر‬ ‫المرات‬ ‫من‬ ‫عدد‬.
■an:‫تكرار‬ ‫من‬ ‫المؤلفة‬ ‫السلسلة‬ ‫يكافئ‬a(n)‫فقط‬ ‫مرة‬.
–‫األبجدية‬ ‫لدينا‬ ‫كانت‬ ‫اذا‬σ = {𝑎,𝑏}‫المنتظمة‬ ‫التعابير‬ ‫من‬ ‫العديد‬ ‫بناء‬ ‫نستطيع‬:
■a*b*:‫عملية‬ ‫عن‬ ‫الناتجة‬ ‫السلسلة‬ ‫يكافئ‬concatenation‫أي‬).(‫نظاميين‬ ‫تعبيرين‬ ‫بين‬.
17
Regular Language
■ L1 = { a*b* : a,b ∈ σ }
– L1 is regular language.
■ L2 = { anbn : a,b ∈ σ , n>=0}
– L2 is not regular language.
■‫اللغة‬ ‫ان‬ ‫نالحظ‬L2‫ير‬ ‫أن‬ ‫يجب‬ ‫مقبولة‬ ‫السلسة‬ ‫تكون‬ ‫حتى‬ ‫ألنه‬ ‫وذلك‬ ،‫ذاكرة‬ ‫الى‬ ‫تحتاج‬‫ى‬
b‫ورود‬ ‫مرات‬ ‫عدد‬ ‫بنفس‬a.
18
Regular Language
■ L1, L2: Regular Languages then:
– L = L1 . L2 = { X.W were X ∈ L1 , W ∈ L2 } is Regular Language
– L1 ∪ L2 is Regular Language
– L1 , L2 is Regular Language
– L1 ∩ L2 is Regular Language
19
Regular Expressions
Exercise 1
■ Suppose the only characters are 0 and 1.
■ A regular expression for strings containing 00 as a substring:
(0 | 1)* 00 (0 | 1)*
11011100101
0000
11111011110011111
20
Regular Expressions
Exercise 2
■ Suppose the only characters are 0 and 1.
■ A regular expression for strings of length exactly four:
(0|1)(0|1)(0|1)(0|1)
(0|1){4}
0000
1010
1111
1000
21
Regular Expressions
Exercise 3
■ Suppose the only characters are 0 and 1.
■ A regular expression for strings that contain at most one zero:
1*(0 | ε)1*
1*0?1*
11110111
111111
0111
0
22
Regular Expressions
Exercise 4
■ Suppose that our alphabet is all ASCII characters.
■ A regular expression for even numbers:
(+|-)?(0|1|2|3|4|5|6|7|8|9)*(0|2|4|6|8)
(+|-)?[0123456789]*[02468]
(+|-)?[0-9]*[02468]
42
+1370
-3248
-9999912
23
Regular Language & Automata
■‫التعبير‬(ab)‫مقابلته‬ ‫يمكن‬‫باالوتومات‬:
■‫التعبير‬(a + b)‫مقابلته‬ ‫يمكن‬‫باالوتومات‬:
24
Regular Language & Automata
■‫التعبير‬(a*)‫مقابلته‬ ‫يمكن‬‫باالوتومات‬:
■‫التعبير‬(a+)‫مقابلته‬ ‫يمكن‬‫باالوتومات‬:
25
Regular Language & Automata
■‫التعبير‬(θ)‫طريق‬ ‫وجود‬ ‫عدم‬ ‫عن‬ ‫تعبر‬(‫فارغة‬ ‫مجموعة‬ ‫هي‬ ‫الحلول‬ ‫مجموعة‬ ‫أي‬):
■‫التعبير‬(ε)‫رموز‬ ‫من‬ ‫رمز‬ ‫أي‬ ‫لوجود‬ ‫الحاجة‬ ‫دون‬ ‫عبره‬ ‫ننتقل‬ ‫مفتوح‬ ‫طريق‬ ‫وجود‬ ‫عن‬ ‫تعبر‬
‫االبجدية‬:
26
Regular Language & Automata
Example numbers
■ Draw a DFA equivalent to the following regular expression:
1) digit = [0-9]
2) nat = digit+
3) signedNat = (+|-)? nat
4) number = signedNat("." nat)? (E signedNat)?
27
nat = digit+
Regular Language & Automata
Example numbers
28
signedNat = (+|-)? nat
Regular Language & Automata
Example numbers
29
signedNat ("." nat)?
Regular Language & Automata
Example numbers
30
number = signedNat ("." nat)? (E signedNat)?
Regular Language & Automata
Example numbers
31
Question 1
■‫التي‬ ‫اللغات‬ ‫هي‬ ‫ما‬‫توصفها‬‫األبجدية‬ ‫على‬ ‫المعرفة‬ ‫التالية‬ ‫المنتظمة‬ ‫التعابير‬{a,b}:
–a (a + b)* b
–aab (aa|bb)*
–(aa)* a
32
Question 1
■‫التي‬ ‫اللغات‬ ‫هي‬ ‫ما‬‫توصفها‬‫األبجدية‬ ‫على‬ ‫المعرفة‬ ‫التالية‬ ‫المنتظمة‬ ‫التعابير‬{a,b}:
–a (a + b)* b
–aab (aa|bb)*
–(aa)* a
■‫الجواب‬:
–‫بالحرف‬ ‫كلماتها‬ ‫جميع‬ ‫تبدأ‬ ‫التي‬ ‫اللغة‬a‫بالحرف‬ ‫وتنتهي‬b
–‫بالسلسلة‬ ‫كلماتها‬ ‫جميع‬ ‫تبدأ‬ ‫التي‬ ‫اللغة‬aab‫األقل‬ ‫على‬ ‫واحد‬ ‫تكرار‬ ‫يليها‬ ‫ثم‬ ‫ومن‬
‫لثنائية‬aa‫من‬ ‫أو‬bb
–‫حرف‬ ‫على‬ ‫إال‬ ‫كلماتها‬ ‫تحتوي‬ ‫ال‬ ‫التي‬ ‫اللغة‬a‫مفردا‬ ‫كلماتها‬ ‫طول‬ ‫ويكون‬
33
Question 2
■‫التالية‬ ‫اللغات‬ ‫تعرف‬ ‫أن‬ ‫يمكن‬ ‫التي‬ ‫المنتظمة‬ ‫التعابير‬ ‫هي‬ ‫ما‬:
–‫األبجدية‬ ‫على‬ ‫اللغة‬{a,b,c}‫بالحرف‬ ‫كلماتها‬ ‫جميع‬ ‫تبدأ‬ ‫والتي‬a
–‫مضاعفات‬ ‫من‬ ‫الصحيحة‬ ‫األعداد‬5
34
Question 2
■‫التالية‬ ‫اللغات‬ ‫تعرف‬ ‫أن‬ ‫يمكن‬ ‫التي‬ ‫المنتظمة‬ ‫التعابير‬ ‫هي‬ ‫ما‬:
–‫األبجدية‬ ‫على‬ ‫اللغة‬{a,b,c}‫بالحرف‬ ‫كلماتها‬ ‫جميع‬ ‫تبدأ‬ ‫والتي‬a
–‫مضاعفات‬ ‫من‬ ‫الصحيحة‬ ‫األعداد‬5
■‫الجواب‬:
–a (a + b + c)*
–(0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9)* 5
35
CONTEXT FREE
GRAMMARS
‫السياق‬ ‫عديمة‬ ‫اللغات‬(‫السياق‬ ‫خارج‬)
36
Context Free Grammars
(‫السياق‬ ‫عديمة‬ ‫)اللغات‬
■‫القواعدي‬ ‫النموذج‬ ‫مفهوم‬:
–‫ال‬ ‫مفهوم‬ ‫من‬ ‫تطورا‬ ‫أكثر‬ ‫مفهوم‬ ‫هو‬DFA‫هو‬ ‫اللغات‬ ‫من‬ ‫بسيط‬ ‫نمط‬ ‫عن‬ ‫يعبر‬ ‫الذي‬
‫النمط‬ ‫هذا‬ ‫تعدي‬ ‫يستطيع‬ ‫وال‬ ‫المنتظمة‬ ‫اللغات‬.
–‫ال‬ ‫يعد‬ ‫وبالتالي‬DFA‫قاصرا‬‫تحمل‬ ‫التي‬ ‫اللغات‬ ‫تمثيل‬ ‫عن‬‫ذاكرة‬‫وبالتالي‬‫نلجا‬‫الى‬
‫اللغات‬ ‫من‬ ‫النوع‬ ‫هذا‬ ‫لتمثيل‬ ‫القواعدي‬ ‫النموذج‬.
■‫من‬ ‫جديد‬ ‫نوع‬ ‫هو‬ ‫المنتظمة‬ ‫اللغات‬ ‫اذا‬‫االوتومات‬‫لغ‬ ‫تمثيل‬ ‫يستطيع‬ ‫بذاكرة‬ ‫مزود‬ ‫وهو‬ ،‫ات‬
‫النمط‬ ‫من‬ ‫السالسل‬ ‫مثل‬ ‫ذاكرة‬ ‫الى‬ ‫تحتاج‬(anbn)‫هذا‬ ‫ان‬ ‫حيث‬‫االوتومات‬‫اعداد‬ ‫يتذكر‬
‫الرموز‬(a,b)‫عليه‬ ‫مرت‬ ‫التي‬.
37
Context Free Grammars
(‫السياق‬ ‫عديمة‬ ‫)اللغات‬
■ Non Terminal Symbols (V):
–‫السالسل‬ ‫توليد‬ ‫عملية‬ ‫على‬ ‫تساعد‬ ‫التي‬ ‫البسيطة‬ ‫الحروف‬ ‫من‬ ‫مجموعة‬ ‫تمثل‬(‫أي‬
‫المساعدة‬ ‫التوابع‬ ‫من‬ ‫مجموعة‬.)
■ Terminal Symbols (T):
–‫اللغة‬ ‫سالسل‬ ‫منها‬ ‫تتألف‬ ‫التي‬ ‫الحروف‬ ‫من‬ ‫تتكون‬ ‫التي‬ ‫االبجدية‬ ‫تؤلف‬.
■ Production (P):
–‫اللغة‬ ‫توليد‬ ‫قواعد‬ ‫هو‬.
■ Starting Symbol (S):
–‫اللغة‬ ‫توليد‬ ‫عملية‬ ‫في‬ ‫منه‬ ‫تبدأ‬ ‫الذي‬ ‫البدائي‬ ‫الرمز‬.
38
Context Free Grammars
Example 1
■‫للغة‬ ‫القواعدي‬ ‫النموذج‬ ‫اوجد‬:
– L = { anbn : a,b ∈ σ , n>=0}
■‫الحل‬:
– L = {V , T , S , P} where
■ S → a S b (1)
■ S → a b (2)
– V = {S}
– S = {S}
– T = {a,b}
39
Context Free Grammars
Example 2
■‫اللغة‬ ‫لدينا‬ ‫ليكن‬L‫بالشكل‬ ‫اللغة‬ ‫هذه‬ ‫تعريف‬ ‫نستطيع‬ ،‫الحسابية‬ ‫للتعابير‬ ‫الممثلة‬
‫التالي‬ ‫القواعدي‬:
– Exp → Exp Op Exp | - ( Exp ) | id
– Op → + | - | * | /
■‫الحل‬:
– V = {Exp, Op}
– T = { + , - , / , id , ( , ) }
– S = {Exp}
40
Context Free Grammars
Left Most Derivation
■ Exp → Exp Op Exp | - ( Exp ) | id
■ Op → + | - | * | /
■‫التالية‬ ‫السلسلة‬ ‫نشتق‬ ‫كيف‬:id + id * id
–‫ال‬ ‫نعوض‬(None Terminal Symbol)‫وهكذا‬ ‫أوال‬ ‫اليسار‬ ‫اقصى‬ ‫في‬ ‫الموجود‬..
–‫التالية‬ ‫المراحل‬ ‫عبر‬ ‫ذلك‬ ‫يتم‬:
■ Exp → Exp Op Exp
→ id Op Exp
→ id + Exp
→ id + Exp Op Exp
→ id + id Op Exp
→ id + id * Exp
→ id + id * id
41
Context Free Grammars
Right Most Derivation
■ Exp → Exp Op Exp | - ( Exp ) | id
■ Op → + | - | * | /
■‫ال‬ ‫تعويض‬ ‫على‬ ‫مبدؤها‬ ‫ينطوي‬ ‫االشتقاق‬ ‫في‬ ‫أخرى‬ ‫خوارزمية‬ ‫هنالك‬(None Terminal
Symbol)‫وهكذا‬ ‫أوال‬ ‫اليمين‬ ‫اقصى‬ ‫في‬ ‫الموجود‬...
■ Exp → Exp Op Exp
→ Exp Op id
→ Exp * id
→ Exp Op Exp * id
→ Exp Op id * id
→ Exp + id * id
→ id + id * id
42
Context Free Grammars
Ambiguity
■‫غامضة‬ ‫لغة‬ ‫هي‬ ‫السابق‬ ‫المثال‬ ‫في‬ ‫اللغة‬ ‫ان‬ ‫على‬ ‫نالحظ‬
–‫نفس‬ ‫الشتقاق‬ ‫طريقتين‬ ‫هنالك‬ ‫االشتقاق‬ ‫خوارزمية‬ ‫نفس‬ ‫أجل‬ ‫من‬ ‫ألنه‬ ‫وذلك‬
‫قابلية‬ ‫مع‬ ‫يتناسب‬ ‫ال‬ ‫الغموض‬ ‫وهذا‬ ‫السلسلة‬‫االوتومات‬‫البرمجي‬ ‫لإلسقاط‬.
43
COMPILER
‫المترجم‬
44
Compiler Definition
■‫المترجم‬ ‫تعريف‬:
–‫المترجم‬ ‫ندعوها‬ ،‫البرمجة‬ ‫عملية‬ ‫في‬ ‫جدا‬ ‫ضرورية‬ ‫أداة‬ ‫مبرمج‬ ‫أي‬ ‫يستخدم‬.
–‫المس‬ ‫عالية‬ ‫برمجية‬ ‫بلغة‬ ‫نكتبه‬ ‫الذي‬ ‫البرمجي‬ ‫النص‬ ‫يترجم‬ ‫حاسوبي‬ ‫برنامج‬‫توى‬
(C, Pascal, C++, C#, Java, …)‫قبل‬ ‫من‬ ‫للتنفيذ‬ ‫قابلة‬ ‫تعليمات‬ ‫مجموعة‬ ‫الى‬
‫الحاسوب‬.
–‫كانت‬ ‫سواء‬ ‫المستوى‬ ‫منخفضة‬ ‫بلغة‬ ‫مكتوبة‬ ‫التنفيذية‬ ‫التعليمات‬ ‫هذه‬ ‫تكون‬‫ثنائية‬ ‫لغة‬
‫و‬ ‫اصفار‬ ‫من‬ ‫مؤلفة‬‫واحدات‬‫تجميع‬ ‫لغة‬ ‫او‬.
–‫المستو‬ ‫عالية‬ ‫أخرى‬ ‫لغة‬ ‫الى‬ ‫المستوى‬ ‫عالية‬ ‫لغة‬ ‫من‬ ‫مترجمات‬ ‫بناء‬ ‫أيضا‬ ‫يمكن‬‫ى‬.
(‫البرمجية‬ ‫البيئة‬ ‫تغيير‬)
■‫المصدري‬ ‫البرنامج‬ ‫تعريف‬(Source Program:)
–‫حاسوبي‬ ‫برنامج‬ ‫تؤلف‬ ‫التي‬ ‫البرمجية‬ ‫النصوص‬ ‫مجموعة‬.
45
Compiler Definition
■‫واحد‬ ‫بأن‬ ‫اثنين‬ ‫بعنصرين‬ ‫تتعلق‬ ‫المترجم‬ ‫بناء‬ ‫عملية‬ ‫إن‬:
–‫المبرمج‬ ‫يستخدمها‬ ‫الذي‬ ‫المستوى‬ ‫عالية‬ ‫المصدرية‬ ‫البرمجة‬ ‫لغة‬.
–‫عليه‬ ‫البرنامج‬ ‫تشغيل‬ ‫سيجري‬ ‫الذي‬ ‫التشغيل‬ ‫نظام‬.
■‫مثال‬:
–‫لغة‬ ‫مترجم‬ ‫يختلف‬C++‫نظام‬ ‫على‬ ‫يعمل‬ ‫الذي‬windows‫لغة‬ ‫مترجم‬ ‫عن‬C++‫الذي‬
‫نظام‬ ‫على‬ ‫يعمل‬Linux‫ف‬ ‫مختلفة‬ ‫تنفيذية‬ ‫تشغيل‬ ‫تعليمات‬ ‫توليد‬ ‫لضرورة‬ ‫نظرا‬ ،‫ي‬
‫اللغة‬ ‫نفس‬ ‫عن‬ ‫نتكلم‬ ‫اننا‬ ‫من‬ ‫بالرغم‬ ،‫الحالتين‬ ‫هاتين‬.
–‫لغة‬ ‫مترجم‬ ‫يختلف‬C++‫لغة‬ ‫مترجم‬ ‫عن‬Pascal‫يعمالن‬ ‫المترجمان‬ ‫كان‬ ‫ولو‬ ‫حتى‬
‫نظام‬ ‫على‬Windows‫مختلفتين‬ ‫برمجيتين‬ ‫لغتين‬ ‫نترجم‬ ‫اننا‬ ‫نظرا‬ ،.
46
Compiler Structure
47
Compiler Structure
■‫أساسيتين‬ ‫مرحلتين‬ ‫من‬ ‫الترجمة‬ ‫عملية‬ ‫تتألف‬:
.1‫التحليل‬ ‫مرحلة‬(Analysis Phase:)
■‫ودالال‬ ‫صحتها‬ ‫من‬ ‫والتأكد‬ ‫وجمل‬ ‫كلمات‬ ‫الى‬ ‫البرمجي‬ ‫النص‬ ‫تقسيم‬ ‫فيها‬ ‫يجري‬ ‫التي‬‫تها‬.
.2‫التركيب‬ ‫مرحلة‬(Synthesis Phase:)
■‫بلغة‬ ‫ولكن‬ ‫المصدري‬ ‫النص‬ ‫داللة‬ ‫بنفس‬ ‫جديد‬ ‫برمجي‬ ‫نص‬ ‫تركيب‬ ‫فيها‬ ‫يجري‬ ‫التي‬‫أخرى‬
‫التشغيل‬ ‫نظام‬ ‫يفهمها‬.
48
Compiler Structure
Analysis Phase (‫التحليل‬ ‫)مرحلة‬
49
Compiler Structure
Analysis Phase (‫التحليل‬ ‫)مرحلة‬
■‫خطوات‬ ‫ثالث‬ ‫تحوي‬ ‫التحليل‬ ‫مرحلة‬:
.1‫المعجمي‬ ‫التحليل‬ ‫مرحلة‬(Lexical Analysis - Scanner:)
■‫الدخل‬ ‫يقرأ‬ ‫أن‬ ‫مهمته‬input‫الى‬ ‫ويحللها‬tokens‫من‬ ‫محدد‬ ‫جزء‬ ‫تمثل‬ ‫الكلمات‬ ‫هذه‬ ‫من‬ ‫وكل‬
‫اللغة‬ ‫في‬ ‫الثابتة‬ ‫الكلمات‬ ‫من‬ ‫ام‬ ‫متغير‬ ‫أكان‬ ‫سواء‬ ‫اللغة‬(reserved word)‫بمسح‬ ً‫ايضا‬ ‫ويقوم‬
‫الـ‬ ‫وحفظ‬ ‫المسافات‬tokens‫الرموز‬ ‫جدول‬ ‫في‬(symboltable.)
.2‫الجملة‬ ‫بناء‬ ‫مرحلة‬(Syntax Analysis - Parser:)
■‫الـ‬ ‫يأخذ‬ ‫ان‬ ‫مهمته‬tokens‫مرحلة‬ ‫عن‬ ‫الناتجة‬(Lexical Analysis)‫برمجية‬ ‫جمل‬ ‫في‬ ‫ويكونها‬
‫اللغة‬ ‫قواعد‬ ‫أساس‬ ‫على‬ ‫صحتها‬ ‫ويختبر‬.
■‫يدعى‬ ‫داللي‬ ‫هيكل‬ ‫عنها‬ ‫ينتج‬ ‫المرحلة‬ ‫هذه‬(semantic structure)‫البرمجية‬ ‫الجمل‬ ‫وتكون‬
‫ب‬ ‫يسمى‬ ‫شجري‬ ‫شكل‬ ‫على‬(parser tree.)
.3‫الداللي‬ ‫التحليل‬ ‫مرحلة‬(Semantic Analysis – Intermediate Code Generator:)
■‫لن‬ ‫القيمة‬ ‫اسناد‬ ‫صحة‬ ‫مثل‬ ،‫بالمنطق‬ ‫المتعلقة‬ ‫األخطاء‬ ‫من‬ ‫التحقق‬ ‫يتم‬ ‫المرحلة‬ ‫هذه‬ ‫في‬‫وع‬
‫وغيرها‬ ‫المتغير‬.
50
Compiler Structure
Analysis Phase (‫التحليل‬ ‫)مرحلة‬
■‫ال‬ ‫عمل‬ ‫مراحل‬compiler:
–‫اللفظي‬ ‫التحليل‬:
■‫ال‬ ‫ام‬ ‫البرمجة‬ ‫لغة‬ ‫في‬ ‫موجودة‬ ‫الكلمة‬ ‫كانت‬ ‫ما‬ ‫اذا‬ ‫معرفة‬ ‫أي‬.
–‫القواعدي‬ ‫التحليل‬:
■‫ال‬ ‫على‬ ‫التعرف‬ ‫أي‬syntax‫المحكية‬ ‫اللغات‬ ‫في‬ ‫كما‬ ‫اللغة‬ ‫كتابة‬ ‫قواعد‬ ‫أي‬ ،.
■‫مثال‬:
–‫يأتي‬ ‫ان‬ ‫يجب‬ ‫العربية‬ ‫اللغة‬ ‫في‬(‫فعل‬–‫فاعل‬–‫به‬ ‫مفعول‬.)
–‫البرمجة‬ ‫لغات‬ ‫في‬:if ( statement )
–‫الداللي‬ ‫التحليل‬:
■‫دالليا‬ ‫ليس‬ ‫لكن‬ ‫و‬ ‫قواعديا‬ ‫صحيحة‬ ‫تكون‬ ‫ان‬ ‫الممكن‬ ‫من‬ ‫الجملة‬ ‫ان‬ ‫اي‬.
■‫مثال‬:
–‫التفاحة‬ ‫الولد‬ ‫اكل‬.
–‫المقعد‬ ‫الولد‬ ‫اكل‬.
51
Natural Language Translation
52
Compiler Structure
Synthesis Phase (‫التركيب‬ ‫)مرحلة‬
■‫المتوسطة‬ ‫اللغة‬ ‫تحويل‬ ‫يتم‬ ‫المرحلة‬ ‫هذه‬ ‫في‬(IntermediateLanguage)‫تفهمها‬ ‫لغة‬ ‫الى‬
‫اآللة‬(Machine Language)‫التالي‬ ‫النحو‬ ‫على‬ ‫ذلك‬ ‫ويتم‬:
.1‫األكواد‬ ‫تحسين‬ ‫مرحلة‬(Code Optimization:)
■‫بأن‬ ‫والتأكد‬ ‫البرنامج‬ ‫وتطوير‬ ‫التكرار‬ ‫وابعاد‬ ‫الكود‬ ‫تحسين‬ ‫مسألة‬ ‫تتولى‬ ‫الخطوة‬ ‫هذه‬‫يكون‬
‫آخر‬ ‫مترجم‬ ‫عن‬ ‫مترجم‬ ‫تميز‬ ‫التي‬ ‫هي‬ ‫الخطوة‬ ‫هذه‬ ‫و‬ ‫حاالته‬ ‫أحسن‬ ‫في‬ ‫البرنامج‬.
.2‫األكواد‬ ‫د‬ّ‫مول‬ ‫مرحلة‬(Code Generation:)
■‫اآللة‬ ‫تفهمه‬ ‫شكل‬ ‫الى‬ ‫نهائي‬ ‫بشكل‬ ‫الكود‬ ‫تحويل‬ ‫يتم‬ ‫هنا‬.
53
Compiler Structure
54
Interpreter
(‫)المفسر‬
■‫خطوة‬ ‫خطوة‬ ‫ينفذه‬ ‫ولكن‬ ‫البرنامج‬ ‫يحول‬ ‫ال‬.
55
Compiler Kinds
■‫معنى‬ ‫ما‬multi-pass compiler‫و‬one pass compiler‫؟‬
–One pass compiler:
■‫التحليل‬ ‫عمل‬ ‫يتم‬‫المفرداتي‬‫والتحلي‬ ‫التجميع‬ ‫فيها‬ ‫يتم‬ ‫التي‬ ‫اللحظة‬ ‫نفس‬ ‫في‬‫ل‬
‫األعل‬ ‫من‬ ‫هذه‬ ‫المسح‬ ‫عملية‬ ‫ثم‬ ‫الداللي‬ ‫التحليل‬ ‫الى‬ ‫مباشرة‬ ‫االنتقال‬ ‫ثم‬ ‫القواعدي‬‫ى‬
‫لالسفل‬.
■‫و‬ ‫بقراءة‬ ‫يعني‬ ‫والداللي‬ ‫والقواعدي‬ ‫اللفظي‬ ‫التحليل‬ ‫على‬ ‫المرور‬ ‫يعني‬ ‫الوحيد‬ ‫المرور‬‫حدة‬.
–Multi pass compiler:
■‫اكتر‬ ‫وقت‬ ‫وتتطلب‬ ‫مرة‬ ‫من‬ ‫اكتر‬ ‫البرنامج‬ ‫مسح‬ ‫يتم‬.
56
Parse Tree Example
(number – number ) * number
exp  exp op exp
 exp op number
 exp * number
( exp ) * number
 ( exp op exp ) * number
 ( exp op number) * number
 ( exp – number ) * number
 (number– number ) * number
57
Abstract Syntax Trees
■ A parse tree contains much more information than is necessaryfor a compiler to
produceexecutable code.
■ Syntax trees contain just the information needed for translation.
58
Abstract Syntax Trees
Example
■ Show the Parse and Syntax Trees of the derivation of the string 3 + 4 using the
grammar.
exp → exp op exp | ( exp ) | number
op → + | - | *
Parse Tree Syntax Tree 59
Left-Recursion Removal
A → A α | β β does not begin with A
A → β is the base case
A → A α is the recursive case
A → β A'
A' → α A' | ε
exp → term exp'
exp' → addop term exp' | ε
To remove the recursion we re-write the rule :
Example
exp → exp addop term | term
60
First Sets
(‫األولى‬ ‫الرموز‬ ‫)مجموعة‬
■ The First Set of a nonterminalA is the set of terminals that may appear as the first
symbols in a string derived from A.
A → BCD
B → b | ε
C → cd | ε
D → e | ε
First (A) = { b , c , e , ε }
First (B) = { b, ε }
First (C) = { c , ε}
First (D) = { e , ε}
A  b
A  bcd
A  bcde
A  be
A  cd
A  cde
A  e
A  ε
61
First Sets
(‫األولى‬ ‫الرموز‬ ‫)مجموعة‬
■ The First Set of a nonterminalA is the set of terminals that may appear as the first
symbols in a string derived from A.
Let X be a grammar symbol (a terminal or nonterminal) or ε. Then the set First(X)
consisting of terminals, and possibly ε, is defined as follows:
1. If X is a terminal or ε, then First (X) = {X}
2. If X is a nonterminal, then
1. for each production choice X → X1 X2 … Xn , First (X) contains First (X1) – {ε}.
2. and if for some i < n, all the sets First (X1), …, First (Xi) contain ε, then First (X)
contains First (Xi+1) – {ε}.
3. and if all the sets First (X1), …, First (Xn) contain ε, then First (X) also contains ε.
62
First Sets
Example
Grammar Rule Pass 1 Pass 2 Pass 3
exp → exp addop term
exp → term First (exp) = { (
, number}
addop → + First (addop) = {+}
addop → - First (adop) = {+,-}
term → term mulop
factor
term → factor First (trem) = { ( ,
number}
mulop → * First (mulop) = {*}
factor→ ( exp ) First (factor) = { ( }
factor→ number First (factor) = { ( ,
number}
exp → exp addop term| term
addop → + | -
term → term mulop factor| factor
mulop → *
factor → ( exp ) | number
63
Follow Sets
(‫الالحقة‬ ‫الرموز‬ ‫)مجموعة‬
■ The Follow Set of a nonterminalA is the set of terminals that may appear after A in a
string derived from the start symbol.
A → BCD
B → b | ε
C → cd | ε
D → e | ε
Follow (A) = $
Follow (B) = First (CD) – {ε} + Follow (A) = {c, e, ε} – {ε} + {$} = {c, e, $}
Follow (C) = First (D) – {ε} + Follow (A) = {$, e}
Follow (D) = Follow (A) = {$}
64
Follow Sets
(‫الالحقة‬ ‫الرموز‬ ‫)مجموعة‬
■ The Follow Set of a nonterminalA is the set of terminals that may appear after A in a
string derived from the start symbol.
Given a nonterminal A, the set Follow(A), consisting of terminals, and possibly $, is
defined as follows:
1. If A is the start symbol, then $ is in Follow(A)
2. If there is a production B → α A γ, then First (γ) – {ε} is in Follow (A).
3. If there is a production B → α A γ such that ε is in First (γ), then Follow (A) contains
Follow (B).
65
Follow Sets
Example
Grammar Rule Pass 1 Pass 2
exp → exp addop term Follow (exp) = {$, + , -}
Follow (addop) = { ( , number}
Follow (term) = {$, + , -}
Follow (term) = { $, + , - , *, ) }
exp → term
term → term mulop factor Follow (term) = {$,+,-, *}
Follow (mulop) = { ( , number}
Follow (factor) = {$, +, -, *}
Follow (factor) = { $, + , - , *, ) }
term → factor
factor → ( exp ) Follow (exp) = { $, + , - , ) }
exp → exp addop term| term
addop → + | -
term → term mulop factor| factor
mulop → *
factor → ( exp ) | number
First (exp) = { ( , number}
First (term) = { ( , number}
First (factor) = { ( , number}
First (adop) = {+,-}
First (mulop) = {*}
66
MCQ
67
Question 1
■‫الصحيحة‬ ‫العبارة‬ ‫حدد‬:
.A‫المفسر‬(interpreter)‫للمترجم‬ ‫اخر‬ ‫اسم‬ ‫هو‬(compiler)‫في‬ ‫فقط‬ ‫واالختالف‬
‫المستخدم‬ ‫المصطلح‬.
.B‫البرمج‬ ‫الكود‬ ‫من‬ ‫التحقق‬ ‫عملية‬ ‫فيها‬ ‫تتم‬ ‫التي‬ ‫المترجمات‬ ‫من‬ ‫نمط‬ ‫هو‬ ‫المفسر‬‫ي‬
‫سطرا‬ ‫للعمليات‬ ‫وتنفيذ‬‫سطرا‬.
.C‫األمثلة‬ ‫مرحلة‬ ‫بدون‬ ‫مترجم‬ ‫هو‬ ‫المفسر‬(Optimization Phase)‫البرمجي‬ ‫للكود‬.
.D‫نهائيا‬ ‫بالمترجمات‬ ‫للمفسرات‬ ‫عالقة‬ ‫ال‬.
68
Question 1
■‫الصحيحة‬ ‫العبارة‬ ‫حدد‬:
.A‫المفسر‬(interpreter)‫للمترجم‬ ‫اخر‬ ‫اسم‬ ‫هو‬(compiler)‫في‬ ‫فقط‬ ‫واالختالف‬
‫المستخدم‬ ‫المصطلح‬.
.B‫البرمج‬ ‫الكود‬ ‫من‬ ‫التحقق‬ ‫عملية‬ ‫فيها‬ ‫تتم‬ ‫التي‬ ‫المترجمات‬ ‫من‬ ‫نمط‬ ‫هو‬ ‫المفسر‬‫ي‬
‫سطرا‬ ‫للعمليات‬ ‫وتنفيذ‬‫سطرا‬.
.C‫األمثلة‬ ‫مرحلة‬ ‫بدون‬ ‫مترجم‬ ‫هو‬ ‫المفسر‬(Optimization Phase)‫البرمجي‬ ‫للكود‬.
.D‫نهائيا‬ ‫بالمترجمات‬ ‫للمفسرات‬ ‫عالقة‬ ‫ال‬.
69
Question 2
■‫ب‬ ‫يتعلق‬ ‫فيما‬ ‫الصحيح‬ ‫الخيار‬ ‫اختر‬top down parsing‫المترجم‬ ‫بناء‬ ‫عند‬:
.A‫حساب‬ ‫دائما‬ ‫الزم‬first‫حساب‬ ‫دون‬follow.
.B‫حساب‬ ‫الزم‬follow‫دون‬first.
.C‫حساب‬ ‫الزم‬follow‫واحيانا‬first.
.D‫خطأ‬ ‫سبق‬ ‫ما‬ ‫كل‬.
70
Question 2
■‫ب‬ ‫يتعلق‬ ‫فيما‬ ‫الصحيح‬ ‫الخيار‬ ‫اختر‬top down parsing‫المترجم‬ ‫بناء‬ ‫عند‬:
.A‫حساب‬ ‫دائما‬ ‫الزم‬first‫حساب‬ ‫دون‬follow.
.B‫حساب‬ ‫الزم‬follow‫دون‬first.
.C‫حساب‬ ‫الزم‬follow‫واحيانا‬first.
.D‫خطأ‬ ‫سبق‬ ‫ما‬ ‫كل‬.
71
Question 3
■‫الصرفية‬ ‫القواعد‬ ‫استخدمنا‬ ‫حال‬ ‫في‬(grammer)‫صحيح‬ ‫عدد‬ ‫أي‬ ‫بنية‬ ‫توصف‬ ‫التي‬ ‫التالية‬
‫حقيقي‬ ‫او‬R‫برمجة‬ ‫لغة‬ ‫ضمن‬:
■‫القوا‬ ‫وفق‬ ‫ما‬ ‫عدد‬ ‫عن‬ ‫للتعبير‬ ‫مقبولة‬ ‫صيغة‬ ‫تعتبر‬ ‫التالية‬ ‫الصيغ‬ ‫من‬ ‫صيغة‬ ‫اي‬‫السابقة‬ ‫عد‬.
.A2.45E02
.BE1-1.1-
.C1.1.1
.DE02+1
72
Question 3
■‫الصرفية‬ ‫القواعد‬ ‫استخدمنا‬ ‫حال‬ ‫في‬(grammer)‫صحيح‬ ‫عدد‬ ‫أي‬ ‫بنية‬ ‫توصف‬ ‫التي‬ ‫التالية‬
‫حقيقي‬ ‫او‬R‫برمجة‬ ‫لغة‬ ‫ضمن‬:
■‫القوا‬ ‫وفق‬ ‫ما‬ ‫عدد‬ ‫عن‬ ‫للتعبير‬ ‫مقبولة‬ ‫صيغة‬ ‫تعتبر‬ ‫التالية‬ ‫الصيغ‬ ‫من‬ ‫صيغة‬ ‫اي‬‫السابقة‬ ‫عد‬.
.A2.45E02
.BE1-1.1-
.C1.1.1
.DE02+1
73
Question 4
■ A grammar that produces more than one parse tree for some sentenceis called:
A. Ambiguous.
B. Unambiguous.
C. Regular.
D. None of the mentioned.
74
Question 4
■ A grammar that produces more than one parse tree for some sentenceis called:
A. Ambiguous.
B. Unambiguous.
C. Regular.
D. None of the mentioned.
75
Question 5
■ An intermediate code form is:
A. Postfix notation.
B. Syntax trees.
C. Three address codes.
D. All of these.
76
Question 5
■ An intermediate code form is:
A. Postfix notation.
B. Syntax trees.
C. Three address codes.
D. All of these.
77
Question 6
■ Compiler translates the source code to:
A. Executable code.
B. Machine code.
C. Binary code.
D. Both B and C.
78
Question 6
■ Compiler translates the source code to:
A. Executable code.
B. Machine code.
C. Binary code.
D. Both B and C.
79
Question 7
■ Which of the following groups is/are token together into semantic structures?
A. Syntax analyzer.
B. Intermediatecode generation.
C. Lexical analyzer.
D. Semantic analyzer.
80
Question 7
■ Which of the following groups is/are token together into semantic structures?
A. Syntax analyzer.
B. Intermediatecode generation.
C. Lexical analyzer.
D. Semantic analyzer.
81
Question 8
■ _________is a process of finding a parse tree for a string of tokens:
A. Parsing.
B. Analyzing.
C. Recognizing.
D. Tokenizing.
82
Question 8
■ _________is a process of finding a parse tree for a string of tokens:
A. Parsing.
B. Analyzing.
C. Recognizing.
D. Tokenizing.
83
Question 9
■ What is the action of parsing the sourceprogram into proper syntactic classes?
A. Lexical analysis.
B. Syntax analysis.
C. General syntax analysis.
D. Interpretation analysis.
84
Question 9
■ What is the action of parsing the sourceprogram into proper syntactic classes?
A. Lexical analysis.
B. Syntax analysis.
C. General syntax analysis.
D. Interpretation analysis.
85
Question 10
■ Which of the following languages is generated by the given grammar?
■ S → a S | b S | ε
A. anbm
B. {a,b}*
C. {a,b}n
D. Other.
86
Question 10
■ Which of the following languages is generated by the given grammar?
■ S → a S | b S | ε
A. anbm
B. {a,b}*
C. {a,b}n
D. Other.
87
Question 11
■ Which of the following strings is not generated by the following grammar?
■ S → SaSbS|ε
A. aabb
B. abab
C. aababb
D. aabbb
88
Question 11
■ Which of the following strings is not generated by the following grammar?
■ S → SaSbS|ε
A. aabb
B. abab
C. aababb
D. aabbb
89
Question 12
■ Which of the following is NOT the set of regular expression R = (ab + abb)* bbab
A. ababbbbab
B. abbbab
C. ababbabbbab
D. abababab
90
Question 12
■ Which of the following is NOT the set of regular expression R = (ab + abb)* bbab
A. ababbbbab
B. abbbab
C. ababbabbbab
D. abababab
91
Question 13
■ In a one pass compiler, the syntax analysis is performed:
A. After the lexical analysis.
B. After the semantic analysis.
C. With the lexical analysis.
92
Question 13
■ In a one pass compiler, the syntax analysis is performed:
A. After the lexical analysis.
B. After the semantic analysis.
C. With the lexical analysis.
93
Question 14
■ When a syntax error appears, the compiler:
A. Stops Immediately.
B. Stops after collecting few Errors.
C. Depends on the organization of the syntax rules.
94
Question 14
■ When a syntax error appears, the compiler:
A. Stops Immediately.
B. Stops after collecting few Errors.
C. Depends on the organization of the syntax rules.
95
Question 15
■ Each deterministic finite automata (DFA) has an equivalent regular expression
A. True.
B. False.
96
Question 15
■ Each deterministic finite automata (DFA) has an equivalent regular expression
A. True.
B. False.
97
Question 16
■ Each Non deterministic finite automata (NFA) has an equivalent regular expression
A. True.
B. False.
98
Question 16
■ Each Non deterministic finite automata (NFA) has an equivalent regular expression
A. True.
B. False.
99
CONTACT INFO
Ad

More Related Content

What's hot (20)

Communication protocols - Embedded Systems
Communication protocols - Embedded SystemsCommunication protocols - Embedded Systems
Communication protocols - Embedded Systems
Emertxe Information Technologies Pvt Ltd
 
Internet of things using Raspberry Pi
Internet of things using Raspberry PiInternet of things using Raspberry Pi
Internet of things using Raspberry Pi
Yash Gajera
 
word level analysis
word level analysis word level analysis
word level analysis
tjs1
 
Introduction to Node-RED
Introduction to Node-REDIntroduction to Node-RED
Introduction to Node-RED
nodered_ug_jp
 
AMBA 5 COHERENT HUB INTERFACE.pptx
AMBA 5 COHERENT HUB INTERFACE.pptxAMBA 5 COHERENT HUB INTERFACE.pptx
AMBA 5 COHERENT HUB INTERFACE.pptx
Sairam Chebrolu
 
Risc processors
Risc processorsRisc processors
Risc processors
Ganesh Rocky
 
Ngrams smoothing
Ngrams smoothingNgrams smoothing
Ngrams smoothing
Digvijay Singh
 
Simple Introduction about ESP32 Presentation
Simple Introduction about ESP32 PresentationSimple Introduction about ESP32 Presentation
Simple Introduction about ESP32 Presentation
Junido
 
Raspberry Pi 3 + UART/Bluetooth issues
Raspberry Pi 3 + UART/Bluetooth issuesRaspberry Pi 3 + UART/Bluetooth issues
Raspberry Pi 3 + UART/Bluetooth issues
yeokm1
 
Random walk on Graphs
Random walk on GraphsRandom walk on Graphs
Random walk on Graphs
Pavan Kapanipathi
 
Automata and Compiler 2020
Automata and Compiler 2020Automata and Compiler 2020
Automata and Compiler 2020
Joud Khattab
 
IOT and its communication models and protocols.pdf
IOT and its communication models and protocols.pdfIOT and its communication models and protocols.pdf
IOT and its communication models and protocols.pdf
MD.ANISUR RAHMAN
 
clustering protocol in WSN:LEACH
clustering protocol in WSN:LEACHclustering protocol in WSN:LEACH
clustering protocol in WSN:LEACH
Jimit Rupani
 
Schedule and Contention based MAC protocols
Schedule and Contention based MAC protocolsSchedule and Contention based MAC protocols
Schedule and Contention based MAC protocols
Darwin Nesakumar
 
System On Chip (SOC)
System On Chip (SOC)System On Chip (SOC)
System On Chip (SOC)
Shivam Gupta
 
IoT internet of things
IoT  internet of thingsIoT  internet of things
IoT internet of things
Gd Insaa
 
Mobile Ad hoc Networks
Mobile Ad hoc NetworksMobile Ad hoc Networks
Mobile Ad hoc Networks
Jagdeep Singh
 
MicroC/OS-II
MicroC/OS-IIMicroC/OS-II
MicroC/OS-II
Digicomm Semiconductor Private Limited
 
The cougar approach to in-network query processing in sensor networks
The cougar approach to in-network query processing in sensor networksThe cougar approach to in-network query processing in sensor networks
The cougar approach to in-network query processing in sensor networks
Dilini Muthumala
 
Introduction to mobile ad hoc network
Introduction to mobile ad hoc networkIntroduction to mobile ad hoc network
Introduction to mobile ad hoc network
Ashish Prajapat
 
Internet of things using Raspberry Pi
Internet of things using Raspberry PiInternet of things using Raspberry Pi
Internet of things using Raspberry Pi
Yash Gajera
 
word level analysis
word level analysis word level analysis
word level analysis
tjs1
 
Introduction to Node-RED
Introduction to Node-REDIntroduction to Node-RED
Introduction to Node-RED
nodered_ug_jp
 
AMBA 5 COHERENT HUB INTERFACE.pptx
AMBA 5 COHERENT HUB INTERFACE.pptxAMBA 5 COHERENT HUB INTERFACE.pptx
AMBA 5 COHERENT HUB INTERFACE.pptx
Sairam Chebrolu
 
Simple Introduction about ESP32 Presentation
Simple Introduction about ESP32 PresentationSimple Introduction about ESP32 Presentation
Simple Introduction about ESP32 Presentation
Junido
 
Raspberry Pi 3 + UART/Bluetooth issues
Raspberry Pi 3 + UART/Bluetooth issuesRaspberry Pi 3 + UART/Bluetooth issues
Raspberry Pi 3 + UART/Bluetooth issues
yeokm1
 
Automata and Compiler 2020
Automata and Compiler 2020Automata and Compiler 2020
Automata and Compiler 2020
Joud Khattab
 
IOT and its communication models and protocols.pdf
IOT and its communication models and protocols.pdfIOT and its communication models and protocols.pdf
IOT and its communication models and protocols.pdf
MD.ANISUR RAHMAN
 
clustering protocol in WSN:LEACH
clustering protocol in WSN:LEACHclustering protocol in WSN:LEACH
clustering protocol in WSN:LEACH
Jimit Rupani
 
Schedule and Contention based MAC protocols
Schedule and Contention based MAC protocolsSchedule and Contention based MAC protocols
Schedule and Contention based MAC protocols
Darwin Nesakumar
 
System On Chip (SOC)
System On Chip (SOC)System On Chip (SOC)
System On Chip (SOC)
Shivam Gupta
 
IoT internet of things
IoT  internet of thingsIoT  internet of things
IoT internet of things
Gd Insaa
 
Mobile Ad hoc Networks
Mobile Ad hoc NetworksMobile Ad hoc Networks
Mobile Ad hoc Networks
Jagdeep Singh
 
The cougar approach to in-network query processing in sensor networks
The cougar approach to in-network query processing in sensor networksThe cougar approach to in-network query processing in sensor networks
The cougar approach to in-network query processing in sensor networks
Dilini Muthumala
 
Introduction to mobile ad hoc network
Introduction to mobile ad hoc networkIntroduction to mobile ad hoc network
Introduction to mobile ad hoc network
Ashish Prajapat
 

Similar to Automate and Compiler 2018 (14)

Computer school-books-3rd-preparatory-1st-term-khawagah-2019-6
Computer school-books-3rd-preparatory-1st-term-khawagah-2019-6Computer school-books-3rd-preparatory-1st-term-khawagah-2019-6
Computer school-books-3rd-preparatory-1st-term-khawagah-2019-6
khawagah
 
3) Logic Circuits.pdf
3) Logic Circuits.pdf3) Logic Circuits.pdf
3) Logic Circuits.pdf
Minasafy
 
Loop.Hamid K
Loop.Hamid KLoop.Hamid K
Loop.Hamid K
Hamid Ateyah
 
Shannon code & shannon fano & huffman method - chapter three
Shannon code  & shannon fano & huffman method  - chapter threeShannon code  & shannon fano & huffman method  - chapter three
Shannon code & shannon fano & huffman method - chapter three
DrMohammed Qassim
 
c# المحاضره 4 @ 5 في
 c# المحاضره 4  @  5  في    c# المحاضره 4  @  5  في
c# المحاضره 4 @ 5 في
nermeenelhamy1
 
عرض الدوائر الرقمية
عرض الدوائر الرقميةعرض الدوائر الرقمية
عرض الدوائر الرقمية
تقانة
 
أسئلة وإجابتها علي منهج الصف الثالث الاعدادي فصل دراسي ثاني
أسئلة وإجابتها علي منهج الصف الثالث الاعدادي فصل دراسي ثانيأسئلة وإجابتها علي منهج الصف الثالث الاعدادي فصل دراسي ثاني
أسئلة وإجابتها علي منهج الصف الثالث الاعدادي فصل دراسي ثاني
Hanaa Ahmed
 
1- Languages Basics
1- Languages Basics1- Languages Basics
1- Languages Basics
Ghadeer AlHasan
 
3- Functions
3-  Functions3-  Functions
3- Functions
Ghadeer AlHasan
 
مقرر معالجة البيانات
مقرر معالجة البياناتمقرر معالجة البيانات
مقرر معالجة البيانات
angel1990girle
 
Artificial Intelligence 2018
Artificial Intelligence 2018Artificial Intelligence 2018
Artificial Intelligence 2018
Joud Khattab
 
نظم-العد.pdf
نظم-العد.pdfنظم-العد.pdf
نظم-العد.pdf
AbdullahOmar64
 
06666666666666666666666666666666666Algorithms.ppt
06666666666666666666666666666666666Algorithms.ppt06666666666666666666666666666666666Algorithms.ppt
06666666666666666666666666666666666Algorithms.ppt
HamidKhemili
 
Computer school-books-3rd-preparatory-1st-term-khawagah-2019-6
Computer school-books-3rd-preparatory-1st-term-khawagah-2019-6Computer school-books-3rd-preparatory-1st-term-khawagah-2019-6
Computer school-books-3rd-preparatory-1st-term-khawagah-2019-6
khawagah
 
3) Logic Circuits.pdf
3) Logic Circuits.pdf3) Logic Circuits.pdf
3) Logic Circuits.pdf
Minasafy
 
Shannon code & shannon fano & huffman method - chapter three
Shannon code  & shannon fano & huffman method  - chapter threeShannon code  & shannon fano & huffman method  - chapter three
Shannon code & shannon fano & huffman method - chapter three
DrMohammed Qassim
 
c# المحاضره 4 @ 5 في
 c# المحاضره 4  @  5  في    c# المحاضره 4  @  5  في
c# المحاضره 4 @ 5 في
nermeenelhamy1
 
عرض الدوائر الرقمية
عرض الدوائر الرقميةعرض الدوائر الرقمية
عرض الدوائر الرقمية
تقانة
 
أسئلة وإجابتها علي منهج الصف الثالث الاعدادي فصل دراسي ثاني
أسئلة وإجابتها علي منهج الصف الثالث الاعدادي فصل دراسي ثانيأسئلة وإجابتها علي منهج الصف الثالث الاعدادي فصل دراسي ثاني
أسئلة وإجابتها علي منهج الصف الثالث الاعدادي فصل دراسي ثاني
Hanaa Ahmed
 
مقرر معالجة البيانات
مقرر معالجة البياناتمقرر معالجة البيانات
مقرر معالجة البيانات
angel1990girle
 
Artificial Intelligence 2018
Artificial Intelligence 2018Artificial Intelligence 2018
Artificial Intelligence 2018
Joud Khattab
 
06666666666666666666666666666666666Algorithms.ppt
06666666666666666666666666666666666Algorithms.ppt06666666666666666666666666666666666Algorithms.ppt
06666666666666666666666666666666666Algorithms.ppt
HamidKhemili
 
Ad

More from Joud Khattab (20)

Customer Engagement Management
Customer Engagement ManagementCustomer Engagement Management
Customer Engagement Management
Joud Khattab
 
Design thinking and Role Playing
Design thinking and Role PlayingDesign thinking and Role Playing
Design thinking and Role Playing
Joud Khattab
 
Algorithms and Data Structure 2020
Algorithms and Data Structure 2020Algorithms and Data Structure 2020
Algorithms and Data Structure 2020
Joud Khattab
 
Artificial Intelligence 2020
Artificial Intelligence 2020Artificial Intelligence 2020
Artificial Intelligence 2020
Joud Khattab
 
Database 2020
Database 2020Database 2020
Database 2020
Joud Khattab
 
Software Engineering 2020
Software Engineering 2020Software Engineering 2020
Software Engineering 2020
Joud Khattab
 
Software Engineering 2018
Software Engineering 2018Software Engineering 2018
Software Engineering 2018
Joud Khattab
 
Database 2018
Database 2018Database 2018
Database 2018
Joud Khattab
 
Algorithms and Data Structure 2018
Algorithms and Data Structure 2018Algorithms and Data Structure 2018
Algorithms and Data Structure 2018
Joud Khattab
 
Data Storytelling
Data StorytellingData Storytelling
Data Storytelling
Joud Khattab
 
Geospatial Information Management
Geospatial Information ManagementGeospatial Information Management
Geospatial Information Management
Joud Khattab
 
Big Data for Development
Big Data for DevelopmentBig Data for Development
Big Data for Development
Joud Khattab
 
Personality Detection via MBTI Test
Personality Detection via MBTI TestPersonality Detection via MBTI Test
Personality Detection via MBTI Test
Joud Khattab
 
Fog Computing
Fog ComputingFog Computing
Fog Computing
Joud Khattab
 
Seasonal ARIMA
Seasonal ARIMASeasonal ARIMA
Seasonal ARIMA
Joud Khattab
 
Optimization Techniques
Optimization TechniquesOptimization Techniques
Optimization Techniques
Joud Khattab
 
Network Address Translation (NAT)
Network Address Translation (NAT)Network Address Translation (NAT)
Network Address Translation (NAT)
Joud Khattab
 
From Image Processing To Computer Vision
From Image Processing To Computer VisionFrom Image Processing To Computer Vision
From Image Processing To Computer Vision
Joud Khattab
 
Spark SQL
Spark SQLSpark SQL
Spark SQL
Joud Khattab
 
Social Networks Analysis
Social Networks AnalysisSocial Networks Analysis
Social Networks Analysis
Joud Khattab
 
Customer Engagement Management
Customer Engagement ManagementCustomer Engagement Management
Customer Engagement Management
Joud Khattab
 
Design thinking and Role Playing
Design thinking and Role PlayingDesign thinking and Role Playing
Design thinking and Role Playing
Joud Khattab
 
Algorithms and Data Structure 2020
Algorithms and Data Structure 2020Algorithms and Data Structure 2020
Algorithms and Data Structure 2020
Joud Khattab
 
Artificial Intelligence 2020
Artificial Intelligence 2020Artificial Intelligence 2020
Artificial Intelligence 2020
Joud Khattab
 
Software Engineering 2020
Software Engineering 2020Software Engineering 2020
Software Engineering 2020
Joud Khattab
 
Software Engineering 2018
Software Engineering 2018Software Engineering 2018
Software Engineering 2018
Joud Khattab
 
Algorithms and Data Structure 2018
Algorithms and Data Structure 2018Algorithms and Data Structure 2018
Algorithms and Data Structure 2018
Joud Khattab
 
Geospatial Information Management
Geospatial Information ManagementGeospatial Information Management
Geospatial Information Management
Joud Khattab
 
Big Data for Development
Big Data for DevelopmentBig Data for Development
Big Data for Development
Joud Khattab
 
Personality Detection via MBTI Test
Personality Detection via MBTI TestPersonality Detection via MBTI Test
Personality Detection via MBTI Test
Joud Khattab
 
Optimization Techniques
Optimization TechniquesOptimization Techniques
Optimization Techniques
Joud Khattab
 
Network Address Translation (NAT)
Network Address Translation (NAT)Network Address Translation (NAT)
Network Address Translation (NAT)
Joud Khattab
 
From Image Processing To Computer Vision
From Image Processing To Computer VisionFrom Image Processing To Computer Vision
From Image Processing To Computer Vision
Joud Khattab
 
Social Networks Analysis
Social Networks AnalysisSocial Networks Analysis
Social Networks Analysis
Joud Khattab
 
Ad

Automate and Compiler 2018

  • 2. Content ■ Overview. ■ Automate. ■ Regular Expression. ■ Context Free Grammars. ■ Compiler. ■ MCQ. 2
  • 4. Automata ■‫عن‬ ‫أمثلة‬‫األوتومات‬: –‫القهوة‬ ‫بيع‬ ‫آلة‬ ‫في‬ ‫الموجود‬ ‫الحاسب‬. –‫اآللي‬ ‫الصراف‬ ‫في‬ ‫الموجود‬ ‫الحاسب‬ATM. ■‫ع‬ ‫لالستعالم‬ ‫المفاتيح‬ ‫لوحة‬ ‫نستخدم‬ ‫ثم‬ ‫ومن‬ ً‫أوال‬ ‫البطاقة‬ ‫نضع‬ ‫اآللي‬ ‫الصراف‬ ‫في‬‫ن‬ ‫أو‬ ‫معين‬ ‫مبلغ‬ ‫لسحب‬ ‫أو‬ ‫الرصيد‬...‫إلخ‬. ■‫تسلسالت‬ ‫في‬ ‫اختالف‬ ‫يوجد‬ ‫ولكن‬ ‫معين‬ ‫بتسلسل‬ ‫األحداث‬ ‫من‬ ‫عدد‬ ‫لدينا‬ ‫يكون‬ ‫أي‬ ‫عل‬ ‫أو‬ ‫المعلومات‬ ‫على‬ ‫يتم‬ ‫الذي‬ ‫هذا‬ ‫األحداث‬ ‫وتسلسل‬ ‫ثابت‬ ‫غير‬ ‫فالتسلسل‬ ‫األحداث‬‫ى‬ ‫عليها‬ ‫يتعرف‬ ‫التي‬ ‫واللغة‬ ‫الحسابات‬ ‫هو‬ ‫المعلومات‬ ‫حركة‬‫األتومات‬،‫النظام‬ ‫بهذا‬ ‫الخاص‬ ■‫ادخال‬ ‫بعد‬ ً‫فمثال‬ ‫مرة‬ ‫كل‬ ‫في‬ ‫التسلسل‬ ‫بنفس‬ ‫السير‬ ‫الضروري‬ ‫من‬ ‫ليس‬ ‫بالتالي‬‫البطاقة‬ ‫الت‬ ‫حساب‬ ‫اختيار‬ ‫ممكن‬ ‫بل‬ ‫الجاري‬ ‫الحساب‬ ‫اختيار‬ ً‫دائما‬ ‫المفروض‬ ‫من‬ ‫ليس‬ ‫للصراف‬‫وفير‬ ‫مختلف‬ ‫بفرعين‬ ‫الذهاب‬ ‫يمكن‬ ‫وبالتالي‬ ‫لنا‬ ‫بالنسبة‬ ‫مقبولتان‬ ‫العمليتين‬ ‫فكال‬‫في‬ ‫ين‬ ‫األتومات‬. 4
  • 5. Automata ■‫الرياضي‬ ‫التعريف‬‫لألوتومات‬: ■ M = ( Q , Σ , δ , q0 , F) were – Q = { q0 , q1 , q2 , q3 } – Σ = { 0 , 1 } – q0 = q0 initial state – F = { q0 } ■‫االصفار‬ ‫عدد‬ ‫فيها‬ ‫يكون‬ ‫التي‬ ‫السالسل‬ ‫جميع‬ ‫يقبل‬ ‫المثال‬‫والواحدات‬‫زوجي‬. 5
  • 6. Different Kinds of Automata (Chomsky) (‫القواعد‬ ‫لتوصيف‬ ‫وهرميتها‬ ‫اللغات‬ ‫أنواع‬) 1. Regular Languages: – Definition Tool: Finite Automata ( ‫األوتومات‬‫المنتهي‬ ) – Memory Type: no temporary memory – Ex: ATM, Search. 2. Context-Free Languages: – Definition Tool: Push down Automata (PDA) ( ‫األوتومات‬‫المكدس‬ ‫ذات‬ ) – Memory Type: stack – Ex: Syntax recognition of a specific language, Identify the conditional (IF) 3. Recursive Languages: – Definition Tool: Turing Machines – Memory Type: random access memory (RAM) – Ex: Recognize all languages regardless their form 6
  • 7. Finite Automata ■‫أنواع‬‫االوتومات‬‫المنتهي‬:Finite Automata –‫االوتومات‬‫الحتمي‬ ‫المنتهي‬( :Deterministic finite automata DFA) ■‫هذا‬ ‫يحقق‬ ‫حيث‬‫االوتومات‬‫الى‬ ‫يكون‬ ‫معين‬ ‫رمز‬ ‫اجل‬ ‫ومن‬ ‫وحيدة‬ ‫حالة‬ ‫من‬ ‫االنتقال‬ ‫ان‬ ‫محددة‬ ‫وحيدة‬ ‫حالة‬. –‫االوتومات‬‫المنتهي‬‫الالحتمي‬( :Non deterministic finite automata NFA) ■‫شرط‬ ‫يحقق‬ ‫ال‬ ‫الذي‬ ‫وهو‬‫االوتومات‬‫السابق‬. ■‫الرمز‬ ‫بنفس‬ ‫حالة‬ ‫من‬ ‫اكثر‬ ‫الى‬ ‫االنتقال‬ ‫نستطيع‬ ‫أي‬. 7
  • 8. Finite Automata ■‫مالحظات‬: –‫اجل‬ ‫من‬‫اوتومات‬‫نوع‬ ‫من‬DFA‫ما‬ ‫لسلسلة‬ ‫القبول‬ ‫خوارزمية‬ ‫بناء‬ ‫يمكن‬. ■‫تنتمي‬ ‫ما‬ ‫سلسلة‬ ‫كانت‬ ‫ما‬ ‫اذا‬ ‫معرفة‬ ‫خاللها‬ ‫من‬ ‫نستطيع‬ ‫التي‬ ‫الخوارزمية‬ ‫أي‬‫لالوتو‬‫مات‬ ‫ال‬ ‫ام‬. –‫حالة‬ ‫في‬ ‫اما‬NFA‫قبول‬ ‫خوارزمية‬ ‫بناء‬ ‫يمكن‬ ‫فال‬. ■‫جدا‬ ‫مكلفة‬ ‫ولكنها‬ ‫تراجعية‬ ‫قبول‬ ‫خوارزمية‬ ‫بناء‬ ‫يمكن‬. –‫على‬ ‫يبرهن‬NFA‫ل‬ ‫بتحويلها‬DFA‫لها‬ ‫مكافئ‬. –‫لكل‬NFA‫هنالك‬DFA‫له‬ ‫مكافئ‬. 8
  • 13. Question 1 ■‫أعط‬‫األوتومات‬‫المكافئ‬ ‫الحتمي‬ ‫المنتهي‬‫لألوتومات‬‫المنتهي‬‫الالحتمي‬‫التالي‬: 13
  • 16. Regular Language ■‫النظامي‬ ‫التعبير‬: –‫لتمثيل‬ ‫مختصرة‬ ‫مبسطة‬ ‫طريقة‬ ‫عن‬ ‫عبارة‬ ‫هو‬‫االوتومات‬‫معينة‬ ‫لغة‬ ‫يوصف‬ ‫الذي‬ ‫من‬ ‫مجموعة‬ ‫على‬ ‫ويعتمد‬‫التراميز‬. ■‫المنتظمة‬ ‫اللغة‬: –‫ب‬ ‫تمثيلها‬ ‫يمكن‬ ‫لغة‬ ‫كل‬ ‫هي‬RE‫ب‬ ‫ممثلة‬ ‫تكون‬ ‫عمليا‬ ‫أي‬DFA. 16
  • 17. Regular Language ■‫امثلة‬: –‫االبجدية‬ ‫لدينا‬ ‫كانت‬ ‫اذا‬σ = {𝑎}‫النظامية‬ ‫التعابير‬ ‫من‬ ‫العديد‬ ‫بناء‬ ‫نستطيع‬: ■a:‫واحد‬ ‫حرف‬ ‫من‬ ‫المؤلفة‬ ‫السلسلة‬ ‫يكافئ‬. ■ε:‫الخالية‬ ‫السلسلة‬ ‫يكافئ‬. ■a*:‫تكرار‬ ‫من‬ ‫المؤلفة‬ ‫السلسلة‬ ‫او‬ ‫الخالية‬ ‫السلسلة‬ ‫يكافئ‬a‫أو‬ ‫أكبر‬ ‫المرات‬ ‫من‬ ‫عدد‬ ‫الواحد‬ ‫يساوي‬. ■a+:‫تكرار‬ ‫من‬ ‫المؤلفة‬ ‫السلسلة‬ ‫يكافئ‬a‫الواحد‬ ‫يساوي‬ ‫أو‬ ‫أكبر‬ ‫المرات‬ ‫من‬ ‫عدد‬. ■an:‫تكرار‬ ‫من‬ ‫المؤلفة‬ ‫السلسلة‬ ‫يكافئ‬a(n)‫فقط‬ ‫مرة‬. –‫األبجدية‬ ‫لدينا‬ ‫كانت‬ ‫اذا‬σ = {𝑎,𝑏}‫المنتظمة‬ ‫التعابير‬ ‫من‬ ‫العديد‬ ‫بناء‬ ‫نستطيع‬: ■a*b*:‫عملية‬ ‫عن‬ ‫الناتجة‬ ‫السلسلة‬ ‫يكافئ‬concatenation‫أي‬).(‫نظاميين‬ ‫تعبيرين‬ ‫بين‬. 17
  • 18. Regular Language ■ L1 = { a*b* : a,b ∈ σ } – L1 is regular language. ■ L2 = { anbn : a,b ∈ σ , n>=0} – L2 is not regular language. ■‫اللغة‬ ‫ان‬ ‫نالحظ‬L2‫ير‬ ‫أن‬ ‫يجب‬ ‫مقبولة‬ ‫السلسة‬ ‫تكون‬ ‫حتى‬ ‫ألنه‬ ‫وذلك‬ ،‫ذاكرة‬ ‫الى‬ ‫تحتاج‬‫ى‬ b‫ورود‬ ‫مرات‬ ‫عدد‬ ‫بنفس‬a. 18
  • 19. Regular Language ■ L1, L2: Regular Languages then: – L = L1 . L2 = { X.W were X ∈ L1 , W ∈ L2 } is Regular Language – L1 ∪ L2 is Regular Language – L1 , L2 is Regular Language – L1 ∩ L2 is Regular Language 19
  • 20. Regular Expressions Exercise 1 ■ Suppose the only characters are 0 and 1. ■ A regular expression for strings containing 00 as a substring: (0 | 1)* 00 (0 | 1)* 11011100101 0000 11111011110011111 20
  • 21. Regular Expressions Exercise 2 ■ Suppose the only characters are 0 and 1. ■ A regular expression for strings of length exactly four: (0|1)(0|1)(0|1)(0|1) (0|1){4} 0000 1010 1111 1000 21
  • 22. Regular Expressions Exercise 3 ■ Suppose the only characters are 0 and 1. ■ A regular expression for strings that contain at most one zero: 1*(0 | ε)1* 1*0?1* 11110111 111111 0111 0 22
  • 23. Regular Expressions Exercise 4 ■ Suppose that our alphabet is all ASCII characters. ■ A regular expression for even numbers: (+|-)?(0|1|2|3|4|5|6|7|8|9)*(0|2|4|6|8) (+|-)?[0123456789]*[02468] (+|-)?[0-9]*[02468] 42 +1370 -3248 -9999912 23
  • 24. Regular Language & Automata ■‫التعبير‬(ab)‫مقابلته‬ ‫يمكن‬‫باالوتومات‬: ■‫التعبير‬(a + b)‫مقابلته‬ ‫يمكن‬‫باالوتومات‬: 24
  • 25. Regular Language & Automata ■‫التعبير‬(a*)‫مقابلته‬ ‫يمكن‬‫باالوتومات‬: ■‫التعبير‬(a+)‫مقابلته‬ ‫يمكن‬‫باالوتومات‬: 25
  • 26. Regular Language & Automata ■‫التعبير‬(θ)‫طريق‬ ‫وجود‬ ‫عدم‬ ‫عن‬ ‫تعبر‬(‫فارغة‬ ‫مجموعة‬ ‫هي‬ ‫الحلول‬ ‫مجموعة‬ ‫أي‬): ■‫التعبير‬(ε)‫رموز‬ ‫من‬ ‫رمز‬ ‫أي‬ ‫لوجود‬ ‫الحاجة‬ ‫دون‬ ‫عبره‬ ‫ننتقل‬ ‫مفتوح‬ ‫طريق‬ ‫وجود‬ ‫عن‬ ‫تعبر‬ ‫االبجدية‬: 26
  • 27. Regular Language & Automata Example numbers ■ Draw a DFA equivalent to the following regular expression: 1) digit = [0-9] 2) nat = digit+ 3) signedNat = (+|-)? nat 4) number = signedNat("." nat)? (E signedNat)? 27
  • 28. nat = digit+ Regular Language & Automata Example numbers 28
  • 29. signedNat = (+|-)? nat Regular Language & Automata Example numbers 29
  • 30. signedNat ("." nat)? Regular Language & Automata Example numbers 30
  • 31. number = signedNat ("." nat)? (E signedNat)? Regular Language & Automata Example numbers 31
  • 32. Question 1 ■‫التي‬ ‫اللغات‬ ‫هي‬ ‫ما‬‫توصفها‬‫األبجدية‬ ‫على‬ ‫المعرفة‬ ‫التالية‬ ‫المنتظمة‬ ‫التعابير‬{a,b}: –a (a + b)* b –aab (aa|bb)* –(aa)* a 32
  • 33. Question 1 ■‫التي‬ ‫اللغات‬ ‫هي‬ ‫ما‬‫توصفها‬‫األبجدية‬ ‫على‬ ‫المعرفة‬ ‫التالية‬ ‫المنتظمة‬ ‫التعابير‬{a,b}: –a (a + b)* b –aab (aa|bb)* –(aa)* a ■‫الجواب‬: –‫بالحرف‬ ‫كلماتها‬ ‫جميع‬ ‫تبدأ‬ ‫التي‬ ‫اللغة‬a‫بالحرف‬ ‫وتنتهي‬b –‫بالسلسلة‬ ‫كلماتها‬ ‫جميع‬ ‫تبدأ‬ ‫التي‬ ‫اللغة‬aab‫األقل‬ ‫على‬ ‫واحد‬ ‫تكرار‬ ‫يليها‬ ‫ثم‬ ‫ومن‬ ‫لثنائية‬aa‫من‬ ‫أو‬bb –‫حرف‬ ‫على‬ ‫إال‬ ‫كلماتها‬ ‫تحتوي‬ ‫ال‬ ‫التي‬ ‫اللغة‬a‫مفردا‬ ‫كلماتها‬ ‫طول‬ ‫ويكون‬ 33
  • 34. Question 2 ■‫التالية‬ ‫اللغات‬ ‫تعرف‬ ‫أن‬ ‫يمكن‬ ‫التي‬ ‫المنتظمة‬ ‫التعابير‬ ‫هي‬ ‫ما‬: –‫األبجدية‬ ‫على‬ ‫اللغة‬{a,b,c}‫بالحرف‬ ‫كلماتها‬ ‫جميع‬ ‫تبدأ‬ ‫والتي‬a –‫مضاعفات‬ ‫من‬ ‫الصحيحة‬ ‫األعداد‬5 34
  • 35. Question 2 ■‫التالية‬ ‫اللغات‬ ‫تعرف‬ ‫أن‬ ‫يمكن‬ ‫التي‬ ‫المنتظمة‬ ‫التعابير‬ ‫هي‬ ‫ما‬: –‫األبجدية‬ ‫على‬ ‫اللغة‬{a,b,c}‫بالحرف‬ ‫كلماتها‬ ‫جميع‬ ‫تبدأ‬ ‫والتي‬a –‫مضاعفات‬ ‫من‬ ‫الصحيحة‬ ‫األعداد‬5 ■‫الجواب‬: –a (a + b + c)* –(0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9)* 5 35
  • 36. CONTEXT FREE GRAMMARS ‫السياق‬ ‫عديمة‬ ‫اللغات‬(‫السياق‬ ‫خارج‬) 36
  • 37. Context Free Grammars (‫السياق‬ ‫عديمة‬ ‫)اللغات‬ ■‫القواعدي‬ ‫النموذج‬ ‫مفهوم‬: –‫ال‬ ‫مفهوم‬ ‫من‬ ‫تطورا‬ ‫أكثر‬ ‫مفهوم‬ ‫هو‬DFA‫هو‬ ‫اللغات‬ ‫من‬ ‫بسيط‬ ‫نمط‬ ‫عن‬ ‫يعبر‬ ‫الذي‬ ‫النمط‬ ‫هذا‬ ‫تعدي‬ ‫يستطيع‬ ‫وال‬ ‫المنتظمة‬ ‫اللغات‬. –‫ال‬ ‫يعد‬ ‫وبالتالي‬DFA‫قاصرا‬‫تحمل‬ ‫التي‬ ‫اللغات‬ ‫تمثيل‬ ‫عن‬‫ذاكرة‬‫وبالتالي‬‫نلجا‬‫الى‬ ‫اللغات‬ ‫من‬ ‫النوع‬ ‫هذا‬ ‫لتمثيل‬ ‫القواعدي‬ ‫النموذج‬. ■‫من‬ ‫جديد‬ ‫نوع‬ ‫هو‬ ‫المنتظمة‬ ‫اللغات‬ ‫اذا‬‫االوتومات‬‫لغ‬ ‫تمثيل‬ ‫يستطيع‬ ‫بذاكرة‬ ‫مزود‬ ‫وهو‬ ،‫ات‬ ‫النمط‬ ‫من‬ ‫السالسل‬ ‫مثل‬ ‫ذاكرة‬ ‫الى‬ ‫تحتاج‬(anbn)‫هذا‬ ‫ان‬ ‫حيث‬‫االوتومات‬‫اعداد‬ ‫يتذكر‬ ‫الرموز‬(a,b)‫عليه‬ ‫مرت‬ ‫التي‬. 37
  • 38. Context Free Grammars (‫السياق‬ ‫عديمة‬ ‫)اللغات‬ ■ Non Terminal Symbols (V): –‫السالسل‬ ‫توليد‬ ‫عملية‬ ‫على‬ ‫تساعد‬ ‫التي‬ ‫البسيطة‬ ‫الحروف‬ ‫من‬ ‫مجموعة‬ ‫تمثل‬(‫أي‬ ‫المساعدة‬ ‫التوابع‬ ‫من‬ ‫مجموعة‬.) ■ Terminal Symbols (T): –‫اللغة‬ ‫سالسل‬ ‫منها‬ ‫تتألف‬ ‫التي‬ ‫الحروف‬ ‫من‬ ‫تتكون‬ ‫التي‬ ‫االبجدية‬ ‫تؤلف‬. ■ Production (P): –‫اللغة‬ ‫توليد‬ ‫قواعد‬ ‫هو‬. ■ Starting Symbol (S): –‫اللغة‬ ‫توليد‬ ‫عملية‬ ‫في‬ ‫منه‬ ‫تبدأ‬ ‫الذي‬ ‫البدائي‬ ‫الرمز‬. 38
  • 39. Context Free Grammars Example 1 ■‫للغة‬ ‫القواعدي‬ ‫النموذج‬ ‫اوجد‬: – L = { anbn : a,b ∈ σ , n>=0} ■‫الحل‬: – L = {V , T , S , P} where ■ S → a S b (1) ■ S → a b (2) – V = {S} – S = {S} – T = {a,b} 39
  • 40. Context Free Grammars Example 2 ■‫اللغة‬ ‫لدينا‬ ‫ليكن‬L‫بالشكل‬ ‫اللغة‬ ‫هذه‬ ‫تعريف‬ ‫نستطيع‬ ،‫الحسابية‬ ‫للتعابير‬ ‫الممثلة‬ ‫التالي‬ ‫القواعدي‬: – Exp → Exp Op Exp | - ( Exp ) | id – Op → + | - | * | / ■‫الحل‬: – V = {Exp, Op} – T = { + , - , / , id , ( , ) } – S = {Exp} 40
  • 41. Context Free Grammars Left Most Derivation ■ Exp → Exp Op Exp | - ( Exp ) | id ■ Op → + | - | * | / ■‫التالية‬ ‫السلسلة‬ ‫نشتق‬ ‫كيف‬:id + id * id –‫ال‬ ‫نعوض‬(None Terminal Symbol)‫وهكذا‬ ‫أوال‬ ‫اليسار‬ ‫اقصى‬ ‫في‬ ‫الموجود‬.. –‫التالية‬ ‫المراحل‬ ‫عبر‬ ‫ذلك‬ ‫يتم‬: ■ Exp → Exp Op Exp → id Op Exp → id + Exp → id + Exp Op Exp → id + id Op Exp → id + id * Exp → id + id * id 41
  • 42. Context Free Grammars Right Most Derivation ■ Exp → Exp Op Exp | - ( Exp ) | id ■ Op → + | - | * | / ■‫ال‬ ‫تعويض‬ ‫على‬ ‫مبدؤها‬ ‫ينطوي‬ ‫االشتقاق‬ ‫في‬ ‫أخرى‬ ‫خوارزمية‬ ‫هنالك‬(None Terminal Symbol)‫وهكذا‬ ‫أوال‬ ‫اليمين‬ ‫اقصى‬ ‫في‬ ‫الموجود‬... ■ Exp → Exp Op Exp → Exp Op id → Exp * id → Exp Op Exp * id → Exp Op id * id → Exp + id * id → id + id * id 42
  • 43. Context Free Grammars Ambiguity ■‫غامضة‬ ‫لغة‬ ‫هي‬ ‫السابق‬ ‫المثال‬ ‫في‬ ‫اللغة‬ ‫ان‬ ‫على‬ ‫نالحظ‬ –‫نفس‬ ‫الشتقاق‬ ‫طريقتين‬ ‫هنالك‬ ‫االشتقاق‬ ‫خوارزمية‬ ‫نفس‬ ‫أجل‬ ‫من‬ ‫ألنه‬ ‫وذلك‬ ‫قابلية‬ ‫مع‬ ‫يتناسب‬ ‫ال‬ ‫الغموض‬ ‫وهذا‬ ‫السلسلة‬‫االوتومات‬‫البرمجي‬ ‫لإلسقاط‬. 43
  • 45. Compiler Definition ■‫المترجم‬ ‫تعريف‬: –‫المترجم‬ ‫ندعوها‬ ،‫البرمجة‬ ‫عملية‬ ‫في‬ ‫جدا‬ ‫ضرورية‬ ‫أداة‬ ‫مبرمج‬ ‫أي‬ ‫يستخدم‬. –‫المس‬ ‫عالية‬ ‫برمجية‬ ‫بلغة‬ ‫نكتبه‬ ‫الذي‬ ‫البرمجي‬ ‫النص‬ ‫يترجم‬ ‫حاسوبي‬ ‫برنامج‬‫توى‬ (C, Pascal, C++, C#, Java, …)‫قبل‬ ‫من‬ ‫للتنفيذ‬ ‫قابلة‬ ‫تعليمات‬ ‫مجموعة‬ ‫الى‬ ‫الحاسوب‬. –‫كانت‬ ‫سواء‬ ‫المستوى‬ ‫منخفضة‬ ‫بلغة‬ ‫مكتوبة‬ ‫التنفيذية‬ ‫التعليمات‬ ‫هذه‬ ‫تكون‬‫ثنائية‬ ‫لغة‬ ‫و‬ ‫اصفار‬ ‫من‬ ‫مؤلفة‬‫واحدات‬‫تجميع‬ ‫لغة‬ ‫او‬. –‫المستو‬ ‫عالية‬ ‫أخرى‬ ‫لغة‬ ‫الى‬ ‫المستوى‬ ‫عالية‬ ‫لغة‬ ‫من‬ ‫مترجمات‬ ‫بناء‬ ‫أيضا‬ ‫يمكن‬‫ى‬. (‫البرمجية‬ ‫البيئة‬ ‫تغيير‬) ■‫المصدري‬ ‫البرنامج‬ ‫تعريف‬(Source Program:) –‫حاسوبي‬ ‫برنامج‬ ‫تؤلف‬ ‫التي‬ ‫البرمجية‬ ‫النصوص‬ ‫مجموعة‬. 45
  • 46. Compiler Definition ■‫واحد‬ ‫بأن‬ ‫اثنين‬ ‫بعنصرين‬ ‫تتعلق‬ ‫المترجم‬ ‫بناء‬ ‫عملية‬ ‫إن‬: –‫المبرمج‬ ‫يستخدمها‬ ‫الذي‬ ‫المستوى‬ ‫عالية‬ ‫المصدرية‬ ‫البرمجة‬ ‫لغة‬. –‫عليه‬ ‫البرنامج‬ ‫تشغيل‬ ‫سيجري‬ ‫الذي‬ ‫التشغيل‬ ‫نظام‬. ■‫مثال‬: –‫لغة‬ ‫مترجم‬ ‫يختلف‬C++‫نظام‬ ‫على‬ ‫يعمل‬ ‫الذي‬windows‫لغة‬ ‫مترجم‬ ‫عن‬C++‫الذي‬ ‫نظام‬ ‫على‬ ‫يعمل‬Linux‫ف‬ ‫مختلفة‬ ‫تنفيذية‬ ‫تشغيل‬ ‫تعليمات‬ ‫توليد‬ ‫لضرورة‬ ‫نظرا‬ ،‫ي‬ ‫اللغة‬ ‫نفس‬ ‫عن‬ ‫نتكلم‬ ‫اننا‬ ‫من‬ ‫بالرغم‬ ،‫الحالتين‬ ‫هاتين‬. –‫لغة‬ ‫مترجم‬ ‫يختلف‬C++‫لغة‬ ‫مترجم‬ ‫عن‬Pascal‫يعمالن‬ ‫المترجمان‬ ‫كان‬ ‫ولو‬ ‫حتى‬ ‫نظام‬ ‫على‬Windows‫مختلفتين‬ ‫برمجيتين‬ ‫لغتين‬ ‫نترجم‬ ‫اننا‬ ‫نظرا‬ ،. 46
  • 48. Compiler Structure ■‫أساسيتين‬ ‫مرحلتين‬ ‫من‬ ‫الترجمة‬ ‫عملية‬ ‫تتألف‬: .1‫التحليل‬ ‫مرحلة‬(Analysis Phase:) ■‫ودالال‬ ‫صحتها‬ ‫من‬ ‫والتأكد‬ ‫وجمل‬ ‫كلمات‬ ‫الى‬ ‫البرمجي‬ ‫النص‬ ‫تقسيم‬ ‫فيها‬ ‫يجري‬ ‫التي‬‫تها‬. .2‫التركيب‬ ‫مرحلة‬(Synthesis Phase:) ■‫بلغة‬ ‫ولكن‬ ‫المصدري‬ ‫النص‬ ‫داللة‬ ‫بنفس‬ ‫جديد‬ ‫برمجي‬ ‫نص‬ ‫تركيب‬ ‫فيها‬ ‫يجري‬ ‫التي‬‫أخرى‬ ‫التشغيل‬ ‫نظام‬ ‫يفهمها‬. 48
  • 49. Compiler Structure Analysis Phase (‫التحليل‬ ‫)مرحلة‬ 49
  • 50. Compiler Structure Analysis Phase (‫التحليل‬ ‫)مرحلة‬ ■‫خطوات‬ ‫ثالث‬ ‫تحوي‬ ‫التحليل‬ ‫مرحلة‬: .1‫المعجمي‬ ‫التحليل‬ ‫مرحلة‬(Lexical Analysis - Scanner:) ■‫الدخل‬ ‫يقرأ‬ ‫أن‬ ‫مهمته‬input‫الى‬ ‫ويحللها‬tokens‫من‬ ‫محدد‬ ‫جزء‬ ‫تمثل‬ ‫الكلمات‬ ‫هذه‬ ‫من‬ ‫وكل‬ ‫اللغة‬ ‫في‬ ‫الثابتة‬ ‫الكلمات‬ ‫من‬ ‫ام‬ ‫متغير‬ ‫أكان‬ ‫سواء‬ ‫اللغة‬(reserved word)‫بمسح‬ ً‫ايضا‬ ‫ويقوم‬ ‫الـ‬ ‫وحفظ‬ ‫المسافات‬tokens‫الرموز‬ ‫جدول‬ ‫في‬(symboltable.) .2‫الجملة‬ ‫بناء‬ ‫مرحلة‬(Syntax Analysis - Parser:) ■‫الـ‬ ‫يأخذ‬ ‫ان‬ ‫مهمته‬tokens‫مرحلة‬ ‫عن‬ ‫الناتجة‬(Lexical Analysis)‫برمجية‬ ‫جمل‬ ‫في‬ ‫ويكونها‬ ‫اللغة‬ ‫قواعد‬ ‫أساس‬ ‫على‬ ‫صحتها‬ ‫ويختبر‬. ■‫يدعى‬ ‫داللي‬ ‫هيكل‬ ‫عنها‬ ‫ينتج‬ ‫المرحلة‬ ‫هذه‬(semantic structure)‫البرمجية‬ ‫الجمل‬ ‫وتكون‬ ‫ب‬ ‫يسمى‬ ‫شجري‬ ‫شكل‬ ‫على‬(parser tree.) .3‫الداللي‬ ‫التحليل‬ ‫مرحلة‬(Semantic Analysis – Intermediate Code Generator:) ■‫لن‬ ‫القيمة‬ ‫اسناد‬ ‫صحة‬ ‫مثل‬ ،‫بالمنطق‬ ‫المتعلقة‬ ‫األخطاء‬ ‫من‬ ‫التحقق‬ ‫يتم‬ ‫المرحلة‬ ‫هذه‬ ‫في‬‫وع‬ ‫وغيرها‬ ‫المتغير‬. 50
  • 51. Compiler Structure Analysis Phase (‫التحليل‬ ‫)مرحلة‬ ■‫ال‬ ‫عمل‬ ‫مراحل‬compiler: –‫اللفظي‬ ‫التحليل‬: ■‫ال‬ ‫ام‬ ‫البرمجة‬ ‫لغة‬ ‫في‬ ‫موجودة‬ ‫الكلمة‬ ‫كانت‬ ‫ما‬ ‫اذا‬ ‫معرفة‬ ‫أي‬. –‫القواعدي‬ ‫التحليل‬: ■‫ال‬ ‫على‬ ‫التعرف‬ ‫أي‬syntax‫المحكية‬ ‫اللغات‬ ‫في‬ ‫كما‬ ‫اللغة‬ ‫كتابة‬ ‫قواعد‬ ‫أي‬ ،. ■‫مثال‬: –‫يأتي‬ ‫ان‬ ‫يجب‬ ‫العربية‬ ‫اللغة‬ ‫في‬(‫فعل‬–‫فاعل‬–‫به‬ ‫مفعول‬.) –‫البرمجة‬ ‫لغات‬ ‫في‬:if ( statement ) –‫الداللي‬ ‫التحليل‬: ■‫دالليا‬ ‫ليس‬ ‫لكن‬ ‫و‬ ‫قواعديا‬ ‫صحيحة‬ ‫تكون‬ ‫ان‬ ‫الممكن‬ ‫من‬ ‫الجملة‬ ‫ان‬ ‫اي‬. ■‫مثال‬: –‫التفاحة‬ ‫الولد‬ ‫اكل‬. –‫المقعد‬ ‫الولد‬ ‫اكل‬. 51
  • 53. Compiler Structure Synthesis Phase (‫التركيب‬ ‫)مرحلة‬ ■‫المتوسطة‬ ‫اللغة‬ ‫تحويل‬ ‫يتم‬ ‫المرحلة‬ ‫هذه‬ ‫في‬(IntermediateLanguage)‫تفهمها‬ ‫لغة‬ ‫الى‬ ‫اآللة‬(Machine Language)‫التالي‬ ‫النحو‬ ‫على‬ ‫ذلك‬ ‫ويتم‬: .1‫األكواد‬ ‫تحسين‬ ‫مرحلة‬(Code Optimization:) ■‫بأن‬ ‫والتأكد‬ ‫البرنامج‬ ‫وتطوير‬ ‫التكرار‬ ‫وابعاد‬ ‫الكود‬ ‫تحسين‬ ‫مسألة‬ ‫تتولى‬ ‫الخطوة‬ ‫هذه‬‫يكون‬ ‫آخر‬ ‫مترجم‬ ‫عن‬ ‫مترجم‬ ‫تميز‬ ‫التي‬ ‫هي‬ ‫الخطوة‬ ‫هذه‬ ‫و‬ ‫حاالته‬ ‫أحسن‬ ‫في‬ ‫البرنامج‬. .2‫األكواد‬ ‫د‬ّ‫مول‬ ‫مرحلة‬(Code Generation:) ■‫اآللة‬ ‫تفهمه‬ ‫شكل‬ ‫الى‬ ‫نهائي‬ ‫بشكل‬ ‫الكود‬ ‫تحويل‬ ‫يتم‬ ‫هنا‬. 53
  • 55. Interpreter (‫)المفسر‬ ■‫خطوة‬ ‫خطوة‬ ‫ينفذه‬ ‫ولكن‬ ‫البرنامج‬ ‫يحول‬ ‫ال‬. 55
  • 56. Compiler Kinds ■‫معنى‬ ‫ما‬multi-pass compiler‫و‬one pass compiler‫؟‬ –One pass compiler: ■‫التحليل‬ ‫عمل‬ ‫يتم‬‫المفرداتي‬‫والتحلي‬ ‫التجميع‬ ‫فيها‬ ‫يتم‬ ‫التي‬ ‫اللحظة‬ ‫نفس‬ ‫في‬‫ل‬ ‫األعل‬ ‫من‬ ‫هذه‬ ‫المسح‬ ‫عملية‬ ‫ثم‬ ‫الداللي‬ ‫التحليل‬ ‫الى‬ ‫مباشرة‬ ‫االنتقال‬ ‫ثم‬ ‫القواعدي‬‫ى‬ ‫لالسفل‬. ■‫و‬ ‫بقراءة‬ ‫يعني‬ ‫والداللي‬ ‫والقواعدي‬ ‫اللفظي‬ ‫التحليل‬ ‫على‬ ‫المرور‬ ‫يعني‬ ‫الوحيد‬ ‫المرور‬‫حدة‬. –Multi pass compiler: ■‫اكتر‬ ‫وقت‬ ‫وتتطلب‬ ‫مرة‬ ‫من‬ ‫اكتر‬ ‫البرنامج‬ ‫مسح‬ ‫يتم‬. 56
  • 57. Parse Tree Example (number – number ) * number exp  exp op exp  exp op number  exp * number ( exp ) * number  ( exp op exp ) * number  ( exp op number) * number  ( exp – number ) * number  (number– number ) * number 57
  • 58. Abstract Syntax Trees ■ A parse tree contains much more information than is necessaryfor a compiler to produceexecutable code. ■ Syntax trees contain just the information needed for translation. 58
  • 59. Abstract Syntax Trees Example ■ Show the Parse and Syntax Trees of the derivation of the string 3 + 4 using the grammar. exp → exp op exp | ( exp ) | number op → + | - | * Parse Tree Syntax Tree 59
  • 60. Left-Recursion Removal A → A α | β β does not begin with A A → β is the base case A → A α is the recursive case A → β A' A' → α A' | ε exp → term exp' exp' → addop term exp' | ε To remove the recursion we re-write the rule : Example exp → exp addop term | term 60
  • 61. First Sets (‫األولى‬ ‫الرموز‬ ‫)مجموعة‬ ■ The First Set of a nonterminalA is the set of terminals that may appear as the first symbols in a string derived from A. A → BCD B → b | ε C → cd | ε D → e | ε First (A) = { b , c , e , ε } First (B) = { b, ε } First (C) = { c , ε} First (D) = { e , ε} A  b A  bcd A  bcde A  be A  cd A  cde A  e A  ε 61
  • 62. First Sets (‫األولى‬ ‫الرموز‬ ‫)مجموعة‬ ■ The First Set of a nonterminalA is the set of terminals that may appear as the first symbols in a string derived from A. Let X be a grammar symbol (a terminal or nonterminal) or ε. Then the set First(X) consisting of terminals, and possibly ε, is defined as follows: 1. If X is a terminal or ε, then First (X) = {X} 2. If X is a nonterminal, then 1. for each production choice X → X1 X2 … Xn , First (X) contains First (X1) – {ε}. 2. and if for some i < n, all the sets First (X1), …, First (Xi) contain ε, then First (X) contains First (Xi+1) – {ε}. 3. and if all the sets First (X1), …, First (Xn) contain ε, then First (X) also contains ε. 62
  • 63. First Sets Example Grammar Rule Pass 1 Pass 2 Pass 3 exp → exp addop term exp → term First (exp) = { ( , number} addop → + First (addop) = {+} addop → - First (adop) = {+,-} term → term mulop factor term → factor First (trem) = { ( , number} mulop → * First (mulop) = {*} factor→ ( exp ) First (factor) = { ( } factor→ number First (factor) = { ( , number} exp → exp addop term| term addop → + | - term → term mulop factor| factor mulop → * factor → ( exp ) | number 63
  • 64. Follow Sets (‫الالحقة‬ ‫الرموز‬ ‫)مجموعة‬ ■ The Follow Set of a nonterminalA is the set of terminals that may appear after A in a string derived from the start symbol. A → BCD B → b | ε C → cd | ε D → e | ε Follow (A) = $ Follow (B) = First (CD) – {ε} + Follow (A) = {c, e, ε} – {ε} + {$} = {c, e, $} Follow (C) = First (D) – {ε} + Follow (A) = {$, e} Follow (D) = Follow (A) = {$} 64
  • 65. Follow Sets (‫الالحقة‬ ‫الرموز‬ ‫)مجموعة‬ ■ The Follow Set of a nonterminalA is the set of terminals that may appear after A in a string derived from the start symbol. Given a nonterminal A, the set Follow(A), consisting of terminals, and possibly $, is defined as follows: 1. If A is the start symbol, then $ is in Follow(A) 2. If there is a production B → α A γ, then First (γ) – {ε} is in Follow (A). 3. If there is a production B → α A γ such that ε is in First (γ), then Follow (A) contains Follow (B). 65
  • 66. Follow Sets Example Grammar Rule Pass 1 Pass 2 exp → exp addop term Follow (exp) = {$, + , -} Follow (addop) = { ( , number} Follow (term) = {$, + , -} Follow (term) = { $, + , - , *, ) } exp → term term → term mulop factor Follow (term) = {$,+,-, *} Follow (mulop) = { ( , number} Follow (factor) = {$, +, -, *} Follow (factor) = { $, + , - , *, ) } term → factor factor → ( exp ) Follow (exp) = { $, + , - , ) } exp → exp addop term| term addop → + | - term → term mulop factor| factor mulop → * factor → ( exp ) | number First (exp) = { ( , number} First (term) = { ( , number} First (factor) = { ( , number} First (adop) = {+,-} First (mulop) = {*} 66
  • 68. Question 1 ■‫الصحيحة‬ ‫العبارة‬ ‫حدد‬: .A‫المفسر‬(interpreter)‫للمترجم‬ ‫اخر‬ ‫اسم‬ ‫هو‬(compiler)‫في‬ ‫فقط‬ ‫واالختالف‬ ‫المستخدم‬ ‫المصطلح‬. .B‫البرمج‬ ‫الكود‬ ‫من‬ ‫التحقق‬ ‫عملية‬ ‫فيها‬ ‫تتم‬ ‫التي‬ ‫المترجمات‬ ‫من‬ ‫نمط‬ ‫هو‬ ‫المفسر‬‫ي‬ ‫سطرا‬ ‫للعمليات‬ ‫وتنفيذ‬‫سطرا‬. .C‫األمثلة‬ ‫مرحلة‬ ‫بدون‬ ‫مترجم‬ ‫هو‬ ‫المفسر‬(Optimization Phase)‫البرمجي‬ ‫للكود‬. .D‫نهائيا‬ ‫بالمترجمات‬ ‫للمفسرات‬ ‫عالقة‬ ‫ال‬. 68
  • 69. Question 1 ■‫الصحيحة‬ ‫العبارة‬ ‫حدد‬: .A‫المفسر‬(interpreter)‫للمترجم‬ ‫اخر‬ ‫اسم‬ ‫هو‬(compiler)‫في‬ ‫فقط‬ ‫واالختالف‬ ‫المستخدم‬ ‫المصطلح‬. .B‫البرمج‬ ‫الكود‬ ‫من‬ ‫التحقق‬ ‫عملية‬ ‫فيها‬ ‫تتم‬ ‫التي‬ ‫المترجمات‬ ‫من‬ ‫نمط‬ ‫هو‬ ‫المفسر‬‫ي‬ ‫سطرا‬ ‫للعمليات‬ ‫وتنفيذ‬‫سطرا‬. .C‫األمثلة‬ ‫مرحلة‬ ‫بدون‬ ‫مترجم‬ ‫هو‬ ‫المفسر‬(Optimization Phase)‫البرمجي‬ ‫للكود‬. .D‫نهائيا‬ ‫بالمترجمات‬ ‫للمفسرات‬ ‫عالقة‬ ‫ال‬. 69
  • 70. Question 2 ■‫ب‬ ‫يتعلق‬ ‫فيما‬ ‫الصحيح‬ ‫الخيار‬ ‫اختر‬top down parsing‫المترجم‬ ‫بناء‬ ‫عند‬: .A‫حساب‬ ‫دائما‬ ‫الزم‬first‫حساب‬ ‫دون‬follow. .B‫حساب‬ ‫الزم‬follow‫دون‬first. .C‫حساب‬ ‫الزم‬follow‫واحيانا‬first. .D‫خطأ‬ ‫سبق‬ ‫ما‬ ‫كل‬. 70
  • 71. Question 2 ■‫ب‬ ‫يتعلق‬ ‫فيما‬ ‫الصحيح‬ ‫الخيار‬ ‫اختر‬top down parsing‫المترجم‬ ‫بناء‬ ‫عند‬: .A‫حساب‬ ‫دائما‬ ‫الزم‬first‫حساب‬ ‫دون‬follow. .B‫حساب‬ ‫الزم‬follow‫دون‬first. .C‫حساب‬ ‫الزم‬follow‫واحيانا‬first. .D‫خطأ‬ ‫سبق‬ ‫ما‬ ‫كل‬. 71
  • 72. Question 3 ■‫الصرفية‬ ‫القواعد‬ ‫استخدمنا‬ ‫حال‬ ‫في‬(grammer)‫صحيح‬ ‫عدد‬ ‫أي‬ ‫بنية‬ ‫توصف‬ ‫التي‬ ‫التالية‬ ‫حقيقي‬ ‫او‬R‫برمجة‬ ‫لغة‬ ‫ضمن‬: ■‫القوا‬ ‫وفق‬ ‫ما‬ ‫عدد‬ ‫عن‬ ‫للتعبير‬ ‫مقبولة‬ ‫صيغة‬ ‫تعتبر‬ ‫التالية‬ ‫الصيغ‬ ‫من‬ ‫صيغة‬ ‫اي‬‫السابقة‬ ‫عد‬. .A2.45E02 .BE1-1.1- .C1.1.1 .DE02+1 72
  • 73. Question 3 ■‫الصرفية‬ ‫القواعد‬ ‫استخدمنا‬ ‫حال‬ ‫في‬(grammer)‫صحيح‬ ‫عدد‬ ‫أي‬ ‫بنية‬ ‫توصف‬ ‫التي‬ ‫التالية‬ ‫حقيقي‬ ‫او‬R‫برمجة‬ ‫لغة‬ ‫ضمن‬: ■‫القوا‬ ‫وفق‬ ‫ما‬ ‫عدد‬ ‫عن‬ ‫للتعبير‬ ‫مقبولة‬ ‫صيغة‬ ‫تعتبر‬ ‫التالية‬ ‫الصيغ‬ ‫من‬ ‫صيغة‬ ‫اي‬‫السابقة‬ ‫عد‬. .A2.45E02 .BE1-1.1- .C1.1.1 .DE02+1 73
  • 74. Question 4 ■ A grammar that produces more than one parse tree for some sentenceis called: A. Ambiguous. B. Unambiguous. C. Regular. D. None of the mentioned. 74
  • 75. Question 4 ■ A grammar that produces more than one parse tree for some sentenceis called: A. Ambiguous. B. Unambiguous. C. Regular. D. None of the mentioned. 75
  • 76. Question 5 ■ An intermediate code form is: A. Postfix notation. B. Syntax trees. C. Three address codes. D. All of these. 76
  • 77. Question 5 ■ An intermediate code form is: A. Postfix notation. B. Syntax trees. C. Three address codes. D. All of these. 77
  • 78. Question 6 ■ Compiler translates the source code to: A. Executable code. B. Machine code. C. Binary code. D. Both B and C. 78
  • 79. Question 6 ■ Compiler translates the source code to: A. Executable code. B. Machine code. C. Binary code. D. Both B and C. 79
  • 80. Question 7 ■ Which of the following groups is/are token together into semantic structures? A. Syntax analyzer. B. Intermediatecode generation. C. Lexical analyzer. D. Semantic analyzer. 80
  • 81. Question 7 ■ Which of the following groups is/are token together into semantic structures? A. Syntax analyzer. B. Intermediatecode generation. C. Lexical analyzer. D. Semantic analyzer. 81
  • 82. Question 8 ■ _________is a process of finding a parse tree for a string of tokens: A. Parsing. B. Analyzing. C. Recognizing. D. Tokenizing. 82
  • 83. Question 8 ■ _________is a process of finding a parse tree for a string of tokens: A. Parsing. B. Analyzing. C. Recognizing. D. Tokenizing. 83
  • 84. Question 9 ■ What is the action of parsing the sourceprogram into proper syntactic classes? A. Lexical analysis. B. Syntax analysis. C. General syntax analysis. D. Interpretation analysis. 84
  • 85. Question 9 ■ What is the action of parsing the sourceprogram into proper syntactic classes? A. Lexical analysis. B. Syntax analysis. C. General syntax analysis. D. Interpretation analysis. 85
  • 86. Question 10 ■ Which of the following languages is generated by the given grammar? ■ S → a S | b S | ε A. anbm B. {a,b}* C. {a,b}n D. Other. 86
  • 87. Question 10 ■ Which of the following languages is generated by the given grammar? ■ S → a S | b S | ε A. anbm B. {a,b}* C. {a,b}n D. Other. 87
  • 88. Question 11 ■ Which of the following strings is not generated by the following grammar? ■ S → SaSbS|ε A. aabb B. abab C. aababb D. aabbb 88
  • 89. Question 11 ■ Which of the following strings is not generated by the following grammar? ■ S → SaSbS|ε A. aabb B. abab C. aababb D. aabbb 89
  • 90. Question 12 ■ Which of the following is NOT the set of regular expression R = (ab + abb)* bbab A. ababbbbab B. abbbab C. ababbabbbab D. abababab 90
  • 91. Question 12 ■ Which of the following is NOT the set of regular expression R = (ab + abb)* bbab A. ababbbbab B. abbbab C. ababbabbbab D. abababab 91
  • 92. Question 13 ■ In a one pass compiler, the syntax analysis is performed: A. After the lexical analysis. B. After the semantic analysis. C. With the lexical analysis. 92
  • 93. Question 13 ■ In a one pass compiler, the syntax analysis is performed: A. After the lexical analysis. B. After the semantic analysis. C. With the lexical analysis. 93
  • 94. Question 14 ■ When a syntax error appears, the compiler: A. Stops Immediately. B. Stops after collecting few Errors. C. Depends on the organization of the syntax rules. 94
  • 95. Question 14 ■ When a syntax error appears, the compiler: A. Stops Immediately. B. Stops after collecting few Errors. C. Depends on the organization of the syntax rules. 95
  • 96. Question 15 ■ Each deterministic finite automata (DFA) has an equivalent regular expression A. True. B. False. 96
  • 97. Question 15 ■ Each deterministic finite automata (DFA) has an equivalent regular expression A. True. B. False. 97
  • 98. Question 16 ■ Each Non deterministic finite automata (NFA) has an equivalent regular expression A. True. B. False. 98
  • 99. Question 16 ■ Each Non deterministic finite automata (NFA) has an equivalent regular expression A. True. B. False. 99
  翻译: