Skip to content

Commit 33a8b96

Browse files
committed
Add a human friendly grammar.
1 parent 8d5baa9 commit 33a8b96

File tree

4 files changed

+327
-0
lines changed

4 files changed

+327
-0
lines changed

bnf/.gitignore

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
Absyn.c
2+
Absyn.c.bak
3+
Absyn.h
4+
Absyn.h.bak
5+
Absyn.o
6+
Bison.h
7+
Buffer.c
8+
Buffer.c.bak
9+
Buffer.h
10+
Buffer.h.bak
11+
Buffer.o
12+
Ds.l
13+
Ds.l.bak
14+
Ds.y
15+
Ds.y.bak
16+
Lexer.c
17+
Lexer.o
18+
Main
19+
Main.o
20+
Parser.c
21+
Parser.h
22+
Parser.h.bak
23+
Parser.o
24+
Printer.c
25+
Printer.c.bak
26+
Printer.h
27+
Printer.h.bak
28+
Printer.o
29+
Skeleton.c
30+
Skeleton.c.bak
31+
Skeleton.h
32+
Skeleton.h.bak
33+
Test.c
34+
Test.c.bak

bnf/Ds.cf

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
-- This file could be used with https://bnfc.digitalgrammars.com/
2+
3+
entrypoints Rule ;
4+
5+
comment "//" ;
6+
7+
token Identity ["!\"#$%&'*+-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"] + ;
8+
-- all char except (),
9+
10+
LogicalOr. Term1 ::= Term1 "||" Term2 ;
11+
LogicalAnd. Term2 ::= Term2 "&&" Term3 ;
12+
BitwiseInclusiveOr. Term3 ::= Term3 "|" Term4 ;
13+
BitwiseExclusiveOr. Term4 ::= Term4 "^" Term5 ;
14+
BitwiseAnd. Term5 ::= Term5 "&" Term6 ;
15+
Equality. Term6 ::= Term6 "==" Term7 ;
16+
Inequality. Term6 ::= Term6 "!=" Term7 ;
17+
LessThan. Term7 ::= Term7 "<" Term8 ;
18+
GreaterThan. Term7 ::= Term7 ">" Term8 ;
19+
LessThanOrEqualTo. Term7 ::= Term7 "<=" Term8 ;
20+
GreaterThanOrEqualTo. Term7 ::= Term7 ">=" Term8 ;
21+
LeftShift. Term8 ::= Term8 "<<" Term9 ;
22+
RightShift. Term8 ::= Term8 ">>" Term9 ;
23+
Addition. Term9 ::= Term9 "+" Term10 ;
24+
Subtraction. Term9 ::= Term9 "-" Term10 ;
25+
Multiplication. Term10 ::= Term10 "*" Term11 ;
26+
Division. Term10 ::= Term10 "/" Term11 ;
27+
Modulus. Term10 ::= Term10 "%" Term11 ;
28+
Complement. Term11 ::= "~" Term11 ;
29+
LogicalNot. Term11 ::= "!" Term11 ;
30+
UnaryNegation. Term11 ::= "-" Term11 ;
31+
UnaryPlus. Term11 ::= "+" Term11 ;
32+
AddressOf. Term11 ::= "&" Term11 ;
33+
Indirection. Term11 ::= "*" Term11 ;
34+
35+
Item. Term64 ::= Identity ;
36+
TypedItem. Term64 ::= Identity ":" Identity ;
37+
Function. Term64 ::= Term64 "(" [Term] ")" ;
38+
Bracket. Term64 ::= "(" Term ")" ;
39+
40+
ProperRule. Rule ::= [Term] "->" Term ;
41+
Fact. Rule ::= Term ;
42+
43+
coercions Term 64 ;
44+
separator Term "," ;

bnf/Main.c

Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
#include <stdio.h>
2+
3+
#include "Absyn.h"
4+
#include "Parser.h"
5+
6+
void _h2f_identity(Identity id) {
7+
printf("%s", id);
8+
}
9+
10+
void _h2f_term(Term term) {
11+
// is_AddressOf, is_Indirection
12+
switch (term->kind) {
13+
case is_LogicalOr:
14+
printf("(op LogicalOr ");
15+
_h2f_term(term->u.logicalOr_.term_1);
16+
printf(" ");
17+
_h2f_term(term->u.logicalOr_.term_2);
18+
printf(")");
19+
break;
20+
case is_LogicalAnd:
21+
printf("(op LogicalAnd ");
22+
_h2f_term(term->u.logicalAnd_.term_1);
23+
printf(" ");
24+
_h2f_term(term->u.logicalAnd_.term_2);
25+
printf(")");
26+
break;
27+
case is_BitwiseInclusiveOr:
28+
printf("(op BitwiseInclusiveOr ");
29+
_h2f_term(term->u.bitwiseInclusiveOr_.term_1);
30+
printf(" ");
31+
_h2f_term(term->u.bitwiseInclusiveOr_.term_2);
32+
printf(")");
33+
break;
34+
case is_BitwiseExclusiveOr:
35+
printf("(op BitwiseExclusiveOr ");
36+
_h2f_term(term->u.bitwiseExclusiveOr_.term_1);
37+
printf(" ");
38+
_h2f_term(term->u.bitwiseExclusiveOr_.term_2);
39+
printf(")");
40+
break;
41+
case is_BitwiseAnd:
42+
printf("(op BitwiseAnd ");
43+
_h2f_term(term->u.bitwiseAnd_.term_1);
44+
printf(" ");
45+
_h2f_term(term->u.bitwiseAnd_.term_2);
46+
printf(")");
47+
break;
48+
case is_Equality:
49+
printf("(op Equality ");
50+
_h2f_term(term->u.equality_.term_1);
51+
printf(" ");
52+
_h2f_term(term->u.equality_.term_2);
53+
printf(")");
54+
break;
55+
case is_Inequality:
56+
printf("(op Inequality ");
57+
_h2f_term(term->u.inequality_.term_1);
58+
printf(" ");
59+
_h2f_term(term->u.inequality_.term_2);
60+
printf(")");
61+
break;
62+
case is_LessThan:
63+
printf("(op LessThan ");
64+
_h2f_term(term->u.lessThan_.term_1);
65+
printf(" ");
66+
_h2f_term(term->u.lessThan_.term_2);
67+
printf(")");
68+
break;
69+
case is_GreaterThan:
70+
printf("(op GreaterThan ");
71+
_h2f_term(term->u.greaterThan_.term_1);
72+
printf(" ");
73+
_h2f_term(term->u.greaterThan_.term_2);
74+
printf(")");
75+
break;
76+
case is_LessThanOrEqualTo:
77+
printf("(op LessThanOrEqualTo ");
78+
_h2f_term(term->u.lessThanOrEqualTo_.term_1);
79+
printf(" ");
80+
_h2f_term(term->u.lessThanOrEqualTo_.term_2);
81+
printf(")");
82+
break;
83+
case is_GreaterThanOrEqualTo:
84+
printf("(op GreaterThanOrEqualTo ");
85+
_h2f_term(term->u.greaterThanOrEqualTo_.term_1);
86+
printf(" ");
87+
_h2f_term(term->u.greaterThanOrEqualTo_.term_2);
88+
printf(")");
89+
break;
90+
case is_LeftShift:
91+
printf("(op LeftShift ");
92+
_h2f_term(term->u.leftShift_.term_1);
93+
printf(" ");
94+
_h2f_term(term->u.leftShift_.term_2);
95+
printf(")");
96+
break;
97+
case is_RightShift:
98+
printf("(op RightShift ");
99+
_h2f_term(term->u.rightShift_.term_1);
100+
printf(" ");
101+
_h2f_term(term->u.rightShift_.term_2);
102+
printf(")");
103+
break;
104+
case is_Addition:
105+
printf("(op Addition ");
106+
_h2f_term(term->u.addition_.term_1);
107+
printf(" ");
108+
_h2f_term(term->u.addition_.term_2);
109+
printf(")");
110+
break;
111+
case is_Subtraction:
112+
printf("(op Subtraction ");
113+
_h2f_term(term->u.subtraction_.term_1);
114+
printf(" ");
115+
_h2f_term(term->u.subtraction_.term_2);
116+
printf(")");
117+
break;
118+
case is_Multiplication:
119+
printf("(op Multiplication ");
120+
_h2f_term(term->u.multiplication_.term_1);
121+
printf(" ");
122+
_h2f_term(term->u.multiplication_.term_2);
123+
printf(")");
124+
break;
125+
case is_Division:
126+
printf("(op Division ");
127+
_h2f_term(term->u.division_.term_1);
128+
printf(" ");
129+
_h2f_term(term->u.division_.term_2);
130+
printf(")");
131+
break;
132+
case is_Modulus:
133+
printf("(op Modulus ");
134+
_h2f_term(term->u.modulus_.term_1);
135+
printf(" ");
136+
_h2f_term(term->u.modulus_.term_2);
137+
printf(")");
138+
break;
139+
case is_Complement:
140+
printf("(op Complement ");
141+
_h2f_term(term->u.complement_.term_);
142+
printf(")");
143+
break;
144+
case is_LogicalNot:
145+
printf("(op LogicalNot ");
146+
_h2f_term(term->u.logicalNot_.term_);
147+
printf(")");
148+
break;
149+
case is_UnaryNegation:
150+
printf("(op UnaryNegation ");
151+
_h2f_term(term->u.unaryNegation_.term_);
152+
printf(")");
153+
break;
154+
case is_UnaryPlus:
155+
printf("(op UnaryPlus ");
156+
_h2f_term(term->u.unaryPlus_.term_);
157+
printf(")");
158+
break;
159+
case is_AddressOf:
160+
printf("(op AddressOf ");
161+
_h2f_term(term->u.addressOf_.term_);
162+
printf(")");
163+
break;
164+
case is_Indirection:
165+
printf("(op Indirection ");
166+
_h2f_term(term->u.indirection_.term_);
167+
printf(")");
168+
break;
169+
case is_Item:
170+
_h2f_identity(term->u.item_.identity_);
171+
break;
172+
case is_TypedItem:
173+
printf("(type ");
174+
_h2f_identity(term->u.typedItem_.identity_1);
175+
printf(" ");
176+
_h2f_identity(term->u.typedItem_.identity_2);
177+
printf(")");
178+
break;
179+
case is_Function:
180+
printf("(func ");
181+
_h2f_term(term->u.function_.term_);
182+
for (ListTerm it = term->u.function_.listterm_; it; it = it->listterm_) {
183+
printf(" ");
184+
_h2f_term(it->term_);
185+
}
186+
printf(")");
187+
break;
188+
case is_Bracket:
189+
_h2f_term(term->u.bracket_.term_);
190+
break;
191+
default:
192+
printf("unknown");
193+
}
194+
}
195+
196+
void _h2f_rule(Rule rule) {
197+
switch (rule->kind) {
198+
case is_ProperRule:
199+
for (ListTerm it = rule->u.properRule_.listterm_; it; it = it->listterm_) {
200+
_h2f_term(it->term_);
201+
printf("\n");
202+
}
203+
printf("----\n");
204+
_h2f_term(rule->u.properRule_.term_);
205+
break;
206+
case is_Fact:
207+
_h2f_term(rule->u.fact_.term_);
208+
break;
209+
}
210+
printf("\n");
211+
}
212+
213+
void h2f() {
214+
Rule parse_tree = pRule(stdin);
215+
if (parse_tree) {
216+
_h2f_rule(parse_tree);
217+
}
218+
}
219+
220+
int main() {
221+
h2f();
222+
}

bnf/Makefile

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
CC = gcc
2+
FLEX = flex
3+
BISON = bison
4+
5+
Main : Main.o Absyn.o Lexer.o Parser.o
6+
${CC} $^ -o $@
7+
8+
Main.o : Main.c Absyn.h Parser.h
9+
${CC} -c $< -o $@
10+
11+
Absyn.o : Absyn.c Absyn.h
12+
${CC} -c $< -o $@
13+
14+
Lexer.o : Lexer.c Absyn.h Bison.h
15+
${CC} -c $< -o $@
16+
17+
Parser.o : Parser.c Absyn.h Bison.h
18+
${CC} -c $< -o $@
19+
20+
Parser.c Bison.h : Ds.y
21+
${BISON} -t -pds_ Ds.y -o Parser.c
22+
23+
Lexer.c : Ds.l
24+
${FLEX} -Pds_ -oLexer.c Ds.l
25+
26+
Absyn.h Absyn.c Ds.l Ds.y Parser.h: Ds.cf
27+
bnfc --c Ds.cf

0 commit comments

Comments
 (0)