-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson-token.cpp.l
More file actions
84 lines (74 loc) · 2.22 KB
/
json-token.cpp.l
File metadata and controls
84 lines (74 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/* -*- c++ -*- */
%{
#include "json.h"
#include "json-grammar.tab.hh"
extern json::json_stack curr_stack;
extern json::json_object last_object;
extern json::json_array last_array;
%}
%option noyywrap c++
digit [0-9]
alnum [-0-9a-zA-Z]
whitespace [ \t\v\r\n\f]
%%
{digit}+(\.{digit}*((E|e)(\+|\-)?)?{digit}*)? {
yylval.jnumber = atof(yytext);
return NUMBER;
}
"{" {
curr_stack.push("{");
json::json_object jobj;
json::json_value jtop(jobj);
curr_stack.push(jtop);
return OPENBRACKET;
}
"}" {
yylval.jchar=yytext[0];
if (curr_stack.size() < 2) {
std::cerr << "syntax error detected by yylex at '}' token\n";
} else {
last_object = curr_stack.top();
curr_stack.pop();
// then, the top of the stack should be '{'
if (curr_stack.top().tostring() == std::string("{")) {
curr_stack.pop();
curr_stack.push(last_object);
} else {
std::cerr << "syntax error detected by yylex at '}' token\n";
return 0; // EOF
}
}
return CLOSEBRACKET; }
"[" {
yylval.jchar=yytext[0];
curr_stack.push("[");
json::json_array jarray;
json::json_value jtop(jarray);
curr_stack.push(jtop);
return LSQUARE_BRAC;
}
"]" {
yylval.jchar=yytext[0];
if (curr_stack.size() < 2) {
std::cerr << "syntax error detected by yylex at '}' token\n";
} else {
last_array = curr_stack.top();
curr_stack.pop();
if (!curr_stack.empty() && curr_stack.top().tostring()==std::string("[")) {
curr_stack.pop(); // pop "["
curr_stack.push(last_array);
} else {
std::cerr << "syntax error detected from yylex at ']' token\n";
return 0; // EOF
}
}
return RSQUARE_BRAC;
}
":" { yylval.jchar=yytext[0]; return COLON; }
"," { yylval.jchar=yytext[0]; return COMMA; }
\"(\\.|[^\"\\])*\" { strcpy(yylval.jstring,yytext); return STRING; }
"null" { strcpy(yylval.jstring,yytext); return NIL; }
"false" { strcpy(yylval.jstring,yytext); return FALSE; }
"true" { strcpy(yylval.jstring,yytext); return TRUE; }
{whitespace}*
%%