move *.md to notes/*.md and add lecture pdf
This commit is contained in:
62
notes/1.md
Normal file
62
notes/1.md
Normal file
@@ -0,0 +1,62 @@
|
||||
컴파일러 1
|
||||
===
|
||||
|
||||
|
||||
# Interpreter in Modern Processors
|
||||
|
||||
|
||||
# Compiler
|
||||
|
||||
* Front-End
|
||||
|
||||
보이는 부분(HW에 신경을 안써도 됨)
|
||||
|
||||
* Back-End
|
||||
|
||||
HW에 밀접한 최적화를 해줌
|
||||
|
||||
|
||||
## General Structure of a Modern Compiler
|
||||
|
||||
* Front-End
|
||||
* Lexial Analysis
|
||||
* Syntax Analysis
|
||||
* Semantic Analysis
|
||||
* Code Generation - 1
|
||||
* Back-End
|
||||
* Control/DataFlow Analysis
|
||||
* Optimization
|
||||
* Code Generation - 2
|
||||
|
||||
이런 모듈식 구조는 다양한 언어와 하드웨어에 쉽게 적용할 수 있도록 도운다.
|
||||
|
||||
### Lexical Analysis (Scanner)
|
||||
|
||||
프로그램을 `token`의 의미 단위로 나눔.
|
||||
그리고 의미가 없는 단위를 지움.
|
||||
|
||||
보통 FSA로 구현함.
|
||||
|
||||
### Syntax Analysis (Parser)
|
||||
|
||||
미리 있는 Grammar를 바탕으로 Syntax Correctness를 진행함.
|
||||
|
||||
### Semantic Analysis
|
||||
|
||||
* identifier 정의 등
|
||||
* 타입 체크
|
||||
* 스코프 체크
|
||||
* 오버로딩 모호화 해소
|
||||
* IR로 변환
|
||||
|
||||
### Optimization
|
||||
|
||||
최적화함
|
||||
* 상수 최적화
|
||||
* 안쓰는 변수
|
||||
* loop에서 안바뀌는 변수
|
||||
* 다시 똑같이 계산하는 변수 제거
|
||||
* ...
|
||||
|
||||
### Code Generation
|
||||
|
||||
123
notes/2.md
Normal file
123
notes/2.md
Normal file
@@ -0,0 +1,123 @@
|
||||
Lexical Analysis
|
||||
===
|
||||
|
||||
포트란은 모든 whitespace를 지움.
|
||||
|
||||
```fortran
|
||||
do 5 I = 1.25
|
||||
|
||||
```
|
||||
|
||||
```
|
||||
do 5 I = 1,25
|
||||
```
|
||||
|
||||
|
||||
## Tokens
|
||||
|
||||
대표적인 토큰의 예시
|
||||
|
||||
* Identifiers
|
||||
* Keywords
|
||||
* Integers
|
||||
* Floating-points
|
||||
* Symbols
|
||||
* Strings
|
||||
|
||||
하기 위해서 하는 것
|
||||
|
||||
* Specification
|
||||
|
||||
확실하게 명세를 해줘야함.
|
||||
|
||||
* Recognition
|
||||
|
||||
DFA를 이용해서 패턴 매칭
|
||||
|
||||
* Automation
|
||||
|
||||
RE로 부터 DFA를 generate해야함
|
||||
|
||||
Lex라는 툴을 이용
|
||||
|
||||
그러나 내부적으로는 Tompson's construction (RE -> NFA), Subset Construction(NFA -> DFA)도 알아야함
|
||||
|
||||
|
||||
## Specification
|
||||
|
||||
**Regular Expression**
|
||||
|
||||
* 여러가지에 사용됨 `grep`, `find`, `sed`, `awk`
|
||||
|
||||
|
||||
|
||||
Multiple Matches
|
||||
|
||||
`elsex = 0`이라는 코드에서
|
||||
|
||||
`else / x / = / 0`
|
||||
또는
|
||||
|
||||
`elsex / = 0` 두가지 선택지가 있음. 둘 중 하나를 무조건 골라야함. 이때 가장 긴 토큰이 선택된다.
|
||||
|
||||
* `elsex`가 `else`보다 더 길어서 `elsex`가 선택됨.
|
||||
|
||||
만약에 두 경우가 모두 똑같다면 토큰 종류의 우선순위에 따라 선택된다.
|
||||
|
||||
* `Keyword`가 `Identifier`가 더 높음.
|
||||
|
||||
## Recognition
|
||||
|
||||
FSA를 이용함.
|
||||
|
||||
DFA와 NFA의 표현력은 동일하나 DFA는 편하게 구현할 수 있다는 장점이 있음.
|
||||
NFA는 RE로부터 쉽게 변환가능하다는 장점이 있음.
|
||||
|
||||
**Lexical Analysis**
|
||||
|
||||
`Lexical Spec -> RE -> NFA -> DFA -> Table`
|
||||
|
||||
## Automation
|
||||
|
||||
* `Lex`(`Flex`: faster implementation of Lex)
|
||||
* `Bison`
|
||||
|
||||
### Lex/Flex
|
||||
|
||||
* Definition Section
|
||||
* can declear or include var, enumeration, using the code in between `%{`, `%}`
|
||||
* provide names sub-rules for complex patterns used in **rules**
|
||||
|
||||
* Rules Section
|
||||
* Lexical Pattern
|
||||
|
||||
|
||||
* User Function Section
|
||||
* Copied to the Lex Program
|
||||
|
||||
|
||||
```c
|
||||
// example.l
|
||||
%{
|
||||
#include <stdio.h>
|
||||
int num_lines = 0;
|
||||
%}
|
||||
%%
|
||||
[ \t] {}
|
||||
a |
|
||||
an |
|
||||
the {printf("%s: is an article\n", yytext)}
|
||||
[a-z]+ {printf("%s: ???\n", yytext)}
|
||||
%%
|
||||
main() {
|
||||
yylex();1
|
||||
}
|
||||
```
|
||||
|
||||
### Handwork
|
||||
|
||||
* Thompson's construction (RE -> NFA)
|
||||
* Subset Construction(NFA -> DFA)
|
||||
|
||||
|
||||
DFA Optimization
|
||||
23
notes/3.md
Normal file
23
notes/3.md
Normal file
@@ -0,0 +1,23 @@
|
||||
# Syntax Analysis
|
||||
|
||||
|
||||
## Context-Free Grammars (CFG)
|
||||
|
||||
## Parse Tree
|
||||
|
||||
A tree representation of the derivation
|
||||
|
||||
parse tree has `terminals` at the leaves, `non-terminals` at the interior.
|
||||
|
||||
An in-order traversal of the leaves is the original input.
|
||||
|
||||
* leftmost derivation
|
||||
* rightmost derivation
|
||||
|
||||
### Ambiguity
|
||||
|
||||
should be resolved
|
||||
|
||||
for example: `A + B * C` should be resolved
|
||||
|
||||
|
||||
Reference in New Issue
Block a user