-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompiler.h
137 lines (120 loc) · 3.15 KB
/
compiler.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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#pragma once
/*
* compiler.h
*/
#define TRUE 1
#define FALSE 0
#define NOOPSTMT 100
#define PRINTSTMT 101
#define ASSIGNSTMT 102
#define IFSTMT 103
#define GOTOSTMT 104
#define KEYWORDS 8
#define RESERVED 28
#define VAR 1
#define IF 2
#define WHILE 3
#define SWITCH 4
#define CASE 5
#define DEFAULT 6
#define PRINT 7
#define ARRAY 8
#define PLUS 9
#define MINUS 10
#define DIV 11
#define MULT 12
#define EQUAL 13
#define COLON 14
#define COMMA 15
#define SEMICOLON 16
#define LBRAC 17
#define RBRAC 18
#define LPAREN 19
#define RPAREN 20
#define LBRACE 21
#define RBRACE 22
#define NOTEQUAL 23
#define GREATER 24
#define LESS 25
#define ID 26
#define NUM 27
#define ERROR 28
// This implementation does not allow tokens that are more
// than 100 characters long
#define MAX_TOKEN_LENGTH 100
// The following global variables are defined in compiler.c:
extern char token[MAX_TOKEN_LENGTH];
extern int ttype;
//---------------------------------------------------------
// Data structures:
struct varNode
{
char* name;
int value;
};
struct gotoStatement
{
struct statementNode * target;
};
struct assignmentStatement
{
struct varNode * lhs;
struct varNode * op1;
struct varNode * op2;
int op; // PLUS, MINUS, MULT, DIV => lhs = op1 op op2;
// 0 => lhs = op1;
};
struct printStatement
{
struct varNode * id;
};
struct ifStatement
{
struct varNode * op1;
struct varNode * op2;
int relop; // GREATER, LESS, NOTEQUAL
struct statementNode * true_branch;
struct statementNode * false_branch;
};
struct statementNode
{
int stmt_type; // NOOPSTMT, PRINTSTMT, ASSIGNSTMT, IFSTMT, GOTOSTMT
struct assignmentStatement * assign_stmt; // NOT NULL iff stmt_type == ASSIGNSTMT
struct printStatement * print_stmt; // NOT NULL iff stmt_type == PRINTSTMT
struct ifStatement * if_stmt; // NOT NULL iff stmt_type == IFSTMT
struct gotoStatement * goto_stmt; // NOT NULL iff stmt_type == GOTOSTMT
struct statementNode * next; // next statement in the list or NULL
};
//---------------------------------------------------------
// Functions that are provided:
void print_debug(const char * format, ...);
void ungetToken();
int getToken();
/*
* You need to write a function with the signature that follows this comment.
* It is supposed to parse the input program and generate an intermediate
* representation for it. Write all your code in a separate file and include
* this header file (compiler.h) in your code.
*
* A) If you write your code in C, compile using the following command:
*
* $ gcc compiler.c yourcode.c
*
* and include the header in your C code like this:
*
* #include "compiler.h"
*
* B) If you use C++, use the following commands to compile your code:
*
* $ gcc -c compiler.c
* $ g++ yourcode.cpp compiler.o
*
* and also make sure that you include this header file in the following way in
* your C++ code:
*
* extern "C" {
* #include "compiler.h"
* }
*
*/
struct statementNode * parse_program_and_generate_intermediate_representation();