-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathparser.h
More file actions
130 lines (111 loc) · 4.93 KB
/
parser.h
File metadata and controls
130 lines (111 loc) · 4.93 KB
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
// ,-. Group 08 //
// , ( {o\ Satvik Golechha :: 2017A7PS0117P ///
// {`"=,___) (`~ Bharat Bhargava :: 2017A7PS0025P ////
// \ ,_.- ) Ayush Jain :: 2017A7PS0093P /////
// ~^~^~^`- ~^ ~^ '~^~^~^~ //////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
#ifndef __PARSER
#define __PARSER
#define MAX_VAR_SIZE 50 // maximum allowed length of variable
//#include "enum.h"
#define GRAMMAR_NODE_FILE_PATH "./grammar.txt"
#define MAX_RULE_NUM 150
#define N_ARY_LIM 15 // max size from current grammar is 12. Increase it if required.
#define MAX_NUM_NON_TERMINALS 100 // number of non-terminals
#include "bool.h"
#include "lexer.h"
//#include "enum.h"
#define PARSE_TREE_FILE_NAME "parse_tree.txt"
#endif
#ifndef __VARIABLES
#define __VARIABLES
enum variables { arithmeticExpr,arithmeticExpr_lr,
arithmeticOrBooleanExpression,assignmentStmt,booleanConst,caseStmt,
caseStmts,conditionalStmt,datatype,declareStmt,default_nt,driverModule,
expression,factor,idList,idList_lr,index_nt,input_plist,
input_plist_lr,ioStmt,iterativeStmt,logicalOp,lvalueARRStmt,
lvalueIDStmt,module,moduleDeclaration,moduleDeclarations,
moduleDef,moduleReuseStmt,one_more_opt,op1,op2,opt_expr,
opt_expr_lr,optional,otherModules,output_plist,output_plist_lr,
printOpt,program,range,rangeArr,relationalOp,ret,simpleStmt,
statement,statements,term,term_lr,type,unary_opt,
unaryExpression,value,var,whichId,whichStmt,
AND,ARRAY,ASSIGNOP,BC,BO,BOOLEAN,BREAK,CASE,COLON,COMMA,
DECLARE,DEF,DEFAULT,DIV,DOLLAR,DRIVER,DRIVERDEF,DRIVERENDDEF,
END,ENDDEF, EPS, EQ,FALSE,FOR,GE,GET_VALUE,GT,ID,IN,INPUT,INTEGER,
LE,LT,MINUS,MODULE,MUL,NE,NUM,OF,OR,PARAMETERS,PLUS,PRINT,
PROGRAM,RANGEOP,REAL,RETURNS,RNUM,SEMICOL,SQBC,SQBO,START,
SWITCH,TAKES,TRUE,USE,WHILE,WITH
};
#endif
#ifndef __VARIABLES_ARRAY
#define __VARIABLES_ARRAY
extern char *variables_array[114];
extern int num_parse_nodes;
typedef struct first_follow_node
{
bool first_set_array[MAX_BOOL_ARRAY_SIZE];
bool follow_set_array[MAX_BOOL_ARRAY_SIZE];
}first_follow_node;
typedef struct first_follow
{
first_follow_node* fnf[MAX_NUM_NON_TERMINALS];
}first_follow;
typedef struct grammar_node
{
// a single node of a grammar rule. If rule is A->FOO BAR, then FOO is a node
char variable[MAX_VAR_SIZE];
int is_terminal_flag;
struct grammar_node *next;
} GRAMMAR_NODE;
typedef struct grammar
{
GRAMMAR_NODE *rules[MAX_RULE_NUM];
int num_rules;
} GRAMMAR;
typedef struct parse_table
{
//******************* EDIT
int matrix[whichStmt - arithmeticExpr + 1][WITH - AND + 1];
} TABLE;
typedef struct stack_of_grammar_symbols
{
int symbol; // enum, maybe a terminal or a non-terminal
struct stack_of_grammar_symbols *next;
} STACK;
typedef struct parse_tree PARSE_TREE;
typedef struct parse_tree_node_data
{
// data for printing and storing the parse tree effectively ftw
char *lexeme; // value in our passed lexeme tuple | a, b
unsigned int line; // line in our passed lexeme
char *token_name; // token in our passed lexeme | ID
char *value_if_number;
PARSE_TREE *parent_node_pointer;
int is_leaf_node; // 1 for yes
char *node_symbol; // "program"
unsigned int rule_number; // rule number used to expand this node (0 in case of leaf node)
} TREE_NODE;
struct parse_tree
{
// n-ary tree structure
TREE_NODE *data;
int num_of_kids;
struct parse_tree *kids[N_ARY_LIM]; // children nodes
} ;
first_follow* get_first_follow_table(GRAMMAR* grammar);
GRAMMAR* generate_grammar(void);
void print_grammar(GRAMMAR* g);
int string_to_enum(char* string);
void print_first_follow(first_follow* table);
void print_rule(GRAMMAR_NODE *rule);
PARSE_TREE *create_new_node (TREE_NODE *data);
TABLE *create_parse_table(first_follow *f, TABLE *t, GRAMMAR *g);
void parse(GRAMMAR *g, FILE *f, TABLE *table, PARSE_TREE **tree, STACK *st, TWIN_BUFFER *twin_buff, int *line_no);
void print_parse_tree(PARSE_TREE *tree, FILE* f);
void v_print_parse_tree(PARSE_TREE *tree);
void remove_comments_driver(FILE* fp);
char *super_stylish_print_charstar(PARSE_TREE *b);
char *stylish_pr_intttttttt(int x);
char *stylish_print_charstar(char *input);
#endif