Skip to content

Instantly share code, notes, and snippets.

@reddragon
Created January 31, 2012 00:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reddragon/1707889 to your computer and use it in GitHub Desktop.
Save reddragon/1707889 to your computer and use it in GitHub Desktop.
%{
#include <cstdio>
#include <iostream>
#include <string>
using namespace std;
extern "C" int yylex();
extern "C" int yyparse();
extern "C" FILE *yyin;
extern "C" int lno;
extern "C" char *yytext;
void yyerror(const char *s);
typedef struct ASTNode {
int type; // yytokentype::...
std::string id;
int val;
ASTNode *left, *right;
ASTNode(int _type)
: type(_type), left(NULL), right(NULL)
{ }
} ASTNode;
static void
print_AST(struct ASTNode *n);
%}
%union {
char *id;
struct ASTNode *astnode;
}
%error-verbose
%token <id> STRING
%token ENDL IMPLIES
%type<astnode> S T U V
%%
LINES: LINES { cerr<<"Menghani\n"; } LINE | LINE { cerr<<"Men2\n"; };
LINE: S '.' {
print_AST($1); cout<<endl;
} ENDL
| ENDL;
S: T IMPLIES S {
ASTNode *nn = new ASTNode(IMPLIES);
nn->left = $1;
nn->right = $3;
$$ = nn;
}
| T {
$$ = $1;
};
T: U '|' T {
ASTNode *nn = new ASTNode('|');
nn->left = $1;
nn->right = $3;
$$ = nn;
}
| U {
$$ = $1;
};
U: V '&' U {
ASTNode *nn = new ASTNode('&');
nn->left = $1;
nn->right = $3;
$$ = nn;
}
| V {
$$ = $1;
};
V: '(' S ')' {
$$ = $2;
}
| '!' V {
ASTNode *nn = new ASTNode('!');
nn->right = $2;
$$ = nn;
}
| STRING {
ASTNode *nn = new ASTNode(STRING);
nn->id = $1;
$$ = nn;
};
%%
static void
print_AST(struct ASTNode *n) {
if (!n) return;
print_AST(n->left);
switch (n->type) {
case STRING:
printf(" %s ", n->id.c_str());
break;
case IMPLIES:
printf(" -> ");
break;
default:
printf(" %c ", (char)n->type);
}
print_AST(n->right);
}
int
main() {
yyparse();
return 0;
}
void yyerror(const char *s) {
fprintf(stderr, "Error: %s\n", s);
exit(-1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment