SlideShare una empresa de Scribd logo
1 de 5
Page 1 of 5
Cairo University
Faculty of Computers and Information
Final Exam
Department: CS
Course Name: Compilers Date: Tuesday 4-June-2013
Course Code: CS419 Duration: 2 hours
Instructor(s): Dr. Hussien Sharaf Total Marks: 60
Question 1 [12 marks]
For each of the following languages do the following:
i. Understand the languages without any assistance;
ii. Write sample words for the language where you show the smallest possible word.
iii. Construct a regular expression that represents the language.
Note: Draw and fill the following table in your answer sheet:
Sample Regular Expression
a. Σ ={a, b} Only words with exactly two consecutive a's [2 marks]
Solution:
Sample = {aa, aab, baa, baab, bbaabbb …..}
b*aab*
b. Σ ={a, b} Only words such that { an
bm
| n is even and m is odd} where n and m indicate number
of “a”s and “b”s respectively. [3 marks]
Solution:
Sample = {b,aab,aabbb,..}
(aa)* b(bb)* + (aa)* a (bb)* b
c. Σ ={a, b} Words where “a” appears ONLY in even positions or doesn’t appear at all. [3 marks]
Solution:
Sample = { Λ b ba bbb bbba baba …..}
b+(bb+ba)*b*
(b(a+b))* + b*
(ba)*(bb)*(ba)*
b* + (b(bb)* (a+b) )*
d. Σ ={a, b} Construct a regular expression for all strings that have exactly one double letter in
them. “One double letter” implies two equal touching letters; triples or more are excluded. [4 marks]
Solution:
Sample = { aa, baa, …..}
(b + Λ)(ab)*aa(ba)*(b + Λ) + (a + Λ)(ba)*bb(ab)*(a + Λ)
Question 2 [8 marks]
For each of the following languages do the following:
i. Understand the languages without any assistance;
ii. Write sample words for the language where you show the smallest possible word.
iv. Draw a deterministic finite automaton that represents the language and handles incorrect
inputs.
Page 2 of 5
a. Σ = {a, b} words where “a” appears ONLY in the second position or doesn’t appear at all. [3 marks]
Solution:
Sample = { Λ b ba bbb bab babbb …..}
b. Σ = {a, b} Words that begin and ending with the same letter. [5 marks]
Solution:
Sample = { Λ b ba bbb bab babbb …..}
Question 3 [10 marks]
An if-statement indicated by L is a language where multiple executable instructions “S” can be used only
by embedding them inside curly brackets pair where execution starts with the inner curly brackets pair;
i.e. {{{S1}S2}S3} means executing s1, s2 then s3. One statement can only be embedded inside one curly
bracket pair where the statement “S” should always stick to the closing curly bracket. Finally a curly
bracket pair must always contain a statement i.e. { {S} } is NOT valid.
Sample for if-statement in L:
if ((c4) OR (c5 AND c6))
{{{s1}s2}s3}
else
{{s5}s6}
a. Understand the language without any assistance and check if the following grammar represents
the conditional statements “C” indicated by L. If NO then explain why? If YES then show left
derivation for “((C4) OR (C5 AND C6))”; Do each step in a separate line and show rule
number. [3 marks]
R1: C (C)
R2: C C AND C
R3: C C OR C
R4: C c1|c2|c3|c4|c5|c6| …|c10
Note: Draw and fill the following table in your answer sheet:
a
b
b
-+1 2
a,b
+3
a
a
-+1
b
4
a
b
3
a
+2
b
b
a
a
5
b
+4
Page 3 of 5
Derivation Rule Number
Solution:
YES
Derivation Rule Number
C (C) R1
(C OR C) R3
((C) OR C) R1
((c4) OR C) R4
((c4) OR (C)) R1
((c4) OR (C AND C)) R2
((c4) OR (c5 AND C)) R4
((c4) OR (c5 AND c6)) R4
b. Write a CFG for the instructions “S” similar to the CFG of the “C”. Can you avoid using
Lambda? [4 marks]
Solution:
R1: S {ST}
R2: S
R3: T s1|s2|s3|s4|s5|s6| …|s10
must be used to recursion of S
c. Can language L be described using regular expressions? Why? [3 marks]
Solution:
No.
Because L have nested structures brackets and parenthesis that must be balanced. RE cannot
describe balanced structures such as an
bn
Question 4 [30 marks]
Ζ delta is a language that allows for nested if-statements L, executable expressions “E” and conditional
statements “C”.
CFG:
R1: Z E
R2: Z L
R3: L if C Z
R4: L if C Z else Z
R5: C (C)
R6: C N and N
R7: E id = N * N
R10: N→ 0
R11: N→ 1
Sample for a Z statement:
if (0 and 1) if (0 and 0) id = 1*1
a. Specify which pair of rule(s) contains the left factoring and rewrite all the CFG with numbers
(after removing left factoring) factoring? Hint: if you need to introduce a new variable then use
letter M. [3 marks]
Solution:
R3 and R4
CFG:
R1: Z →E
R2: Z →L
Page 4 of 5
R3: L→ if C Z M
R4:M→
R5:M→ else Z
R6: C→ (C)
R7: C→N and N
R8: E →id = N * N
R9: N→ 0
R10: N→ 1
b. Construct a parsing table for Z. Note that the set of terminals is {if, else, (,),id=,*, and, 0,1} and
set of non-terminals is {Z, E, L, M, C, N} [3 marks]
Solution:
If else ( ) and id = * 0 1 $
Z R2 R1
E R8
L R3
M R5 R4 R4 R4
C R6 R7 R7
N R9 R10
c. Show the top-down parsing steps of the sample above using the parsing table constructed in the
previous section. [16 marks]
Solution:
Stack Input Parser action
Z if (0 and 1) if (0 and 0) id = 1*1
$
R2
L .. R3
if C Z M if .. Match
C Z M (0 and 1) .. R6
(C) Z M (0 and 1) .. Match
C) Z M 0 and 1) .. R7
N and N) Z M 0 and 1) .. R9
0 and N) Z M 0 and 1) .. Match
N) Z M 1) … R9
1) Z M 1) if (0 and 0) id = 1*1 $ Match
Z M if (0 and 0) id = 1*1 $ R2
L M if (0 and 0) id = 1*1 $ R3
if C Z M M if (0 and 0) id = 1*1 $ Match
C Z M M (0 and 0) id = 1*1 $ R6
(C) Z M M (0 and 0) id = 1*1 $ Match
C) Z M M 0 and 0) id = 1*1 $ R7
N and N) Z M M 0 and 0) id = 1*1 $ R9
0 and N) Z M M 0 and 0) id = 1*1 $ Match
N) Z M M 0) id = 1*1 $ R9
0) Z M M 0) id = 1*1 $ Match
Z M M id = 1*1 $ R1
E M M id = 1*1 $ R8
id = N * N M M id = 1*1 $ Match
N * N M M 1*1 $ R10
1 * N M M 1*1 $ Match
N M M 1 $ R10
1 M M 1 $ Match
M M $ R4
M $ R4
Page 5 of 5
Empty $ Accept
d. Show the 10 steps for bottom-up parsing of the sample above. [10 marks]
Solution:
Stack Input Action
$ if (0 and 1) if (0 and 0) id = 1*1
$
Shift
$if (0 and 1) … Shift
$if( 0 and 1) …. Shift
$if(0 and 1) …. Reduce R9
$if(N and 1) …. Shift
$if(N and 1) …. Shift
$if(N and 1 ) if (0 and 0) id = 1*1 $ Reduce R10
$if(N and N ) if (0 and 0) id = 1*1 $ Reduce R7
$if(C ) if (0 and 0) id = 1*1 $ Shift
$if(C) if (0 and 0) id = 1*1 $ Reduce R6
$if C if (0 and 0) id = 1*1 $ Shift
$if C if (0 and 0) id = 1*1 $ Shift
$if C if( 0 and 0) id = 1*1 $ Shift
$if C if(0 and 0) id = 1*1 $ Reduce R9
$if C if(N and 0) id = 1*1 $ Shift
$if C if(N and 0) id = 1*1 $ Shift
$if C if(N and 0 ) id = 1*1 $ Reduce R9
$if C if(N and N ) id = 1*1 $ Reduce R7
$if C if(C ) id = 1*1 $ Shift
$if C if(C) id = 1*1 $ Shift
$if C if(C) id = 1*1 $ Reduce R6
$if C if C id = 1*1 $ Shift
$if C if C id 1*1 $ Shift
$if C if C id = 1*1 $ Shift
$if C if C id = 1 *1 $ Reduce R10
$if C if C id = N *1 $ Shift
$if C if C id = N * 1 $ Shift
$if C if C id = N * 1 $ Reduce R10
$if C if C id = N * N $ Reduce R8
$if C if C E $ Reduce R1
$if C if C Z $ Reduce R4
$if C if C Z M $ Reduce R3
$if C L $ Reduce R2
$if C Z $ Reduce R4
$if C Z M $ Reduce R3
$ L $ Reduce R2
$Z $ Accept

Más contenido relacionado

La actualidad más candente

DESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSDESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMS
Gayathri Gaayu
 

La actualidad más candente (20)

Integrating Public & Private Clouds
Integrating Public & Private CloudsIntegrating Public & Private Clouds
Integrating Public & Private Clouds
 
Hash Function
Hash FunctionHash Function
Hash Function
 
Lec 5 asymptotic notations and recurrences
Lec 5 asymptotic notations and recurrencesLec 5 asymptotic notations and recurrences
Lec 5 asymptotic notations and recurrences
 
Task scheduling Survey in Cloud Computing
Task scheduling Survey in Cloud ComputingTask scheduling Survey in Cloud Computing
Task scheduling Survey in Cloud Computing
 
Text similarity measures
Text similarity measuresText similarity measures
Text similarity measures
 
Asymptotic Notation and Complexity
Asymptotic Notation and ComplexityAsymptotic Notation and Complexity
Asymptotic Notation and Complexity
 
Artificial Intelligence (November – 2018) [Choice Based | Question Paper]
Artificial Intelligence (November – 2018) [Choice Based | Question Paper]Artificial Intelligence (November – 2018) [Choice Based | Question Paper]
Artificial Intelligence (November – 2018) [Choice Based | Question Paper]
 
Computational Complexity: Complexity Classes
Computational Complexity: Complexity ClassesComputational Complexity: Complexity Classes
Computational Complexity: Complexity Classes
 
