summaryrefslogtreecommitdiff
path: root/CMP/Compiler Construction.md
blob: d833d74919ca0f5f963c7988bc3fc360d07fb93e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# Strategies
## Interpretation
- **Portability** : no need to compile
- **Impact on the input language** : first order eval function, dynamic everything

## Virtual machines
Allows processor emulation to allow execution on any platform

## Transpilation
Conversion from a language to another (e.g. Arduino $\rightarrow$ C)
## Compilation
**Ahead of time** compilation : produce a binary file that can be run in the targeted architecture
**Just in time** compilation : involves compilation during execution

## Bytecode strategy
- portable
- low level
- no physical machine can understand it
- can be :
	- compiled
	- interpreted
	- executed by a VM

# Development tools

- Use warnings
- Use **assert/static_assert** (C++11)
- {Address,Thread,Memory,Leaks}Sanitizer
- lcov
## Autotools

- **autoconf** : package configuration
- **automake** : package build
- **libtool** : a portable build of shared libs
- **gettext** : 

# Lexical analysis

$$
chars \rightrightarrows SCAN \rightrightarrows tokens
$$

## Flex

- **F**ast **L**exical Analyzer generator
- GNU version of Lex
- TC now uses RE/Flex
### Variables
- **yytext** : token text
- **yyleng** : size of token text
- **yylex** : starts the scanning
- **yywrap** : called when end of text is encountered, can be redefined if needed

## Bison

- **yyparse**
- **yyerror**

## Coupling Flex & Bison

```sh
$ bison -o parser.cc -d --graph tmp.yy
$ flex -o lexer.cc tmp.ll
$ g++ -std=c++20 lexer.cc parser.cc
$ echo "1+2+3+98" | ./a.out
104
$ echo "1+2+3++98" | ./a.out
1.7: syntax error, unexpected +, expecting number
0
```
## Error recovery