(i) (ii) - ntua › compilers › 2005a › slides-handout.pdf · decompilers...
TRANSCRIPT
������������
����� ��������
������ ������ �� ��������
��� � � ����� ���� ���� ��� ���� ��� ������
��������� ����� ����� ���� ���!
�� ��������!�� �" #$%&' (���)*���
�� ��������� �� ���� �� �� �������� ���� �����
�������� (i)
��������� ����� ������ ���� ��
���� ������������
�����
���������� �decompilers�
����� ������������� �meta-compilers�
������� ���������� ������������
��������������� �preprocessors�
! "��� ���������� �assemblers�
#�$����� ������ %��
�program generators�
�� ��������� �� ���� �� �� �������� ���� �����
�������� (ii)
!��$ ��������
&��� ���� �interpreters�
&��'�������� "�"���(�� �library managers�
!����� �linkers�
)������� �loaders�
���*��� ������ %�� �program editors�
��������� ���� %�� �debuggers�
���������� ���!��� �profilers�
�� ��������� �� ���� �� �� �������� ���� �����
�������� �������������(i)
+������ �����$�����
,� �����!���� ����%
,� �! ������� � ��� ������������ ���
��'��$� ��� ��� �����$� ������
,� ���������-�� �����% ���
�%(� ���(�!�
�� ��������� �� ���� �� �� �������� ���� �����
�������� �������������(ii)
�����*�(���� �����$�����
,� ���%��� ��������* �����
,� �'�� ���* '�*� �����������
,� �'�� ����� �����$���� $ �� ���% ��
����������
,� ���� ���% ���������% �� ���
,� �!�'�-�� ������ ��* �%(�
,� ���� �������� ��
�� ��������� �� ���� �� �� �������� ���� �����
����� �� ������������
.�����$ �%�!��
!������$ �%�!��
� ���������$ �%�!��
�������$ ���% ���! �����
+�������������
�������$ ������� �����
�� ��������� �� ���� �� �� �������� ���� �����
����� ��� ��������
αρχικό πρόγραµµα
Λεκτική ανάλυση
Συντακτική Ανάλυση
Σηµασιολογική Ανάλυση
Παραγωγή ενδιάµεσου κώδικα
Βελτιστοποίηση ενδιάµεσου κώδικα
Παραγωγή τελικού κώδικα
λεκτικές µονάδες
συντακτικό δέντρο
συντακτικό δέντρο
ενδιάµεσος κώδικας
ενδιάµεσος κώδικας
τελικός κώδικας
Βελτιστοποίηση τελικού κώδικα
τελικό πρόγραµµα
Πίνακαςσυµβόλων
Χειριστήςσφαλµάτων
�� ��������� �� ���� �� �� �������� ���� �����
����� ���������
/��%��� �� ���%� ���
/��%��� �� � ��*�(�� ��� ����(�� � $ �
�front-end 0 back-end�
1����'�� ��(*�����
���� ��������� �! %�� ��� �%�2�
��������% �internal�
�%� ��� �errors�
��������������% �� ��� �warnings�
���% �� ��� �messages�
�� ��������� �� ���� �� �� �������� ���� �����
�������� � bootstrapping
+$ � 3� ������������$� ��� S ⊂ A ��� T 4
+$ � 5� ������������$� ��� �� A ��� S4
+$ � 6� ������������$� ��� �� A ��� A4
S T
T
A T
S
A T
T
A T
A
A T
T
�� ��������� �� ���� �� �� �������� ���� ����
�������� �� �� ������
Πίνακαςσυµβόλων
Χειριστήςσφαλµάτων
Γεννήτορας τελικούκώδικα
Βελτιστοποιητής τελικούκώδικα
Οπίσθιο τµήµα
Συντακτικόςαναλυτής
Σηµασιολογικός αναλυτής
Γεννήτορας ενδιάµεσουκώδικαΛεκτικός
αναλυτής Βελτιστοποιητής ενδιάµεσουκώδικα
Εµπρόσθιο τµήµα
�� ��������� �� ���� �� �� �������� ���� ������
����� ������
#�� ����$ G = (T,N, P, S)
T � ��� ����% �� "��� a
N � � ��� ����% �� "��� A
P � ��*�� �������$� α → β
S � ��'��* �� "���
���������� � α, β, γ, δ ∈ (T ∪ N)∗��� (α → β) ∈ P
�*�� γαδ ⇒ γβδ
#����� L(G) = {α ∈ T ∗ | S ⇒+ α }
�� ��������� �� ���� �� �� �������� ���� ������
����!�� Chomsky
7���! 8� *��� �� ��� ������9 α → β
7���! 3� ��� ������ � �! ���-* ��
�context-sensitive�9 α → β � |α| ≤ |β|
7���! 5� ��� ������ '���� �! ���-* ��
�context-free� A → β
7���! 6� ������� ��� ������ �regular�
A → aB $ A → a
�����$ ���������� ������ ��! ���%��! ��
��$ �! "�������%
�� ��������� �� ���� �� �� �������� ���� ������
"���������
7���! 8� �'�$ Turing
7���! 3� ��� ��% �������� �� �'�$ Turing
7���! 5� �!�* ��� ����"��
�push-down automata�
:�$�� � ��� �!������$ �%�!��
7���! 6� ������� �� �!�* ���
�finite automata�
:�$�� � ��� ������$ �%�!��
�� ��������� �� ���� �� �� �������� ���� ������
������ ������
;������ ����%���� �regular expressions�
;�$ �! "�������%� ε
;%(� �� "���� a ∈ Σ
���%(��� ��� ����� ����%���� (rs)
&�%-�!�� ��� ����� ����%���� (r|s)
;����� � �$ %����� Kleene� (r∗)
!�� ���������
�������$ ������ ����(����
r+ [a1, a2, . . . , an] r? .
�� ��������� �� ���� �� �� �������� ���� ������
#��������� ��������
;�����%���� ��� ���"%����
,����� ������% �,���9 � ����� ������%
����� ��� ��� � ���� ���"%���� �����ε�
0 0
A B1
1
< ����� �� �! "������� ��!
����������� ��* 8 ��� 3 ��� �����'�! %����
���( * 3
�� ��������� �� ���� �� �� �������� ���� ������
"����� ��� ���$������
�����$ ��� ����$ ⇒ ����ε
����ε ⇒ �����$ ��� ����$
�����$ ������� ⇒ ����ε
����ε ⇒ �����$ �������
����ε ⇒ ,��
���'���������� ,���� ��������� �� ���� �� �� �������� ���� ������
%������ �������
.������� �%��� �tokens�
������-���� � ������� �� �!�* ���
��!����"%-�! ���'� ��� ������*����!�
'�����$���
����(���� �� � '��������
���(���! ����� ��! '���� ��������� ���
�!������$ �%�!��
�����*� �! "���� *�� �����% ��� ��%"����
�� ��������� �� ���� �� �� �������� ���� ������
&���������� ����'��� (i)
����������% ��� Pascal
D
L
L 10
D
L
L0 21 όχι L/D
*
(T_id)
�� ��������� �� ���� �� �� �������� ���� ������
&���������� ����'��� (ii)
7������� > ��� >=
> 10 = 2
>0
2
1
*
(T_gt)
όχι =
3 (T_ge)
=
�� ��������� �� ���� �� �� �������� ���� � ����
�������� ��� %" (i)
;�������$ ��� ����* ��� '�����$��
mapping : (ASCII ∪ { EOF }) → Σ
;�������$ ��� ����* ��� ������ �%��
;����������� ������ �%��
�����!(�� '�����$�� �lexeme�
'������ ��! �����% ���� ��%"����
��������� ��! �������� ���!�$
�� ��������� �� ���� �� �� �������� ���� ������
�������� ��� %" (ii)
��� ���!� (� ���
7�*��� ���'���� �� ������ �%��
'*���
&�%����� ��- 0 �������� ��� %��
���% ��� $ � �buffer�
�%�2� ��* ��%� ���
�� ��������� �� ���� �� �� �������� ���� ������
�������� ��� %" (iii)
'������
�!������
�����% ����
��%"���� =
W
.
20
0
21
;
22
(
23
)
:
4024
26
=
30
+
31
-
όχι ==
1
*
(T_assign)
(T_dot) (T_smcolon) (T_lparen) (T_eq) (T_plus) (T_minus)(T_rparen)
(T_colon)
4125
*
(T_le) (T_lt)
4227
*
(T_ge) (T_gt)
4328
*
(T_ne) (T_div)
4429
*
(T_power) (T_times)
< > */
L|E
L|E|D|_
45
*
(T_id)
6όχι L|E|D|_
D
D
.
D
E +|-10
D
D
11όχι D
46
*
(T_const)
όχι }
12}
0{
45
*
(T_eof)
EOF
όχι == όχι == όχι = όχι **
2 3 4 5
Ε
D
9
όχι D|Ε
8
όχι D|.|Ε
7
�� ��������� �� ���� �� �� �������� ���� ������
�������� ��� %" (iv)
����������� ��*��� !����������
:���������%
�� ����� ���"%���
�� �� ����������� flex�� ��������� �� ���� �� �� �������� ���� ������
(�������� %" �� �� flex (i)
������������ flex� ��$����� .�
�������� �����*��� � ��! ������%��� ���
�������� �%���
1������� ��*��� � �� C
< �!%����� yylex !������� �� .�
���������� �� �����* ��� ������$� �%���
��! �����������9 $ 8 ��� ����� ���
�! "�������%� ���*��!
7���(���� ��� ���"���$ yytext ��
�������'� �����!(�� '�����$�� �lexeme�
�� ��������� �� ���� �� �� �������� ���� ������
(�������� %" �� �� flex (ii)
&� $ ��! ��������% ����
����� �
%%
����� �
%%
����� �
;�� �� ���� ��� ����� � ���� ��%
�� ��������� �� ���� �� �� �������� ���� ������
(�������� %" �� �� flex (iii)
����� �9 �����'��
'*���9 *��� ��� C
;���� C9 ��� �� %{ ��� %}
�� ���% �* ��� �� �!�� ��������
����� ����%���
&������ ��'�� ������%���
�� ��������� �� ���� �� �� �������� ���� ������
(�������� %" �� �� flex (iv)
����� �9 ���%���� �
%{#define T_eof 0#define T_id 1
...#define T_while 52
void ERROR (const char msg []);%}
L [A-Za-z] /* letters */D [0-9] /* digits */W [ \t\n] /* white space */
�� ��������� �� ���� �� �� �������� ���� ������
(�������� %" �� �� flex (v)
����� +9 �����'�� ��*�� ��� ���$�
����� �� ��� ������
;%(� ������� ���� �� ����$ ��� C
.����!�����
&��"%-���� �� �������� ��*(� � ���
�! "�������%� ���*��! ��! ����� �
���������� ��* �%���� �����$ �������
���������� � �������'� �������
�� ��������� �� ���� �� �� �������� ���� ������
(�������� %" �� �� flex (vi)
;������ ����%����
a � ���������� a�. ��� ��� ���������� ����� ��� ���
����� ��������
\x �� x ��� �� �� a� b� f� n� r� t� v � 0� ����
��� ��� C� ������ � �� ���������� x�
\123 � ���������� ASCII �� ��� ��� ���� 123�
\x3f � ���������� ASCII �� ������ ��� ���� 3F�
"abc" � ��������� abc�
[abc] !"��� �� ��� ���������� a� b � c�
[a-z] !"��� �� ��� ���������� a ��� z�
[ac-fs] !"��� �� ��� ���������� a� c ��� f� � s�
�� ��������� �� ���� �� �� �������� ���� � ����
(�������� %" �� �� flex (vii)
;������ ����%���� ��!�'����
[^a-z] !"��� �� ��� ���������� ����� ����
������ ���� ����� a ��� z�
{name} � ������� ��#���� �� �������� ����
name�
rs � �� $��� ��� r ��� s�
r|s � � %���� ��� r ��� s�
r/s � ������� ��#���� r ��� ��� ��
����$�� � ������� ��#���� s�
(r) � ������� ��#���� r� �� ����$����� ������
��&���� ��� �� �����
�� ��������� �� ���� �� �� �������� ���� ������
(�������� %" �� �� flex (viii)
;������ ����%���� ��!�'����
r* � r �������� ����� �� �� � �����������
#����
r+ � r �������� ����� ��� � �����������
#����
r? � r ����� ��������� '�������� �����
�� �� � ��� #� (�
r{7} � r �������� ����� ������� 7 #����
r{3,5} � r �������� ����� �� 3 ��� 5 #����
r{4,} � r �������� ����� 4 � �����������
#����
^r � r ��� ��� ���� ���� ���� ��������
r$ � r ��� ��� �� ���� ���� ��������
�� ��������� �� ���� �� �� �������� ���� ������
(�������� %" �� �� flex (ix)
;������ ����%���� ��!�'����
<S>r � r ��� ��� ���� � ������� ������
��� ����� ����� � S�
<S1,S2,S3>r � r� ��� ��� ���� � ������� ���
���� ��� ����� ����� ��� �� ��� S1�
S2 � S3�
<*>r � r �� �� ��� ������ ��� ������
<<EOF>> ) ���� �� ������ ���� ��
�� ��������� �� ���� �� �� �������� ���� ������
(�������� %" �� �� flex (x)
����� +9 ���%���� �
"and" { return T_and; }...
"while" { return T_while; }
":=" { return T_assign; }":" { return T_colon; }
{L}({L}|{D}|_)* { return T_id; }{D}+(\.{D}*(e\-?{D}+)?)? { return T_const; }{W}+ { /* nothing */ }"(*"([^*]+|\*+[^*)])*\*+")" { /* nothing */ }
. { ERROR("illegal token"); }
�� ��������� �� ���� �� �� �������� ���� ������
(�������� %" �� �� flex (xi)
����� #9 �����'�� ����� C
���%���� �
void ERROR (const char msg []){
fprintf(stderr, "ERROR: %s\n", msg);exit(1);
}
�� ��������� �� ���� �� �� �������� ���� ������
(�������� %" �� �� flex (xii)
���%���� � ��!�'����int main (){
int token;
do {token = yylex();printf("token=%d, lexeme=\"%s\"\n",
token, yytext);} while (token != T_eof);
return 0;}
�� ��������� �� ���� �� �� �������� ���� ������
(�������� %" �� �� flex (xiii)
���%���� �� ���( ��� ���
int lineno = 1;
[ \t]+ { /* nothing */ }\n { lineno++; }
void ERROR (const char msg []){
fprintf(stderr, "ERROR, line %d: %s\n",lineno, msg);
exit(1);}
��*"�� �� .%(�� ���( ��� �� �'*���
�� ��������� �� ���� �� �� �������� ���� ������
(�������� %" �� �� flex (xiv)
��'���� ������%����
;����� %s
�������������� %x
������ ��*�� �� �%���� ���%�����
���%"��� ����� ������%���� BEGIN(s)
��'��$ ���%����� ���% �� ����� �����!�����
��! .�� INITIAL
�� ��������� �� ���� �� �� �������� ���� ������
(�������� %" �� �� flex (xv)
���%���� �� ���( ��� ��� ���*�(����
%x COMMENT
"(*" { BEGIN(COMMENT); }<COMMENT>"*)" { BEGIN(INITIAL); }<COMMENT>\n { lineno++; }<COMMENT>"*" { /* nothing */ }<COMMENT>[^*\n]+ { /* nothing */ }
�� ��������� �� ���� �� �� �������� ���� ������
)����� !��� ���*��+�����
#�� ������ '���� �! ���-* ��� A → α
�������*���� 0 ����*���� �������$
�leftmost / rightmost derivation�
!������% �����
�parse trees�
S ⇒L aABC ⇒L aAbBc⇒L acSBbBc ⇒L acBbBc⇒L acabBc ⇒L acabac
S ⇒R aABc ⇒R aAac⇒R aAbac ⇒R acSBbac⇒R acSabac ⇒R acabac
S
a A B c
abA
c
a
BS
ε
�� ��������� �� ���� �� �� �������� ���� � ����
&�*���,���� ��������� (i)
��� ��� ����$ ���� ������� �� �ambiguous�
*�� !�%�'�! ��� $ ������*���� �!������%
����� ��� �� ���� �����* �� �! "�������%
#�� ������ ��� ������ ����� ������� ���
�inherently ambiguous�
���%���� � ������� ��� ��� ����$��
����� ���� if �dangling if�
stmt → if cond stmt else stmt | if cond stmt | s1 | s2
cond → c1 | c2
��� � �! "�������%� if c1 if c2 s1 else s2
�� ��������� �� ���� �� �� �������� ���� ������
&�*���,���� ��������� (ii)stmt
if cond stmt
c1 if cond stmt
c2 s1
else stmt
s2
if c1 ( if c2 s1 else s2 )
if c1 ( if c2 s1 ) else s2
stmt
if cond stmt
c1 if cond stmt
c2 s1
else stmt
s2
�� ��������� �� ���� �� �� �������� ���� ������
-��'������ BNF/EBNF
Backus-Naur Form
� "��� ::= ���!� ��*��
�� ��� ����% �� "��� �� �������
����(�����9 �4'4 〈expr〉
� "��� | ��� ��%-�!��
Extended Backus-Naur Form
7�� ����% �� "��� �� ���������%
����(����� ��� � ���������
������� ��� ����������% � $ ���
� "��� ∗ ��� + ��� ���%��2�
�� ��������� �� ���� �� �� �������� ���� ������
-��������� �������
!������* ����� �parse tree�
;������!%-���� � ��� ��*��!��
��* �%� ���� �� �%�� �top-down�
�����$ ������� ��* �� ��-� ���
���'����� ���� �� �����
��* �%�� ���� �� �%� �bottom-up�
�����$ ������� ��* �� ����� ���
���'����� ���� �� ��-�
�� ��������� �� ���� �� �� �������� ���� ������
Top-down ��� bottom-up
S
a A B c
abA
c
a
BS
ε
από πάνω προς τα κάτω(top-down)
1
2 3
4
5 6
7
8
9
10
11
12
13
S
a A B c
abA
c
a
BS
ε
από κάτω προς τα πάνω(bottom-up)
12
1 8
6
25
4
3
7
10
9
11
�� ��������� �� ���� �� �� �������� ���� ������
.��/���� ����� (i)
���� FIRST
1���� �! "�������% α ∈ (T ∪ N)∗
7� ����� FIRST(α) ⊆ (T ∪ { ε }) �����'��
�� ��� ����% �� "��� ��* �� �����
��'�-�! �� �! "��������� ��! ���%�����
��* �� α
� α ⇒ ε9 �*�� ε ∈ FIRST(α)
�� ��������� �� ���� �� �� �������� ���� ������
.��/���� ����� (ii)
���� FOLLOW
1���� � ��� ����* �� "��� A
7� ����� FOLLOW(A) ⊆ (T ∪ { EOF })
�����'�� �� ��� ����% �� "��� ��! �����
� �����!(�� �� A ��� ��%����� ���
�������$�
� �� A ����� � ���� �� ����!����
�� "��� �� �� �������$9 �*��
EOF ∈ FOLLOW(A)
�� ��������� �� ���� �� �� �������� ���� ������
(��������� FIRST (i)
FIRST(ε) = { ε }FIRST(aβ) = { a }
� ε �∈ FIRST(A)
�*�� FIRST(Aβ) = FIRST(A)
� ε ∈ FIRST(A)
�*�� FIRST(Aβ) = (FIRST(A) − { ε })∪ FIRST(β)
��� �%(� ��*� A → α9 ������
FIRST(α) ⊆ FIRST(A)
�� ��������� �� ���� �� �� �������� ���� ������
(��������� FIRST (ii)
���%���� �
FIRST(E) = { id, ( }FIRST(T) = { id, ( }FIRST(F) = { id, ( }FIRST(E’) = {+, ε }FIRST(T’) = { *, ε }
E → T E’
E’ → ε
E’ → + T E’
T → F T’
T’ → ε
T’ → * F T’
F → ( E )
F → id
�� ��������� �� ���� �� �� �������� ���� ������
(��������� FOLLOW (i)
EOF ∈ FOLLOW(S)
��� �%(� ��*� A → αBβ
(FIRST(β) − { ε }) ⊆ FOLLOW(B)
� ε ∈ FIRST(β)
�*�� FOLLOW(A) ⊆ FOLLOW(B)
�� ��������� �� ���� �� �� �������� ���� � ����
(��������� FOLLOW (ii)
���%���� �
FOLLOW(E) = { ), EOF }FOLLOW(T) = {+, ), EOF }FOLLOW(F) = { *,+, ), EOF }FOLLOW(E’) = { ), EOF }FOLLOW(T’) = {+, ), EOF }
FIRST(E’) = {+, ε }FIRST(T’) = { *, ε }
E → T E’
E’ → ε
E’ → + T E’
T → F T’
T’ → ε
T’ → * F T’
F → ( E )
F → id
�� ��������� �� ���� �� �� �������� ���� ������
-" bottom-up (i)
< �!������$ �%�!�� ����% ��* �� �����
;%(� ���%9 ��-��%�
�� �������*���� �* "� ��! �����!
��! �� �'�� ��* � �������!�����
� *�� �� �����% ��! �'�! �������!�����
������ "%�� �'�� � �������!����� � ��-�
��%����� �reducing�� � ������$ �� �* "�
��! (� ���������! �� �����% �*� ��!
�* "�!
�� ��������� �� ���� �� �� �������� ���� ������
-" bottom-up (ii)
r
B
D
a . br
D
a . b r
B
D D
a . b
r
B
B
D D
a . b
S
r
B
B
D D
a . b
S → r B
B → D | B , D
D → a | b
�� ��������� �� ���� �� �� �������� ���� ������
-" bottom-up (iii)
� ����(�������%������ �shift-reduce�
:���� ������ �� ���'��% ��$� ����"�
*��! ����(���� �� "��� ��� ��� ����$�
/���(��� �shift�� ������% �*� �! "*��!
��* �� ������ ��� ���!�$ ��� ����"��
��%����� �reduce�� �������� ��* ��
���!�$ ��� ����"�� ��! ������ ���!� �*�
��*� ��� ��*�(��� ��! ��������� ���!�
����!'��� � ����"� �����'�� *� �� S ���
�� �� "��� ��� ���*��! �'�! ������(��
�� ��������� �� ���� �� �� �������� ���� ������
-" bottom-up (iv)
���� ������ ����� ��� ����� ������
0 ε r a , b ��������
1 r a , b ��������
2 r a , b �������� �� D → a3 r D , b �������� �� B → D4 r B , b ��������
��� �������� �� S → r B�
shift/reduceconflict
5 r B , b ��������
6 r B , b ε �������� �� D → b7 r B , D ε �������� �� B → B , D
��� �������� �� B → D�
reduce/reduceconflict
8 r B ε �������� �� S → r B9 S ε ������� ��
�� ��������� �� ���� �� �� �������� ���� ������
-" bottom-up (v)
LR(k)
LR(0)
SLR(1)
LALR(1)
LR(1)Πίνακες ελέγχου
συντακτικής ανάλυσηςLR(1)
Συντακτικός Αναλυτής LR(1)
συµβολοσειρά εισόδου
στοίβα
a1 a2 an... EOF
επόµενοσύµβολο
κορυφή
s0
x1
sm
...
s1
xm
ACTION NEXT
�� ��������� �� ���� �� �� �������� ���� ������
(�������� -" �� �� bison(i)
������������ bison� ��$����� � LALR(1)
�������� �����*��� � ��! ������%��� ��
������ ��� ��� �� ����������� ��!����
1������� ��*��� � �� C
< �!%����� yyparse !������� �� �
���������� 8 � ���������� �
�! "�������% ���*��! $ 3 �� ���������
�!�������� ��%� ����
!���%-���� � �� ������* ���!�$
��!%����� yylex��� ��������� �� ���� �� �� �������� ���� ������
(�������� -" �� �� bison(ii)
&� $ ��! ��������% ����
����� �%%
����� �
%%����� �
;�� �� ���� ��� ����� � ���� ��%
�� ��������� �� ���� �� �� �������� ���� ������
(�������� -" �� �� bison(iii)
����� �9 �����'��
'*���9 *��� ��� C
;���� C9 ��� �� %{ ��� %}
&������ ������ �%��
&������ ������ ��� ��'��$� ������
���������*����9 ��������������*�����
&$���� ��! �!*��! �� ��������� ��
������ YYSTYPE $ � '�$�� ��! %union�
&$���� ��!
����! ��� �� ���������$� �� $� �%(�
�! "*��!
�� ��������� �� ���� �� �� �������� ���� ������
(�������� -" �� �� bison(iv)
����� �9 ���%���� �
%{void ERROR (const char msg []);%}
%token T_program "program"%token T_div T_mod%token T_if T_then T_else
%nonassoc ’=’ ’<’ ’>’%left ’+’ ’-’%left ’*’ ’/’ T_div T_mod
�� ��������� �� ���� �� �� �������� ���� � ����
(�������� -" �� �� bison(v)
����� �9 ���%���� � ��!�'����
%union{int i;double f;char str[80];
}
%token<str> T_id%token<i> T_int_const%token<f> T_float_const
%type<f> expression
�� ��������� �� ���� �� �� �������� ���� ������
(�������� -" �� �� bison(vi)
����� +9 �����'���
��!� ��*�� �������$� �� ���$ BNF
�� ����������� ��!���� ��! ����������
���% �� �!������$ �%�!��
/� ��*�� �'�! �� ���$�
A : x11 x1
2 . . . x1m1
| x21 x2
2 . . . x2m2
. . .
| xn1 xn
2 . . . xnmn
;
�� ��������� �� ���� �� �� �������� ���� ������
(�������� -" �� �� bison(vii)
����� #9 �����'�� ����� C
7� �����*��� � ��! bison ���� "%�� ��
������* ����'� ��! �����������$ ��!
����!�'%���� � �� �!������� ��
�����%���
��! �������� ���!�$
��! �!�������� ���!�$
��! ����� �! "*��
��! �� ����������� ���!�$
��! ��$���� ���% ���! �����
�� ��������� �� ���� �� �� �������� ���� ������
(�������� -" �� �� bison(viii)
����� +9 ���%���� �
program : { count=0; } block_list{ printf("Counted %d block(s)\n",
count); };
block_list : /* nothing */| block_list block { count++; };
block : "begin" block_list "end";
�� ��������� �� ���� �� �� �������� ���� ������
(�������� -" �� �� bison(ix)
����� #9 ���%���� �
void yyerror (const char * msg){
fprintf(stderr,"syntax error in line %d: %s\n",linecount, msg);
exit(1);}
int main (){
return yyparse();}
�� ��������� �� ���� �� �� �������� ���� ������
(�������� -" �� �� bison(x)
���%���� � � �� ����������� �� ��
E → T
E → E + T
T → F
T → T * F
F → ( E )
F → num
=���� ��� � �������!����� � ��! �
!������-�� �� �� $ ��� ���( ����$� ��������
�� ��������� �� ���� �� �� �������� ���� ������
(�������� -" �� �� bison(xi)
���%���� � ��!�'����
%{typedef int YYSTYPE;%}
%token T_num
%%
program :expression { printf("Value: %d\n", $1); }
;
�� ��������� �� ���� �� �� �������� ���� ������
(�������� -" �� �� bison(xii)
���%���� � ��!�'����
expression :term { $$ = $1; }
| expression ’+’ term { $$ = $1 + $3; };
term :factor { $$ = $1; }
| term ’*’ factor { $$ = $1 * $3; };
�� ��������� �� ���� �� �� �������� ���� ������
(�������� -" �� �� bison(xiii)
���%���� � ��!�'����
factor :’(’ expression ’)’ { $$ = $2; }
| T_num { $$ = $1; };
%%
��������� ��� ����� ��
� �!%����� yylex ���(�� �� ��'�����*
��'���9 � '���� �����(�� �� flex�
�� �!���$���� yyerror ��� main
�� ��������� �� ���� �� �� �������� ���� ������
(�������� -" �� �� bison(xiv)
���%���� � > ��������� .� '���������%
int yylex (){
int c;
while (isspace(c = fgetc(stdin)));if (isdigit(c)) {
yylval = c - ’0’;while (isdigit(c = fgetc(stdin)))
yylval = yylval * 10 + c - ’0’;ungetc(c, stdin);return T_num;
}
�� ���� #
�� ��������� �� ���� �� �� �������� ���� � ����
(�������� -" �� �� bison(xv)
���%���� � ��!�'����
if (strchr("+*()", c)) return c;if (c != EOF)
fprintf(stderr,"Illegal character: %c\n", c);
return 0;}
�!�� ��������� ��� ����������� ��! �
mytest1: mytest1.ybison mytest1.ygcc -o mytest1 mytest1.tab.c
Makefile
�� ��������� �� ���� �� �� �������� ���� ������
(�������� -" �� �� bison(xvi)
���%���� � > ��������� .� � �� flex%{#include "mytest2.tab.h"%}
%%
[0-9]+ { yylval = atoi(yytext); return T_num; }
\(|\)|\+|\* { return yytext[0]; }[ \t\n]+ { /* nothing */ }. { yyerror("illegal character"); }
%%
mytest2.l�� ��������� �� ���� �� �� �������� ���� ������
(�������� -" �� �� bison(xvii)
�!�� ��������� ��� ����������� .� ��� �
mytest2: mytest2.l mytest2.ybison -d mytest2.yflex -s mytest2.lgcc -o mytest2 mytest2.tab.c lex.yy.c \
-lfl
����!�� �!�������� ��� bison
shift-reduce� �%�� reduce
reduce-reduce� � ����� ��*��
Makefile
�� ��������� �� ���� �� �� �������� ���� ������
-" top-down (i)
< �!������$ �%�!�� ����% ��* �� ��-�
;%(� ���%9 ��-��%�
�� � ��� ����* �� "��� ��! (�
����������(��
⇒ �!$(�� ���������� �� �������*����
�� ��*� �������$� ��! (� ���� �����
⇒ "%��� �� ��* �� k ������ �%��
��� �! "�������% ���*��!� LL(k)
������ "%���� �'�� � ������(�� �� �
��� ����%
�� ��������� �� ���� �� �� �������� ���� ������
)�������� LL(1)
����������� ���?��(������
���!��� �������$� ����� $� �% ���� $
� �����
���!��� ����� ���(� ���� ��
������������ ��*��
������� ����� ���� �!��*� �
����'� ���� *� ��� ��� ����$� ��
������ � LL(1)
⇒ �������$ �������$� ����� $�
⇒ �������$ ��������������
�� ��������� �� ���� �� �� �������� ���� ������
�����!�������� �� LL(1)
������%�����
� → α1 | . . . | αn
� → β1�β2⇒ � → α1 | . . . | αn
� → β1α1β2 | . . . | β1αnβ2
�������$ ��������������
� → αβ1 | . . . | αβn ⇒ � → α�
� → β1 | . . . | βn
�������$ % ���� �������$� ����� $�
� → �α1 | . . . | �αn | β1 | . . . | βm
⇒ � → β1� | . . . | βm�
� → α1� | . . . | αn� | ε
�� ��������� �� ���� �� �� �������� ���� ������
-" ���$������ ����'���
� → α1 | . . . | αn
����������� �� ����� ��� ���$��
�� token ∈ FIRST(α1) ����
�� ���� ��� ��� ���������� ��� α1
. . .
������ �� token ∈ FIRST(αn) ����
�� ���� ��� ��� ���������� ��� αn
������ �� ε �∈ FIRST(α1) ∪ . . . ∪ FIRST(αn) ����
���������� �# ���
������ �� token �∈ FOLLOW(A) ����
���������� �# ���
��� ��
�� ��������� �� ���� �� �� �������� ���� ������
-" LL(1) (i)
:���� ������ �� ����"� *��! ����(����
�� "��� ��� ��� ����$� > ��'��% *� �� S
;%(� ���% ����%-���� � ���!�$ ��� ����"���
� ���� ��� ����* �� "��� ��� ���� �� ����
� �� ��* �� ��� �! "�������%� ���*��!9
�*�� ���������� ��� �� ���
� ���� � ��� ����* �� "���9 �*��
�%���� � �� ��* �� ��� �! "�������%�
���*��! ���� *-���� �%����� ��*��
����!'��� � ����"� ��� � �! "�������% ���*��!
���� %�����
�� ��������� �� ���� �� �� �������� ���� ������
-" LL(1) (ii)
/ ���*��( ��
�������!$� ��!
����� M ���-��
�� ���������
�� �����
LL(1)Πίνακας M
συντακτικής ανάλυσηςLL(1)
Συντακτικός Αναλυτής LL(1)
συµβολοσειρά εισόδου
στοίβα
a1 a2 an... EOF
x1
x2
xm
...
επόµενοσύµβολο
κορυφή
�� ��������� �� ���� �� �� �������� ���� ������
�������� -" LL(1)
E → T E’E’ → + T E’ | εT → F T’T’ → * F T’ | εF → ( E ) | id
FIRST(E) = FIRST(T) = FIRST(F) = { id, ( }FIRST(E’) = {+, ε }FIRST(T’) = { *, ε }FOLLOW(E) = FOLLOW(E’) = { ), EOF }FOLLOW(T) = FOLLOW(T’) = {+, ), EOF }FOLLOW(F) = { *,+, ), EOF }
id + * ( ) EOFE E → TE’ E → TE’E’ E’ → +TE’ E’ → ε E’ → εT T → FT’ T → FT’T’ T’ → ε T’ → *FT’ T’ → ε T’ → εF F → id F → (E)
�� ��������� �� ���� �� �� �������� ���� � ����
%��������� -" LL(1)0 E id + id * id EOF E → T E’1 E’ T id + id * id EOF T → F T’2 E’ T’ F id + id * id EOF F → id3 E’ T’ id id + id * id EOF4 E’ T’ + id * id EOF T’ → ε5 E’ + id * id EOF E’ → + T E’6 E’ T + + id * id EOF7 E’ T id * id EOF T → F T’8 E’ T’ F id * id EOF F → id9 E’ T’ id id * id EOF10 E’ T’ * id EOF T’ → * F T’11 E’ T’ F * * id EOF12 E’ T’ F id EOF F → id13 E’ T’ id id EOF14 E’ T’ EOF T’ → ε15 E’ EOF E’ → ε16 ε EOF ������� ��
�� ��������� �� ���� �� �� �������� ���� ������
�������� ��������� (i)
;��������$ ��� ����$ �attribute grammar��
��� ����$ '���� �! ���-* �� *��! �%(�
�� "��� ����� �� ����� �������� %��
/� �� �� �� �������� %�� !������-����
"%��� ��! �!�������� �����!�
!(����% �������$ ���� �� �� �� ��!�
�������� *� ��* �������$ ��� ��
����� �%(� �* "�!
;����� �� �� �������$ ���� �� �� ��
��!� �������� *� ��* �������$ ���
��! @������A ��� �� @�����A �%(� �* "�!
�� ��������� �� ���� �� �� �������� ���� ������
�������� ��������� (ii)
E → E + T { E1.val := E2.val + T.val }E → T { E.val := T.val }T → T * F { T1.val := T2.val ∗ F.val }T → F { T.val := F.val }F → ( E ) { F.val := E.val }F → num { F.val := num.val }
� ����������� ��*��
�� ��������� �� ���� �� �� �������� ���� ������
#����� ���'����
!������� ����������� ��� �� �* ��� ��!
� ���-���� ��� ��'��* ��*��� �
/* ��� �����
�� ��*��� �
�� ���"�����
�� !�������% ��� ������������9 �!���$�����
�� ���% ����� �� !�������� %��
�� ��������� ����
�� ���(����
�� ����� ���� ���� ��������� �� ���� �� �� �������� ���� ������
0������������� ��������
;�������� ���($��!��� �storage class�
;�(������ ���"����� �global variables�
����"����� ����"�� �stack variables�
������� ���"����� �static variables�
� "����� �scope�
/���*���� �visibility�
&�%����� -�$� �lifetime��� ��������� �� ���� �� �� �������� ���� ������
#����!����� ������ ���'����
� "����� �� ����
/���*���� �� ����
&�%����� -�$�
7����
B��� �����(!�� $ ��9 ����'����$�9 444�
���( *� ���� ���� !�������% ����
7���� ���� ���� !�������% ����
7�*��� ���%� ���� ���� ���� !�������% ����
7���� �������� ���� �!%������
�� ��������� �� ���� �� �� �������� ���� ������
�������� ������ ���'����
+������ �����!�����
����($�� �* ����
��-$���� �* ����
&������$ �* ���� $ � %��� �� %��
;*���� ����($��� $ ��-$����� �%���� �
�� !���������
��� ��$ ����� O(n)
�!����* ����� ��-$����� O(log n)
������ ������� ���� �� O(n/k)
�� ��������� �� ���� �� �� �������� ���� ������
(�������� �� #
��������
��������
��������
��������
��������
��������
��������
��������
��������
��������
��������
��������
��������
��������
��������
��������
εµβέλεια 1����
����
����
����
����
����
����
����
����
����
����
����
����
����
����
����
εµβέλεια 2
������
������
������
������
������
������
������
������
������
������
������
������
������
������
������
������
εµβέλεια 3
a
b
c
στοίβααποθήκευσης
y
aa
a
0
1
2
k – 1
k – 2
.
.
.
x
c
b
πίνακαςκατακερµατισµού
στοίβαεµβελειών
3
2
1
�� ��������� �� ���� �� �� �������� ���� ������
-,���1� ��� ������������
������ ���$ ��� �� $ �� ����
�'� ���� �� ������ %��
� ���������� �� ���� �� ����
�'� ���� �� ������ %��
�����$ �� ���������� ������ *�
�� ��������� ���� %�� ���% ��
��%����� ��� �����������
&!� ��$ �� ���������� ��*���� �� �����
��� �����% ��� ���% �� �������$ ��!�
�� ��������� �� ���� �� �� �������� ���� ������
-������ ������������ (i)
����"%����� ����
Γ1 = { i �→ integer , x �→ real }
'��� �������'���� ����
Γ E : τ
;�*�� ����
Γ E1 : integer Γ E2 : integer
Γ E1+E2 : integer
�� ��������� �� ���� �� �� �������� ���� � ����
-������ ������������ (ii)
��������� ����
Γ1 i : integer Γ1 1 : integerΓ1 i+1 : integer Γ1 x : real
Γ1 (i+1)*x : real
< �������'��� ���� � ��*�� ����
����������� �� *�� �� � $ ��� �����% ����
Γ E : boolean Γ S : stmtΓ while E do S : stmt
�� ��������� �� ���� �� �� �������� ���� �����
&������� ������������ (i)
.����!����$ �� ��������� �operational semantics�
⇒ �����!(�� !���������� "� %��
&������$ �� ��������� �denotational semantics�
⇒ �(� ����$ �!%����� ��* �� ����� ��
���� �� ���*��! ��� ����� ��
�������� %��
���� ����$ �� ��������� �axiomatic semantics�
⇒ � �� ���� ��(���-���� � ��� ���
����� ����%��� ��! ������%��!
���*����� ��! �����% ����
�� ��������� �� ���� �� �� �������� ���� �����
&������� ������������ (ii)
< ����$ �%(���� I=E
.����!����$ �� ���������
〈E, σ 〉 → v
〈 I=E, σ 〉 → σ[I �→ v]
&������$ �� ���������
C[[I=E ]](s) = s[I �→ E [[E ]](s)]
���� ����$ �� ���������
{P [I �→ E] } I=E {P }
�� ��������� �� ���� �� �� �������� ���� �����
-������������ ���!�
1����'�� ����
1����'�� ��$�
1����'�� ������� �� %��
1����'�� �����*�����
1����'�� �!�������� ��������� �� ���� �� �� �������� ���� �����
-,����� �,��� (i)
+������ ����� �integer, boolean, real, char, ...�
�(���� �����
������ �arrays�
=���� �products� ��� ����%��� �tuples�
�������� �records�
&������ �pointers�
!���$���� �functions�
7���� ����� �%��� �first class�
�� ��������� �� ���� �� �� �������� ���� �����
-,����� �,��� (ii)
���������� ���� �type casting�
�����*����� ������ �operator overloading�
���! ������� �������� �polymorphic operators�
�������� ���� ��� !�������� �subtypes�
���! �����% �!��$ ��� ����
�polymorphic type systems�
�����$ ��� �!� ��$ �������'��� ����
�type binding�
������$ ���� �type inference�
�� ��������� �� ���� �� �� �������� ���� �����
&������� ���!� �,���
���"%������ *�� !�%�'��
�!� ��$ �������'��� ����
������ ����� * �� ���������� ��� ��
������$ �������'��� ����9 �4'4 ����'��
���� �� arrays ��� Pascal�
a[i] := 42
�� i ≥ 0 ��� i ≤ 100 ����
������ ��� �� �%(��� ��! 42 ��� a[i]
������
��%� � ���������
��� ��
�� ��������� �� ���� �� �� �������� ���� �����
2����!� ��� �1����� �,���
1����'�� ���� �type checking�
&������ Γ9 E9 τ
=�������� � �������$ Γ E : τ
7� ������ � �� !�%�'��C
������$ ���� �type inference�
&������ Γ9 E
=�������� τ ��� � �������$ Γ E : τ
7� ������ � �� !�%�'�!C
�� ��������� �� ���� �� �� �������� ���� �����
�1����� �,��� (i)
D���� ����'� ���� *� ��! ���"�$ ����
à E : ? �� �� ����� �������
� �!�* �'�� ����9 "�����! � �� τ ������ ���
Γ E : τ
&���������% �� !�%�'�� τ ������ ���
Γ E : τ
�� ��������� �� ���� �� �� �������� ���� �����
�1����� �,��� (ii)
�������� �� ���� � ���"����� ����
τ ::= int | bool | τ → τ | α
������%����� ���� �type substitution��
�����*��� ���"��� ���� �� ����!�
σ = [α �→ int , β �→ bool → α]
���� ��$ ������%������ ����� ��!�*'���
��� �� ���%
σ(α) = int σ(β) = bool → α
σ(β → γ) = (bool → α) → γ
�� ��������� �� ���� �� �� �������� ���� ����
�1����� �,��� (iii)
�(��� ���������%��� σ1 ◦ σ2 ���� ���
(σ1 ◦ σ2)(τ) = σ1(σ2(τ))
1/��! ������������ �� ����� ��� ��'��*
��*��� �9 �! �����! � � �������
���"����� ����9 ���� @19 @29 . . .
let f g x = g x (x + 1)
and m a b = a * b
�������
let f (g : @1) (x : @2) : @3 = g x (x + 1)
and m (a : @4) (b : @5) : @6 = a * b
�� ��������� �� ���� �� �� �������� ���� �������
�1����� �,��� (iv)
7� ��*"�� � à E : ? �%����� ��� ������
��� ������%������ σ ��� �*� ����! τ ���
σ(Γ) σ(E) : τ
���%���� �
let f (g : @1) (x : @2) : @3 = g x (x + 1)
and m (a : @4) (b : @5) : @6 = a * b
in f m 6
����"%��� ��� *�� �� �������� Γ = ∅
����"%��� ��� �� � � f m 6�
Γb = { f �→ @1 → @2 → @3, m �→ @4 → @5 → @6 }
�� ��������� �� ���� �� �� �������� ���� �������
�1����� �,��� (v)
���%���� � ��!�'����
let f (g : @1) (x : @2) : @3 = g x (x + 1)
and m (a : @4) (b : @5) : @6 = a * b
in f m 6
��� ���� �� �����$�
σ = [ @1 �→ int → int → int , @2 �→ int , @3 �→ int ,@4 �→ int , @5 �→ int , @6 �→ int ]
τ = int
#���% �� ������ �� ���� �������E
let f (x : @1) : @2 = x
�� ��������� �� ���� �� �� �������� ���� �������
�1����� �,��� (vi)
�� "�������� � ����C
�������� *� �constraint�� ������� ���� τ1 = τ2
��*"�� � 3� ������ �!*��! �������� C
let f (g : @1) (x : @2) : @3 = g x (x + 1)
and m (a : @4) (b : @5) : @6 = a * b
in f m 6
������ ��� ����� �������� �
C = { @1 = @2 → int → @3, @2 = int ,
@4 = int , @5 = int , @6 = int ,
@1 = @4 → @5 → @6, @2 = int }
�� ��������� �� ���� �� �� �������� ���� �������
�1����� �,��� (vii)
�������$ ���� � �������� ���
Γ E : τ ′ | C
��*"�� � 5� ����!�� �!*��! ��������
��� ������%����� σ ������� �������$�
�unifier� ��� �� �������� * τ1 = τ2 � �� �����
σ(τ1) ��� σ(τ2) ��!��-���� σ(τ1) ≡ σ(τ2)
.��� ��� �� ��*"�� � ��� ������$� �����
��� �������$� σ ��� �%(� �������� * ��! C
� ����� τ = σ(τ ′)
�� ��������� �� ���� �� �� �������� ���� �������
�1����� �,��� (viii)
��������� ����!�� �!*��! ��������
unify(∅) = σ0 {� ���� ������� �����}unify({ τ1 = τ2 } ∪ C) =
�� τ1 ≡ τ2 ����
unify(C)
������ �� τ1 ≡ α ��� �� ��#���%���� �� τ2 ����
unify([α �→ τ2]C) ◦ [α �→ τ2]
������ �� τ2 ≡ α ��� �� ��#���%���� �� τ1 ����
unify([α �→ τ1]C) ◦ [α �→ τ1]
������ �� τ1 ≡ τ11 → τ12 ��� τ2 ≡ τ21 → τ22 ����
unify(C ∪ { τ11 = τ21, τ12 = τ22 })
������
� ������ ����� ���
�� ��������� �� ���� �� �� �������� ���� �������
��$������ ��$��� (i)
.*��� �������
&��!������ �� ���� ��� ��%������
&��!������ �� "�������������
&��!������ �� ���%� ��� �� � ��*�(��
��� ����(�� � $ ��� ��������� �� ���� �� �� �������� ���� �������
��$������ ��$��� (ii)
���%����� ������ �� ��* �� ������
�syntax-directed translation�
#�� �%(� �� $ ��� ������ ���������-����
� �������'�� ���% ���� ������
&��!������ � �!������*� ���!�$� �
�� ����������� ��!���� ��! ���%��!
���% ��� �����
'���� �������$� ���% ���! �����
����"����� �����$�� �attributes� ��� �%(�
�� "��� ��� ��� ����$�
�� ��������� �� ���� �� �� �������� ���� �������
��$������ ������ (i)
7���%��� �quadruples�
n: op, x , y , z
���%���� �� b*b-4*a*c
1: ∗, b, b, $1
2: ∗, 4, a, $23: ∗, $2, c, $34: −, $1, $3, $4
�� ��������� �� ���� �� �� �������� ���� �������
��$������ ������ (ii)
7��%��� �triples�
n: op, x , y
���%���� �� b*b-4*a*c
1: ∗, b, b
2: ∗, 4, a3: ∗, (2), c4: −, (1), (3)
�� ��������� �� ���� �� �� �������� ���� �� ����
��$������ ������ (iii)
����� �� �!������% �����
�abstract syntax trees�
���%���� �� b*b-4*a*c
–
* *
cbb *
a4
�� ��������� �� ���� �� �� �������� ���� �������
��$������ ������ (iv)
���(� ����*� ��� ���(� ����*� ������
�prefix/postfix code�
���%���� �� b*b-4*a*c
− ∗ b b ∗ ∗ 4 a c ���(� ����*�
b b ∗ 4 a ∗ c ∗ − ���(� ����*�
�� ��������� �� ���� �� �� �������� ���� �������
��$������ ������ (v)
;���!(!* ��� ��!������ ��%���
�directed acyclic graphs�
���%���� ��
b+(a+1)*(a+1)+c-(a+1)*(a+1)+2/(a+1)
*
c +
1a
+
1a
2
–
+
/
+
1a
+
1a
+
1a
b
+
*
+ c
2
+
1a
b
+
+
/
+
–
*
�� ��������� �� ���� �� �� �������� ���� �������
)����� �����$��
����$ ����%����
n: op, x , y , z
*��!�
n� ������� ����%��� ��!���*� ���( *��
op� ������$�
x, y, z� ����� ��
�%���� � �� ����� ��! ������$9 �%����
����� �� ���'� ��� ������������
�� ��������� �� ���� �� �� �������� ���� �������
����,���� (i)
��(��%
�������9 ���� ����$9 �����$
'�����$���9 �! "�������%9 nil
1/� � ���"���$9 ���� �����9 !����*��� �
�������$ ���"���$� $n
�������� � �!%������� $$
����������*����� [x] x ���* ����� ��
&���(!��� {x} x ���* ����� ��
�� ��������� �� ���� �� �� �������� ���� �������
����,���� (ii)
�����������$� ��� ��'��* ��*��� �
����%���
7�*��� ���%� ����
V � ���1 ����
R � ���1 �����%
RET � (��� �������� ���� �!%������
;�* � −
�������% ��* � * ���� backpatching�
�� ��������� �� ���� �� �� �������� ���� �������
������ (i)
unit, I ,−,−endu, I ,−,−
��'$ ��� ����� �� ��$� �%���
op, x , y , z op ∈ {+, -, *, /, %}z := x op y
:=, x ,−, zz := x
array, x , y , zz := � ����(!�� ��! ����'���! x[y]
�� ��������� �� ���� �� �� �������� ���� �������
������ (ii)
op, x , y , z op ∈ {=, <>, >, <, >=, <=}
� x op y �*�� �$���� ��� ����%�� z
ifb, x ,−, z
� � �����$ �� $ x ���� ���($� �*�� �$����
��� ����%�� z
jump,−,−, z
�$���� ��� ����%�� z
label, I ,−,−jumpl,−,−, I
���� *� �������� ��� %� � ���� �!�$
�� ��������� �� ���� �� �� �������� ���� �������
������ (iii)
call,−,−, I
�%���� �� �� ��$ �%�� I
par, x ,m,−
������ �� ���� ����$ ���% ���� x � ��*��
���%� ���� m
ret,−,−,−
��������$ ��* �� ���'�!�� �� ��$ �%��
�� ��������� �� ���� �� �� �������� ���� �������
����'��� �$�������
PLACE � (��� *��! "�������� ���(���! �� �
�� $ ��� l-value $ ��� r-value
TYPE � ����� ��� l-value $ ��� r-value
NEXT � ����� ��* �������� ����%�� ��!
�����'�! %� ��� ��� ��* �� ����$
TRUE 9 FALSE � ������ ��* �������� ����%��
��! �����'�! %� ��� ��� ����� ��! ������
� ���������� � �� �!($�� ���� ���($� $
2�!�$�
�� ��������� �� ���� �� �� �������� ���� �� ����
.��/���� ���������� (i)
NEXTQUAD()
���������� �� ���( * ��� ��* ��� ����%���
GENQUAD(op, x , y , z )
#�% �� ��* �� ����%�� op, x , y , z
NEWTEMP(t)
&� ��!���� �� �� �������$ ���"���$
����! t
EMPTYLIST()
&� ��!���� �� ��$ ����� ������ ����%��
�� ��������� �� ���� �� �� �������� ���� �������
.��/���� ���������� (ii)
MAKELIST(x )
&� ��!���� �� ����� ������ ����%�� ��!
�����'�� *� �� ����'��� x
MERGE(l1, . . . , ln)
!���� �� ���� ������ ����%��
l1 . . . ln
BACKPATCH(l , z )
�����(���% �� *��� ��� ����%��� ��!
�����'���� ��� l �� %����� �������
����%��� � �� z �backpatching�
�� ��������� �� ���� �� �� �������� ���� �������
"��/����� ��*�����
�������� ���(����
〈r-value〉 ::= 〈integer-const〉 {P1 }P1 : { 〈r-value〉.PLACE = 〈integer-const〉; }
7������� � ��� ����� ��
〈r-value〉 ::= 〈expr〉 〈binop〉 〈expr〉 {P14 }P14 : { W = NEWTEMP(〈r-value〉.TYPE );
GENQUAD(〈binop〉.NAME ,
〈expr〉1.PLACE ,
〈expr〉2.PLACE ,W );〈r-value〉.PLACE = W ; }
�� ��������� �� ���� �� �� �������� ���� �������
%���� ��*����� (i)
ju mp , – , – , 31
ju mp , – , – , 42
...
...
ju mp , – , – , 31
ju mp , – , – , 31
...
...
ju mp , – , – , 42...
...
< co n d > .TR U E< c o n d > .F A LS E
< co n d >
�� ��������� �� ���� �� �� �������� ���� �������
%���� ��*����� (ii)
.������ ����%���� �� �! "���� * 803
〈cond〉 ::= 〈expr〉
TR U EF A LS E
< co n d >
ifb , < ex p r> .P LA C E , – , *ju mp , – , – , *
< exp r>
�� ��������� �� ���� �� �� �������� ���� �������
%���� ��*����� (iii)
.������ ����%���� �� �! "���� * 803
〈cond〉 ::= 〈expr〉 {P21 }P21 : { 〈cond〉.TRUE = MAKELIST(NEXTQUAD());
GENQUAD(ifb, 〈expr〉.PLACE ,−, ∗);〈cond〉.FALSE = MAKELIST(NEXTQUAD());GENQUAD(jump,−,−, ∗); }
�� ��������� �� ���� �� �� �������� ���� �������
%���� ��*����� (iv)
7������� ���������
〈cond〉 ::= 〈expr〉1 〈relop〉 〈expr〉2
TR U EF A LS E
< co n d >
re lo p , < e xp r> 1 .P LA C E , < e xp r> 2 .P LA C E , *ju mp , – , – , *
< e xp r> 1
< e xp r> 2
�� ��������� �� ���� �� �� �������� ���� �������
%���� ��*����� (v)
7������� ���������
〈cond〉 ::= 〈expr〉1 〈relop〉 〈expr〉2 {P23 }P23 : { 〈cond〉.TRUE = MAKELIST(NEXTQUAD());
GENQUAD(〈relop〉.NAME ,
〈expr〉1.PLACE ,
〈expr〉2.PLACE , ∗);〈cond〉.FALSE = MAKELIST(NEXTQUAD());GENQUAD(jump,−,−, ∗); }
�� ��������� �� ���� �� �� �������� ���� �������
%���� ��*����� (vi)
1�����
〈cond〉 ::= “not” 〈cond〉
T R U E F A L S E
< co n d >
< co n d >
F A LS E TR U E
�� ��������� �� ���� �� �� �������� ���� �������
%���� ��*����� (vii)
�-�!��
〈cond〉 ::= 〈cond〉1 “and” 〈cond〉2
F A L S E T R U E
< co n d >
< co n d > 1
F A LS E TR U E
F A L S E T R U E< co n d > 2
�� ��������� �� ���� �� �� �������� ���� �� ����
%���� ��*����� (viii)
�-�!��
〈cond〉 ::= 〈cond〉1 “and” {P25 } 〈cond〉2 {P26 }P25 : { BACKPATCH(〈cond〉1.TRUE ,NEXTQUAD()); }P26 : { 〈cond〉.FALSE = MERGE(〈cond〉1.FALSE ,
〈cond〉2.FALSE );〈cond〉.TRUE = 〈cond〉2.TRUE ; }
�� ��������� �� ���� �� �� �������� ���� �������
"�� �����
;�$ ����$
〈stmt〉 ::= ε {P29 }P29 : { 〈stmt〉.NEXT = EMPTYLIST(); }
����$ �%(����
〈stmt〉 ::= 〈l-value〉 “:=” 〈expr〉 {P30 }P30 : { GENQUAD(“:=”, 〈expr〉.PLACE ,−,
〈l-value〉.PLACE );〈stmt〉.NEXT = EMPTYLIST(); }
�� ��������� �� ���� �� �� �������� ���� �������
-,�/��� ������ (i)
〈stmt〉 ::= 〈block〉〈block〉 ::= “begin” 〈stmt〉 ( “;” 〈stmt〉 )∗ “end”
N E X T
< b lo ck >
< stm t> 1
N E X T
N E X T< stm t> 2
�� ��������� �� ���� �� �� �������� ���� �������
-,�/��� ������ (ii)
〈stmt〉 ::= 〈block〉 {P34 }P34 : { 〈stmt〉.NEXT = 〈block〉.NEXT ; }
〈block〉 ::= “begin” 〈stmt〉1 {P35 }( “;” {P36 } 〈stmt〉2 {P37 } )∗ “end” {P38 }
P35 : { L = 〈stmt〉1.NEXT ; }P36 : { BACKPATCH(L,NEXTQUAD()); }P37 : { L = 〈stmt〉2.NEXT ; }P38 : { 〈block〉.NEXT = L; }
�� ��������� �� ���� �� �� �������� ���� �������
������ if (i)
〈stmt〉 ::= “if” 〈cond〉 “then” 〈stmt〉 [ “else” 〈stmt〉 ]
T R U E F A L S E
< stm t>
< co n d >
N E X T
N E X T< stm t> 1
F A L S E T R U E
< stm t>
< c o n d >
N E X T
N E X T< stm t> 1
< stm t> 2
ju mp , – , – , *N E X T
�� ��������� �� ���� �� �� �������� ���� �������
������ if (ii)
〈stmt〉 ::= “if” 〈cond〉 {P39 } “then” 〈stmt〉1[ “else” {P40 } 〈stmt〉2 {P41 } ] {P42 }
P39 : { BACKPATCH(〈cond〉.TRUE ,NEXTQUAD());L1 = 〈cond〉.FALSE ;L2 = EMPTYLIST(); }
P40 : { L1 = MAKELIST(NEXTQUAD());GENQUAD(jump,−,−, ∗);BACKPATCH(〈cond〉.FALSE ,NEXTQUAD()); }
P41 : { L2 = 〈stmt〉2.NEXT ; }P42 : { 〈stmt〉.NEXT = MERGE(L1, 〈stmt〉1.NEXT ,L2); }
�� ��������� �� ���� �� �� �������� ���� �������
������ while (i)
〈stmt〉 ::= “while” 〈cond〉 “do” 〈stmt〉
F A L S E T R U E
< stm t>
< co n d >
N E X T
N E X T< stm t> 1
ju mp , – , – , *
�� ��������� �� ���� �� �� �������� ���� �������
������ while (ii)
〈stmt〉 ::= “while” {P43 } 〈cond〉 “do” {P44 } 〈stmt〉1 {P45 }P43 : { Q = NEXTQUAD(); }P44 : { BACKPATCH(〈cond〉.TRUE ,NEXTQUAD()); }P45 : { BACKPATCH(〈stmt〉1.NEXT ,Q);
GENQUAD(jump,−,−,Q);〈stmt〉.NEXT = 〈cond〉.FALSE ; }
�� ��������� �� ���� �� �� �������� ���� �������
���� ��������������� (i)
〈call〉 ::= 〈id〉 “(” [ 〈expr〉 ( “,” 〈expr〉 )∗ ] “)”
〈r-value〉 ::= 〈call〉〈stmt〉 ::= 〈call〉
����� � ���� ���� � ����%��� par
����� � (���� �������� ���� � ����%�� par
� ��*������ ��� �!%�����
;�$�� � ����%�� call
�� ��������� �� ���� �� �� �������� ���� �������
���� ��������������� (ii)
〈call〉 ::= 〈id〉 “(” {P46 } [ 〈expr〉1 {P47 }( “,” 〈expr〉2 {P48 } )∗ ] “)” {P49 }
P46 : { N = 1; }P47 : { GENQUAD(“par”, 〈expr〉1.PLACE ,
PARAMMODE(〈id〉,N ),−);N = N + 1; }
P48 : { GENQUAD(“par”, 〈expr〉2.PLACE ,
PARAMMODE(〈id〉,N ),−);N = N + 1; }
�� ��������� �� ���� �� �� �������� ���� �� ����
���� ��������������� (iii)
〈call〉 ::= 〈id〉 “(” {P46 } [ 〈expr〉1 {P47 }( “,” 〈expr〉2 {P48 } )∗ ] “)” {P49 }
'��������(
P49 : { if (ISFUNCTION(〈id〉)) {W = NEWTEMP(FUNCRESULT(〈id〉));GENQUAD(par,RET,W ,−);〈call〉.PLACE = W ;
}GENQUAD(call,−,−, 〈id〉); }
�� ��������� �� ���� �� �� �������� ���� �������
���� ��������������� (iv)
;�$�� �!%������
〈r-value〉 ::= 〈call〉 {P50 }P50 : { 〈r-value〉.PLACE = 〈call〉.PLACE ; }
;�$�� �����������〈stmt〉 ::= 〈call〉 {P51 }
P51 : { 〈stmt〉.NEXT = EMPTYLIST(); }
�� ��������� �� ���� �� �� �������� ���� �������
���� ��������������� (v)
��������$ ��* !����*��� �
〈stmt〉 ::= “return” [ 〈expr〉 {P52 } ] {P53 }P52 : { GENQUAD(retv, 〈expr〉.PLACE ,−,−); }P53 : { GENQUAD(ret,−,−,−); }
&$���� !�������% ����
〈body〉 ::= ( 〈local〉 )∗ {P56 } 〈block〉 “;” {P57 }P56 : { GENQUAD(unit, I ,−,−); }P57 : { BACKPATCH(〈block〉.NEXT ,NEXTQUAD());
GENQUAD(endu, I ,−,−); }
�� ��������� �� ���� �� �� �������� ���� �������
������ ��$��� (i)
��* (�������$� %��2��9 �� ��*"�� � ���
�������!$� "�������! ������� ����� �� �'��
���� �undecidable�
������ ������� ������
#���� �'�$� �� ��*�!�� ���$ �absolute�
#���� �'�$� �� ��������(��$�� � ���
����!���� � ���$ �relocatable, linkable�
! "����$ ����� �assembly�
1���� ����� '� ���� �������!
�� ��������� �� ���� �� �� �������� ���� �������
������ ��$��� (ii)
��� ���!� ���"�$ ����
������$ ����
⇒ �� �����%-���� �%(� ����$ ��!
���% ���! �����
⇒ �� �����%-���� �����!(��� ������
����
&��'������ ��� $ �� ��� '�*� ���������
⇒ ��� ���(�������� �� ���� ��
⇒ �� ������ � ��������� �% ��� ����
�� ���� �%����� ��������� �� ���� �� �� �������� ���� �������
������ ���������� (i)
:������������%�
����������$�� Intel F8FG
.����!����* ����� �� MS-DOS
������ $ ��� COM 0 tiny⇒ !����$ $ � ≤ 64 K⇒ /��%��� �� �� segment⇒ ��'��$ ����(!�� ��! �����% ���� � 100h
! "����$ ������ �! "��$ � ��
�! "��� ��������$ MASM �Microsoftmacro assembler�
�� ��������� �� ���� �� �� �������� ���� �������
������ ���������� (ii)
;���'������9 ���(�!� 3G bit
#���$� ������ ax9 bx9 cx9 dx⇒ �� -���� �� F bit� ah9 al9 �4��4
;���'������ �������� sp �������� ����"���
��� bp �������� "%����
;���'������ �����%�� si ��� di
;���'������ � � %��� cs �code�9 ds
�data�9 ss �stack� ��� es �extra�
������� ����'������� ip �instructionpointer� ��� ����'����$� �� �� �flags�
�� ��������� �� ���� �� �� �������� ���� �������
������ ���������� (iii)
&��!(������
address = segment ∗ 16 + offset
����$ ����$��
[ label ] opname [ operand1 [ , operand2 ] ]�� ��������� �� ���� �� �� �������� ���� �������
������ ���������� (iv)
�������
�������%�� mov9 lea
���( ���� ��%���� add9 sub9 neg9 imul9
idiv9 cmp9 cwd
.���� ��%���� and9 or9 xor9 not9 test
1�� ����� jmp9 jz9 jnz9 jl9 jle9 jg9 jge
&��'������� ����"��� push9 pop
��������� %��� call9 ret
��%��� ����$� !���������$� �x87 FPU�
�� ��������� �� ���� �� �� �������� ���� �������
����� ����*���mov destination, source (move)
mov ax, 42mov ax, bxmov ax, [1000h]mov ax, [si]mov ax, [si + 6]mov ax, [bp + 6]mov ax, [si + bp + 6]
lea destination, source (load effective address)
;�(���� *� ���(�!� ���� ��
mov ax, word ptr [bp + 6]mov al, byte ptr [bp + 6]
�� ��������� �� ���� �� �� �������� ���� �� ����
"��/����� ���1��
add op1, op2 op1 := op1 + op2
sub op1, op2 op1 := op1 − op2
neg op op := −op
imul op (dx, ax) := ax ∗ op
idiv op ax := (dx, ax) div op
dx := (dx, ax) mod op
cwd ������� ����� �� ax ��� dx
cmp op1, op2 �&������ �� op1 ��� op2
��������� ��� �������
�� ��������� �� ���� �� �� �������� ���� �������
%���� ���1��
and op1, op2 op1 := op1 and op2
or op1, op2 op1 := op1 or op2
not op op := not op
xor op1, op2 op1 := op1 xor op2
test op1, op2 op1 and op2
��������� ��� �������
�� ��������� �� ���� �� �� �������� ���� �������
����� ������
jmp address ����� ���$���
jz address $ je address �� �� * ��
jnz address $ jne address ��� �� �� * � #�
jl address ��������
jle address �������� � ��
jg address �����&���
jge address �����&��� � ��
�� ��������� �� ���� �� �� �������� ���� �������
����� ����'�
push op ���$��� ��� �����
sp := sp− 2, [sp] := op
pop op �#������ �� �� �����
op := [sp], sp := sp + 2
⇒ < ����"� �!�%�� ���� �� �%��9 �����$ ����
���*����� ���!(������� ��������� �� ���� �� �� �������� ���� �������
����� ���������������
call address �����
sp := sp− 2, [sp] := ip, ip := address
ret �����#�
ip := [sp], sp := sp + 2
⇒ < �� $ ��! ip ��! ����(������� ��� ����"� ��*
�� call ���� � ����(!�� ��� ����$� ��!
�����!(�� �� call
�� ��������� �� ���� �� �� �������� ���� �������
����� x87 FPU (i)
⇒ �����$ ����"� F ����'����� ST(0)9 444 ST(7)
fld source (load real & push)
fld tbyte ptr @real1
fild source (load integer & push)
fild word ptr [bp− 2]
fstp destination (pop & store real)
fld tbyte ptr [bp− 10]
fistp destination (pop & store integer)
fild word ptr [bp− 2]
�� ��������� �� ���� �� �� �������� ���� �������
����� x87 FPU (ii)
faddp ST(1), ST(0) ST(1) := ST(1) + ST(0) & pop
fsubp ST(1), ST(0) ST(1) := ST(1) − ST(0) & pop
fmulp ST(1), ST(0) ST(1) := ST(1) ∗ ST(0) & pop
fdivp ST(1), ST(0) ST(1) := ST(1)/ST(0) & pop
fchs ST(0) := −ST(0)
fcompp ST(1) � ST(0) & pop both
fstsw destination (store x87 FPU flags)
fstsw axfstsw word ptr [bp− 2]
�� ��������� �� ���� �� �� �������� ���� �������
&��!������ ����� (i)
&� $ ���$�� �block structure�
�� �����% ���� ��
����%�� � ���������������� �activationrecord�
���% �����
�������� �
����������� ���%������ �'�$�
7������ ���"�����
��������� ���"�����
�� ��������� �� ���� �� �� �������� ���� �������
&��!������ ����� (ii)
Παράµετρος 1
∆ιεύθυνση αποτελέσµατος
Τοπική µεταβλητή 1
Παράµετρος 2...
Παράµετρος n
Σύνδεσµος προσπέλασης∆ιεύθυνση επιστροφής
Προηγούµενο bp
Τοπική µεταβλητή 2
...
Τοπική µεταβλητή mΠροσωρινή µεταβλητή 1
Προσωρινή µεταβλητή 2
...
Προσωρινή µεταβλητή k
Παράµετρος 1
∆ιεύθυνση αποτελέσµατος
Τοπική µεταβλητή 1
Παράµετρος 2...
Παράµετρος n
∆ιεύθυνση επιστροφής
Προηγούµενο bp
Τοπική µεταβλητή 2
...
Τοπική µεταβλητή mΠροσωρινή µεταβλητή 1
Προσωρινή µεταβλητή 2
...
Προσωρινή µεταβλητή k
Προηγούµενο display
...bp+8
bp+6
bp+4
bp+2
bp
bp-2
bp-4
...
Παράµετροι
τέλος
Σταθερό
Τµήµα
Τοπικές
µεταβλητές
Προσωρινές
µεταβλητές
α) Σύνδεσµοι προσπέλασης β) Πίνακας δεικτών
βάση
αρχή
�� ��������� �� ���� �� �� �������� ���� �������
&��!������ ����� (iii)
µεταφρασµένοςκώδικας
Ε∆ κύριου προγράµµατοςΕ∆ υποπρογράµµατος 1
Ε∆ υποπρογράµµατος m...
µνήµη γιααποθήκευσητου κώδικα τουπρογράµµατος
64Κ
0Κ
ελεύθερηµνήµη
στοίβαεκτέλεσης
µνήµη γιααποθήκευσηδεδοµένων
συνολικήδιαθέσιµη
µνήµη
δυναµικό δεδοµένο 1
δυναµικό δεδοµένο k... σωρός
�� ��������� �� ���� �� �� �������� ���� �� ����
#�������� ��������
7����%� [bp + offset ]
�� �����%� [si + offset ]
⇒ � si ������ � ���'�� ��� "%�� ��!
������$ ���� ���������������� *��! ��
���� �� ���� �����%
7� ��*"�� � �%����� ��� ������ * ��!
�������'�! ������$ ���� ����������������
.����� ��! "���-���� ��� "%(�� ����%� �����
���� �� ����������� �access links�
������ ����� �link tables / displays�
�� ��������� �� ���� �� �� �������� ���� �������
-,�$����� ��������� (i)
��'$ �����!�����
1���� *�� � �� ��$ �%�� p "��������
������ �� ��� ��� �� ��$ �%�� q
⇒ �� �& ��� p ����(������� �� ����� ��
���� �� "%�� ��! �& ��� ��� ��*������
��$��� ��� q
;��% �� ��$�� !�������� %��9 ����������
�����*� ������ ��� �� �� ����� ��
�!��� � �����������
�� ��������� �� ���� �� �� �������� ���� �������
-,�$����� ��������� (ii)
7�*��� '�$���
1���� *�� -������� �� ���� �� a ��! ����
�����* �� �� �� ��$ �%�� � "%(��
����%� ���� na
1���� *�� "����* ���� �� �� �� ��$
�%�� p � "%(�� ����%� ���� np ≥ na
⇒ �����!(�� � np − na �!��� �!�
�����������
;��% �� ���������� �� %��9 ����������
�����*� ������ ��� �� !�������� �� �����%�
�� ��������� �� ���� �� �� �������� ���� �������
#����� ���������
;�$�� ���1 ���� �call by value�
;�$�� ���1 �����% �call by reference�
;�$�� ���1 *� � �call by name�
;�$�� ���1 �%��� �call by need�
;�$�� ���1 ���� ��� �������� �
�call by value-result�
⇒ 7�*��� !��������� ��(�*�
�� ��������� �� ���� �� �� �������� ���� �������
&������ ����!������ (i)
��*"�� � 3� ������$ ����! �� ��! (�
���(���!��� �� ����'������
��*"�� � 5� ������$ ����'���� *��! (�
���(���!��� �� ����� ��
7� ��*"�� � ��� "�������� '�$���
����'���� ���� NP-complete9 ��* � ���
'���� �������� �������� ���
< ���� ��! �%����� ��� �������!$ ��!
��%��! ����������%��� ����� ��
���"��� ��� ��� '�� ���� * �!��� � �*��
'� ��� *��� �� ����'������
�� ��������� �� ���� �� �� �������� ���� �������
&������ ����!������ (ii)
���%���� �� d := b*b-4*a*c
1: ∗, b, b, $1
2: ∗, 4, a, $23: ∗, $2, c, $34: −, $1, $3, $4
5: :=, $4,−, d $1
$3
$2
$4
ax
bx
�� ��������� �� ���� �� �� �������� ���� �������
������� ������� (i)
���$ ���% ���$ ����������� ����� �'$ �
�������$� ������� ����� ��� �%(� �� $ ��!
���% ���! �����
;������� ����������� ����*������ �tiling�
�� �'�� *� ��! ���% ���! ����� ��
� $ ���
�%(� � $ � �������'�� �� �� ����$
+������� ���% '���"*�� �����������
�!� ��*� ������ ���� *� �dynamicprogramming�
�� ��������� �� ���� �� �� �������� ���� �������
������� ������� (ii)
���%���� �� d := b*b-4*a*c
* *
cbb *
a4
:=
–d
*
*
4
:=
–mem
bp offset d
+
bp offset b
+ mem
bp offset a
+
mem
bp offset c
+
*
mem
�� ��������� �� ���� �� �� �������� ���� �������
������� ������� (iii)
���%���� �� tiling � ������ ��! F8FG
Rbi
CONST
+
mem
R1
mov R1,[Rbi+CONST]
ax
ax
imul R1
R1
*
*
*
4
:=
–mem
bp offset d
+
bp offset b
+ mem
bp offset a
+
mem
bp offset c
+
*
mem
ax
ax
ax
ax
ax bx
ax → bx
bx
�� ��������� �� ���� �� �� �������� ���� �������
������� ������� (iv)
���%���� �� tiling � ������ ��! F86FG
imul R1,[Rbi+CONST1],CONST2
*
mem
+
R1
Rbi CONST1
CONST2*
*
4
:=
–mem
bp offset d
+
bp offset b
+ mem
bp offset a
+
mem
bp offset c
+
*
mem
ax
ax
ax
ax
bx
�� ��������� �� ���� �� �� �������� ���� �� ����
�� ������ ��������� (i)
�����*��
xseg segment public ′code′assume cs : xseg, ds : xseg, ss : xsegorg 100h
main proc nearcall near ptr programmov ax, 4C00hint 21h
main endp
... ��� �� �� �� ��� �������� ...
xseg endsend main
�� ��������� �� ���� �� �� �������� ���� �������
�� ������ ��������� (ii)
+�"���($�� '�*�! ��������� �run-time library�
extrn function : proc
��(���� �! "��������� ��� ����$�
!���������$�
@str1 db ′this is′db 10db ′an example′db 0
@real1 dt 1e−10@real2 dt 2.0
�� ��������� �� ���� �� �� �������� ���� �������
.��/���� ������� (i)
getAR(a) '#������ ��&$����� "+(
mov si, word ptr [bp + 4]mov si, word ptr [si + 4]. . .mov si, word ptr [si + 4]
}(ncur − na − 1 ����)
updateAL() '��������� ��� ����� ��������(
'�( push bp �� np < nx
'�( push word ptr [bp + 4] �� np = nx
'�( mov si, word ptr [bp + 4] �� np > nx
mov si, word ptr [si + 4]. . .mov si, word ptr [si + 4]
}(np − nx − 1 ����)
push word ptr [si + 4]
�� ��������� �� ���� �� �� �������� ���� �������
.��/���� ������� (ii)
load(R, a) '#������ ��������(
��� ��� a �� �� ��� ��������
�� � �� ���� �� ������� mov R, a
��!� ��� �� ������� true mov R, 1
��"� ��� �� ������� false mov R, 0
��#� ������� ��������� mov R, ASCII(a)
��$� ������� nil mov R, 0
�� � ��� �� ��������%
���������&
��������� ���' �(��& �
������ �� ���������
mov R, size ptr [bp + offset ]
��!� ��� �� ��������%
��������� ���' �������
mov si, word ptr [bp + offset ]mov R, size ptr [si]
�� ��������� �� ���� �� �� �������� ���� �������
.��/���� ������� (iii)
load(R, a) '#������ ��������(
��� ��� a �� �� ��� ��������
�� � �� ��� �� ��������%
���������&
��������� ���' �(��& �
������ �� ���������
getAR(a)mov R, size ptr [si + offset ]
��!� �� ��� �� ��������%
��������� ���' �������
getAR(a)mov si, word ptr [si + offset ]mov R, size ptr [si]
�� [x] load(di, x)mov R, size ptr [di]
��� {x} loadAddr(R, x)
�� ��������� �� ���� �� �� �������� ���� �������
.��/���� ������� (iv)
loadAddr(R, a) '#������ ��&$����� ��������(
��� ��� a �� �� ��� ��������
��)� ������� ��������� �� lea R, byte ptr a
�� � ��� �� ��������%
��������� ���' �(��& �
������ �� ���������
lea R, size ptr [bp + offset ]
��!� ��� �� ��������%
��������� ���' �������
mov R, word ptr [bp + offset ]
�� � �� ��� �� ��������%
��������� ���' �(��& �
������ �� ���������
getAR(a)lea R, size ptr [si + offset ]
��!� �� ��� �� ��������%
��������� ���' �������
getAR(a)mov R, word ptr [si + offset ]
�� [x] load(R, x)
�� ��������� �� ���� �� �� �������� ���� �������
.��/���� ������� (v)
���* ��� !�������� ��� ����
loadReal(a)
store(R, a)
storeReal(a)
H�!���� ��� �������� ������� ������
name(p) p num
endof(p) @p num
label(n) @n
label(�) @p num �
�� ��������� �� ���� �� �� �������� ���� �������
#������� ��$��� (i)
7���%�� :=, x,−, z
load(R, x)store(R, z)
loadReal(x)storeReal(z)
7���%�� array, x, y, z
load(ax, y)mov cx, sizeimul cxloadAddr(cx, x)add ax, cxstore(ax, z)
�� ��������� �� ���� �� �� �������� ���� �������
#������� ��$��� (ii)
7���%��� +, x, y, z −, x, y, z
load(ax, x)load(dx, y)instr ax, dxstore(ax, z)
instr = add � sub
loadReal(x)loadReal(y)finstr ST(1), ST(0)storeReal(z)
finstr = faddp �*��*
7���%��� ∗, x, y, z /, x, y, z %, x, y, z
load(ax, x)load(cx, y)imul cxstore(ax, z)
load(ax, x)cwdload(cx, y)idiv cxstore(ax, z)
load(ax, x)cwdload(cx, y)idiv cxstore(dx, z)
�� ��������� �� ���� �� �� �������� ���� �������
#������� ��$��� (iii)
7���%��� =, x, y, z <>, x, y, z <, x, y, z>, x, y, z <=, x, y, z >=, x, y, z
load(ax, x)load(dx, y)cmp ax, dxinstr label(z)
instr = je, jne, �*��*loadReal(x)loadReal(y)fcomppfstsw axtest ax, valueinstr label(z)
value �� instr ���
+����� ,* ���* !#,
�� ��������� �� ���� �� �� �������� ���� �� ����
#������� ��$��� (iv)
7���%�� ifb, x,−, z
load(al, x)or al, aljnz label(z)
7���%�� jump,−,−, z
jmp label(z)
7���%�� jumpl,−,−, z
jmp label(z)
7���%�� label,−,−, z
label(z) :
�� ��������� �� ���� �� �� �������� ���� �������
#������� ��$��� (v)
7���%�� unit, x,−,−name(x) proc near
push bpmov bp, spsub sp, size
7���%�� endu, x,−,−endof(x) : mov sp, bp
pop bpret
name(x) endp
�� ��������� �� ���� �� �� �������� ���� �������
#������� ��$��� (vi)
7���%�� call,−,−, z
sub sp, 2 �� z ���� � �����
updateAL()call near ptr name(z)add sp, size + 4
7���%�� ret,−,−,−jmp endof(current)
�� ��������� �� ���� �� �� �������� ���� �������
#������� ��$��� (vii)
7���%�� par, x, y,−
�� y , V ��� x ����� -. bit
load(ax, x)push ax
�� y , V ��� x ����� / bit
load(al, x)sub sp, 1mov si, spmov byte ptr [si], al
�� ��������� �� ���� �� �� �������� ���� �������
#������� ��$��� (vii)
7���%�� par, x, y,− ��!�'����
�� y , V ��� x ����� /0 bit
loadReal(x)sub sp, 10mov si, spfstp tbyte ptr [si]
�� y , R � RET
loadAddr(si, x)push si
�� ��������� �� ���� �� �� �������� ���� �������
#��!������ /����
����������� ������ ������� �����
:������������% ��! ������ ����������
������ ���� ��
����� ��* ����� �� �� ����� ��*
������������ ������ ��
������ �� 0 ��%��
���!�������� �encapsulation�
������ ��*���� �inheritance�
���! ����� *� !������ �subtypepolymorphism�
�� ��������� �� ���� �� �� �������� ���� �������
"���������� ��� �/�$�� (i)
������ �� = ������$ �record� + �(����
�����%����� ������ ��� ���� � ��
�����%����� ������
&�� ������ '��� ��� �� ����� ��!
������ ��!
/� �(���� �� �! ������ "%���� ���
�����%�����
/� ��$���� �(*�� �����%-���� ��
��$���� �!���$��� � �� �������
���% ����� �� ������ ��� ������ �� *��!
���������
�� ��������� �� ���� �� �� �������� ���� �������
"���������� ��� �/�$�� (ii)class Shapevar posX, posY : real;
procedure move (dx, dy : real);begin
posX := posX + dx;posY := posY + dy
end;end;
Shape
παράστασηαντικειµένου
posX
posY
⇓procedure Shape@move (var self : Shape; dx, dy : real);beginself.posX := self.posX + dx;self.posY := self.posY + dy
end; s.move(1, 2) ⇒ Shape@move(s, 1, 2)
�� ��������� �� ���� �� �� �������� ���� �������
���������� & ��������*��
1D��� ��� ������ � ��� �(*��!�
class Shape...procedure constructor (x, y : real);...procedure destructor ();...
end; ⇓procedure Shape@constructor (var self : Shape;
x, y : real);procedure Shape@destructor (var self : Shape);
var s : Shape(10, 20); ⇒Shape@constructor(s, 10, 20);...Shape@destructor(s);
�� ��������� �� ���� �� �� �������� ���� �������
"��� ��������������� (i)
�����%�� 0 !�����%��
��������� �(*�� �method override�
�������� �subtypes�
�����%����� ������ ��
7� ����� ��� !�����%��� ����(�������
���� ���� ��'$ ��! ������ ��!�
�!�* ���� *-���� ����� ��%
7� ����� ��� !����%��� �����!(��
�� ��������� �� ���� �� �� �������� ���� �� ����
"��� ��������������� (ii)class Circle extends Shapevar radius : real;procedure scale (s : real);...
end;
class Line extends Shapevar endX, endY : real;procedure move (dx, dy : real);...
end;
Shape
παράστασηαντικειµένου
posX
posY
Circle posX
posY
radius
Line posX
posY
endX
endY
�� ��������� �� ���� �� �� �������� ���� � �����
"��� ��������������� (iii)
�����* ���� � �(*�� �static binding�
var s : Shape(10, 20);c : Circle(30, 30, 10);l : Line(10, 20, 30, 30);p : ^Shape;
...p := @l;
s.move(5, 5);c.move(5, 5);l.move(5, 5);c.scale(2);p^.move(5, 5);
⇒Shape@move(s, 5, 5);Shape@move(c, 5, 5);Line@move(l, 5, 5);Circle@scale(c, 2);Shape@move(p^, 5, 5)
�� ��������� �� ���� �� �� �������� ���� � �����
#������*���� ����,��� (i)
&!� ��* ���� � �(*�� �dynamic binding�
1/�� ������ ��$�� �(*�� ��� ����� $
����� �� ������ �� ��� !�����%���9
�������� �� �������'�� �(���� ��
!����%���
������ ����*������ �(*��
�method dispatch table� $
����������� ��%��� �class descriptor�
;��*� ��� *�� �� ������ �� ��� ��%���
�� ��������� �� ���� �� �� �������� ���� � �����
#������*���� ����,��� (ii)class Shapevar posX, posY : real;dynamic procedure move (dx, dy : real);
end;
class Line extends Shapevar endX, endY : real;dynamic procedure move (dx, dy : real);
end;
var p : ^Shape;
...p := @l;
p^.move(5, 5)
�� ��������� �� ���� �� �� �������� ���� � �����
#������*���� ����,��� (iii)var p : ^Shape;
...p := @l;
p^.move(5, 5)
⇓f := p^.dispatch^.move;f^(p^, 5, 5)
/ ���� ���
����� ��� �abstract�
�(*��!�
Shape
παράστασηαντικειµένου
posX
posY
Circle
posX
posY
radius
Line
posX
posY
endX
endY
dispatch
dispatch
dispatch
πίνακαςανταπόκρισης
µεθόδων
move
move
move
Shape@move
Shape@move
Line@move
υλοποιήσειςµεθόδων
�� ��������� �� ���� �� �� �������� ���� � �����
#������� ��������������� (i)
��*"�� � � �� �����%����� ��
������ �� �� �!�!�� * � ��!� !������!�
class TwoDimvar color : ColorType;abstract function area () : real;dynamic procedure paint (c : ColorType);
end;
class Circle extends Shape, TwoDim...dynamic function area () : real;
end;
�� ��������� �� ���� �� �� �������� ���� � �����
#������� ��������������� (ii)var c : Circle(...);
p1 : ^Shape;p2 : ^TwoDim;
...p1 := @c;p2 := @c παράσταση
αντικειµένου
Circle
posX
posY
radius
dispatch
πίνακαςανταπόκρισης
µεθόδων
move
length
Shape@move
υλοποιήσειςµεθόδων
Circle@length
color
dispatch
area
paint
Circle@area
TwoDim@paint
�� ��������� �� ���� �� �� �������� ���� � �����
#������� ��������������� (iii)
������ �� �������$ ������ ��*����
class Avar a1, a2 : integer;dynamic procedure m1 ();dynamic procedure m2 ();
end;
class B extends Avar b1, b2 : integer;dynamic procedure m1 ();dynamic procedure m3 ();
end;
class C extends Avar c1, c2 : integer;dynamic procedure m2 ();dynamic procedure m4 ();
end;
class D extends B, Cvar d1, d2 : integer;dynamic procedure m2 ();dynamic procedure m3 ();dynamic procedure m5 ();
end;
�� ��������� �� ���� �� �� �������� ���� � �����
#������� ��������������� (iv)
������ ���������
�offset table�
p^.a1 ⇒(p + p^.offset^.a1)^
παράστασηαντικειµένου
A
a1
a2
dispatch
πίνακαςανταπόκρισης
µεθόδων
m1
m2
A@m1
υλοποιήσειςµεθόδων
A@m2offset
πίνακαςαποκλίσεων
a1: 4
a2: 6
B
a1
a2
dispatch m1
m2
B@m1
A@m2offset
a1: 4
a2: 6
b1: 8
b2: 10
b1
b2
m3 B@m3
C
a1
a2
dispatch m1
m2
A@m1
C@m2offset
a1: 4
a2: 6
c1: 8
c2: 10
c1
c2
m4 C@m4
D
a1
a2
dispatch m1
m2
B@m1
D@m2offset
a1: 4
a2: 6
b1: 8
b2: 10
b1
b2
m3 D@m3
dispatch
offset
c1
c2
d1
d2
c1: 16
c2: 18
d1: 20
d2: 22
m1
m2
m4
m5
A@m1
D@m2
C@m4
D@m5
a1: – 10
a2: – 8
c1: – 6
c2: – 4
�� ��������� �� ���� �� �� �������� ���� � �����
2����!� ����,��� (i)
������ 3� �� ���� ��%�� �$��� �� ������ �� XC
��%����� ���� ������� ��!� ����������� ��%���
������ 5� �$��� �� ������ �� X ��� ��%�� CC
��%����� 444���� ��� �������444
var p : ^Shape;...if p^ instanceOf Circle then
...1...else
...2...
�� ��������� �� ���� �� �� �������� ���� � ����
2����!� ����,��� (ii)d := p^.descriptor;
loop:if d = Circle@descriptorthen begin
...1...goto next
endelse if d = nil thenbegin
...2...goto next
endelse
d = d^.super;goto loop;
next:
παράστασηαντικειµένου
Shape
posX
posY
Line
posX
posY
endX
endY
descriptor
descriptor
πίνακαςανταπόκρισης
µεθόδων
move
length
Shape@move
υλοποιήσειςµεθόδων
Line@move
Line@length
Circle
posX
posY
radius
descriptor
Shape@move
Circle@length
super
move
length
super
move
length
super
Foo
posX
posY
radius
descriptor
Foo@move
Circle@length
move
length
super
bar
�� ��������� �� ���� �� �� �������� ���� �������
&����������� (i)interface Printable
dynamic procedure print ();end;
class Circle extends Shape implements Printable...dynamic procedure print ();
end;
παράστασηαντικειµένου
Circle
posX
posY
radius
dispatch
πίνακαςανταπόκρισης
µεθόδων
move
length
Shape@move
υλοποιήσειςµεθόδων
Circle@length
print Circle@printiPrintable
�� ��������� �� ���� �� �� �������� ���� �������
&����������� (ii)var c : Circle(...);
p : ^Printable;
...p := @c;
p^.print()
⇓p.self := @c;p.iPrintable := c.iPrintable
παράστασηαντικειµένου
Circle
posX
posY
radius
dispatch
πίνακαςανταπόκρισης
µεθόδων
move
length
Shape@move
υλοποιήσειςµεθόδων
Circle@length
print Circle@printiPrintable
^Printable self
iPrintable
�� ��������� �� ���� �� �� �������� ���� �������