SlideShare uma empresa Scribd logo
1 de 5
Baixar para ler offline
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

Mais procurados (19)

Micro teaching junior high school
Micro teaching junior high schoolMicro teaching junior high school
Micro teaching junior high school
 
K map
K mapK map
K map
 
Conversion of in fix pre fix,infix by sarmad baloch
Conversion of in fix pre fix,infix by sarmad balochConversion of in fix pre fix,infix by sarmad baloch
Conversion of in fix pre fix,infix by sarmad baloch
 
Chapter 1 review (algebra) (UPDATED for A.C.)
Chapter 1 review (algebra) (UPDATED for A.C.)Chapter 1 review (algebra) (UPDATED for A.C.)
Chapter 1 review (algebra) (UPDATED for A.C.)
 
Str8ts: Solution to Weekly Extreme Str8ts #29
Str8ts: Solution to Weekly Extreme Str8ts #29Str8ts: Solution to Weekly Extreme Str8ts #29
Str8ts: Solution to Weekly Extreme Str8ts #29
 
Core 3 Functions 3
Core 3 Functions 3Core 3 Functions 3
Core 3 Functions 3
 
Exam1 (example with solutions)
Exam1 (example with solutions)Exam1 (example with solutions)
Exam1 (example with solutions)
 
2015 CMS Winter Meeting Poster
2015 CMS Winter Meeting Poster2015 CMS Winter Meeting Poster
2015 CMS Winter Meeting Poster
 
Core 3 Functions 1
Core 3 Functions 1Core 3 Functions 1
Core 3 Functions 1
 
Rationalnumbers
RationalnumbersRationalnumbers
Rationalnumbers
 
Vector
VectorVector
Vector
 
Vectors
VectorsVectors
Vectors
 
Graph Coloring
Graph ColoringGraph Coloring
Graph Coloring
 
Pointers
PointersPointers
Pointers
 
1.3 notes
1.3 notes1.3 notes
1.3 notes
 
Gsp 215 Effective Communication / snaptutorial.com
Gsp 215  Effective Communication / snaptutorial.comGsp 215  Effective Communication / snaptutorial.com
Gsp 215 Effective Communication / snaptutorial.com
 
Tutorials--Equations with Fractions
Tutorials--Equations with FractionsTutorials--Equations with Fractions
Tutorials--Equations with Fractions
 
1301 d1 january_2013_mark_scheme
1301 d1 january_2013_mark_scheme1301 d1 january_2013_mark_scheme
1301 d1 january_2013_mark_scheme
 
And or search
And or searchAnd or search
And or search
 

Destaque

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 13mattbentley34
 
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 studentsmattbentley34
 
Jan 13 mark scheme, report & model answer
Jan 13   mark scheme, report & model answerJan 13   mark scheme, report & model answer
Jan 13 mark scheme, report & model answermattbentley34
 
Concept of computer files
Concept of computer filesConcept of computer files
Concept of computer filesSamuel Igbanogu
 
As government and politics the constitution
As government and politics the constitutionAs government and politics the constitution
As government and politics the constitutionflissxoxo
 
Operating system notes
Operating system notesOperating system notes
Operating system notesSANTOSH RATH
 
Os solved question paper
Os solved question paperOs solved question paper
Os solved question paperAnkit Bhatnagar
 
Operating system concepts (notes)
Operating system concepts (notes)Operating system concepts (notes)
Operating system concepts (notes)Sohaib Danish
 
AS Politics - Revision Guide: Unit 1
AS Politics - Revision Guide: Unit 1AS Politics - Revision Guide: Unit 1
AS Politics - Revision Guide: Unit 1mattbentley34
 
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...vtunotesbysree
 

Destaque (19)

Compilers Final spring 2013 model answer
 Compilers Final spring 2013 model answer Compilers Final spring 2013 model answer
Compilers Final spring 2013 model answer
 
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
 
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
 
To lec 03
To lec 03To lec 03
To lec 03
 
Infos2014
Infos2014Infos2014
Infos2014
 
Jan 13 mark scheme, report & model answer
Jan 13   mark scheme, report & model answerJan 13   mark scheme, report & model answer
Jan 13 mark scheme, report & model answer
 
