Skip to content

Commit 118ccee

Browse files
committed
LR parser table
1 parent b3b69a2 commit 118ccee

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed

Lab 5/README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# How to run
22

33
- compile `gcc main.c`
4-
- run `./a.out < input.txt`
4+
- run `./a.out < input.txt > output.txt`. Result will be saved in [output.txt](./output.txt)
55
- input format (file:[input.txt](./input.txt))
66
- The first line of input contains number of rules n.
77
- Then next n lines contains rules property lhs(left hand side symbol) and rhs(no of symbols in rhs). Eg. `A->Aa` becomes `A 2`
@@ -13,3 +13,7 @@
1313
- Next line Table rules (action) in matrix form: 00=blank, si=shift i, ri=reduce i, a0=accept
1414
- Next line Table rules (goto) in matrix form: -1=blank
1515
- Final line expression to evaluate
16+
17+
# Tools
18+
- Use [LR1 machine](http://jsmachines.sourceforge.net/machines/lr1.html) to create and verify programs.
19+
- Tool's indexing starts from 0 and my program's from 1. so if tools has reduce instruction `r3` my program's input will be `r4`. shift instructions are as it is.

Lab 5/input2.txt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
6
2+
S 1
3+
E 3
4+
E 3
5+
E 1
6+
B 1
7+
B 1
8+
3
9+
5
10+
9
11+
S E B
12+
+ * 0 1 $
13+
00 00 s3 s4 00
14+
s5 s6 00 00 a0
15+
r4 r4 00 00 r4
16+
r5 r5 00 00 r5
17+
r6 r6 00 00 r6
18+
00 00 s3 s4 00
19+
00 00 s3 s4 00
20+
r2 r2 00 00 r2
21+
r3 r3 00 00 r3
22+
-1 1 2
23+
-1 -1 -1
24+
-1 -1 -1
25+
-1 -1 -1
26+
-1 -1 -1
27+
-1 -1 7
28+
-1 -1 8
29+
-1 -1 -1
30+
-1 -1 -1
31+
1*1+1+1$

Lab 5/main.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,14 +125,10 @@ int* append_int(int n, int *arr, int *p){
125125
}
126126

127127
int getReduction(int k){
128-
// int a[] = {2,2,1};
129-
// return a[k-1];
130128
return rules[k-1].n;
131129
}
132130

133131
char getRedChar(int k){
134-
// char a[] = {'S','A','A'};
135-
// return a[k-1];
136132
return rules[k-1].c;
137133
}
138134

0 commit comments

Comments
 (0)