Skip to content

Instantly share code, notes, and snippets.

@hastern
Created September 26, 2015 11:17
Show Gist options
  • Save hastern/ac2d7eab7a2a85f588d1 to your computer and use it in GitHub Desktop.
Save hastern/ac2d7eab7a2a85f588d1 to your computer and use it in GitHub Desktop.
Simple S-Expression parsing with pyparsing
import pyparsing as pp
LP = pp.Literal("(").suppress()
RP = pp.Literal(")").suppress()
String = pp.Word(pp.alphanums + '_')
SingleQuoteString = pp.QuotedString(quoteChar="'", unquoteResults=False)
DoubleQuoteString = pp.QuotedString(quoteChar='"', unquoteResults=False)
QuotedString = SingleQuoteString | DoubleQuoteString
Atom = String | QuotedString
SExpr = pp.Forward()
SExprList = pp.Group(pp.ZeroOrMore(SExpr | Atom))
SExpr << (LP + SExprList + RP)
@ackdav
Copy link

ackdav commented Feb 20, 2017

could you append it with a simple example? I struggle a bit applying it with my example:

(union (bgp (triple <http://dbpedia.org/resource/Diane_Fletcher> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ?x1)) (bgp (triple <http://dbpedia.org/resource/Diane_Fletcher> <http://www.w3.org/2000/01/rdf-schema#label> ?x2)))

thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment