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
/* SI 413 Fall 2012
 * Lab 05
 * Recursive descent parser for pat language.
 * YOUR NAME HERE
 * PARTNER'S NAME HERE
 */
 
#include "pat.hpp"
#include <cstdlib>
using namespace std;
 
// These are the prototypes for your recursive descent function.
// Of course you need to implement these below!
void stmt();
void seq(); void seqtail();
void catseq(); void cattail();
void opseq(); void optail();
void atom();
 
int next = -1; // store next token
 
// These are the colored output streams to make things all pretty.
colorout resout(1, 'u');
colorout errout(2, 'r');
 
// --------- Helper Functions
 
// Gets called if we see an unexpected token
void perror(const char* funcname) { 
  errout << "Parse error in " << funcname << endl; 
  exit(2); 
}
 
// Returns the next token without actually consuming it
int peek() { 
  if (next == -1) next = yylex(); 
  return next; 
}
 
// Consumes the next token, and makes sure it matches the argument.
// If so, the semantic value of the token (a string) is returned.
string match(int tok) { 
  if (tok == peek()) {
    next = -1; 
    return yylval;
  }
  else {
    errout << "Token match error" << endl;
    exit(3);
  }
}
 
// ---------------- Recursive descent functions
 
void stmt() {
  // YOU NEED TO FILL THIS IN!
  perror("stmt (Not implemented yet!)");
}
 
//-- Main method
int main(void) {
  // This checks whether the output is a terminal.
  bool tty = isatty(0) && isatty(2);
 
  while(true) {
    if (tty) cerr << "> " << flush;
    if (peek() == 0) break;
    stmt();
    if (tty) cerr << "(Parse OK)" << endl;
  }
 
  if (tty) cerr << "Goodbye" << endl;
  return 0;
}