%{ // // Graph Generator /* This parser recognizes the following graph grammar adjacencyList => nodeAdjacencyList+ nodeAdjacencyList => NODE_NAME -> NODE_NAME "EDGE_LABEL"? (, NODE_NAME "EDGE_LABEL"?)* ; */ #include #include #include #include using namespace std; // for keeping track of line numbers in the program we are parsing int line_num = 1; // function prototypes, we need the yylex return prototype so C++ won't complain int yylex(); void yyerror(char * s); string fromVtx; %} %union { int num; char *name; char *lab; } %error-verbose %token NAME %token LABEL %token COMMA SEMI %token ARROW %% adjacencyList : adjacencyList edgeHead SEMI | edgeHead SEMI ; edgeHead : NAME { fromVtx = $1; } ARROW edgeList edgeList : edgeList COMMA edge | edge ; edge : NAME { cout << fromVtx << " -> " << $1 << ";" << endl; } | NAME LABEL { cout << fromVtx << " -> " << $1; cout << " [ label = \"" << $2 << "\" ];" << endl; } ; %% main() { // yydebug = 1; cout << "digraph {" << endl; yyparse(); cout << "}" << endl; } void yyerror(char * s) { fprintf(stderr, "line %d: %s\n", line_num, s); }