Compare commits
3 Commits
9af73c5a15
...
844f29a803
| Author | SHA1 | Date | |
|---|---|---|---|
| 844f29a803 | |||
| cc3de91036 | |||
| f9a5bcf4b2 |
71
notes/4.md
71
notes/4.md
@@ -83,12 +83,12 @@ L -> S | L,S
|
|||||||
|
|
||||||
It can be represented as a NFA:
|
It can be represented as a NFA:
|
||||||
|
|
||||||
```python {cmd matplotlib}
|
```python {cmd matplotlib hide}
|
||||||
import sys
|
import sys
|
||||||
import pymupdf
|
import pymupdf
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
doc = pymupdf.open("../pdf/L4.pdf")
|
doc = pymupdf.open("../pdf/L4.pdf")
|
||||||
pix = doc[22].get_pixmap(dpi=500)
|
pix = doc[22].get_pixmap(dpi=360)
|
||||||
img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
|
img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
|
||||||
|
|
||||||
plt.imshow(img)
|
plt.imshow(img)
|
||||||
@@ -97,8 +97,73 @@ plt.tight_layout()
|
|||||||
plt.show()
|
plt.show()
|
||||||
```
|
```
|
||||||
|
|
||||||
|
* SLR(1) Parsing
|
||||||
|
|
||||||
|
* LR(1) Grammar
|
||||||
|
|
||||||
|
|
||||||
|
```python {cmd matplotlib hide}
|
||||||
|
import sys
|
||||||
|
import pymupdf
|
||||||
|
from PIL import Image
|
||||||
|
doc = pymupdf.open("../pdf/L4.pdf")
|
||||||
|
pix = doc[47].get_pixmap(dpi=360)
|
||||||
|
img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
|
||||||
|
|
||||||
|
plt.imshow(img)
|
||||||
|
plt.axis('off')
|
||||||
|
plt.tight_layout()
|
||||||
|
plt.show()
|
||||||
|
```
|
||||||
|
|
||||||
|
LR(1) Parsing Table
|
||||||
|
|
||||||
|
is same as LR(0) parsing table construction except for reductions:
|
||||||
|
|
||||||
|
|
||||||
* LR(1) Grammar
|
* LALR(1) Grammar
|
||||||
|
|
||||||
|
LALR(1) generally has the same number of states as SLR (much less than LR(1))
|
||||||
|
for Pascal language, SLR requires several hundred states, LR(1) requires several thousand states.
|
||||||
|
|
||||||
|
#### Ambiguous Grammar
|
||||||
|
|
||||||
|
Ambiguity is mainly from
|
||||||
|
|
||||||
|
* Precedence
|
||||||
|
* The production at higher levels will have operators with lower priorities (and vice versa).
|
||||||
|
* we can insert non-terminals to enforce precendence.
|
||||||
|
* Associativity
|
||||||
|
* we should determine where to place recursion depending on the associativity
|
||||||
|
|
||||||
|
|
||||||
|
for example: `if-then-else`
|
||||||
|
|
||||||
|
|
||||||
|
**Automatic Disambiguation**
|
||||||
|
|
||||||
|
We can define precedence to use ambiguous grammars w/o shift-reduce conflicts.
|
||||||
|
|
||||||
|
|
||||||
|
## AST
|
||||||
|
|
||||||
|
### AST Construction LL
|
||||||
|
|
||||||
|
```c
|
||||||
|
expr parse_S() {
|
||||||
|
switch(token) {
|
||||||
|
case num:
|
||||||
|
case '(':
|
||||||
|
expr child1 = parse_E();
|
||||||
|
expr child2 = parse_Sp();
|
||||||
|
return new S(child1, child2);
|
||||||
|
default:
|
||||||
|
parseError();
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
### AST Construction LR
|
||||||
|
|
||||||
|
|||||||
73
src/res/result.1.txt
Normal file
73
src/res/result.1.txt
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
|
||||||
|
C-MINUS COMPILATION: ./test.1.txt
|
||||||
|
4: reserved word: int
|
||||||
|
4: ID, name= gcd
|
||||||
|
4: (
|
||||||
|
4: reserved word: int
|
||||||
|
4: ID, name= u
|
||||||
|
4: ,
|
||||||
|
4: reserved word: int
|
||||||
|
4: ID, name= v
|
||||||
|
4: )
|
||||||
|
5: {
|
||||||
|
6: reserved word: if
|
||||||
|
6: (
|
||||||
|
6: ID, name= v
|
||||||
|
6: ==
|
||||||
|
6: NUM, val= 0
|
||||||
|
6: )
|
||||||
|
6: reserved word: return
|
||||||
|
6: ID, name= u
|
||||||
|
6: ;
|
||||||
|
7: reserved word: else
|
||||||
|
7: reserved word: return
|
||||||
|
7: ID, name= gcd
|
||||||
|
7: (
|
||||||
|
7: ID, name= v
|
||||||
|
7: ,
|
||||||
|
7: ID, name= u
|
||||||
|
7: -
|
||||||
|
7: ID, name= u
|
||||||
|
7: /
|
||||||
|
7: ID, name= v
|
||||||
|
7: *
|
||||||
|
7: ID, name= v
|
||||||
|
7: )
|
||||||
|
7: ;
|
||||||
|
9: }
|
||||||
|
11: reserved word: void
|
||||||
|
11: ID, name= main
|
||||||
|
11: (
|
||||||
|
11: reserved word: void
|
||||||
|
11: )
|
||||||
|
12: {
|
||||||
|
13: reserved word: int
|
||||||
|
13: ID, name= x
|
||||||
|
13: ;
|
||||||
|
13: reserved word: int
|
||||||
|
13: ID, name= y
|
||||||
|
13: ;
|
||||||
|
14: ID, name= x
|
||||||
|
14: =
|
||||||
|
14: ID, name= input
|
||||||
|
14: (
|
||||||
|
14: )
|
||||||
|
14: ;
|
||||||
|
14: ID, name= y
|
||||||
|
14: =
|
||||||
|
14: ID, name= input
|
||||||
|
14: (
|
||||||
|
14: )
|
||||||
|
14: ;
|
||||||
|
15: ID, name= output
|
||||||
|
15: (
|
||||||
|
15: ID, name= gcd
|
||||||
|
15: (
|
||||||
|
15: ID, name= x
|
||||||
|
15: ,
|
||||||
|
15: ID, name= y
|
||||||
|
15: )
|
||||||
|
15: )
|
||||||
|
15: ;
|
||||||
|
16: }
|
||||||
|
17: EOF
|
||||||
77
src/res/result.2.txt
Normal file
77
src/res/result.2.txt
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
|
||||||
|
C-MINUS COMPILATION: ./test.2.txt
|
||||||
|
1: reserved word: void
|
||||||
|
1: ID, name= main
|
||||||
|
1: (
|
||||||
|
1: reserved word: void
|
||||||
|
1: )
|
||||||
|
2: {
|
||||||
|
3: reserved word: int
|
||||||
|
3: ID, name= i
|
||||||
|
3: ;
|
||||||
|
3: reserved word: int
|
||||||
|
3: ID, name= x
|
||||||
|
3: [
|
||||||
|
3: NUM, val= 5
|
||||||
|
3: ]
|
||||||
|
3: ;
|
||||||
|
5: ID, name= i
|
||||||
|
5: =
|
||||||
|
5: NUM, val= 0
|
||||||
|
5: ;
|
||||||
|
6: reserved word: while
|
||||||
|
6: (
|
||||||
|
6: ID, name= i
|
||||||
|
6: <
|
||||||
|
6: NUM, val= 5
|
||||||
|
6: )
|
||||||
|
7: {
|
||||||
|
8: ID, name= x
|
||||||
|
8: [
|
||||||
|
8: ID, name= i
|
||||||
|
8: ]
|
||||||
|
8: =
|
||||||
|
8: ID, name= input
|
||||||
|
8: (
|
||||||
|
8: )
|
||||||
|
8: ;
|
||||||
|
10: ID, name= i
|
||||||
|
10: =
|
||||||
|
10: ID, name= i
|
||||||
|
10: +
|
||||||
|
10: NUM, val= 1
|
||||||
|
10: ;
|
||||||
|
11: }
|
||||||
|
13: ID, name= i
|
||||||
|
13: =
|
||||||
|
13: NUM, val= 0
|
||||||
|
13: ;
|
||||||
|
14: reserved word: while
|
||||||
|
14: (
|
||||||
|
14: ID, name= i
|
||||||
|
14: <=
|
||||||
|
14: NUM, val= 4
|
||||||
|
14: )
|
||||||
|
15: {
|
||||||
|
16: reserved word: if
|
||||||
|
16: (
|
||||||
|
16: ID, name= x
|
||||||
|
16: [
|
||||||
|
16: ID, name= i
|
||||||
|
16: ]
|
||||||
|
16: !=
|
||||||
|
16: NUM, val= 0
|
||||||
|
16: )
|
||||||
|
17: {
|
||||||
|
18: ID, name= output
|
||||||
|
18: (
|
||||||
|
18: ID, name= x
|
||||||
|
18: [
|
||||||
|
18: ID, name= i
|
||||||
|
18: ]
|
||||||
|
18: )
|
||||||
|
18: ;
|
||||||
|
19: }
|
||||||
|
20: }
|
||||||
|
21: }
|
||||||
|
22: EOF
|
||||||
73
src/res/test.1.result.txt
Normal file
73
src/res/test.1.result.txt
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
|
||||||
|
C-MINUS COMPILATION: ./res/test.1.txt
|
||||||
|
4: reserved word: int
|
||||||
|
4: ID, name= gcd
|
||||||
|
4: (
|
||||||
|
4: reserved word: int
|
||||||
|
4: ID, name= u
|
||||||
|
4: ,
|
||||||
|
4: reserved word: int
|
||||||
|
4: ID, name= v
|
||||||
|
4: )
|
||||||
|
5: {
|
||||||
|
6: reserved word: if
|
||||||
|
6: (
|
||||||
|
6: ID, name= v
|
||||||
|
6: ==
|
||||||
|
6: NUM, val= 0
|
||||||
|
6: )
|
||||||
|
6: reserved word: return
|
||||||
|
6: ID, name= u
|
||||||
|
6: ;
|
||||||
|
7: reserved word: else
|
||||||
|
7: reserved word: return
|
||||||
|
7: ID, name= gcd
|
||||||
|
7: (
|
||||||
|
7: ID, name= v
|
||||||
|
7: ,
|
||||||
|
7: ID, name= u
|
||||||
|
7: -
|
||||||
|
7: ID, name= u
|
||||||
|
7: /
|
||||||
|
7: ID, name= v
|
||||||
|
7: *
|
||||||
|
7: ID, name= v
|
||||||
|
7: )
|
||||||
|
7: ;
|
||||||
|
9: }
|
||||||
|
11: reserved word: void
|
||||||
|
11: ID, name= main
|
||||||
|
11: (
|
||||||
|
11: reserved word: void
|
||||||
|
11: )
|
||||||
|
12: {
|
||||||
|
13: reserved word: int
|
||||||
|
13: ID, name= x
|
||||||
|
13: ;
|
||||||
|
13: reserved word: int
|
||||||
|
13: ID, name= y
|
||||||
|
13: ;
|
||||||
|
14: ID, name= x
|
||||||
|
14: =
|
||||||
|
14: ID, name= input
|
||||||
|
14: (
|
||||||
|
14: )
|
||||||
|
14: ;
|
||||||
|
14: ID, name= y
|
||||||
|
14: =
|
||||||
|
14: ID, name= input
|
||||||
|
14: (
|
||||||
|
14: )
|
||||||
|
14: ;
|
||||||
|
15: ID, name= output
|
||||||
|
15: (
|
||||||
|
15: ID, name= gcd
|
||||||
|
15: (
|
||||||
|
15: ID, name= x
|
||||||
|
15: ,
|
||||||
|
15: ID, name= y
|
||||||
|
15: )
|
||||||
|
15: )
|
||||||
|
15: ;
|
||||||
|
16: }
|
||||||
|
17: EOF
|
||||||
16
src/res/test.1.txt
Normal file
16
src/res/test.1.txt
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
/* A program to perform Euclid's
|
||||||
|
Algorithm to computer gcd */
|
||||||
|
|
||||||
|
int gcd (int u, int v)
|
||||||
|
{
|
||||||
|
if (v == 0) return u;
|
||||||
|
else return gcd(v,u-u/v*v);
|
||||||
|
/* u-u/v*v == u mod v */
|
||||||
|
}
|
||||||
|
|
||||||
|
void main(void)
|
||||||
|
{
|
||||||
|
int x; int y;
|
||||||
|
x = input(); y = input();
|
||||||
|
output(gcd(x,y));
|
||||||
|
}
|
||||||
77
src/res/test.2.result.txt
Normal file
77
src/res/test.2.result.txt
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
|
||||||
|
C-MINUS COMPILATION: ./res/test.2.txt
|
||||||
|
1: reserved word: void
|
||||||
|
1: ID, name= main
|
||||||
|
1: (
|
||||||
|
1: reserved word: void
|
||||||
|
1: )
|
||||||
|
2: {
|
||||||
|
3: reserved word: int
|
||||||
|
3: ID, name= i
|
||||||
|
3: ;
|
||||||
|
3: reserved word: int
|
||||||
|
3: ID, name= x
|
||||||
|
3: [
|
||||||
|
3: NUM, val= 5
|
||||||
|
3: ]
|
||||||
|
3: ;
|
||||||
|
5: ID, name= i
|
||||||
|
5: =
|
||||||
|
5: NUM, val= 0
|
||||||
|
5: ;
|
||||||
|
6: reserved word: while
|
||||||
|
6: (
|
||||||
|
6: ID, name= i
|
||||||
|
6: <
|
||||||
|
6: NUM, val= 5
|
||||||
|
6: )
|
||||||
|
7: {
|
||||||
|
8: ID, name= x
|
||||||
|
8: [
|
||||||
|
8: ID, name= i
|
||||||
|
8: ]
|
||||||
|
8: =
|
||||||
|
8: ID, name= input
|
||||||
|
8: (
|
||||||
|
8: )
|
||||||
|
8: ;
|
||||||
|
10: ID, name= i
|
||||||
|
10: =
|
||||||
|
10: ID, name= i
|
||||||
|
10: +
|
||||||
|
10: NUM, val= 1
|
||||||
|
10: ;
|
||||||
|
11: }
|
||||||
|
13: ID, name= i
|
||||||
|
13: =
|
||||||
|
13: NUM, val= 0
|
||||||
|
13: ;
|
||||||
|
14: reserved word: while
|
||||||
|
14: (
|
||||||
|
14: ID, name= i
|
||||||
|
14: <=
|
||||||
|
14: NUM, val= 4
|
||||||
|
14: )
|
||||||
|
15: {
|
||||||
|
16: reserved word: if
|
||||||
|
16: (
|
||||||
|
16: ID, name= x
|
||||||
|
16: [
|
||||||
|
16: ID, name= i
|
||||||
|
16: ]
|
||||||
|
16: !=
|
||||||
|
16: NUM, val= 0
|
||||||
|
16: )
|
||||||
|
17: {
|
||||||
|
18: ID, name= output
|
||||||
|
18: (
|
||||||
|
18: ID, name= x
|
||||||
|
18: [
|
||||||
|
18: ID, name= i
|
||||||
|
18: ]
|
||||||
|
18: )
|
||||||
|
18: ;
|
||||||
|
19: }
|
||||||
|
20: }
|
||||||
|
21: }
|
||||||
|
22: EOF
|
||||||
21
src/res/test.2.txt
Normal file
21
src/res/test.2.txt
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
void main(void)
|
||||||
|
{
|
||||||
|
int i; int x[5];
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while( i < 5 )
|
||||||
|
{
|
||||||
|
x[i] = input();
|
||||||
|
|
||||||
|
i = i + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
i = 0;
|
||||||
|
while( i <= 4 )
|
||||||
|
{
|
||||||
|
if( x[i] != 0 )
|
||||||
|
{
|
||||||
|
output(x[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -110,6 +110,8 @@ TokenType getToken(void) { /* index for storing into tokenString */
|
|||||||
save = FALSE;
|
save = FALSE;
|
||||||
else if (c == '=')
|
else if (c == '=')
|
||||||
state = INASSIGN;
|
state = INASSIGN;
|
||||||
|
else if(c == '!')
|
||||||
|
state = INNE;
|
||||||
else if (c == '<') {
|
else if (c == '<') {
|
||||||
state = INLT;
|
state = INLT;
|
||||||
} else if (c == '>') {
|
} else if (c == '>') {
|
||||||
|
|||||||
Reference in New Issue
Block a user