5. cs8451 daa anna univ question bank unit 5
5. cs8451 daa anna univ question bank unit 55. cs8451 daa anna univ question bank unit 5
5. cs8451 daa anna univ question bank unit 5
 
CMSC 56 | Lecture 16: Equivalence of Relations & Partial Ordering
CMSC 56 | Lecture 16: Equivalence of Relations & Partial OrderingCMSC 56 | Lecture 16: Equivalence of Relations & Partial Ordering
CMSC 56 | Lecture 16: Equivalence of Relations & Partial Ordering
 
Primality
PrimalityPrimality
Primality
 
Hybrid Cloud and Its Implementation
Hybrid Cloud and Its ImplementationHybrid Cloud and Its Implementation
Hybrid Cloud and Its Implementation
 
Ch04
Ch04Ch04
Ch04
 
5. message authentication and hash function
5. message authentication and hash function5. message authentication and hash function
5. message authentication and hash function
 
Aws Architecture Fundamentals
Aws Architecture FundamentalsAws Architecture Fundamentals
Aws Architecture Fundamentals
 
Bellman ford algorithm
Bellman ford algorithmBellman ford algorithm
Bellman ford algorithm
 
DESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSDESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMS
 
Ngrams smoothing
Ngrams smoothingNgrams smoothing
Ngrams smoothing
 
The MD5 hashing algorithm
The MD5 hashing algorithmThe MD5 hashing algorithm
The MD5 hashing algorithm
 
Presentation on Databases in the Cloud
Presentation on Databases in the CloudPresentation on Databases in the Cloud
Presentation on Databases in the Cloud
 

Destacado

Question, mark scheme, examiners report and model answer jan 13
Question, mark scheme, examiners report and model answer jan 13Question, mark scheme, examiners report and model answer jan 13
Question, mark scheme, examiners report and model answer jan 13
mattbentley34
 
Unit 2: Excellent revision aid..a must for all students
Unit 2: Excellent revision aid..a must for all studentsUnit 2: Excellent revision aid..a must for all students
Unit 2: Excellent revision aid..a must for all students
mattbentley34
 
Operating system notes
Operating system notesOperating system notes
Operating system notes
SANTOSH RATH
 
Os solved question paper
Os solved question paperOs solved question paper
Os solved question paper
Ankit Bhatnagar
 
Operating system concepts (notes)
Operating system concepts (notes)Operating system concepts (notes)
Operating system concepts (notes)
Sohaib Danish
 

Destacado (12)

Model answer of compilers june spring 2013
Model answer of compilers june spring 2013Model answer of compilers june spring 2013
Model answer of compilers june spring 2013
 
To lec 03
To lec 03To lec 03
To lec 03
 
Infos2014
Infos2014Infos2014
Infos2014
 
File Organization & processing Mid term summer 2014 - modelanswer
File Organization & processing Mid term summer 2014 - modelanswerFile Organization & processing Mid term summer 2014 - modelanswer
File Organization & processing Mid term summer 2014 - modelanswer
 
Question, mark scheme, examiners report and model answer jan 13
Question, mark scheme, examiners report and model answer jan 13Question, mark scheme, examiners report and model answer jan 13
Question, mark scheme, examiners report and model answer jan 13
 
Final Exam OS fall 2012-2013 with answers
Final Exam OS fall 2012-2013 with answersFinal Exam OS fall 2012-2013 with answers
Final Exam OS fall 2012-2013 with answers
 
Os Question Bank
Os Question BankOs Question Bank
Os Question Bank
 
Unit 2: Excellent revision aid..a must for all students
Unit 2: Excellent revision aid..a must for all studentsUnit 2: Excellent revision aid..a must for all students
Unit 2: Excellent revision aid..a must for all students
 
Operating system notes
Operating system notesOperating system notes
Operating system notes
 
Os solved question paper
Os solved question paperOs solved question paper
Os solved question paper
 
Operating system concepts (notes)
Operating system concepts (notes)Operating system concepts (notes)
Operating system concepts (notes)
 
SOLUTION MANUAL OF OPERATING SYSTEM CONCEPTS BY ABRAHAM SILBERSCHATZ, PETER B...
SOLUTION MANUAL OF OPERATING SYSTEM CONCEPTS BY ABRAHAM SILBERSCHATZ, PETER B...SOLUTION MANUAL OF OPERATING SYSTEM CONCEPTS BY ABRAHAM SILBERSCHATZ, PETER B...
SOLUTION MANUAL OF OPERATING SYSTEM CONCEPTS BY ABRAHAM SILBERSCHATZ, PETER B...
 

Similar a Compilers Final spring 2013 model answer

Similar a Compilers Final spring 2013 model answer (20)

KARNAUGH MAP(K-MAP)
KARNAUGH MAP(K-MAP)KARNAUGH MAP(K-MAP)
KARNAUGH MAP(K-MAP)
 
3rd Semester Computer Science and Engineering (ACU) Question papers
3rd Semester Computer Science and Engineering  (ACU) Question papers3rd Semester Computer Science and Engineering  (ACU) Question papers
3rd Semester Computer Science and Engineering (ACU) Question papers
 
regular expressions (Regex)
regular expressions (Regex)regular expressions (Regex)
regular expressions (Regex)
 
algo1
algo1algo1
algo1
 
Intermediate code generator
Intermediate code generatorIntermediate code generator
Intermediate code generator
 
Instruction Set Architecture: MIPS
Instruction Set Architecture: MIPSInstruction Set Architecture: MIPS
Instruction Set Architecture: MIPS
 
Compiling fµn language
Compiling fµn languageCompiling fµn language
Compiling fµn language
 
Exercises-set-theory-answer-key
Exercises-set-theory-answer-keyExercises-set-theory-answer-key
Exercises-set-theory-answer-key
 
Unicamp 2015 - aberta
Unicamp 2015 - abertaUnicamp 2015 - aberta
Unicamp 2015 - aberta
 
Tech-1.pptx
Tech-1.pptxTech-1.pptx
Tech-1.pptx
 
SSC STUDY MATERIAL
SSC STUDY MATERIALSSC STUDY MATERIAL
SSC STUDY MATERIAL
 
Lecture 3 (algebraic expressions)
Lecture 3 (algebraic expressions)Lecture 3 (algebraic expressions)
Lecture 3 (algebraic expressions)
 
Outrageous Ideas for Graph Databases
Outrageous Ideas for Graph DatabasesOutrageous Ideas for Graph Databases
Outrageous Ideas for Graph Databases
 
C Programming Interview Questions
C Programming Interview QuestionsC Programming Interview Questions
C Programming Interview Questions
 
Ayush Jajoo(MCA2501622) AOA .pptx
Ayush Jajoo(MCA2501622) AOA .pptxAyush Jajoo(MCA2501622) AOA .pptx
Ayush Jajoo(MCA2501622) AOA .pptx
 
