-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.lex
47 lines (40 loc) · 974 Bytes
/
parse.lex
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
%{
#include "string.h"
#include "y.tab.h"
char *new_str(const char *str);
%}
blanks [ \t\n\r]+
VALID_ESCAPE_CHARACTER \\n|\\b|\\\"|\\\\|\\\/|\\f|\\r|\\t|\\u[0-9A-F]{4}
VALID_CHARACTER [^\\\"]
LC \{
RC \}
CN :
CM ,
LS \[
RS \]
STRING_LITERAL \"({VALID_CHARACTER}|{VALID_ESCAPE_CHARACTER})*\"
BOOLEAN true|false|TRUE|FALSE
NUMBER (-)?0(\.[0-9]*)?([e|E][+|-][0-9]*)?|(-)?[1-9][0-9]*(\.[0-9]*)?([e|E][+|-][0-9]*)?
NULL null|NULL
%%
{blanks} { /* ignore */ }
{LC} {return '{';}
{RC} {return '}';}
{STRING_LITERAL} {yylval.str=new_str(yytext); return(__STRING_LITERAL);}
{CN} {return ':';}
{CM} {return ',';}
{LS} {return '[';}
{RS} {return ']';}
{BOOLEAN} {yylval.str=new_str(yytext); return(__BOOLEAN);}
{NUMBER} {yylval.str=new_str(yytext); return(__NUMBER); }
{NULL} {yylval.str=new_str(yytext); return(__NULL);}
%%
char *new_str(const char *str)
{
char *new=malloc(strlen(str)+1);
strcpy(new,str);
return new;
}
int yywrap() {
return 1;
}