The Bourne-Again Shell by Chet Ramey
from The Architecture of Open Source Applications I (http://aosabook.org/en/bash.html)
@ Eva
Focus on Bash as interpreter, rather than System shell.
인터프리터 동작에 초점을 맞춰 진행했습니다.
Introduction
Levels ofAbstraction in Computing
Natural Language
Application Area
Programming Language
Compiler/Interpreter
Operating System
Machine Language
Interpreter
“In computerscience, an interpreter is a computer program that
directly executes” via Wikipedia/Interpreter(computing)
인터프리터는 컴퓨터 프로그램을 직접 실행하는 프로그램
Parse the source code and perform its behavior directly
소스코드를 분석하여 그 동작을 수행한다.
Translate source code into some efficient intermediate
representation and immediately execute this
소스코드를 (효율적인) 중간 표현으로 변환하여 실행한다.
Explicitly execute stored precompiled code made by a compiler
which is part of the interpreter system
인터프리터 시스템에서 컴파일러가 미리 컴파일한 코드를 실행한다.
Interpreter Design
구문 분석문맥 체크 입/출력 처리 명령 수행
입력 출력
Lexical
Analysis
구문 분석
Grammar
Syntactic
Analysis
문법 체크
Syntax
I/O
Processing
입/출력처리
Command
Execution
명령 수행
입력 출력
Semantic
Analysis
문맥 체크
Type
22.
Interpreter Design
LexicalAnalysis (Lexer/Scanner)
Automata
Token 생성
{Type, Contents}
int a = n * 5; [TYPE: int]
[ID: a]
[ASSIGN]
[ID: n]
[MULT]
[CONST: 5]
[SEMICOLON]
23.
Interpreter Design
SyntacticAnalysis (Syntactic Analyzer/Parser)
BNF (Backus-Naur Form) 에 의해 Syntax Tree 생성
Formula → Assignment
Assignment → Variable EQ Expression
Variable → TYPE ID | ID
Expression → Elem OP Elem
Elem → ID | CONST
OP → + | - | * | /
ID → a | b | c | … | z
CONST → 0 | 1 | 2| … | 9
Symbol Table 생성
int a = n * 5;
Formula
Assignment
Variable
TYPE Elem
EQ Expression
Elem ElemOP
int ID ID CONST*
a n 5
24.
Interpreter Design
SemanticAnalysis (Type Checker)
앞의 두 단계에서 생성한 Symbol Table에 Type 등의 정보를 추가
Syntax Tree에서 특정 변수와 연결된 변수 또는 상수의 타입이
Symbol Table에 있는 정보와 다른지 확인
Bash – TheBourne-Again Shell
Bourne Shell(sh)의 후계자
리눅스를 비롯한 대부분의 유닉스 시스템에서
가장 보편적으로 사용되는 쉘
GPLv3+ (http://www.gnu.org/software/bash/)
28.
Bash Shell Script
$ NAME=Juhwan
$ echo “Hello, $NAME”
Hello, Juhwan
$ if [[ "x$NAME" != "x" ]]; then echo "Anybody there?“; fi
Anybody there?
$ for N in {1..5}; do echo “Line #$N”; done
Line #1
Line #2
Line #3
Line #4
Line #5
29.
Bash Component Architecture
Input
LexicalAnalysis
and Parsing
Expansion
Command
Execution
Exit Status
Brace
Expansion
Variable and
Parameter
Expansion,
Command
Process,
Arithmetic
Substitution
Expansion Expansion
Tilde
Expansion
Input Processing
GNUReadline
줄 편집 및 입력 기록 저장 등의 역할을 하는 라이브러리
입력 자동 완성, 커서 이동, 잘라내기, 복사, 붙여 넣기 등 지원
Read
Dispatch
Execute
Re-display
키보드 또는
매크로 입력
키-인덱스
테이블 생성
키 입력 반영 &
App 실행 결과
표시
입력을 통해
App 호출
Word Splitting
InternelField Seperator (IFS)
IFS=$' tn' (in env. variable)
Expansion 수행 후 하나 이상의 단어로 변환
34.
Command Execution
POSIX에서는단순하게 명령을 호출하도록 정하고 있음
명령에 인자들을 끼워서 전달
ex) gcc –Wall –o a a.c
리다이렉션
Parser에서 File descriptor(fd)를 복사하는 형태로 전달
빌트인 명령
별도 프로세스를 만들지 않고 자체 실행
작업 제어
{ PID, state, return }
Foreground, Background
#17 Program을 실행하는 대신 Native Code 를 생성하면 Compiler가 된다.
Intermediate code에 비해 프로그램 구조 유지 및 최적화된 경우에는 더 간단한 표현이 가능하기 때문에 일반적으로 Intermediate code보다 선호됨 (컴파일러 내부동작 관점?)
최악의 경우에는 트리 노드에서 활용도에 비해 찾아다니는 비용이 더 커져서 오버헤드가 더 커지기도 한다.
#19 실행 중에 일부 코드를 Native Code로 변환, 최적화된 Intermediate code를 생성하는 기법
#20 해당 언어의 컴파일러가 없을 때 Self-interpreter를 만들어서 명령어 수행을 가능하게 한다.