Chapter 6 intermediate code generation
Chapter 6   intermediate code generationChapter 6   intermediate code generation
Chapter 6 intermediate code generation
 
Starr pvt. ltd. rachit's group ppt (1)
Starr pvt. ltd. rachit's group ppt (1)Starr pvt. ltd. rachit's group ppt (1)
Starr pvt. ltd. rachit's group ppt (1)
 
Computer programming mcqs
Computer programming mcqsComputer programming mcqs
Computer programming mcqs
 
Adobe
AdobeAdobe
Adobe
 
Data_Structure_and_Algorithms_Lecture_1.ppt
Data_Structure_and_Algorithms_Lecture_1.pptData_Structure_and_Algorithms_Lecture_1.ppt
Data_Structure_and_Algorithms_Lecture_1.ppt
 

Más de Arab Open University and Cairo University

Más de Arab Open University and Cairo University (20)

Theory of computation Lec6
Theory of computation Lec6Theory of computation Lec6
Theory of computation Lec6
 
Lec4
Lec4Lec4
Lec4
 
Theory of computation Lec3 dfa
Theory of computation Lec3 dfaTheory of computation Lec3 dfa
Theory of computation Lec3 dfa
 
Theory of computation Lec7 pda
Theory of computation Lec7 pdaTheory of computation Lec7 pda
Theory of computation Lec7 pda
 
Setup python with eclipse
Setup python with eclipseSetup python with eclipse
Setup python with eclipse
 
Cs419 lec8 top-down parsing
Cs419 lec8    top-down parsingCs419 lec8    top-down parsing
Cs419 lec8 top-down parsing
 
Cs419 lec11 bottom-up parsing
Cs419 lec11   bottom-up parsingCs419 lec11   bottom-up parsing
Cs419 lec11 bottom-up parsing
 
Cs419 lec12 semantic analyzer
Cs419 lec12  semantic analyzerCs419 lec12  semantic analyzer
Cs419 lec12 semantic analyzer
 
Cs419 lec9 constructing parsing table ll1
Cs419 lec9   constructing parsing table ll1Cs419 lec9   constructing parsing table ll1
Cs419 lec9 constructing parsing table ll1
 
Cs419 lec10 left recursion and left factoring
Cs419 lec10   left recursion and left factoringCs419 lec10   left recursion and left factoring
Cs419 lec10 left recursion and left factoring
 
Cs419 lec7 cfg
Cs419 lec7   cfgCs419 lec7   cfg
Cs419 lec7 cfg
 
Cs419 lec6 lexical analysis using nfa
Cs419 lec6   lexical analysis using nfaCs419 lec6   lexical analysis using nfa
Cs419 lec6 lexical analysis using nfa
 
Cs419 lec5 lexical analysis using dfa
Cs419 lec5   lexical analysis using dfaCs419 lec5   lexical analysis using dfa
Cs419 lec5 lexical analysis using dfa
 
Cs419 lec4 lexical analysis using re
Cs419 lec4   lexical analysis using reCs419 lec4   lexical analysis using re
Cs419 lec4 lexical analysis using re
 
Cs419 lec3 lexical analysis using re
Cs419 lec3   lexical analysis using reCs419 lec3   lexical analysis using re
Cs419 lec3 lexical analysis using re
 
Cs419 Compiler lec1&2 introduction
Cs419 Compiler lec1&2  introductionCs419 Compiler lec1&2  introduction
Cs419 Compiler lec1&2 introduction
 
CS215 - Lec 8 searching records
CS215 - Lec 8  searching recordsCS215 - Lec 8  searching records
CS215 - Lec 8 searching records
 
CS215 - Lec 7 managing records collection
CS215 - Lec 7  managing records collectionCS215 - Lec 7  managing records collection
CS215 - Lec 7 managing records collection
 
CS215 - Lec 6 record index
CS215 - Lec 6  record indexCS215 - Lec 6  record index
CS215 - Lec 6 record index
 
CS215 - Lec 5 record organization
CS215 - Lec 5  record organizationCS215 - Lec 5  record organization
CS215 - Lec 5 record organization
 

Último

Último (20)

Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 

