3. 3
Formal Specification
lexical structure : regular expression
syntactic structure : context-free grammar
the remaining phases of compilation : no such notations
but, we use a syntax-directed translation scheme which is a
method associating semantic rules(or actions) with production.
SDTS ::= cfg + semantic actions
cfg의 production rule에 있는 grammar symbol을 이용하여 직접
semantic action을 기술하는 방법.
Introduction
4. 4
A Model for Intermediate code generation
Our implementations:
Source program : Mini C 프로그램
Intermediate Representation : Abstract Syntax Tree (AST)
Intermediate code : U-Code
Execution : U-Code Interpreter
Scanner
Parser
Intermediate
Code Generation
Source
Program
Intermediate
Representation
Introduction
5. 5
Implementation Model
scanner : shift action of parser
parser : main program (LR parser)
SDT : reduce action of parser (AST generation)
ICG : Intermediate code generation by traversing AST.
※ Semantic Analysis와 Intermediate Code Generation을 효율적으로 처
리하기 위해서 AST의 design은 매우 중요.
Scanner Parser
ICG
Token
Stream
AST
Mini C Compiler
SDT
Source
Program
*.mc Ucode
InterpreterUcode
*.uco
data
result
Introduction
6. 6
Syntax-Directed Translation
Syntax-Directed Translation Scheme(SDTS)
::= a production rule + semantic action(no widely-accepted formalism)
A Model of SDTS
whenever a reduction takes place, the semantic rule
corresponding to the applied syntactic rule is activated.
Scanner
Source
Program
Parsing table
get_token
token
Parser
(main)
Result
.
.
.
call semantic
Semantic Actions
7. 7
Description of Semantic Actions
Description of Semantic Actions
(1) Conventional PL.
(2) Meta Language - Formal Semantic Language(FSL)
SDTS에서는 문법 심볼의 속성을 이용하여 기술
구조 변수의 멤버 처럼 기술
문법 심볼 A에 대한 속성 값 : A.value
문법 심볼 B에 대한 기억 장소 : A.addr 등으로 표기
Preprocessor
Semantic
Description
table
Input OutputSDT
8. 8
ex) 덧셈, 곱셈이 있는 수식의 생성규칙과 그 값을 계산하여 출력
하는 의미 규칙
note
첨자 : 같은 문법 심벌이 생성규칙에 여러 번 나타나는 경우의 구별용
val : 문법기호의 계산된 값을 기억하고 있는 속성
lexval : 스캐너에서 받아온 토큰의 값/terminal digit의 변환
Production Semantic Rules
L → E$
E → E1 + T
E → T
T → T1 * F
T → F
F → (E)
F → digit
print(E.val)
E.val := E1.val + T.val
E.val := T.val
T.val := T1.val * F.val
T.val := F.val
F.val := E.val
F.val := digit.lexval
Description of Semantic Actions
9. 9
값을 구하는 위치에 따라 2가지로 구분
합성화된 속성(Synthesized attribute)
생성 규칙의 왼쪽에 있는 nonterminal의 속성이 오른쪽에 있는 문법 심볼들
의 속성으로 결정되는 경우
ex) A XYZ A := F(X,Y,Z)
더 자연스러운 방법
상속된 속성(Inherited attribute)
생성 규칙의 오른쪽에 있는 nonterminal의 속성이 왼쪽에 있는 nonterminal
의 속성에 의해서 결정되는 경우
ex) A XYZ Y.val := 2 * X.val
문법 심볼의 속성
10. 10
Implementation of SDT
SDT를 이용하여 컴파일러 전단부 구성이 가능
입력 스캐너, 파서, 의미규칙 중간 언어 출력
SDT의 설계 과정
1) Input design - language construct에 대한 grammar를 cfg를 이용하여
design.
2) Scanner, Parser의 작성.
3) 의미 수행 코드 작성
4) 모듈의 통합
Examples :
1. Desk Calculator
2. AST 구성
11. 11
(1) Input design
0. S -> E $
1. E -> E + E
2. E -> E * E
3. E -> ( E )
4. E -> num
(2) Parsing table
9 r3 r3
8 r2 r2
7 r1 s5
6 s4 s5
r3
r2
r1
s8
5 s3
4 s3
3 r4 r4
2 s3
1 s4 s5
0 s3
symbols
states
num + *
s2
s2
r4
s2
s2
( )
r3
r2
r1
8
7
r4
6
acc
1
$ E
Desk Calculator
12. 12
(3) Semantic Specification
(4) Implementation of Desk Calculator
Parsing stack : Symbol stack + State stack + Value stack
Value stack : holding the values of the corresponding attribute.
Production Semantic Rules
L → E$
E → E1 + E2
E → E1 * E2
E → (E1)
E → num
print E.val
E.val := E1.val + E2.val
E.val := E1.val * E2.val
E.val := E1.val
E.val := num.lexval
Desk Calculator
13. 13
the code fragments do not show how variable top is managed.
lexval : token value
the code fragments are executed before a reduction takes place.
Production Code Fragment
S → E$
E → E + E
E → E * E
E → (E)
E → num
print (val[top])
val[top-2] := val[top-2] + val[top]
val[top-2] := val[top-2] * val[top]
val[top-2] := val[top-1]
val[top] := num.lexval
Desk Calculator
15. 15
AST는 소스 프로그램의 구조를 파스트리보다 훨씬 효율적으로 표현 할 수 있는 자료구조.
ex) a = b + 1;
ex) if (a > b) x = a; else x = b;
=
b
a +
1
if
x
=
aa
>
b x b
=
Construction of AST
16. 16
이항 연산자를 갖는 수식에 대한 AST 생성
각 함수는 새로 생성된 노드의 포인터 반환
1. mktree(op,left,right)는 레이블이 OP인 연산자 노드 생성하고 두 개
의 필드는 left와 right의 포인터를 포함.
2. mknode(a)는 a에 대한 터미널 노드 생성하고 노트 포인터 반
환.
Semantic Specification
E와 T에 대한 합성화된 속성 nptr은 함수 호출에 의해 복귀되는 포인터 값을 기억.
Production Semantic Rules
E → E1 + E
E → E1 - E
E → T
T → (E)
T → a
E.nptr := mktree(‘+’, E1.nptr, T.nptr)
E.nptr := mktree(‘-’, E1.nptr, T.nptr)
E.nptr := T.nptr
T.nptr := E.nptr
T.nptr := mknode(a)
Construction of AST
17. 17
AST for a - 4 + c
+
id
id num 4
to entry for a
to entry for c
-
Construction of AST