forked from linkeddata/swap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathebnf2bnf.n3
73 lines (55 loc) · 1.93 KB
/
ebnf2bnf.n3
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
73
@prefix dc: <http://purl.org/dc/elements/1.1/>. # @@ no #
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
<> dc:description """reduce EBNF to BNF;
i.e. rewrite ?, +, * operators using alt and seq
""",
"$Id$";
rdfs:seeAlso <ebnf>, <ebnf.n3>, <ebnf.rdf>.
@prefix g: <http://www.w3.org/2000/10/swap/grammar/ebnf#>.
g:empty g:seq ().
{ ?A g:opt ?B } => { ?A g:alt ( g:empty ?B ) }.
{ ?A g:plus ?B } => { ?A g:seq ( ?B [ g:star ?B] ) }.
{ ?A g:star ?B } => { ?A g:alt ( g:empty [ g:seq (?B ?A) ] ) }.
# find all the non-terminals
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix list: <http://www.w3.org/2000/10/swap/list#>.
g:alt rdfs:domain g:Production.
{ ?S [ rdfs:domain ?C] [] } => { ?S a ?C }.
# empty is not a production
{ ?S g:seq [ rdf:rest []] } => { ?S a g:Production }.
{ ?L g:start ?A }
=> { ?A g:nonTerminal ?L }.
{ [ g:seq [ list:member ?A]] g:nonTerminal ?L.
?A a g:Production.
}
=> { ?A g:nonTerminal ?L }.
{ [ g:alt [ list:member ?A]] g:nonTerminal ?L.
?A a g:Production.
}
=> { ?A g:nonTerminal ?L }.
# and find all the terminals used in NonTerminal rules
@prefix log: <http://www.w3.org/2000/10/swap/log#>.
# promote literals to Terminals
{
[ g:alt [ list:member ?T ]] g:nonTerminal ?L.
?T log:rawType log:Literal.
} => { ?L g:terminal ?T }.
{ [ g:seq [ list:member ?T ]] g:nonTerminal ?L.
?T log:rawType log:Literal.
} => { ?L g:terminal ?T }.
# find the Terminals that are used...
@prefix re: <http://www.w3.org/2000/10/swap/grammar/regex#>.
re:alt rdfs:domain re:Regex.
re:seq rdfs:domain re:Regex.
re:matches rdfs:domain re:Regex.
re:diff rdfs:domain re:Regex.
re:opt rdfs:domain re:Regex.
re:plus rdfs:domain re:Regex.
re:star rdfs:domain re:Regex.
{ [ g:alt [ list:member ?T ]] g:nonTerminal ?L.
?T a re:Regex.
} => { ?L g:terminal ?T }.
{ [ g:seq [ list:member ?T ]] g:nonTerminal ?L.
?T a re:Regex.
} => { ?L g:terminal ?T }.