Created
February 28, 2012 19:58
-
-
Save agudulin/1934735 to your computer and use it in GitHub Desktop.
Bison: python code parser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%{ | |
#include <string> | |
#include <iostream> | |
using namespace std; | |
#define DEBUG | |
#define YYSTYPE string | |
string className = ""; | |
int yylex(void); | |
void yyerror(const char *str) { | |
printf("%s\n", str); | |
} | |
int main(); | |
%} | |
%token CLASS DEFINED COLON DOT OBRACKET EBRACKET ID OTHER | |
%% | |
input: /* empty */ | |
| input class_def | |
| input other_token | |
; | |
classname: ID | |
{ | |
className = $1; | |
} | |
; | |
expression_list: /* empty */ | |
| expression | |
; | |
expression: ID | |
{ | |
#ifdef DEBUG | |
cout << "Class: " << className | |
<< ": child to: " << $1 << endl; | |
#endif | |
} | |
| expression OTHER | |
| expression ID | |
{ | |
#ifdef DEBUG | |
cout << "Class: " << className | |
<< ": child to: " << $2 << endl; | |
#endif | |
} | |
; | |
inheritance: /* empty */ | |
| OBRACKET expression_list EBRACKET | |
; | |
class_def: CLASS classname inheritance COLON suite | |
{ | |
#ifdef DEBUG | |
// cout << "Class: " << className << endl; | |
#endif | |
} | |
; | |
suite: | |
; | |
other_token: DEFINED | |
| COLON | |
| DOT | |
| ID | |
| OTHER | |
| OBRACKET | |
| EBRACKET | |
; | |
%% | |
int main() | |
{ | |
return yyparse(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment