SlideShare a Scribd company logo
1 of 17
Download to read offline
Chapter 10
Generation of Intermediate
Language
2
목 차
 Introduction
 Syntax-Directed Translation
 SDT 적용 사례
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
 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
 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
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
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
 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
 값을 구하는 위치에 따라 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
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
 (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
 (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
 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
14
 (5) 입력 수식 23 * 5 + 4 $에 대한 구문분석기의 수행과정
 state input symbol value parse
 (0 , 23 * 5 + 4$, , , )
 s3
 ===> (0 3 , * 5 + 4$, num , , )
 r4
 ===> (0 1 , * 5 + 4$, E , 23 , 4 )
 s5
 ===> (0 1 5 , 5 + 4$, E * , 23_ , 4 )
 s3
 ===> (0 1 5 3 , + 4$, E * num , 23__ , 4 )
 r4
 ===> (0 1 5 8 , + 4$, E * E , 23_5 , 4 4 )
 r2
 ===> (0 1 , + 4$, E , 115 , 4 4 2 )
 s4
 ===> (0 1 4 , 4$, E + , 115_ , 4 4 2 )
 s3
 ===> (0 1 4 3 , $, E + num , 115__ , 4 4 2 )
 r4
 ===> (0 1 4 7 , $, E + E , 115_4 , 4 4 2 4 )
 r1
 ===> (0 1 , $, E , 119 , 4 4 2 4 1 )
 ===> accept
Desk Calculator
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
 이항 연산자를 갖는 수식에 대한 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
 AST for a - 4 + c
+
id
id num 4
to entry for a
to entry for c
-
Construction of AST

More Related Content

What's hot

Data Structure - 1st Study
Data Structure - 1st StudyData Structure - 1st Study
Data Structure - 1st StudyChris Ohk
 
[C++ Korea] Effective Modern C++ Study item14 16 +신촌
[C++ Korea] Effective Modern C++ Study item14 16 +신촌[C++ Korea] Effective Modern C++ Study item14 16 +신촌
[C++ Korea] Effective Modern C++ Study item14 16 +신촌Seok-joon Yun
 
6장 표현식 및 문장
6장 표현식 및 문장6장 표현식 및 문장
6장 표현식 및 문장재정 이
 
[C++ Korea] Effective Modern C++ MVA item 8 Prefer nullptr to 0 and null +윤석준
[C++ Korea] Effective Modern C++ MVA item 8 Prefer nullptr to 0 and null +윤석준[C++ Korea] Effective Modern C++ MVA item 8 Prefer nullptr to 0 and null +윤석준
[C++ Korea] Effective Modern C++ MVA item 8 Prefer nullptr to 0 and null +윤석준Seok-joon Yun
 
Design Pattern In Functional Language
Design Pattern In Functional LanguageDesign Pattern In Functional Language
Design Pattern In Functional LanguageSH Park
 
[C++ Korea] Effective Modern C++ mva item 7 distinguish between and {} when c...
[C++ Korea] Effective Modern C++ mva item 7 distinguish between and {} when c...[C++ Korea] Effective Modern C++ mva item 7 distinguish between and {} when c...
[C++ Korea] Effective Modern C++ mva item 7 distinguish between and {} when c...Seok-joon Yun
 
[C++ Korea] Effective Modern C++ MVA item 9 Prefer alias declarations to type...
[C++ Korea] Effective Modern C++ MVA item 9 Prefer alias declarations to type...[C++ Korea] Effective Modern C++ MVA item 9 Prefer alias declarations to type...
[C++ Korea] Effective Modern C++ MVA item 9 Prefer alias declarations to type...Seok-joon Yun
 
2.Startup JavaScript - 연산자
2.Startup JavaScript - 연산자2.Startup JavaScript - 연산자
2.Startup JavaScript - 연산자Circulus
 
[C++ korea] effective modern c++ study item 7 distinguish between () and {} w...
[C++ korea] effective modern c++ study item 7 distinguish between () and {} w...[C++ korea] effective modern c++ study item 7 distinguish between () and {} w...
[C++ korea] effective modern c++ study item 7 distinguish between () and {} w...Seok-joon Yun
 
[C++ korea] Effective Modern C++ study item 19 use shared ptr for shared owne...
[C++ korea] Effective Modern C++ study item 19 use shared ptr for shared owne...[C++ korea] Effective Modern C++ study item 19 use shared ptr for shared owne...
[C++ korea] Effective Modern C++ study item 19 use shared ptr for shared owne...Seok-joon Yun
 
[C++ korea] effective modern c++ study item8~10 정은식
[C++ korea] effective modern c++ study item8~10 정은식[C++ korea] effective modern c++ study item8~10 정은식
[C++ korea] effective modern c++ study item8~10 정은식은식 정
 
Effective Modern C++ MVA item 18 Use std::unique_ptr for exclusive-ownership ...
Effective Modern C++ MVA item 18 Use std::unique_ptr for exclusive-ownership ...Effective Modern C++ MVA item 18 Use std::unique_ptr for exclusive-ownership ...
Effective Modern C++ MVA item 18 Use std::unique_ptr for exclusive-ownership ...Seok-joon Yun
 
[C++ korea] effective modern c++ study item 14 declare functions noexcept if ...
[C++ korea] effective modern c++ study item 14 declare functions noexcept if ...[C++ korea] effective modern c++ study item 14 declare functions noexcept if ...
[C++ korea] effective modern c++ study item 14 declare functions noexcept if ...Seok-joon Yun
 
RNC C++ lecture_5 Array
RNC C++ lecture_5 ArrayRNC C++ lecture_5 Array
RNC C++ lecture_5 Arrayitlockit
 
Startup JavaScript 3 - 조건문, 반복문, 예외처리
Startup JavaScript 3 - 조건문, 반복문, 예외처리Startup JavaScript 3 - 조건문, 반복문, 예외처리
Startup JavaScript 3 - 조건문, 반복문, 예외처리Circulus
 
[C++ Korea] Effective Modern C++ Study, Item 1 - 3
[C++ Korea] Effective Modern C++ Study, Item 1 - 3[C++ Korea] Effective Modern C++ Study, Item 1 - 3
[C++ Korea] Effective Modern C++ Study, Item 1 - 3Chris Ohk
 

What's hot (20)

Data Structure - 1st Study
Data Structure - 1st StudyData Structure - 1st Study
Data Structure - 1st Study
 
6 swift 고급함수
6 swift 고급함수6 swift 고급함수
6 swift 고급함수
 
5 swift 기초함수
5 swift 기초함수5 swift 기초함수
5 swift 기초함수
 
[C++ Korea] Effective Modern C++ Study item14 16 +신촌
[C++ Korea] Effective Modern C++ Study item14 16 +신촌[C++ Korea] Effective Modern C++ Study item14 16 +신촌
[C++ Korea] Effective Modern C++ Study item14 16 +신촌
 
6장 표현식 및 문장
6장 표현식 및 문장6장 표현식 및 문장
6장 표현식 및 문장
 
[C++ Korea] Effective Modern C++ MVA item 8 Prefer nullptr to 0 and null +윤석준
[C++ Korea] Effective Modern C++ MVA item 8 Prefer nullptr to 0 and null +윤석준[C++ Korea] Effective Modern C++ MVA item 8 Prefer nullptr to 0 and null +윤석준
[C++ Korea] Effective Modern C++ MVA item 8 Prefer nullptr to 0 and null +윤석준
 
Design Pattern In Functional Language
Design Pattern In Functional LanguageDesign Pattern In Functional Language
Design Pattern In Functional Language
 
[C++ Korea] Effective Modern C++ mva item 7 distinguish between and {} when c...
[C++ Korea] Effective Modern C++ mva item 7 distinguish between and {} when c...[C++ Korea] Effective Modern C++ mva item 7 distinguish between and {} when c...
[C++ Korea] Effective Modern C++ mva item 7 distinguish between and {} when c...
 
[C++ Korea] Effective Modern C++ MVA item 9 Prefer alias declarations to type...
[C++ Korea] Effective Modern C++ MVA item 9 Prefer alias declarations to type...[C++ Korea] Effective Modern C++ MVA item 9 Prefer alias declarations to type...
[C++ Korea] Effective Modern C++ MVA item 9 Prefer alias declarations to type...
 
2.Startup JavaScript - 연산자
2.Startup JavaScript - 연산자2.Startup JavaScript - 연산자
2.Startup JavaScript - 연산자
 
[C++ korea] effective modern c++ study item 7 distinguish between () and {} w...
[C++ korea] effective modern c++ study item 7 distinguish between () and {} w...[C++ korea] effective modern c++ study item 7 distinguish between () and {} w...
[C++ korea] effective modern c++ study item 7 distinguish between () and {} w...
 
[C++ korea] Effective Modern C++ study item 19 use shared ptr for shared owne...
[C++ korea] Effective Modern C++ study item 19 use shared ptr for shared owne...[C++ korea] Effective Modern C++ study item 19 use shared ptr for shared owne...
[C++ korea] Effective Modern C++ study item 19 use shared ptr for shared owne...
 
[C++ korea] effective modern c++ study item8~10 정은식
[C++ korea] effective modern c++ study item8~10 정은식[C++ korea] effective modern c++ study item8~10 정은식
[C++ korea] effective modern c++ study item8~10 정은식
 
(Lisp)
(Lisp)(Lisp)
(Lisp)
 
Effective Modern C++ MVA item 18 Use std::unique_ptr for exclusive-ownership ...
Effective Modern C++ MVA item 18 Use std::unique_ptr for exclusive-ownership ...Effective Modern C++ MVA item 18 Use std::unique_ptr for exclusive-ownership ...
Effective Modern C++ MVA item 18 Use std::unique_ptr for exclusive-ownership ...
 
[C++ korea] effective modern c++ study item 14 declare functions noexcept if ...
[C++ korea] effective modern c++ study item 14 declare functions noexcept if ...[C++ korea] effective modern c++ study item 14 declare functions noexcept if ...
[C++ korea] effective modern c++ study item 14 declare functions noexcept if ...
 
RNC C++ lecture_5 Array
RNC C++ lecture_5 ArrayRNC C++ lecture_5 Array
RNC C++ lecture_5 Array
 
Startup JavaScript 3 - 조건문, 반복문, 예외처리
Startup JavaScript 3 - 조건문, 반복문, 예외처리Startup JavaScript 3 - 조건문, 반복문, 예외처리
Startup JavaScript 3 - 조건문, 반복문, 예외처리
 
[C++ Korea] Effective Modern C++ Study, Item 1 - 3
[C++ Korea] Effective Modern C++ Study, Item 1 - 3[C++ Korea] Effective Modern C++ Study, Item 1 - 3
[C++ Korea] Effective Modern C++ Study, Item 1 - 3
 
Changes in c++0x
Changes in c++0xChanges in c++0x
Changes in c++0x
 

Viewers also liked

01.표준프레임워크개요
01.표준프레임워크개요01.표준프레임워크개요
01.표준프레임워크개요Hankyo
 
01.모바일 프레임워크 이론
01.모바일 프레임워크 이론01.모바일 프레임워크 이론
01.모바일 프레임워크 이론Hankyo
 
컴파일러 Ch01
컴파일러 Ch01컴파일러 Ch01
컴파일러 Ch01Hankyo
 
03.[참고]표준프레임워크기반 개발방법
03.[참고]표준프레임워크기반 개발방법03.[참고]표준프레임워크기반 개발방법
03.[참고]표준프레임워크기반 개발방법Hankyo
 
05.실행환경 교육교재(업무처리,연계통합)
05.실행환경 교육교재(업무처리,연계통합)05.실행환경 교육교재(업무처리,연계통합)
05.실행환경 교육교재(업무처리,연계통합)Hankyo
 
04.모바일 device api_실습교재
04.모바일 device api_실습교재04.모바일 device api_실습교재
04.모바일 device api_실습교재Hankyo
 
01.공통컴포넌트 교육교재
01.공통컴포넌트 교육교재01.공통컴포넌트 교육교재
01.공통컴포넌트 교육교재Hankyo
 
01.개발환경 교육교재
01.개발환경 교육교재01.개발환경 교육교재
01.개발환경 교육교재Hankyo
 
02.모바일 실습교재(ux component)
02.모바일 실습교재(ux component)02.모바일 실습교재(ux component)
02.모바일 실습교재(ux component)Hankyo
 
예측 분석 산업별 사례 147
예측 분석 산업별 사례 147예측 분석 산업별 사례 147
예측 분석 산업별 사례 147eungjin cho
 
How to do a photo frame?
How to do a photo frame?How to do a photo frame?
How to do a photo frame?Rocío Delgado
 
In danh thiếp tphcm
In danh thiếp tphcmIn danh thiếp tphcm
In danh thiếp tphcmHIEP NGUYEN
 
Calidad en el servicio
Calidad en el servicioCalidad en el servicio
Calidad en el servicioYamilett123
 
Alain CV Avril 2016
Alain CV  Avril 2016Alain CV  Avril 2016
Alain CV Avril 2016Alain Savage
 
How to Start a Self-Hosted Blog in WordPress
How to Start a Self-Hosted Blog in WordPressHow to Start a Self-Hosted Blog in WordPress
How to Start a Self-Hosted Blog in WordPressFlordeliza Angeles
 

Viewers also liked (20)

Ch02
Ch02Ch02
Ch02
 
01.표준프레임워크개요
01.표준프레임워크개요01.표준프레임워크개요
01.표준프레임워크개요
 
01.모바일 프레임워크 이론
01.모바일 프레임워크 이론01.모바일 프레임워크 이론
01.모바일 프레임워크 이론
 
Ch09
Ch09Ch09
Ch09
 
Ch06
Ch06Ch06
Ch06
 
컴파일러 Ch01
컴파일러 Ch01컴파일러 Ch01
컴파일러 Ch01
 
03.[참고]표준프레임워크기반 개발방법
03.[참고]표준프레임워크기반 개발방법03.[참고]표준프레임워크기반 개발방법
03.[참고]표준프레임워크기반 개발방법
 
Ch03
Ch03Ch03
Ch03
 
Ch04
Ch04Ch04
Ch04
 
05.실행환경 교육교재(업무처리,연계통합)
05.실행환경 교육교재(업무처리,연계통합)05.실행환경 교육교재(업무처리,연계통합)
05.실행환경 교육교재(업무처리,연계통합)
 
04.모바일 device api_실습교재
04.모바일 device api_실습교재04.모바일 device api_실습교재
04.모바일 device api_실습교재
 
01.공통컴포넌트 교육교재
01.공통컴포넌트 교육교재01.공통컴포넌트 교육교재
01.공통컴포넌트 교육교재
 
01.개발환경 교육교재
01.개발환경 교육교재01.개발환경 교육교재
01.개발환경 교육교재
 
02.모바일 실습교재(ux component)
02.모바일 실습교재(ux component)02.모바일 실습교재(ux component)
02.모바일 실습교재(ux component)
 
예측 분석 산업별 사례 147
예측 분석 산업별 사례 147예측 분석 산업별 사례 147
예측 분석 산업별 사례 147
 
How to do a photo frame?
How to do a photo frame?How to do a photo frame?
How to do a photo frame?
 
In danh thiếp tphcm
In danh thiếp tphcmIn danh thiếp tphcm
In danh thiếp tphcm
 
Calidad en el servicio
Calidad en el servicioCalidad en el servicio
Calidad en el servicio
 
Alain CV Avril 2016
Alain CV  Avril 2016Alain CV  Avril 2016
Alain CV Avril 2016
 
How to Start a Self-Hosted Blog in WordPress
How to Start a Self-Hosted Blog in WordPressHow to Start a Self-Hosted Blog in WordPress
How to Start a Self-Hosted Blog in WordPress
 

Similar to Ch10

R 기초 : R Basics
R 기초 : R BasicsR 기초 : R Basics
R 기초 : R BasicsYoonwhan Lee
 
불어오는 변화의 바람, From c++98 to c++11, 14
불어오는 변화의 바람, From c++98 to c++11, 14 불어오는 변화의 바람, From c++98 to c++11, 14
불어오는 변화의 바람, From c++98 to c++11, 14 명신 김
 
스칼라와 스파크 영혼의 듀오
스칼라와 스파크 영혼의 듀오스칼라와 스파크 영혼의 듀오
스칼라와 스파크 영혼의 듀오Taeoh Kim
 
[C++ Korea 2nd Seminar] C++17 Key Features Summary
[C++ Korea 2nd Seminar] C++17 Key Features Summary[C++ Korea 2nd Seminar] C++17 Key Features Summary
[C++ Korea 2nd Seminar] C++17 Key Features SummaryChris Ohk
 
파이썬정리 20160130
파이썬정리 20160130파이썬정리 20160130
파이썬정리 20160130Yong Joon Moon
 
포트폴리오에서 사용한 모던 C++
포트폴리오에서 사용한 모던 C++포트폴리오에서 사용한 모던 C++
포트폴리오에서 사용한 모던 C++KWANGIL KIM
 
[0312 조진현] good bye dx9
[0312 조진현] good bye dx9[0312 조진현] good bye dx9
[0312 조진현] good bye dx9진현 조
 
파이썬 스터디 2주차
파이썬 스터디 2주차파이썬 스터디 2주차
파이썬 스터디 2주차Han Sung Kim
 
자바8 스트림 API 소개
자바8 스트림 API 소개자바8 스트림 API 소개
자바8 스트림 API 소개beom kyun choi
 
Modern C++의 타입 추론과 람다, 컨셉
Modern C++의 타입 추론과 람다, 컨셉Modern C++의 타입 추론과 람다, 컨셉
Modern C++의 타입 추론과 람다, 컨셉HyunJoon Park
 
Python + Excel
Python + Excel Python + Excel
Python + Excel POSTECH
 
파이썬 기본 문법
파이썬 기본 문법파이썬 기본 문법
파이썬 기본 문법SeongHyun Ahn
 
프로그래밍 대회: C++11 이야기
프로그래밍 대회: C++11 이야기프로그래밍 대회: C++11 이야기
프로그래밍 대회: C++11 이야기Jongwook Choi
 
Scala, Scalability
Scala, ScalabilityScala, Scalability
Scala, ScalabilityDongwook Lee
 
R 프로그램의 이해와 활용 v1.1
R 프로그램의 이해와 활용 v1.1R 프로그램의 이해와 활용 v1.1
R 프로그램의 이해와 활용 v1.1happychallenge
 

Similar to Ch10 (20)

R 기초 : R Basics
R 기초 : R BasicsR 기초 : R Basics
R 기초 : R Basics
 
불어오는 변화의 바람, From c++98 to c++11, 14
불어오는 변화의 바람, From c++98 to c++11, 14 불어오는 변화의 바람, From c++98 to c++11, 14
불어오는 변화의 바람, From c++98 to c++11, 14
 
강의자료 2
강의자료 2강의자료 2
강의자료 2
 
함수적 사고 2장
함수적 사고 2장함수적 사고 2장
함수적 사고 2장
 
스칼라와 스파크 영혼의 듀오
스칼라와 스파크 영혼의 듀오스칼라와 스파크 영혼의 듀오
스칼라와 스파크 영혼의 듀오
 
Java8 람다
Java8 람다Java8 람다
Java8 람다
 
[C++ Korea 2nd Seminar] C++17 Key Features Summary
[C++ Korea 2nd Seminar] C++17 Key Features Summary[C++ Korea 2nd Seminar] C++17 Key Features Summary
[C++ Korea 2nd Seminar] C++17 Key Features Summary
 
파이썬정리 20160130
파이썬정리 20160130파이썬정리 20160130
파이썬정리 20160130
 
포트폴리오에서 사용한 모던 C++
포트폴리오에서 사용한 모던 C++포트폴리오에서 사용한 모던 C++
포트폴리오에서 사용한 모던 C++
 
[0312 조진현] good bye dx9
[0312 조진현] good bye dx9[0312 조진현] good bye dx9
[0312 조진현] good bye dx9
 
파이썬 스터디 2주차
파이썬 스터디 2주차파이썬 스터디 2주차
파이썬 스터디 2주차
 
자바8 스트림 API 소개
자바8 스트림 API 소개자바8 스트림 API 소개
자바8 스트림 API 소개
 
Modern C++의 타입 추론과 람다, 컨셉
Modern C++의 타입 추론과 람다, 컨셉Modern C++의 타입 추론과 람다, 컨셉
Modern C++의 타입 추론과 람다, 컨셉
 
Python + Excel
Python + Excel Python + Excel
Python + Excel
 
파이썬 기본 문법
파이썬 기본 문법파이썬 기본 문법
파이썬 기본 문법
 
06장 함수
06장 함수06장 함수
06장 함수
 
프로그래밍 대회: C++11 이야기
프로그래밍 대회: C++11 이야기프로그래밍 대회: C++11 이야기
프로그래밍 대회: C++11 이야기
 
Scalability
ScalabilityScalability
Scalability
 
Scala, Scalability
Scala, ScalabilityScala, Scalability
Scala, Scalability
 
R 프로그램의 이해와 활용 v1.1
R 프로그램의 이해와 활용 v1.1R 프로그램의 이해와 활용 v1.1
R 프로그램의 이해와 활용 v1.1
 

More from Hankyo

01.실행환경 실습교재(공통기반)
01.실행환경 실습교재(공통기반)01.실행환경 실습교재(공통기반)
01.실행환경 실습교재(공통기반)Hankyo
 
07.실행환경 교육교재(표준프레임워크 세부 적용기준)
07.실행환경 교육교재(표준프레임워크 세부 적용기준)07.실행환경 교육교재(표준프레임워크 세부 적용기준)
07.실행환경 교육교재(표준프레임워크 세부 적용기준)Hankyo
 
06.실행환경 실습교재(easy company,해답)
06.실행환경 실습교재(easy company,해답)06.실행환경 실습교재(easy company,해답)
06.실행환경 실습교재(easy company,해답)Hankyo
 
06.실행환경 실습교재(easy company,문제)
06.실행환경 실습교재(easy company,문제)06.실행환경 실습교재(easy company,문제)
06.실행환경 실습교재(easy company,문제)Hankyo
 
04.실행환경 실습교재(화면처리)
04.실행환경 실습교재(화면처리)04.실행환경 실습교재(화면처리)
04.실행환경 실습교재(화면처리)Hankyo
 
04.실행환경 교육교재(화면처리)
04.실행환경 교육교재(화면처리)04.실행환경 교육교재(화면처리)
04.실행환경 교육교재(화면처리)Hankyo
 
04.[참고]개발환경 실습교재
04.[참고]개발환경 실습교재04.[참고]개발환경 실습교재
04.[참고]개발환경 실습교재Hankyo
 
03.실행환경 실습교재(배치처리)
03.실행환경 실습교재(배치처리)03.실행환경 실습교재(배치처리)
03.실행환경 실습교재(배치처리)Hankyo
 
03.실행환경 교육교재(배치처리)
03.실행환경 교육교재(배치처리)03.실행환경 교육교재(배치처리)
03.실행환경 교육교재(배치처리)Hankyo
 
03.모바일 실습교재(모바일 공통컴포넌트 실습)
03.모바일 실습교재(모바일 공통컴포넌트 실습)03.모바일 실습교재(모바일 공통컴포넌트 실습)
03.모바일 실습교재(모바일 공통컴포넌트 실습)Hankyo
 
03.[참고]개발환경 교육교재
03.[참고]개발환경 교육교재03.[참고]개발환경 교육교재
03.[참고]개발환경 교육교재Hankyo
 
02.실행환경 실습교재(데이터처리)
02.실행환경 실습교재(데이터처리)02.실행환경 실습교재(데이터처리)
02.실행환경 실습교재(데이터처리)Hankyo
 
02.실행환경 교육교재(데이터처리)
02.실행환경 교육교재(데이터처리)02.실행환경 교육교재(데이터처리)
02.실행환경 교육교재(데이터처리)Hankyo
 
02.개발환경 실습교재
02.개발환경 실습교재02.개발환경 실습교재
02.개발환경 실습교재Hankyo
 
02.[참고]오픈소스sw라이선스가이드라인
02.[참고]오픈소스sw라이선스가이드라인02.[참고]오픈소스sw라이선스가이드라인
02.[참고]오픈소스sw라이선스가이드라인Hankyo
 
02.공통컴포넌트 실습교재
02.공통컴포넌트 실습교재02.공통컴포넌트 실습교재
02.공통컴포넌트 실습교재Hankyo
 

More from Hankyo (16)

01.실행환경 실습교재(공통기반)
01.실행환경 실습교재(공통기반)01.실행환경 실습교재(공통기반)
01.실행환경 실습교재(공통기반)
 
07.실행환경 교육교재(표준프레임워크 세부 적용기준)
07.실행환경 교육교재(표준프레임워크 세부 적용기준)07.실행환경 교육교재(표준프레임워크 세부 적용기준)
07.실행환경 교육교재(표준프레임워크 세부 적용기준)
 
06.실행환경 실습교재(easy company,해답)
06.실행환경 실습교재(easy company,해답)06.실행환경 실습교재(easy company,해답)
06.실행환경 실습교재(easy company,해답)
 
06.실행환경 실습교재(easy company,문제)
06.실행환경 실습교재(easy company,문제)06.실행환경 실습교재(easy company,문제)
06.실행환경 실습교재(easy company,문제)
 
04.실행환경 실습교재(화면처리)
04.실행환경 실습교재(화면처리)04.실행환경 실습교재(화면처리)
04.실행환경 실습교재(화면처리)
 
04.실행환경 교육교재(화면처리)
04.실행환경 교육교재(화면처리)04.실행환경 교육교재(화면처리)
04.실행환경 교육교재(화면처리)
 
04.[참고]개발환경 실습교재
04.[참고]개발환경 실습교재04.[참고]개발환경 실습교재
04.[참고]개발환경 실습교재
 
03.실행환경 실습교재(배치처리)
03.실행환경 실습교재(배치처리)03.실행환경 실습교재(배치처리)
03.실행환경 실습교재(배치처리)
 
03.실행환경 교육교재(배치처리)
03.실행환경 교육교재(배치처리)03.실행환경 교육교재(배치처리)
03.실행환경 교육교재(배치처리)
 
03.모바일 실습교재(모바일 공통컴포넌트 실습)
03.모바일 실습교재(모바일 공통컴포넌트 실습)03.모바일 실습교재(모바일 공통컴포넌트 실습)
03.모바일 실습교재(모바일 공통컴포넌트 실습)
 
03.[참고]개발환경 교육교재
03.[참고]개발환경 교육교재03.[참고]개발환경 교육교재
03.[참고]개발환경 교육교재
 
02.실행환경 실습교재(데이터처리)
02.실행환경 실습교재(데이터처리)02.실행환경 실습교재(데이터처리)
02.실행환경 실습교재(데이터처리)
 
02.실행환경 교육교재(데이터처리)
02.실행환경 교육교재(데이터처리)02.실행환경 교육교재(데이터처리)
02.실행환경 교육교재(데이터처리)
 
02.개발환경 실습교재
02.개발환경 실습교재02.개발환경 실습교재
02.개발환경 실습교재
 
02.[참고]오픈소스sw라이선스가이드라인
02.[참고]오픈소스sw라이선스가이드라인02.[참고]오픈소스sw라이선스가이드라인
02.[참고]오픈소스sw라이선스가이드라인
 
02.공통컴포넌트 실습교재
02.공통컴포넌트 실습교재02.공통컴포넌트 실습교재
02.공통컴포넌트 실습교재
 

Ch10

  • 1. Chapter 10 Generation of Intermediate Language
  • 2. 2 목 차  Introduction  Syntax-Directed Translation  SDT 적용 사례
  • 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
  • 14. 14  (5) 입력 수식 23 * 5 + 4 $에 대한 구문분석기의 수행과정  state input symbol value parse  (0 , 23 * 5 + 4$, , , )  s3  ===> (0 3 , * 5 + 4$, num , , )  r4  ===> (0 1 , * 5 + 4$, E , 23 , 4 )  s5  ===> (0 1 5 , 5 + 4$, E * , 23_ , 4 )  s3  ===> (0 1 5 3 , + 4$, E * num , 23__ , 4 )  r4  ===> (0 1 5 8 , + 4$, E * E , 23_5 , 4 4 )  r2  ===> (0 1 , + 4$, E , 115 , 4 4 2 )  s4  ===> (0 1 4 , 4$, E + , 115_ , 4 4 2 )  s3  ===> (0 1 4 3 , $, E + num , 115__ , 4 4 2 )  r4  ===> (0 1 4 7 , $, E + E , 115_4 , 4 4 2 4 )  r1  ===> (0 1 , $, E , 119 , 4 4 2 4 1 )  ===> accept 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