1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/* SI 413 Fall 2013
 * Lab 6
 * Parser and interpreter for pat
 * YOUR NAME HERE
 * PARTNER NAME HERE
 */
 
%code requires {
 
#include <cstdlib>
#include <iostream>
#include <vector>
#include <string>
#include <map>
#include "colorout.hpp"
using namespace std;
 
// This says that semantic values of tokens should be ParseTree pointers.
#define YYSTYPE vector<string>*
 
int yylex(); 
extern colorout resout;
extern colorout errout;
 
}
 
%code {
 
// These helper functions provided at no cost by Dr Roche.
 
// Prints out a vector of strings with spaces in between
// You can call this just like resout << some_vector << endl;
ostream& operator<< (ostream& out, const vector<string>* vec) {
  if (vec->empty()) return out;
  out << (*vec)[0];
  for (unsigned long i=1; i<vec->size(); ++i)
    out << ' ' << (*vec)[i];
  return out;
}
 
// Computes the "fold" or interleaving of two vectors of strings
vector<string>* fold(const vector<string> *A, const vector<string> *B) {
  vector<string>* res = new vector<string>;
  unsigned long int i = 0;
  for (; i < A->size() && i < B->size(); ++i) {
    res->push_back((*A)[i]);
    res->push_back((*B)[i]);
  }
  for (; i < A->size(); ++i)
    res->push_back((*A)[i]);
  for (; i < B->size(); ++i)
    res->push_back((*B)[i]);
  return res;
}
 
// Concatenates two vectors of strings
vector<string>* concat(const vector<string> *A, const vector<string> *B) {
  vector<string>* res = new vector<string>;
  unsigned long int i;
  for(i = 0; i < A->size(); ++i) res->push_back((*A)[i]);
  for(i = 0; i < B->size(); ++i) res->push_back((*B)[i]);
  return res;
}
 
// Reverses a vector of strings
vector<string>* rev(const vector<string> *A) {
  vector<string>* res = new vector<string>;
  long int i;
  for(i = A->size() - 1; i >= 0; --i) res->push_back((*A)[i]);
  return res;
}
 
int yyerror(const char *p) { 
  errout << "Parser error: " << p << endl; 
  exit(3);
}
 
// These are the colored output streams to make things all pretty.
colorout resout(1, 'u');
colorout errout(2, 'r');
 
// Global variable to indicate to stop parsing.
bool done = false;
 
}
 
  /* Tell bison to give descriptive error mesages. */
%error-verbose
 
  /* COPY YOUR TOKEN AND GRAMMAR SPEC FROM PART 1 INTO HERE. 
     Delete everything up to the part with
     %%
     int main()
   */
 
%token STOP FOLD COLON REV SYM NAME LB RB
 
%%
 
S: STOP { YYACCEPT; }
 
%%
int main()
{
  // This checks whether the output is a terminal.
  bool tty = isatty(0) && isatty(2);
 
  while(true) {
    if (tty) cerr << "> ";
    yyparse();
    if (done) break;
  }
  if (tty) cerr << "Goodbye!" << endl;
  return 0;
}