Major Phases of a Compiler 컴파일러의 주요 단계들

What the phases do 각 단계는 무엇을 하나?

Lexical Analyzer

or scanning
Reads the stream of characters and group the characters into meaningful sequences called lexemes.
글자들을 읽어서 글자들에 의미를 부여하는 단계; 문장에서 단어를 잘라준다고 생각하면 편하다.
ex a = 1 + 2 는 a,=,1,+,2 5개의 token이 된다.

Syntax Analyzer

or parsing
The parser uses the first components of the tokens produced by the lexical analyzer to produce a tree-like intermediate representation that describes the grammatical structure of the token stream.
Parser는 lexer가 만들어준 토큰을 가지고, 문장에 의미를 부여하기 시작한다. (컴퓨터 과학에서는 tree로 token을 엮어낸다.)

Semantic Analyzer

Semantic analyzer uses the syntax tree and the information in the symbol table to check the source program for semantic consistency with the language definition.
Parser가 만들어준 트리로, 이 것이 과연 문법적으로 맞는 놈인지 문법을 검사한다.

Intermediate Code Generator
Machine-independent Code optimizer
Code Generator
Machine-dependent Code Optimizer

Data flow between phases 각 단계별 데이터의 흐름

  • Character Stream : Before lexical analysis ; lexer는 character들을 받아서
  • Token : Before syntax analysis ; token을 생성하고
  • Syntax Tree : before intermediate code generation ; parser는 syntax tree를 만들며
  • Intermediate representation : before code generation ; 코드 제네레이터는 중간 코드를 생성하고
  • Target Machine Code : after code generation ; 코드 제네레이터는 컴퓨터가 읽을수 있는 형태로 뽑아준다.

Citation:

  • Aho, Lam, Sethi, & Ullman, Compilers: Principles, Techniques, and Tools
  • UT Austin CS375: Compilers by G.Novak.