1.2 The Structure of a Compiler


Compiler에서 mapping을 2개로 나눈다면 analysis와 synthesis가 있다.


Analysis ( Front-end )

- Program을 문법적인 여러개의 조각으로 나눔.(break up, seperate) => 중간 결과물을 만들기 위해서.

- 코드가 잘못되면 사용자에게 유익한 정보를 줌.

- 소소코드 정보를 Symbol table에 저장.


Synthesis( Back-end )

- Intermediate representation, information in the symbol table을 가지고 target program 구성.


Compilation process (in more detail)

- intermediate representation은 명시적으로 구성 될 필요가 없다.

- Symbol table은 모두 Parser의 과정에서 사용된다.

- 몇몇 Compiler는 Machine-independent optimization phase 를 가짐.


Optimization phase

- front-end와 back-end 사이에서 작용.

- 목표 : 최적화 하지 않은 것보다 좋은 target program을 만들기 위해.

- 그렇지만, optimization은 선택 사항이다.




1.2.1. Lexical Analysis


- Compiler의 첫 번째 단계

1. source program의 문자열을 읽는다.

2. 의미 있는 sequences를 그룹화한다. (의미 있는 sequences == lexemes(어휘소))

그룹화 : Token stream  =  <token-name, attribute-value>

token-name : syntax-anlayzer에서 abstract symbol
attribute-value : symbol table의 entry

- Semantic analysis와 code generation에서 symbol-table entry 정보 필요



position = initial + rate * 60 (1.1)

<id,1> <=> <id,2> <+> <id,3> <*> <60> (1.2)



1.2.2 Syntax Analysis (Parsing) - 구문 분석


- Compiler의 두 번째 단계

- Syntax tree 생성 (for the token stream)

- 산술적인 관습을 따름 ( *는 +보다 먼저 연산)



1.2.3. Semantic Analysis - 의미 분석

- source program의 semantic consistency 검사

- type checking is important

- each Operator has matching operands

- 예시, arr[1] (OK) <-> arr[1.5] (소수점 허용 안됨)

- may permit some type conversions called coercions

- 정수, 소수 연산시 타입은 소수가 됨.

- inttofloat(60)은 명시적인 type convert 임.


1.2.4. Intermediate Code Generation


- 중간 코드는 다양한 형식으로 1개 이상 생성된다.

- Syntax tree는 syntax analysis와 semantic analysis에서 사용된다.

- Syntax and Semantic analysis 이후에 보통 Compiler는 low-level 언어 생성. 이를 abstract machine으로 생각할 수 있다.


- three-address assignment (6장에 나옴)

- 어셈블리어처럼 순서를 지닌다. 그리고 명령당 3개의 연산자가 있다.

- register처럼 동작

- output of the intermediate code generator consists of three-address code sequnce

- instruction은 오른쪽에 많아도 1개의 연산자를 갖는다.

- Compiler는 three-address instruction에 의해 계산된 값을 고정하려고 temporary name을 생성한다. 

- 첫 번째와 마지막 three-address instructions은 operand가 3개보다 작다.


1.2.5 Code Optimization


- inttofloat 함수를 사용하지 않아도 되게끔 코드를 만들어준다.

- shorter code, or target code that consumes less power.

- 정수를 소수로 변환해줌. (inttofloat 함수가 없어도 됨 = 자동 type convert)

- Simple optimizaer : 컴파일 속도를 너무 많이 늦추지 않고 target program의 속도 향상 가능

- optimizing compilers는 code optimization 단계에서 많은 시간을 보냄.


1.2.6. Code Generation




1.2.7. Symbol-Table Management


- Compiler의 직접적인 기능 : 변수 이름 저장하고 각각의 다양한 애용들 모음.

- Symbol table은 필드에 각 변수 명을 기록하는 데이터 구조다.

(데이터 구조 : 빠르게 이름을 찾고, 데이터를 빠르게 검색하는 구조)


1.2.8. The Grouping of Phases into Passes




1.2.9. Compiler-Construction Tools



+ Recent posts