File organisation
File organisationFile organisation
File organisation
 
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
 
Concept of computer files
Concept of computer filesConcept of computer files
Concept of computer files
 
CS215 - Lec 9 indexing and reclaiming space in files
CS215 - Lec 9  indexing and reclaiming space in filesCS215 - Lec 9  indexing and reclaiming space in files
CS215 - Lec 9 indexing and reclaiming space in files
 
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
 
As government and politics the constitution
As government and politics the constitutionAs government and politics the constitution
As government and politics the constitution
 
Operating system notes
Operating system notesOperating system notes
Operating system notes
 
Aidan ferris
Aidan ferrisAidan ferris
Aidan ferris
 
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)
 
AS Politics - Revision Guide: Unit 1
AS Politics - Revision Guide: Unit 1AS Politics - Revision Guide: Unit 1
AS Politics - Revision Guide: Unit 1
 
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 Model answer of compilers june spring 2013

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
 
Simplifying algebraic expressions
Simplifying algebraic expressionsSimplifying algebraic expressions
Simplifying algebraic expressionsMalini Sharma
 
Ayush Jajoo(MCA2501622) AOA .pptx
Ayush Jajoo(MCA2501622) AOA .pptxAyush Jajoo(MCA2501622) AOA .pptx
Ayush Jajoo(MCA2501622) AOA .pptxAmanChoudhary329978
 
Outrageous Ideas for Graph Databases
Outrageous Ideas for Graph DatabasesOutrageous Ideas for Graph Databases
Outrageous Ideas for Graph DatabasesMax De Marzi
 
Lecture 3 (algebraic expressions)
Lecture 3 (algebraic expressions)Lecture 3 (algebraic expressions)
Lecture 3 (algebraic expressions)HarithaRanasinghe
 
Form 5 Additional Maths Note
Form 5 Additional Maths NoteForm 5 Additional Maths Note
Form 5 Additional Maths NoteChek Wei Tan
 
Add math may june 2016 p1
Add math may june 2016 p1Add math may june 2016 p1
Add math may june 2016 p1Don Cunningham
 
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
 

Semelhante a Model answer of compilers june spring 2013 (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
 
Unicamp 2015 - aberta
Unicamp 2015 - abertaUnicamp 2015 - aberta
Unicamp 2015 - aberta
 
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
 
SSC STUDY MATERIAL
SSC STUDY MATERIALSSC STUDY MATERIAL
SSC STUDY MATERIAL
 
Simplifying algebraic expressions
Simplifying algebraic expressionsSimplifying algebraic expressions
Simplifying algebraic expressions
 
Ayush Jajoo(MCA2501622) AOA .pptx
Ayush Jajoo(MCA2501622) AOA .pptxAyush Jajoo(MCA2501622) AOA .pptx
Ayush Jajoo(MCA2501622) AOA .pptx
 
Tech-1.pptx
Tech-1.pptxTech-1.pptx
Tech-1.pptx
 
Outrageous Ideas for Graph Databases
Outrageous Ideas for Graph DatabasesOutrageous Ideas for Graph Databases
Outrageous Ideas for Graph Databases
 
Lecture 3 (algebraic expressions)
Lecture 3 (algebraic expressions)Lecture 3 (algebraic expressions)
Lecture 3 (algebraic expressions)
 
Form 5 Additional Maths Note
Form 5 Additional Maths NoteForm 5 Additional Maths Note
Form 5 Additional Maths Note
 
PEA 305.pdf
PEA 305.pdfPEA 305.pdf
PEA 305.pdf
 
Add math may june 2016 p1
Add math may june 2016 p1Add math may june 2016 p1
Add math may june 2016 p1
 
DBMS CS3
DBMS CS3DBMS CS3
DBMS CS3
 
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)
 

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 Lec2
Theory of computation Lec2Theory of computation Lec2
Theory of computation Lec2
 
Theory of computation Lec1
Theory of computation Lec1Theory of computation Lec1
Theory of computation Lec1
 
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
 

Último

A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...RKavithamani
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 

Último (20)

A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 

Model answer of compilers june spring 2013

  • 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