Skip to content

Instantly share code, notes, and snippets.

@agudulin
Created February 28, 2012 19:58
Show Gist options
  • Save agudulin/1934735 to your computer and use it in GitHub Desktop.
Save agudulin/1934735 to your computer and use it in GitHub Desktop.
Bison: python code parser
%{
#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