Context-Free Path Queries
Alexander Tchitchigin
Positive Technologies
Path Queries
Constrained Path Queries
S -> a b
Context-Free Path Queries
S -> a S b | a b
Use Cases
- Graph DB Queries
- Geographical Routes
- Alias Analysis
- Taint Analysis
- Label Flow Analysis
Programming Language Embedding
Parser Combinators
Examples
let simplePath () =
<@
let rec connected() = tok "road" <|> connected() + connected()
in connected()
@>
let alias () =
<@
let rec aliasTo() = tok "field1_rev" + aliasTo() + tok "field1" <|>
tok "field2_rev" + aliasTo() + tok "field2" <|>
...
aliasTo() + aliasTo() <|>
Eps
in aliasTo()
@>
Problems
- Left Recursion
- Ambiguity
- Infinite Paths
Remedy
- Generalized LL Parsing
- Shared Packed Parse Forest
Generalized LL Parsing
- Top-down parser
- Supports left-recursion and ambiguity
- Returns multiple derivations
- Discovers derivations in parallel
- Founded by E. Scott and A. Johnstone
LL Parsing
S -> a S a | B
B -> b
let B = function
| 'b'::tl -> Some tl
| _ -> None
let rec S = function
| 'a'::tl ->
match S tl with
| Some ('a'::tl2) -> Some tl2
| _ -> None
| input ->
match B input with
| Some tl -> Some tl
| None -> None
Limitations
- No left recursion
- E -> E "+" "1" | "1"
- Call-stack can't record cycles
- No non-determinism
- Call-stack tracks single derivation
Use Continuations, Luke! :D
Explicit state, continuation and position in input
(S -> • a S a, S -> a S • a, aba)
Shared Packed Parse Forest
- Symbol nodes
- have labels of the form (x,j,i) where x is a terminal, nonterminal, or ε
- Packed nodes
- have labels of the form (t,k), where t is of the form X -> α⋅β
- Intermediate nodes
- have the same labels as symbol nodes
- are used to binarize the SPPF
An Image Worth a Hundred Words
S -> ABCD; A -> a; B -> b; C -> c; D -> d
References
- Verbitskaia, E., Kirillov, I., Nozkin, I. and Grigorev, S., 2018, September. Parser combinators for context-free path querying.
- https://github.com/YaccConstructor/Meerkat
- https://github.com/YaccConstructor/YaccConstructor
- Joan Gerard Rekers. 1992. Parser generation for interactive environments. Ph.D. Dissertation. Universiteit van Amsterdam.
- Elizabeth Scott and Adrian Johnstone. 2010. GLL parsing. Electronic Notes in Theoretical Computer Science 253, 7 (2010), 177–189.
Thanks for your attention!
Questions?
CFPQ
By Alexander Letov
CFPQ
Context-Free Path Queries for Graphs.
- 165