Skip to content

Instantly share code, notes, and snippets.

@melchisedech333
Created March 27, 2022 02:20
Show Gist options
  • Save melchisedech333/0b3ed0488e2b6d9e0c11ce0cae4b9289 to your computer and use it in GitHub Desktop.
Save melchisedech333/0b3ed0488e2b6d9e0c11ce0cae4b9289 to your computer and use it in GitHub Desktop.
Processa chave e valor
%{
#include <stdio.h>
#include <string.h>
void yyerror (char *c);
int yylex (void);
%}
%union {
char *value;
};
%token OPEN CLOSE ATTR SEPARATOR JSTRING JINTEGER
%%
PROGRAM: OBJECT {
printf("FINAL: %s\n", $<value>$);
}
;
OBJECT: OPEN MEMBERS CLOSE {
int size = strlen($<value>2);
$<value>$ = (char *) malloc(size);
sprintf($<value>$, "{%s}", $<value>2);
printf("OBJECT: %s\n", $<value>$);
}
;
MEMBERS: PAIR {
$<value>$ = $<value>1;
printf("MEMBERS: %s\n", $<value>$);
}
| PAIR SEPARATOR MEMBERS {
int size = strlen($<value>1) + strlen($<value>3);
$<value>$ = (char *) malloc(size);
sprintf($<value>$,"%s,%s", $<value>1, $<value>3);
printf("MEMBERS: %s\n", $<value>$);
}
;
PAIR: JSTRING ATTR VALUE {
int size = strlen($<value>1) + strlen($<value>3);
$<value>$ = (char *) malloc(size);
sprintf($<value>$,"%s:%s", $<value>1, $<value>3);
printf("PAIR: %s\n", $<value>$);
}
;
VALUE: JSTRING
| JINTEGER
;
%%
void yyerror (char *c) {
printf("Error: %s\n", c);
}
int main () {
yyparse();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment