csci 3120 compiler construction · 2011. 8. 28. · csci 3120 compiler construction 2nd term,...
TRANSCRIPT
-
Page 1 of 33
CSCI 3120 Compiler Construction
2nd Term, 2010/2011
Lecturer: Leung Ho Fung
Time: T9 & W5, 6
-
Page 2 of 33
Teacher
Leung Ho Fung Room 1011, Ho Sin-hang Engineering Building
E-mail: [email protected]
Tel.: 2609 8428
Office Hours: Tuesday and Thursday 10am – 11am
-
Page 3 of 33
Teaching Assistants
Li Baichuan Room 1024, HSH Engg Bldg.
Sheng Bin Room 913, HSH Engg Bldg. [email protected]
Tang Xingfan Room 114A, HSH Engg Bldg.
Wang Qiong Room 1026, HSH Engg Bldg.
Wang Weiming Room 1026, HSH Engg Bldg. [email protected]
Xiong Junjie Room 101, HSH Engg Bldg.
-
Page 4 of 33
Reference Book
A. V. AHO, M. S. LAM, R. SETHI AND J. D. ULLMAN, 2007. Compilers: Principles,
Techniques, and Tools: International
Edition, 2/E. Pearson Higher Education.
-
Page 5 of 33
Schedule:
Week Topic Aho et al.
2007 Remarks
1
Lexical
Analysis Chapter 3
2
3 Assignment 1
CHINESE NEW YEAR HOLIDAYS
4
Syntax
Analysis Chapter 4
5 Quiz 1 (15 Feb.)
6
7 Assignment 2
8 Syn-
tax-Directed
Translation
Chapter 5
(partly)
9
10 Intermedi-
ate-Code
Generation
Chapter 6
(partly)
Assignment 3
11 Quiz 2 (29 Mar.)
12 Run-time
Environment
Chapter 7
(very part-
ly)
13 Code Gen-
eration
Chapter 8
(partly)
Assignment 4
14
-
Page 6 of 33
Assessment Scheme:
Assignments: 40% (10% each) Quizzes: 20% (10% each)
Examination: 40%
Each assignment includes written and programming
parts.
There is no mid-term examination.
There will be two quizzes on 15 February and 29 March
(45 min each).
The final examination lasts for 2 hours.
-
Page 7 of 33
What Will You Learn in this Course
Understanding the phases of compilation.
Algorithms in different compiler components and their
implementations.
Understanding what a compiler can do and why it can do
it.
-
Page 8 of 33
What is a COMPILER?
program Hello;
begin
writeln('Hello, world!');
end.
C:\>pascal_compile hello.pas
C:\>
hello.exe
-
Page 9 of 33
11010101010001010001010001…
Structure of a Compiler
program Hello;begin writeln('Hello, world!');end.
-
Page 10 of 33
11010101010001010001010001…
Structure of a Compiler
program Hello;begin writeln('Hello, world!');end.
-
Page 11 of 33
Lexical Analysis
int main(void)
{
printf("Hello, world!\n");
return 0;
}
Lexical
Analyser
int
main
(
void
)
{
printf
(
"Hello, world!\n"
⁞
-
Page 12 of 33
Lexical Analysis
program Hello;
begin
writeln('Hello, world!');
end.
Lexical
Analyser
program
Hello
;
begin
writeln
(
'Hello, world!'
)
⁞
;
-
Page 13 of 33
Lexical Analysis
with TEXT_IO;
procedure HELLO is
begin
end HELLO;
Lexical
Analyser
TEXT_IO
;
procedure
HELLO
is
begin
.
⁞
TEXT_IO
with
-
Page 14 of 33
Lexical Analysis
int main()
{
cout
-
Page 15 of 33
Tokens
i n t x 1 ; … e l s e
int
main
(
)
{
cout
⁞
-
Page 16 of 33
Inside tokens
= M * C ** E 2
-
Page 17 of 33
Inside tokens
= id 65234
* id 9745
** id 23749
832
number 43
-
Page 18 of 33
Inside tokens
id 9745
id 23749
832
number 43
id 65234
= M * C ** E 2
-
Page 19 of 33
Inside tokens
id 65234
id 9745
id 23749
832
number 43
= M * C ** E 2
-
Page 20 of 33
Inside tokens
id 65234
id 9745
id 23749
832
number 43
43
9745
65234
23749832
Lexeme Type …
2
…
…
C
…
…
…
M
…
…
E
…
…
-
Page 21 of 33
Inside tokens
id 65234
id 9745
id 23749
832
number 43
number 1
number 0
-
Page 22 of 33
Examples of Tokens
-
Page 23 of 33
Lexical Error
i n t x 1 ; … f i ( a = =
int
lexeme
;
lexeme?
x1
lexeme
-
Page 24 of 33
Lexical Error
i n t x 1 ; … f i ( a = =
int
lexeme
;
lexeme?
x1 The lexical ana-
lyser should think
this is an id and
return an id token.
lexeme
-
Page 25 of 33
Lexical Error
i n t x 1 ; … f i ~ = 1
int
lexeme
;
x1 What can we do?
lexeme
-
Page 26 of 33
Lexical Error
i n t x 1 ; … f i ~ = 1
int
lexeme
;
x1 ‘Panic Mode’ Recovery
Return an ‘error’ token, and
ignore all characters until
the next recognised token.
lexeme
-
Page 27 of 33
Lexical Error
i n t x 1 ; … f i ~ = 1
int
lexeme
;
x1 Other Possible Recovery
Delete 1 character.
Insert 1 character.
Replace 1 character.
Transpose two adjacent
characters.
lexeme
-
Page 28 of 33
Class Discussion
i n t 1 y 1 ;
i n t a ( ) ;
r e a l r 1 ;
i n t [ ] a ;
D O 1 0 I = 1 , 1 0
A D D 1 T O X .
-
Page 29 of 33
A Tricky Problem
(The for-loop in FORTRAN, called a DO-loop)
DO 10 I = 1, 10
A(I) = 0 10 CONTINUE
-
Page 30 of 33
A Tricky Problem
(The for-loop in FORTRAN, called a DO-loop)
DO 10 I = 1, 25
A(I) = 0.0 10 CONTINUE
…
…
DO 10 I = 1. 25
A(I) = 0.0
10 CONTINUE
IF (A(1) .EQ. 0.0)
… …
-
Page 31 of 33
A Tricky Problem
(The for-loop in FORTRAN, called a DO-loop)
DO 10 I = 1, 25
A(I) = 0.0 10 CONTINUE
…
…
DO10I = 1.25
A(I) = 0.0 10 CONTINUE
IF (A(1) .EQ. 0.0) … …
-
Page 32 of 33
Lexical Analyser and Parser
-
Page 33 of 33
Lexical Analyser and Parser