-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotes.txt
47 lines (41 loc) · 1.22 KB
/
notes.txt
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
--- Grammar
start := expr
expr := expr op | op
op := + | - | . | , | < | > | loop
loop := [expr]
--- Left recursion elimination
(0) start := expr
(1) expr := op expr'
(2) expr' := op expr' | ε
(3) op := + | - | . | , | < | > | loop
(4) loop := [expr]
--- FIRST Sets
FIRST(start) = FIRST(expr)
FIRST(expr) = FIRST(op)
FIRST(expr') = FIRST(op) ∪ ε
FIRST(op) = { +, -, ., ,, <, > } ∪ FIRST(loop)
FIRST(loop) = { [ }
--- FOLLOW Sets
FOLLOW(start) = { $ }
FOLLOW(expr) = FOLLOW(start) ∪ { ] }
FOLLOW(expr') = FOLLOW(expr)
FOLLOW(op) = FOLLOW(expr') - ε ∪ FOLLOW(expr)
FOLLOW(loop) = FOLLOW(op)
--- Syntax-Directed Translation
start := expr { print('char array[LARGE_BUFFER] = { 0 }, *ptr = array;') }
expr := op expr'
| op
expr' := op expr' | ε
op := + { print('++*ptr') }
| - { print('--*ptr') }
| . { print('putchar(*ptr)') }
| , { print('*ptr = getchar()') }
| > { print('++ptr') }
| < { print('--ptr') }
loop := [ { print('while(*ptr) {') } expr ] { print('}') }
--- Test cases
- Source has no instructions
- Brackets not balanced
-- Disadvantage
- Invalid memory access (e.g. negative index)
- Fixed memory buffer