Compilers Final spring 2013 model answer

  • 1. Page 1 of 5 Cairo University Faculty of Computers and Information Final Exam Department: CS Course Name: Compilers Date: Tuesday 4-June-2013 Course Code: CS419 Duration: 2 hours Instructor(s): Dr. Hussien Sharaf Total Marks: 60 Question 1 [12 marks] For each of the following languages do the following: i. Understand the languages without any assistance; ii. Write sample words for the language where you show the smallest possible word. iii. Construct a regular expression that represents the language. Note: Draw and fill the following table in your answer sheet: Sample Regular Expression a. Σ ={a, b} Only words with exactly two consecutive a's [2 marks] Solution: Sample = {aa, aab, baa, baab, bbaabbb …..} b*aab* b. Σ ={a, b} Only words such that { an bm | n is even and m is odd} where n and m indicate number of “a”s and “b”s respectively. [3 marks] Solution: Sample = {b,aab,aabbb,..} (aa)* b(bb)* + (aa)* a (bb)* b c. Σ ={a, b} Words where “a” appears ONLY in even positions or doesn’t appear at all. [3 marks] Solution: Sample = { Λ b ba bbb bbba baba …..} b+(bb+ba)*b* (b(a+b))* + b* (ba)*(bb)*(ba)* b* + (b(bb)* (a+b) )* d. Σ ={a, b} Construct a regular expression for all strings that have exactly one double letter in them. “One double letter” implies two equal touching letters; triples or more are excluded. [4 marks] Solution: Sample = { aa, baa, …..} (b + Λ)(ab)*aa(ba)*(b + Λ) + (a + Λ)(ba)*bb(ab)*(a + Λ) Question 2 [8 marks] For each of the following languages do the following: i. Understand the languages without any assistance; ii. Write sample words for the language where you show the smallest possible word. iv. Draw a deterministic finite automaton that represents the language and handles incorrect inputs.
  • 2. Page 2 of 5 a. Σ = {a, b} words where “a” appears ONLY in the second position or doesn’t appear at all. [3 marks] Solution: Sample = { Λ b ba bbb bab babbb …..} b. Σ = {a, b} Words that begin and ending with the same letter. [5 marks] Solution: Sample = { Λ b ba bbb bab babbb …..} Question 3 [10 marks] An if-statement indicated by L is a language where multiple executable instructions “S” can be used only by embedding them inside curly brackets pair where execution starts with the inner curly brackets pair; i.e. {{{S1}S2}S3} means executing s1, s2 then s3. One statement can only be embedded inside one curly bracket pair where the statement “S” should always stick to the closing curly bracket. Finally a curly bracket pair must always contain a statement i.e. { {S} } is NOT valid. Sample for if-statement in L: if ((c4) OR (c5 AND c6)) {{{s1}s2}s3} else {{s5}s6} a. Understand the language without any assistance and check if the following grammar represents the conditional statements “C” indicated by L. If NO then explain why? If YES then show left derivation for “((C4) OR (C5 AND C6))”; Do each step in a separate line and show rule number. [3 marks] R1: C (C) R2: C C AND C R3: C C OR C R4: C c1|c2|c3|c4|c5|c6| …|c10 Note: Draw and fill the following table in your answer sheet: a b b -+1 2 a,b +3 a a -+1 b 4 a b 3 a +2 b b a a 5 b +4
  • 3. Page 3 of 5 Derivation Rule Number Solution: YES Derivation Rule Number C (C) R1 (C OR C) R3 ((C) OR C) R1 ((c4) OR C) R4 ((c4) OR (C)) R1 ((c4) OR (C AND C)) R2 ((c4) OR (c5 AND C)) R4 ((c4) OR (c5 AND c6)) R4 b. Write a CFG for the instructions “S” similar to the CFG of the “C”. Can you avoid using Lambda? [4 marks] Solution: R1: S {ST} R2: S R3: T s1|s2|s3|s4|s5|s6| …|s10 must be used to recursion of S c. Can language L be described using regular expressions? Why? [3 marks] Solution: No. Because L have nested structures brackets and parenthesis that must be balanced. RE cannot describe balanced structures such as an bn Question 4 [30 marks] Ζ delta is a language that allows for nested if-statements L, executable expressions “E” and conditional statements “C”. CFG: R1: Z E R2: Z L R3: L if C Z R4: L if C Z else Z R5: C (C) R6: C N and N R7: E id = N * N R10: N→ 0 R11: N→ 1 Sample for a Z statement: if (0 and 1) if (0 and 0) id = 1*1 a. Specify which pair of rule(s) contains the left factoring and rewrite all the CFG with numbers (after removing left factoring) factoring? Hint: if you need to introduce a new variable then use letter M. [3 marks] Solution: R3 and R4 CFG: R1: Z →E R2: Z →L
  • 4. Page 4 of 5 R3: L→ if C Z M R4:M→ R5:M→ else Z R6: C→ (C) R7: C→N and N R8: E →id = N * N R9: N→ 0 R10: N→ 1 b. Construct a parsing table for Z. Note that the set of terminals is {if, else, (,),id=,*, and, 0,1} and set of non-terminals is {Z, E, L, M, C, N} [3 marks] Solution: If else ( ) and id = * 0 1 $ Z R2 R1 E R8 L R3 M R5 R4 R4 R4 C R6 R7 R7 N R9 R10 c. Show the top-down parsing steps of the sample above using the parsing table constructed in the previous section. [16 marks] Solution: Stack Input Parser action Z if (0 and 1) if (0 and 0) id = 1*1 $ R2 L .. R3 if C Z M if .. Match C Z M (0 and 1) .. R6 (C) Z M (0 and 1) .. Match C) Z M 0 and 1) .. R7 N and N) Z M 0 and 1) .. R9 0 and N) Z M 0 and 1) .. Match N) Z M 1) … R9 1) Z M 1) if (0 and 0) id = 1*1 $ Match Z M if (0 and 0) id = 1*1 $ R2 L M if (0 and 0) id = 1*1 $ R3 if C Z M M if (0 and 0) id = 1*1 $ Match C Z M M (0 and 0) id = 1*1 $ R6 (C) Z M M (0 and 0) id = 1*1 $ Match C) Z M M 0 and 0) id = 1*1 $ R7 N and N) Z M M 0 and 0) id = 1*1 $ R9 0 and N) Z M M 0 and 0) id = 1*1 $ Match N) Z M M 0) id = 1*1 $ R9 0) Z M M 0) id = 1*1 $ Match Z M M id = 1*1 $ R1 E M M id = 1*1 $ R8 id = N * N M M id = 1*1 $ Match N * N M M 1*1 $ R10 1 * N M M 1*1 $ Match N M M 1 $ R10 1 M M 1 $ Match M M $ R4 M $ R4
  • 5. Page 5 of 5 Empty $ Accept d. Show the 10 steps for bottom-up parsing of the sample above. [10 marks] Solution: Stack Input Action $ if (0 and 1) if (0 and 0) id = 1*1 $ Shift $if (0 and 1) … Shift $if( 0 and 1) …. Shift $if(0 and 1) …. Reduce R9 $if(N and 1) …. Shift $if(N and 1) …. Shift $if(N and 1 ) if (0 and 0) id = 1*1 $ Reduce R10 $if(N and N ) if (0 and 0) id = 1*1 $ Reduce R7 $if(C ) if (0 and 0) id = 1*1 $ Shift $if(C) if (0 and 0) id = 1*1 $ Reduce R6 $if C if (0 and 0) id = 1*1 $ Shift $if C if (0 and 0) id = 1*1 $ Shift $if C if( 0 and 0) id = 1*1 $ Shift $if C if(0 and 0) id = 1*1 $ Reduce R9 $if C if(N and 0) id = 1*1 $ Shift $if C if(N and 0) id = 1*1 $ Shift $if C if(N and 0 ) id = 1*1 $ Reduce R9 $if C if(N and N ) id = 1*1 $ Reduce R7 $if C if(C ) id = 1*1 $ Shift $if C if(C) id = 1*1 $ Shift $if C if(C) id = 1*1 $ Reduce R6 $if C if C id = 1*1 $ Shift $if C if C id 1*1 $ Shift $if C if C id = 1*1 $ Shift $if C if C id = 1 *1 $ Reduce R10 $if C if C id = N *1 $ Shift $if C if C id = N * 1 $ Shift $if C if C id = N * 1 $ Reduce R10 $if C if C id = N * N $ Reduce R8 $if C if C E $ Reduce R1 $if C if C Z $ Reduce R4 $if C if C Z M $ Reduce R3 $if C L $ Reduce R2 $if C Z $ Reduce R4 $if C Z M $ Reduce R3 $ L $ Reduce R2 $Z $ Accept