date: 2024-11-06
title: Compiler Introduction
status: DONE
author:
- AllenYGY
tags:
- NOTE
- Compiler
- Introduction
publish: true
Compiler Introduction
A compiler is a program that reads a program written in one programming language (the source language) and translates it into an equivalent program in another programming language (the target language).
Analysis comes in three phases:
The program does lexical analysis is called lexer or scanner.
We use regular expressions to do lexical analysis.
Example
num=1+23;
No. | Lexeme | Token |
---|---|---|
1 | num | identifier |
2 | = | ass_opt |
3 | 1 | int_literal |
4 | + | plus_opt |
5 | 23 | int_literal |
6 | ; | semicolon |
Here is a list of common tokens:
if
;关键字{}
;+
;true
, 123
;字面量The program which does syntax analysis is called a parser.
Same as using English grammar to check if the nouns, verbs, adjectives, etc., in a sentence are in the correct order.
Sometimes the parse tree can be extremely large.
Semantic Analysis also does type conversions for many programming languages.
float x=3.5;
int y=x;
the compiler adds a type conversion on