Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %option noyywrap bison-bridge bison-locations
- %{
- #include <stdio.h>
- #include <stdlib.h>
- #define TAG_IDENT 1
- #define TAG_NUMBER 2
- #define TAG_STRING 3
- #define TAG_ERROR 4
- #define TAG_TRUE 5
- #define TAG_FALSE 6
- #define TAG_NULL 7
- #define TAG_OBJ_BEG 8
- #define TAG_OBJ_END 9
- #define TAG_ARR_BEG 10
- #define TAG_ARR_END 11
- #define TAG_SYM_COMMA 12
- #define TAG_SYM_COLON 13
- char *tag_names[] = {
- "END_OF_PROGRAM",
- "IDENT", "NUMBER", "STRING", "ERROR",
- "TRUE", "FALSE", "NULL",
- "OBJ_BEG", "OBJ_END",
- "ARR_BEG", "ARR_END",
- "SYM_COMMA", "SYM_COLON"
- };
- struct Position {
- int line, pos, index;
- };
- void print_pos(struct Position *p) {
- printf("(%d, %d)", p->line, p->pos);
- }
- struct Fragment {
- struct Position starting, following;
- };
- typedef struct Fragment YYLTYPE;
- void print_frag(struct Fragment *f) {
- print_pos(&(f->starting));
- printf(" - ");
- print_pos(&(f->following));
- }
- union Token {
- char *tokenName;
- };
- typedef union Token YYSTYPE;
- int continued;
- struct Position cur;
- #define YY_USER_ACTION \
- { \
- int i; \
- if (!continued) \
- yylloc->starting = cur; \
- continued = 0; \
- for (i = 0; i < yyleng; i++) { \
- if ('\n' == yytext[i]) { \
- cur.line++; \
- cur.pos = 1; \
- } \
- else \
- cur.pos++; \
- cur.index++; \
- } \
- yylloc->following = cur; \
- } \
- void init_scanner(char *program) {
- continued = 0;
- cur.line = 1;
- cur.pos = 1;
- cur.index = 0;
- yy_scan_string(program);
- }
- void err(char *msg) {
- printf("Error ");
- print_pos(&cur);
- printf(": %s\n", msg);
- }
- %}
- QUOTE ["]
- CHAR [a-zA-Z0-9_\/\\.]
- CHARS {CHAR}+
- STRING {QUOTE}{QUOTE}|{QUOTE}{CHARS}{QUOTE}
- DIGIT [0-9]
- DIGIT1TO9 [1-9]
- DIGITS {DIGIT}+
- INT {DIGIT}|{DIGIT1TO9}{DIGITS}|-{DIGIT}|-{DIGIT1TO9}{DIGITS}
- FRAC [.]{DIGITS}
- E [eE][+-]?
- EXP [E]{DIGITS}
- NUMBER {INT}|{INT}{FRAC}|{INT}{EXP}|{INT}{FRAC}{EXP}
- %%
- [ \n\t]+ /*ignore ws*/
- true { yylval->tokenName = yytext; return TAG_TRUE; }
- false { yylval->tokenName = yytext; return TAG_FALSE; }
- null { yylval->tokenName = yytext; return TAG_NULL; }
- {STRING} { yylval->tokenName = yytext; return TAG_STRING; }
- {NUMBER} { yylval->tokenName = yytext; return TAG_NUMBER; }
- \{ { yylval->tokenName = yytext; return TAG_OBJ_BEG; }
- \} { yylval->tokenName = yytext; return TAG_OBJ_END; }
- \[ { yylval->tokenName = yytext; return TAG_ARR_BEG; }
- \] { yylval->tokenName = yytext; return TAG_ARR_END; }
- : { yylval->tokenName = yytext; return TAG_SYM_COLON; }
- , { yylval->tokenName = yytext; return TAG_SYM_COMMA; }
- <<EOF>> return 0;
- . {
- err("Unexpected character");
- return TAG_ERROR;
- }
- %%
- #define PROGRAM "{ \"scripts\": [\"jquery.js\", \"background.js\"], \"persistent\": false }"
- int main()
- {
- int tag;
- YYSTYPE value;
- YYLTYPE coords;
- init_scanner(PROGRAM);
- do {
- tag = yylex(&value, &coords);
- if (0 != tag && TAG_ERROR != tag) {
- printf("%s ", tag_names[tag]);
- print_frag(&coords);
- printf(": %s\n", value.tokenName);
- }
- } while (0 != tag);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment