SlideShare uma empresa 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

Mais conteúdo relacionado

Mais procurados (20)

Merge sort
Merge sortMerge sort
Merge sort
 
Rsa
RsaRsa
Rsa
 
Cryptography-Known plain text attack
Cryptography-Known plain text attack Cryptography-Known plain text attack
Cryptography-Known plain text attack
 
Time complexity
Time complexityTime complexity
Time complexity
 
Approximation Algorithms
Approximation AlgorithmsApproximation Algorithms
Approximation Algorithms
 
Matrix chain multiplication
Matrix chain multiplicationMatrix chain multiplication
Matrix chain multiplication
 
Lec 5 asymptotic notations and recurrences
Lec 5 asymptotic notations and recurrencesLec 5 asymptotic notations and recurrences
Lec 5 asymptotic notations and recurrences
 
My presentation minimum spanning tree
My presentation minimum spanning treeMy presentation minimum spanning tree
My presentation minimum spanning tree
 
Heap and heapsort
Heap and heapsortHeap and heapsort
Heap and heapsort
 
Graphics in C++
Graphics in C++Graphics in C++
Graphics in C++
 
Elliptic curve cryptography
Elliptic curve cryptographyElliptic curve cryptography
Elliptic curve cryptography
 
Sudoku Solving with Computational Intelligence
Sudoku Solving with Computational IntelligenceSudoku Solving with Computational Intelligence
Sudoku Solving with Computational Intelligence
 
Hashing
HashingHashing
Hashing
 
Cracking WPA/WPA2 with Non-Dictionary Attacks
Cracking WPA/WPA2 with Non-Dictionary AttacksCracking WPA/WPA2 with Non-Dictionary Attacks
Cracking WPA/WPA2 with Non-Dictionary Attacks
 
Network flows
Network flowsNetwork flows
Network flows
 
DFS and BFS
DFS and BFSDFS and BFS
DFS and BFS
 
Cryptography
CryptographyCryptography
Cryptography
 
Knapsack Problem
Knapsack ProblemKnapsack Problem
Knapsack Problem
 
CYBER SECURITY audit course report
CYBER SECURITY audit course reportCYBER SECURITY audit course report
CYBER SECURITY audit course report
 
Cryptography and network security
Cryptography and network securityCryptography and network security
Cryptography and network security
 

Destaque

Destaque (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...
 

Semelhante a Compilers Final spring 2013 model answer

KARNAUGH MAP(K-MAP)
KARNAUGH MAP(K-MAP)KARNAUGH MAP(K-MAP)
KARNAUGH MAP(K-MAP)mihir jain
 
regular expressions (Regex)
regular expressions (Regex)regular expressions (Regex)
regular expressions (Regex)Rebaz Najeeb
 
Intermediate code generator
Intermediate code generatorIntermediate code generator
Intermediate code generatorsanchi29
 
Instruction Set Architecture: MIPS
Instruction Set Architecture: MIPSInstruction Set Architecture: MIPS
Instruction Set Architecture: MIPSPrasenjit Dey
 
Exercises-set-theory-answer-key
Exercises-set-theory-answer-keyExercises-set-theory-answer-key
Exercises-set-theory-answer-keyIreneJoy4
 
SSC STUDY MATERIAL
SSC STUDY MATERIALSSC STUDY MATERIAL
SSC STUDY MATERIALAvula Yadav
 
Lecture 3 (algebraic expressions)
Lecture 3 (algebraic expressions)Lecture 3 (algebraic expressions)
Lecture 3 (algebraic expressions)HarithaRanasinghe
 
Outrageous Ideas for Graph Databases
Outrageous Ideas for Graph DatabasesOutrageous Ideas for Graph Databases
Outrageous Ideas for Graph DatabasesMax De Marzi
 
C Programming Interview Questions
C Programming Interview QuestionsC Programming Interview Questions
C Programming Interview QuestionsGradeup
 
Ayush Jajoo(MCA2501622) AOA .pptx
Ayush Jajoo(MCA2501622) AOA .pptxAyush Jajoo(MCA2501622) AOA .pptx
Ayush Jajoo(MCA2501622) AOA .pptxAmanChoudhary329978
 
Chapter 6 intermediate code generation
Chapter 6   intermediate code generationChapter 6   intermediate code generation
Chapter 6 intermediate code generationVipul Naik
 
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)Rachit Mehta
 
Computer programming mcqs
Computer programming mcqsComputer programming mcqs
Computer programming mcqssaadkhan672
 
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.pptISHANAMRITSRIVASTAVA
 

Semelhante 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
 

Mais de Arab Open University and Cairo University

Mais 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

On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxPooja Bhuva
 
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...Poonam Aher Patil
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Pooja Bhuva
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...Amil baba
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 
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.pptxDr. Ravikiran H M Gowda
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jisc
 
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 FellowsMebane Rash
 
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 17Celine George
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
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 ClassroomPooky Knightsmith
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 

Último (20)

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
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
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...
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
NO1 Top Black Magic Specialist In Lahore Black magic In Pakistan Kala Ilam Ex...
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.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
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
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
 
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
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
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
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 

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