-
Notifications
You must be signed in to change notification settings - Fork 3
/
ast.h
83 lines (68 loc) · 2.55 KB
/
ast.h
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
/**
* File: ast.h
* -----------
* This file defines the abstract base class Node and the concrete
* Identifier and Error node subclasses that are used through the tree as
* leaf nodes. A parse tree is a hierarchical collection of ast nodes (or,
* more correctly, of instances of concrete subclassses such as VarDecl,
* ForStmt, and AssignExpr).
*
* Location: Each node maintains its lexical location (line and columns in
* file), that location can be NULL for those nodes that don't care/use
* locations. The location is typcially set by the node constructor. The
* location is used to provide the context when reporting semantic errors.
*
* Parent: Each node has a pointer to its parent. For a Program node, the
* parent is NULL, for all other nodes it is the pointer to the node one level
* up in the parse tree. The parent is not set in the constructor (during a
* bottom-up parse we don't know the parent at the time of construction) but
* instead we wait until assigning the children into the parent node and then
* set up links in both directions. The parent link is typically not used
* during parsing, but is more important in later phases.
*
* Semantic analysis: For pp3 you are adding "Check" behavior to the ast
* node classes. Your semantic analyzer should do an inorder walk on the
* parse tree, and when visiting each node, verify the particular
* semantic rules that apply to that construct.
*/
#ifndef _H_ast
#define _H_ast
#include <stdlib.h> // for NULL
#include "location.h"
#include <iostream>
#include "hashtable.h"
#include "errors.h"
#include "codegen.h"
using namespace std;
class Node {
protected:
yyltype *location;
Node *parent;
public:
Node(yyltype loc);
Node();
virtual ~Node() {}
yyltype *GetLocation() { return location; }
void SetParent(Node *p) { parent = p; }
Node *GetParent() { return parent; }
};
class Identifier : public Node
{
protected:
char *name;
public:
Identifier(yyltype loc, const char *name);
friend ostream& operator<<(ostream& out, Identifier *id) { return out << id->name; }
const char *GetName() { return name; }
};
// This node class is designed to represent a portion of the tree that
// encountered syntax errors during parsing. The partial completed tree
// is discarded along with the states being popped, and an instance of
// the Error class can stand in as the placeholder in the parse tree
// when your parser can continue after an error.
class Error : public Node
{
public:
Error() : Node() {}
};
#endif