EBNF grammars for parsing OSF structures

RDF Support

fosf supports basic Turtle-like RDF syntax, including absolute URIs, qualified names, and literals.

fosf RDF grammar
?rdf_graph : turtle_preamble* triple*

?turtle_preamble : prefix_dec | base_dec
prefix_dec : "@prefix" PREFIX? ":" uriref "."
base_dec   : "@base" uriref "."

triple    : subject predicate object "."
subject   : uriref | qname
predicate : uriref | qname
object    : uriref | qname | literal

uriref : /<[^>]+>/

qname      : [PREFIX] ":" LOCAL_NAME
PREFIX     : /[a-z_][a-zA-Z0-9_.-]*/
LOCAL_NAME : /[a-zA-Z_0-9][a-zA-Z0-9_.-]*(?<!\.)/

literal          : simple_literal
                 | language_literal
                 | datatype_literal
                 | integer_literal
                 | decimal_literal
simple_literal   : STRING
language_literal : STRING "@" /[a-z][a-z]/
datatype_literal : STRING "^^" (uriref|qname)
integer_literal  : (MINUS|PLUS)?INT
decimal_literal  : (MINUS|PLUS)?FLOAT
MINUS            : "-"
PLUS             : "+"

Base OSF Grammar

Defines rules for sorts, disjunctive sorts (sets of sorts), and features.

In short:

  • A sort is a sequence of characters starting with a lowercase letter, a URI, a qualified name, or a literal. E.g.: person, student, movie, owl:Thing, …

  • A feature is a sequence of characters starting with a lowercase letter, a URI, or a qualified name. E:g.: spouse, directed_by, rdfs:label

  • Disjunctive sorts are sets {s1, ..., sn} of sorts.

  • A tag is a sequence of characters starting with an uppercase letter. E.g.: X, Y, X0, …

fosf base OSF grammar
?sort            : base_sort
                 | disjunctive_sort
base_sort        : ATOM
                 | uriref
                 | qname
                 | literal
disjunctive_sort : "{" base_sort ("," base_sort)* "}"
feature          : ATOM
                 | uriref
                 | qname
ATOM             : /[a-z]\w*/
TAG              : /[A-Z]\w*/

Graph Grammar

The graph grammar used by fosf.parsers.parse_graph().

  • Subsort declarations have shape s0 < s1, meaning that s0 is subsumed by s1.

  • Fuzzy subsort declarations have shape s0 < s1 (0.5), meaning that s0 is subsumed by s1 with degree 0.5

  • Multiple sorts can appear to the left and right of <. E.g., s0, s1 < s3 (0.2), s4 means that s0 and s1 are subsumed by s3 (both with degree 0.2), and also by s4 (implicitly with degree 1).

  • It is possible to prepend Turtle-like @prefix and @base declarations to resolve sorts.

fosf graph grammar
subsort_declarations : (turtle_preamble)* (subsort_declaration)+

subsort_declaration  : sorts "<" fuzzysorts "."

fuzzysorts   : fuzzysort ("," fuzzysort)*
sorts        : base_sort ("," base_sort)*
fuzzysort    : base_sort ("("FLOAT")")?

Similarity Grammar

The rules for parsing a sort similarity relation.

  • Similarity declarations have shape s0 ~ s1 = 0.5, meaning that s0 is similar to s1 with degree 0.5.

  • Multiple sorts can appear to the left of =. E.g., s0 ~ s1 ~ s2 ~ s3 =  0.2 . means that sorts s0 up to s3 are all similar to degree 0.2.

fosf sort similarity grammar
sim_equations : (sim_equation)+
sim_equation  : base_sort ("~" base_sort)+ "=" FLOAT "." -> sim_equation

Taxonomy Grammar

The taxonomy grammar used by fosf.parsers.parse_taxonomy(). It combines the subsort declarations of Graph Grammar and the similarity declarations of Similarity Grammar, and adds rules to parse (fuzzy) instance declarations. (Fuzzy) instance declarations have this shape:

  • { a, 0.5/b, c } < s0, s1, meaning that a, b and c all belong to the sorts s0 and s1. The membership degree of b with respect to s0 and also s1 is 0.5.

