Skip to content

Instantly share code, notes, and snippets.

@melchisedech333
Created March 27, 2022 01:13
Show Gist options
  • Save melchisedech333/2b19520e374570ab19a62e988c6921fc to your computer and use it in GitHub Desktop.
Save melchisedech333/2b19520e374570ab19a62e988c6921fc to your computer and use it in GitHub Desktop.
Código Yacc
%{
#include <stdio.h>
#include <string.h>
void yyerror (char *c);
int yylex (void);
%}
%union {
struct {
int type;
int integer;
char * unknown;
} token;
};
%token TOKEN
%token TOKEN_UNKNOWN TOKEN_INTEGER
%%
SENTENCE:
SENTENCE TOKEN {
switch ( $<token.type>2 ) {
case TOKEN_UNKNOWN:
printf("type: UNKNOWN\ntoken: %s\n\n", $<token.unknown>2);
break;
case TOKEN_INTEGER:
printf("type: INTEGER\ntoken: %d\n\n", $<token.integer>2);
break;
}
}
|
;
%%
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