Skip to content

Instantly share code, notes, and snippets.

@melchisedech333
Created March 27, 2022 04:14
Show Gist options
  • Save melchisedech333/41303dc242e5f34122e348d17d61b484 to your computer and use it in GitHub Desktop.
Save melchisedech333/41303dc242e5f34122e348d17d61b484 to your computer and use it in GitHub Desktop.
Yacc para analisar variáveis
%{
#include <stdio.h>
#include <string.h>
void yyerror (char *c);
int yylex (void);
%}
%union {
char *value;
};
%token START VARIABLE TYPE ATTR STRING INTEGER SEPARATOR END
%%
PROGRAM: PROGRAM EXPRESSION
|
;
EXPRESSION: START VARIABLES END
;
VARIABLES: DEFINITION
| DEFINITION SEPARATOR VARIABLES
;
DEFINITION: VARIABLE ATTR VALUE {
printf("var %s -> %s\n", $<value>1, $<value>3);
}
| VARIABLE TYPE ATTR VALUE {
printf("var %s (%s) -> %s\n", $<value>1, $<value>2, $<value>4);
}
;
VALUE: VARIABLE
| STRING
| INTEGER
;
%%
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