Permalink
Browse files

Updates for building branch tables.

  • Loading branch information...
1 parent 7dd3e0a commit 7c365f314f02adbf2e0db8345cb3ac9a19fd33ff @gkellogg gkellogg committed Jan 10, 2011
View
@@ -69,7 +69,21 @@ results in:
h = RDF::Query::Variable.new(<#h>)
g = RDF::Node.new()
RDF::Statement.new(f, <#loves>, h)
-
+
+Implementation Notes
+--------------------
+The parser is driven through a rules table contained in lib/rdf/n3/reader/meta.rb. This includes
+branch rules to indicate productions to be taken based on a current production. Terminals are denoted
+through a set of regular expressions used to match each type of terminal.
+
+The meta.rb file is generated from lib/rdf/n3/reader/n3-selectors.n3
+(taken from http://www.w3.org/2000/10/swap/grammar/n3-selectors.n3) which is the result of parsing
+http://www.w3.org/2000/10/swap/grammar/n3.n3 (along with bnf-rules.n3) using cwm using the following command sequence:
+
+ cwm n3.n3 bnf-rules.n3 --think --purge --data > n3-selectors.n3
+
+n3-selectors.n3 is itself used to generate meta.rb using script/build_meta.
+
Dependencies
------------
* [RDF.rb](http://rubygems.org/gems/rdf) (>= 0.3.0)
@@ -0,0 +1,134 @@
+#
+# Baccus - Naur Form (BNF) vocabulary
+#
+
+@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
+@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>.
+@prefix bnf: <http://www.w3.org/2000/10/swap/grammar/bnf#>.
+@prefix : <http://www.w3.org/2000/10/swap/grammar/bnf-rules#>.
+@prefix rul: <http://www.w3.org/2000/10/swap/grammar/bnf-rules#>.
+@prefix n3: <http://www.w3.org/2000/10/swap/grammar/n3#>.
+@prefix list: <http://www.w3.org/2000/10/swap/list#>.
+@prefix doc: <http://www.w3.org/2000/10/swap/pim/doc#>.
+@prefix log: <http://www.w3.org/2000/10/swap/log#>.
+@prefix string: <http://www.w3.org/2000/10/swap/string#>.
+@keywords a, is, of.
+
+
+<> rdfs:comment
+
+"""This set of rules process a BNF graph in its basic
+cfg:mustBeOneOf BNF form and create the branching tables to drive a
+predictive parser.
+
+See also cfg2bnf.n3 which expands the shothand ontology into the basic
+BNF terms.
+""".
+
+#_____________________________________
+
+
+# Enumerate options:
+
+{ ?x bnf:mustBeOneSequence ?y} => { ?x optionTail ?y }.
+
+{?x optionTail [rdf:first ?y; rdf:rest ?z]} => {
+ ?x bnf:branch [ bnf:sequence ?y];
+ optionTail ?z.
+ }.
+
+{ ?x bnf:branch [bnf:sequence ?y] } => { ?y sequenceTail ?y }.
+
+sequenceTail a log:Chaff.
+optionTail a log:Chaff.
+
+{ ?x sequenceTail [ rdf:rest ?z ] } => { ?x sequenceTail ?z }.
+
+# What productions can follow each other?
+# This is used for working out when to
+
+{ ?x sequenceTail [ rdf:first ?y; rdf:rest [ rdf:first ?z ]] } =>
+ { ?y bnf:canPrecede ?z }.
+
+{ ?x bnf:branch [
+ bnf:sequence [
+ list:last ?y]].
+ ?x bnf:canPrecede ?z} =>
+ { ?y bnf:canPrecede ?z }.
+
+{ ?x bnf:canPrecede ?y.
+ ?y bnf:branch [ bnf:sequence () ].
+ ?y bnf:canPrecede ?z.
+} => {
+
+ ?x bnf:canPrecede ?z.
+}.
+
+
+bnf:eof bnf:canStartWith "@EOFDUMMY". # @@ kludge
+
+# Have to separate the next three rules or cwm seems to
+# get screwed up and assume there is no solution @@@
+
+{ ?x bnf:branch [bnf:sequence [ rdf:first ?y ]].
+ } => { ?x bnf:TEST ?y }.
+
+{ ?x bnf:TEST ?y .
+ ?y log:rawType log:Literal. } => { ?x bnf:canStartWithLiteral ?y }.
+
+{ ?x bnf:canStartWithLiteral ?y .
+# (?y "(.).*") string:scrape ?c # Use whole string
+ } => { ?y bnf:canStartWith ?y }.
+
+#______________________________________________________________
+
+
+
+# Rules for determining branching
+
+# A branch has a sequence, which is the given BNF production, and
+# one or more conditions, which are the strings on which to consider
+# that branch. N3 is a langauge in whch the look-ahead often is only
+# one character, and may allways be a constsnt string rather than a
+# regexp (check).
+
+# A branchTail is a sequnece which a branch could start with
+{ ?x bnf:branch ?b.
+ ?b bnf:sequence ?s.
+} => {
+ ?b bnf:branchTail ?s.
+}.
+
+{ ?b bnf:branchTail ?s.
+ ?s rdf:first [ bnf:branch [ bnf:sequence () ]];
+ rdf:rest ?t
+} => {
+ ?b bnf:branchTail ?t.
+}.
+
+
+{ ?x bnf:branch ?b.
+ ?b bnf:branchTail ?s.
+ ?s rdf:first [bnf:canStartWith ?y].
+} => {
+ ?x bnf:canStartWith ?y.
+ ?b bnf:condition ?y.
+}.
+
+
+
+{ ?x bnf:branch ?b;
+ bnf:canPrecede ?z.
+ ?z log:rawType log:Literal.
+ ?b bnf:sequence ().
+} => { ?b bnf:condition ?z}.
+
+{ ?x bnf:branch ?b;
+ bnf:canPrecede [bnf:canStartWith ?z].
+ ?b bnf:sequence ().
+} => { ?b bnf:condition ?z}.
+
+
+
+
+#ends
Binary file not shown.
Oops, something went wrong.

0 comments on commit 7c365f3

Please sign in to comment.