fosf sort taxonomy grammar
taxonomy_declarations : subsort_declarations (sim_equation)* (instance_dec)*

baseinstance  : ATOM | uriref | qname
fuzzyinstance : FLOAT "/" (ATOM | uriref | qname)
instance      : baseinstance | fuzzyinstance
instances     : instance ("," instance)*
instance_dec  : "{" instances "}" "<" sorts "."

OSF Term Grammar

The OSF term grammar used by fosf.parsers.parse_term().

fosf OSF term grammar
prefixed_term : (turtle_preamble)* term
?term    : sort (subterms)? -> untagged_term
         | TAG (subterms)?  -> unsorted_term
         | TAG ":" sort (subterms)? -> term
         | TAG ":" "{" term ( ";" term )+ "}" -> disjunctive_term
         | "{" term ( ";" term )+ "}" -> untagged_disjunctive_term
subterms : "(" subterm ("," subterm)* ")"
subterm  : feature ("-"|"=")+">" term

The following are all examples of valid expressions for OSF terms.

  • X:s, a tagged term without subterms.

  • s(g -> X), an untagged term with a single unsorted subterm.

  • X:s(f -> Y:s1, g -> Z), a tagged term with two subterms.

  • X0:person(spouse -> X1:director(last_name -> N:string, spouse -> X0), last_name -> N)

  • It is possible to specify Turtle-like prefixes:

    @base <http://example.org/> .
    @prefix owl: <http://www.w3.org/2002/07/owl#> .
    
    X0:owl:Thing(spouse -> X1:director(last_name -> N:string,
                                       spouse    -> X0),
                 last_name -> N)
    

    In this way, for instance, person is resolved through @base as <http://example.org/person>.

OSF Clause Grammar

The OSF clause grammar used by fosf.parsers.parse_clause().

fosf OSF clause grammar
prefixed_clause : (turtle_preamble)* clause
clause     : constraint ("&" constraint)* "."?
constraint : TAG ":" sort          -> sort_constraint
           | TAG"."feature "=" TAG -> feature_constraint
           | TAG "=" TAG           -> equality_constraint

An OSF clause is a conjunction of constraints of shape

  • X:s, a sort constraint, expressing that the object denoted by X must be of sort (type) s.

  • X.f = Y, a feature constraint, expressing that applying the feature f to the object denoted by X results in the object denoted by Y.

  • X = Y, expressing that X and Y represent the same entity.

An example of a valid expression for an OSF clause is:

X0:person   & X0.spouse = X1 & X0.last_name = N &
X1:director & X1.spouse = X0 & X1.last_name = N & N:string .

OSF Theory Grammar

The OSF theory grammar used by fosf.parsers.parse_theory().

fosf OSF theory grammar
theory : taxonomy_declarations (definition | domain | range)*

definition : base_sort ":=" theory_term "."
domain     : "dom"("ain")? "(" feature ")" ":=" base_sort "."
range      : "ran"("ge")?  "(" feature ")" ":=" base_sort "."

theory_term     : TAG ":" base_sort (theory_subterms)? -> theory_term
                | TAG (theory_subterms)?               -> theory_unsorted_term
theory_subterms : "(" theory_subterm ("," theory_subterm)* ")"
theory_subterm  : feature ("-"|"=")+">" theory_term

Following the taxonomy declarations of Taxonomy Grammar, it is possible to specify sort definitions and feature domain/range declarations.

A sort definitions associates a sort with an OSF term such that

  • the OSF term is in normal form,

  • untagged terms are not allowed,

  • the set of tags in two different sort definitions must be disjoint.

An example of a valid sort definition is:

person := Yp:person(spouse -> Y1:person(spouse -> Yp)).

Feature domain/range declarations have the following shape:

  • domain(spouse) := person .

  • range(spouse) := person .