3 ms·
Corrected, based on some new test JSON^1 Print state transitions to standard error Usage: yy059 < 1.json 2>1.dbg | less less 1.dbg /* extract
by 1vuio0pswjnm7 5y ago
Corrected, based on some new test JSON^1
Print state transitions to standard error
Usage:
yy059 < 1.json 2>1.dbg | less
less 1.dbg
/* extract, reformat, print json */
int fileno (FILE *);
#define echo do{if(fwrite(yytext,(size_t)yyleng,1,yyout)){}}while(0)
#define jmp (yy_start) = 1 + 2 *
#define pc(x) putchar(x)
#define dbg(x) fprintf(stderr,"%s\t%s\n",x,yytext);
%option nounput noinput noyywrap
%s x1 x2 x3 x4
%%
^\40+
/* quotes
+-------------------+
| "key":" |
| "key": " |
+-------------------+
*/
\"[^\":\]\)]*\":\40*\" {
dbg("0--x1");
echo;
jmp x1;}
/* bracket + brace
+-------------------+
| "key":[{ |
| "key": [{ |
+-------------------+
*/
\"[^\":\]\)]*\":\40*\[\40*\{ {
dbg("0--0");
echo;pc(10);}
/* number
+-------------------+
|"key":1 |
|"key": 1 |
+-------------------+
*/
\"[^\":\]\)]*\":\40*[0-9\40\12\15]* {
dbg("0--x2");
echo;
jmp x2;}
/* bracket
+-------------------+
| "key":[ |
| "key": [ |
| "key":[ |
| "value" |
| "key": [ |
| "value" |
| "key":[ |
| "[value]" |
| "key": [ |
| "[value]" |
+-------------------+
*/
\"[^\":\]\)]*\":\40*\[ {
dbg("0--x3");
echo;
jmp x3;}
/* brace
+-------------------+
| "key":{ |
| "key": { |
+-------------------+
*/
\"[^\":\]\)]*\":\40*\{ {
echo;pc(10);}
/* NB. [\{] open brace */
<x1>[\]\{\"] {
dbg("x1--0");
echo;pc(10);
jmp 0;}
<x1>[^\]\{\"\12\40]* {
echo;}
<x2>[,}] {
dbg("x2--0");
pc(10);
jmp 0;}
<x3>[\]\"] {
dbg("x3--0");
echo;pc(10);
jmp 0;}
<x3>[^\{\12\40\]]* echo;
\n|.
%%
int main(){ yylex();exit(0);}
1. https://gist.githubusercontent.com/wololock/0ba478cfeb73b36c41c4f8c814111db3/raw/4dd7d45b7c4a6c374a8233407be4f39ede7e420c/openlibrary.json https://gist.githubusercontent.com/wololock/0ba478cfeb73b36c...
Need more JSON samples to test.