Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions midterm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# TIP 언어 파서 · CFG/ICFG · Interval Analysis

TIP (Tiny Imperative Programming) 언어를 파싱하여 AST를 생성하고, CFG/ICFG를 구성한 뒤 Interval Analysis(Widening/Narrowing 포함)를 수행합니다. 결과는 Graphviz DOT(및 선택적 PDF)와 JSON으로 출력됩니다.

## 구현 내용
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

구현 언어 - Typescript

추가 1, 2, 3, 4 - 구현
추가 5 - 미구현

데모 보여주기
테스트 파일과 분석 결과를 동시에 보여주면서 설명
사실 코드를 잘 짜면 TIP 코드에 assert 없이 control sensitive analysis를 수행하도록 할 수 있음


- [x] Flow-sensitive 분석
- [x] Interval analysis
- [x] Widening
- [x] Narrowing
- [x] Control-sensitive analysis
- [x] Inter-procedural analysis
- [-] Context-sensitive analysis (k-callsite sensitivity w/ parameterized k) (구현 못함)

## 파일 구조

- `parser.ts` - TIP 파서 (Ohm.js 기반), 소스 → AST(`Program`)
- `tip-cfg-converter.ts` - 함수별 CFG 생성기 (DOT 내보내기 지원)
- `tip-icfg-converter.ts` - 프로그램 단일 ICFG 생성기 (함수 간 call/return 엣지 포함)
- `interval-analysis.ts` - ICFG 기반 Interval Analysis
- threshold 기반 widen/normalize, 후속 narrowing 단계 포함
- 분기 조건(`>`, `==`)에 대한 보수적 정제 지원
- `tip-all-in-one.ts` - 통합 실행 스크립트
- AST 생성 및 저장: `output/ast.json`
- CFG DOT(+PDF) 생성: `output/cfg/*.dot(.pdf)`
- ICFG DOT(+PDF) 생성: `output/icfg/main.dot(.pdf)`
- Interval 결과: `output/intervals.json`, `output/intervals_trace.json`
- `grammar.ohm` - TIP 문법 정의
- `types.ts` - AST 타입 정의
- `tip_code.txt` - 실행 입력 TIP 코드
- `test/` - 예제/평가용 입력 (`1. widening_narrowing.txt`, `2. control_sensitive.txt`, `3. interprocedural.txt`)

## 요구 사항

- Node.js (권장: v18+)
- `npm install`로 의존성 설치
- Graphviz(선택) 설치 시 DOT → PDF 자동 변환
- macOS: `brew install graphviz`
- Ubuntu: `sudo apt-get install graphviz`
- Windows: `https://graphviz.org/download/`

## 실행 방법 (통합)
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

설명 순서: grammar -> parser -> tip-icfg-converter -> interval analysis (iteration) -> interval analysis


```bash
# 의존성 설치
npm install

# 입력 코드 편집
# - 분석할 TIP 코드를 midterm/tip_code.txt에 작성

# 통합 실행 (AST → CFG/ICFG → Interval → DOT/PDF 변환까지)
npm run tip-all

# 생성물은 midterm/output/ 아래에 저장됩니다.
```

Graphviz 미설치 시 DOT 파일만 생성되며, 다음과 같이 수동 변환할 수 있습니다.

```bash
dot -Tpdf output/cfg/<func>.dot -o output/cfg/<func>.pdf
dot -Tpdf output/icfg/main.dot -o output/icfg/main.pdf
```

## 출력물 요약

- `output/ast.json`: 파싱된 AST
- `output/cfg/`: 함수별 CFG DOT(+PDF)
- `output/icfg/main.dot(.pdf)`: 단일 ICFG
- `output/intervals.json`: 노드별 추정 구간 환경(Top/±inf 포함)
- `output/intervals_trace.json`: 고정점 수렴 과정(와이덴/내로잉) 추적 로그

## 참고

- `package.json`의 권장 스크립트: `tip-all`
- 기타 과거 스크립트(`parser`, `cfg`, `normal`, `all`)는 현재 통합 흐름과 무관합니다.
73 changes: 73 additions & 0 deletions midterm/grammar.ohm
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
TIP {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIP 문법 명시파일
기존 TIP 구문에서 포인터 & 레코드 제외
ohm 파일로 문법을 작성해놓으면 parser에서 ohm-js 라이브러리를 통해 ast로 파싱

// 프로그램: 함수들의 시퀀스
Program = Function+

// 함수: x(x, ..., x){ (var x, ..., x;)? s return e; }
Function = identifier "(" Params? ")" "{" VarDecl? Statement* ReturnStmt "}"

Params = identifier ("," identifier)*
VarDecl = "var" identifier ("," identifier)* ";"

// 구문들
Statement = AssignmentStmt
| OutputStmt
| IfStmt
| WhileStmt
| CallStmt
| AssertStmt
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

control sensitive 분석을 위해 Assert 구문 추가



AssignmentStmt = identifier "=" Expression ";"
OutputStmt = "output" Expression ";"
CallStmt = identifier "(" Args? ")" ";"
IfStmt = "if" "(" Expression ")" Block ElseClause?
ElseClause = "else" Block
WhileStmt = "while" "(" Expression ")" Block
ReturnStmt = "return" Expression ";"

AssertStmt = "assert" "(" Expression ")" ";"

Block = "{" BlockStatement* "}"
BlockStatement = Statement


// 표현식들 (우선순위 순서대로)
Expression = ComparisonExpr

ComparisonExpr = ComparisonExpr ">" ArithExpr -- greater
| ComparisonExpr "==" ArithExpr -- equal
| ArithExpr

ArithExpr = ArithExpr "+" MulExpr -- add
| ArithExpr "-" MulExpr -- sub
| MulExpr

MulExpr = MulExpr "*" UnaryExpr -- mul
| MulExpr "/" UnaryExpr -- div
| UnaryExpr

UnaryExpr = PrimaryExpr

PrimaryExpr = "input" -- input
| "(" Expression ")" -- paren
| FunctionCallOrAccess
| number -- number
| identifier -- identifier

FunctionCallOrAccess = FunctionCallOrAccess "(" Args? ")" -- call
| identifier -- base



Args = Expression ("," Expression)*



// 기본 토큰들
identifier = letter (alnum | "_")*
number = digit+

// 공백 처리
space += comment
comment = "//" (~"\n" any)* "\n"
}
Loading