Skip to content

Instantly share code, notes, and snippets.

@dnmfarrell
Created March 21, 2023 02:01
Show Gist options
  • Save dnmfarrell/8d5b24d4eb18d182a70f89d407d18dca to your computer and use it in GitHub Desktop.
Save dnmfarrell/8d5b24d4eb18d182a70f89d407d18dca to your computer and use it in GitHub Desktop.
Scryer-Prolog definite clause grammar for JSON
% https://www.json.org/json-en.html
:- use_module(library(dcgs)).
:- use_module(library(lists)).
json --> element.
value --> object.
value --> array.
value --> string.
value --> number.
value --> truw.
value --> fals.
value --> null.
truw --> ['t','r','u','e'].
fals --> ['f','a','l','s','e'].
null --> ['n','u','l','l'].
object --> ['{'], ws, ['}'].
object --> ['{'], pairs, ['}'].
pairs --> pair.
pairs --> pair, [','], pairs.
pair --> ws, string, ws, [':'], element.
array --> ['['], ws, [']'].
array --> ['['], elements, [']'].
elements --> element.
elements --> element, [','], elements.
element --> ws, value, ws.
string --> ['"'], characters, ['"'].
characters --> [].
characters --> character, characters.
character --> [C], { \+ member(C, ['"', '\\']) }.
character --> ['\\'], escape.
escape --> ['"'].
escape --> ['\\'].
escape --> ['/'].
escape --> ['b'].
escape --> ['f'].
escape --> ['n'].
escape --> ['r'].
escape --> ['t'].
escape --> ['u'], hexa, hexa, hexa, hexa.
hexa --> digit.
hexa --> ['A'].
hexa --> ['B'].
hexa --> ['C'].
hexa --> ['D'].
hexa --> ['E'].
hexa --> ['F'].
hexa --> ['a'].
hexa --> ['b'].
hexa --> ['c'].
hexa --> ['d'].
hexa --> ['e'].
hexa --> ['f'].
number --> integer, fraction, exponent.
integer --> digit.
integer --> onenine, digits.
integer --> ['-'], digit.
integer --> ['-'], onenine, digits.
digits --> digit.
digits --> digit, digits.
digit --> ['0'].
digit --> onenine.
onenine --> ['0'].
onenine --> ['1'].
onenine --> ['2'].
onenine --> ['3'].
onenine --> ['4'].
onenine --> ['5'].
onenine --> ['6'].
onenine --> ['7'].
onenine --> ['8'].
onenine --> ['9'].
fraction --> [].
fraction --> ['.'], digits.
exponent --> [].
exponent --> ['E'], sign, digits.
exponent --> ['e'], sign, digits.
sign --> [].
sign --> ['+'].
sign --> ['-'].
ws --> [].
ws --> [' '], ws.
ws --> ['\n'], ws.
ws --> ['\t'], ws.
ws --> ['\r'], ws.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment