-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlambda_parser_test.c
591 lines (449 loc) · 19.7 KB
/
lambda_parser_test.c
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "lambda_parser.h"
#include "lambda_expr.h"
#include "charhash.h"
#include "hash.h"
#include "list.h"
struct hash * NAMESPACE = NULL;
// Define the reduction functions.
/*============================================================
Function: compress_HASH2
Input: An integer ( prod_num ) and a pointer to
the parsing stack which just recognized
the production HASH -> HASH newline STMT.
Output: A node of type HASH, having no children.
============================================================*/
struct lambda_expr * compress_HASH2( int prod_num, struct list * stack ) {
pop_from_list( stack ); // Old state number.
pop_from_list( stack ); // STMT node ( already referenced in NAMESPACE. )
pop_from_list( stack ); // Old state number.
free( pop_from_list( stack ) ); // Unneeded token ( newline ).
pop_from_list( stack ); // Old state number.
struct lambda_expr * hash_place_holder = pop_from_list( stack );
return hash_place_holder;
}
/*============================================================
Function: compress_HASH1
Input: an integer ( prod_num ) and a pointer to a list ( stack )
Output: A node of type HASH, with no children
============================================================*/
struct lambda_expr * compress_HASH1( int prod_num, struct list * stack ) {
pop_from_list( stack ); // Old state number.
pop_from_list( stack ); // STMT node ( already referenced in NAMESPACE. )
struct lambda_expr * hash_place_holder = new_lambda_expr();
strcpy( hash_place_holder->type, "HASH" );
return hash_place_holder;
}
/*============================================================
Function: install_name
Input: An integer ( prod_num ), and a pointer to a list ( stack ),
which has just recognized the production STMT -> LHS RHS.
Output: A node of type LAMBDA, BETA, or VAR,
representing the RHS of the recognized production.
Furthermore, the RHS expression has been installed
in NAMESPACE, under the key specified by LHS.
============================================================*/
struct lambda_expr * install_name( int prod_num, struct list * stack ) {
printf( "Inside install_name, got called.<br />\n" );
struct lambda_expr * rhs_expr;
struct lambda_expr * lhs_text;
int name_length;
char * name;
struct hash * var_hash;
pop_from_list( stack ); // Old state number.
rhs_expr = pop_from_list( stack );
pop_from_list( stack ); // Old state number.
lhs_text = pop_from_list( stack );
name_length = lhs_text->last - lhs_text->first + 1;
name = malloc( ( name_length + 1 ) * sizeof( char ) );
strncpy( name, get_INPUT() + lhs_text->first, name_length );
name[name_length] = '\0';
try_to_delete( lhs_text );
var_hash = hashlookup( get_NODE_HASH(), "VAR" )->data;
struct hash * looked_up = hashlookup( var_hash, name );
if ( looked_up ) { // A VAR like this already exists.
add_to_hash( NAMESPACE, name, (void *) rhs_expr );
free( name );
}
else { // Create a VAR to avoid covering this name.
struct lambda_expr * var_placeholder = new_lambda_expr();
strcpy( var_placeholder->type, "VAR" );
var_placeholder->data = name;
add_to_hash( var_hash, name, (void *) var_placeholder );
add_to_hash( NAMESPACE, name, (void *) rhs_expr );
}
rhs_expr->parents++;
update_variables( rhs_expr );
return rhs_expr;
}
/*============================================================
Function: compress_LHS
Input: An integer ( prod_num ), and a pointer to a list ( stack ),
which has just recognized the production LHS -> TEXT colon.
Output: A pointer to a lambda_expr of type TEXT.
============================================================*/
struct lambda_expr * compress_LHS( int prod_num, struct list * stack ) {
pop_from_list( stack ); // Old state number.
free( pop_from_list( stack ) ); // Unneeded token ( colon ).
pop_from_list( stack ); // Old state number.
return pop_from_list( stack ); // TEXT node.
}
/*============================================================
Function: compress_RHS
Input: An integer ( prod_num ), and a pointer to a list ( stack ),
which has just recognized the production RHS -> EXPR semicolon.
Output: A pointer to a node of type LAMBDA, BETA, or VAR,
depending on what the EXPR was.
============================================================*/
struct lambda_expr * compress_RHS( int prod_num, struct list * stack ) {
pop_from_list( stack ); // Old state number.
free( pop_from_list( stack ) ); // Unneeded token ( semicolon ).
pop_from_list( stack ); // Old state number.
return pop_from_list( stack );
}
/*============================================================
Function: transitive_down
Input: A production number ( int ), and a pointer to a stack.
Output: A pointer to a finished lambda_expr,
based on the one on top of the stack,
which has been removed from the stack.
============================================================*/
struct lambda_expr * transitive_down( int prod_num, struct list * stack ) {
pop_from_list( stack ); // Throw away old state.
struct lambda_expr * node = pop_from_list( stack );
return node;
}
/*============================================================
Function: compress_BETA
Input: A production number ( int ), and a pointer to a stack.
Output: A pointer to a finished node,
representing the compressed tree for the specified production,
realized with the child nodes on top of the stack,
which have been removed from the stack.
============================================================*/
struct lambda_expr * compress_BETA( int prod_num, struct list * stack ) {
pop_from_list( stack ); // Throw away an old state.
free( pop_from_list( stack ) ); // Throw away the "rparen" token.
pop_from_list( stack ); // Throw away an old state.
struct lambda_expr * right = pop_from_list( stack ); // Get the second expr.
pop_from_list( stack ); // Throw away an old state.
free( pop_from_list( stack ) ); // Throw away the "comma" token.
pop_from_list( stack ); // Throw away an old state.
struct lambda_expr * left = pop_from_list( stack ); // Get first expr.
pop_from_list( stack ); // Throw away an old state.
free( pop_from_list( stack ) ); // Throw away the "lparen" token.
char * data = NULL;
char * key = hash_key( left, right, data );
struct lambda_expr * node;
struct hash * hash_node;
struct hash * BETAS = hashlookup( get_NODE_HASH(), "BETA" )->data;
if ( hash_node = hashlookup( BETAS, key ) ) {
free( key );
return hash_node->data;
}
node = new_lambda_expr();
strcpy( node->type, "BETA" );
node->child[0] = left;
node->child[1] = right;
left->parents += 1;
right->parents += 1;
add_to_hash( BETAS, key, (void *) node );
return node;
}
/*============================================================
Function: compress_LAMBDA1
Input: A production number ( int ), and a pointer to a stack.
Output: A pointer to a finished node,
representing the compressed tree for the specified production,
realized with the child nodes on top of the stack,
which have been removed from the stack.
============================================================*/
struct lambda_expr * compress_LAMBDA1( int prod_num, struct list * stack ) {
pop_from_list( stack ); // Throw away an old state.
struct lambda_expr * body = pop_from_list( stack ); // Get the lambda body.
pop_from_list( stack ); // Throw away an old state.
struct lambda_expr * var = pop_from_list( stack ); // Get the VAR node.
pop_from_list( stack ); // Throw away an old state.
free( pop_from_list( stack ) ); // Throw away the "backslash" token.
char * data = NULL;
char * key = hash_key( var, body, data );
struct lambda_expr * node;
struct hash * hash_node;
struct hash * LAMBDAS = hashlookup( get_NODE_HASH(), "LAMBDA" )->data;
if ( hash_node = hashlookup( LAMBDAS, key ) ) {
free( key );
return hash_node->data;
}
node = new_lambda_expr();
strcpy( node->type, "LAMBDA" );
node->child[0] = var;
node->child[1] = body;
var->parents += 1;
body->parents += 1;
add_to_hash( LAMBDAS, key, (void *) node );
return node;
}
/*============================================================
Function: compress_LAMBDA2
Input: A production number ( int ), and a pointer to a stack.
Output: A pointer to a finished node,
representing the compressed tree for the specified production,
realized with the child nodes on top of the stack,
which have been removed from the stack.
============================================================*/
struct lambda_expr * compress_LAMBDA2( int prod_num, struct list * stack ) {
pop_from_list( stack ); // Throw away an old state.
struct lambda_expr * body = pop_from_list( stack ); // Get the lambda body.
pop_from_list( stack ); // Throw away an old state.
free( pop_from_list( stack ) ); // Throw away the "period" token.
pop_from_list( stack ); // Throw away an old state.
struct lambda_expr * var = pop_from_list( stack ); // Get the VAR node.
pop_from_list( stack ); // Throw away an old state.
free( pop_from_list( stack ) ); // Throw away the "backslash" token.
char * data = NULL;
char * key = hash_key( var, body, data );
struct lambda_expr * node;
struct hash * hash_node;
struct hash * LAMBDAS = hashlookup( get_NODE_HASH(), "LAMBDA" )->data;
if ( hash_node = hashlookup( LAMBDAS, key ) ) {
free( key );
return hash_node->data;
}
node = new_lambda_expr();
strcpy( node->type, "LAMBDA" );
node->child[0] = var;
node->child[1] = body;
var->parents += 1;
body->parents += 1;
add_to_hash( LAMBDAS, key, (void *) node );
return node;
}
/*============================================================
Function: compress_VAR
Input: A production number ( int ), and a pointer to a stack.
Output: A pointer to a finished node,
representing the compressed tree for the specified production,
realized with the child nodes on top of the stack,
which have been removed from the stack.
============================================================*/
struct lambda_expr * compress_VAR( int prod_num, struct list * stack ) {
int old_state = (long int) pop_from_list( stack ); // Throw away an old state.
struct lambda_expr * text = pop_from_list( stack ); // Turn TEXT to VAR.
int varname_length = text->last - text->first + 1;
char * varname = malloc( ( varname_length + 1 ) * sizeof( char ) );
strncpy( varname, get_INPUT() + text->first, varname_length ); // Substring.
varname[varname_length] = '\0';
try_to_delete( text );
struct hash * VARS = hashlookup( get_NODE_HASH(), "VAR" )->data;
struct lambda_expr * node;
struct hash * hash_node;
if ( hash_node = hashlookup( VARS, varname ) ) {
free( varname );
return hash_node->data;
}
node = new_lambda_expr();
strcpy( node->type, "VAR" );
node->data = varname;
add_to_hash( VARS, varname, (void *) node );
return node;
}
/*============================================================
Function: compress_TEXT1
Input: A production number ( int ), and a pointer to a stack.
Output: A pointer to a lambda_expr,
based on the token on top of the stack,
which has been removed from the stack.
============================================================*/
struct lambda_expr * compress_TEXT1( int prod_num, struct list * stack ) {
pop_from_list( stack ); // Throw away an old state.
struct token * t = pop_from_list( stack ); // Get the token
struct lambda_expr * text = new_lambda_expr();
strcpy( text->type, "TEXT" );
text->first = t->first;
text->last = t->last;
free(t);
return text;
}
/*============================================================
Function: compress_TEXT2
Input: A production number ( int ), and a pointer to a stack.
Output: A pointer to a lambda_expr,
representing the concatenation of two substrings of text.
The token on top of the stack,
and the TEXT node directly underneath it have been removed
from the stack.
============================================================*/
struct lambda_expr * compress_TEXT2( int prod_num, struct list * stack ) {
pop_from_list( stack ); // Throw away an old state.
struct token * right = pop_from_list( stack ); // Get extending token.
pop_from_list( stack ); // Throw away an old state.
struct lambda_expr * left = pop_from_list( stack ); // Get extendable TEXT.
left->last = right->last;
free( right );
return left;
}
////////////////////////////////////////////////////////////
// Helper Functions ////////////////////////////////////////
////////////////////////////////////////////////////////////
void print_NAMESPACE() {
struct list * key_list = new_list();
list_keys_in_hash( NAMESPACE, key_list, "" );
int i;
for ( i = 0; i < key_list->next_index; i++ ) {
char * key = listlookup( key_list, i );
struct lambda_expr * value = hashlookup( NAMESPACE, key )->data;
printf( "%s =><br />\n", key );
printf( "%s<br />\n", print_lambda_expr( value ) );
}
destroy_key_list( key_list );
}
void print_TABLES() {
struct list * my_table = get_TABLE();
struct list * key_list;
struct hash * row;
char * key_j;
int i;
int j;
int k;
struct list * trans_list;
struct transition * trans;
printf( "The computed table rows look like: <br />\n" );
for ( i = 0; i < my_table->next_index; i++ ) {
printf( "row %d<br />\n", i );
printf( "%s<br />\n", hashtreeprint( listlookup( my_table, i ), "" ) );
row = listlookup( my_table, i );
key_list = new_list();
list_keys_in_hash( row, key_list, "" );
for ( j = 0; j < key_list->next_index; j++ ) {
key_j = listlookup( key_list, j );
printf( "%s => [", key_j );
trans_list = hashlookup( row, key_j )->data;
for ( k = 0; k < trans_list->next_index; k++ ) {
if ( k ) { printf( "," ); }
trans = listlookup( trans_list, k );
printf( "[%s,%d]", trans->action, trans->arg );
}
printf( "]<br />\n" );
}
destroy_key_list( key_list );
}
struct list * my_goto = get_GOTO();
printf( "The computed goto rows look like: <br />\n" );
for ( i = 0; i < my_goto->next_index; i++ ) {
printf( "row %d<br />\n", i );
printf( "%s<br />\n", hashtreeprint( listlookup( my_goto, i ), "" ) );
row = listlookup( my_goto, i );
key_list = new_list();
list_keys_in_hash( row, key_list, "" );
for ( j = 0; j < key_list->next_index; j++ ) {
key_j = listlookup( key_list, j );
printf( "%s => [", key_j );
trans_list = hashlookup( row, key_j )->data;
for ( k = 0; k < trans_list->next_index; k++ ) {
if ( k ) { printf( "," ); }
int state = (long int ) listlookup( trans_list, k );
printf( "%d", state );
}
printf( "]<br />\n" );
}
destroy_key_list( key_list );
}
}
int main() {
struct list * production0 = new_list();
append_items_to_list( production0, 3, "PHI", "HASH", "end" );
struct list * production1 = new_list();
append_items_to_list( production1, 4, "HASH", "HASH", "newline", "STMT" );
struct list * production2 = new_list();
append_items_to_list( production2, 2, "HASH", "STMT" );
struct list * production3 = new_list();
append_items_to_list( production3, 3, "STMT", "LHS", "RHS" );
struct list * production4 = new_list();
append_items_to_list( production4, 3, "LHS", "TEXT", "colon" );
struct list * production5 = new_list();
append_items_to_list( production5, 3, "RHS", "EXPR", "semicolon" );
struct list * production6 = new_list();
append_items_to_list( production6, 2, "EXPR", "BETA" );
struct list * production7 = new_list();
append_items_to_list( production7, 2, "EXPR", "LAMBDA" );
struct list * production8 = new_list();
append_items_to_list( production8, 2, "EXPR", "VAR" );
struct list * production9 = new_list();
append_items_to_list( production9, 6, "BETA", "lparen", "EXPR", "comma", "EXPR", "rparen" );
struct list * production10 = new_list();
append_items_to_list( production10, 4, "LAMBDA", "backslash", "VAR", "BETA" );
struct list * production11 = new_list();
append_items_to_list( production11, 4, "LAMBDA", "backslash", "VAR", "LAMBDA" );
struct list * production12 = new_list();
append_items_to_list( production12, 5, "LAMBDA", "backslash", "VAR", "period", "EXPR" );
struct list * production13 = new_list();
append_items_to_list( production13, 2, "VAR", "TEXT" );
struct list * production14 = new_list();
append_items_to_list( production14, 3, "TEXT", "TEXT", "text" );
struct list * production15 = new_list();
append_items_to_list( production15, 2, "TEXT", "text" );
struct grammar_entry my_grammar[] = {
{ production0, NULL },
{ production1, &compress_HASH2 },
{ production2, &compress_HASH1 },
{ production3, &install_name },
{ production4, &compress_LHS },
{ production5, &compress_RHS },
{ production6, &transitive_down },
{ production7, &transitive_down },
{ production8, &transitive_down },
{ production9, &compress_BETA },
{ production10, &compress_LAMBDA1 },
{ production11, &compress_LAMBDA1 },
{ production12, &compress_LAMBDA2 },
{ production13, &compress_VAR },
{ production14, &compress_TEXT2 },
{ production15, &compress_TEXT1 }
};
struct hash * token_types = new_hash();
add_pairs_to_hash( token_types, 8,
"(", (void *) "lparen",
")", (void *) "rparen",
".", (void *) "period",
",", (void *) "comma",
"\\", (void *) "backslash",
":" , (void *) "colon",
";" , (void *) "semicolon",
"\n", (void *) "newline"
);
printf( "Setting GRAMMAR.<br />\n" );
set_GRAMMAR( my_grammar, ( sizeof my_grammar ) / ( sizeof my_grammar[0] ) );
printf( "Analyzing productions.<br />\n" );
analyze_productions();
printf( "Filling the FIRST hash.<br />\n" );
fill_FIRST();
printf( "Filling the FOLLOW hash.<br />\n" );
fill_FOLLOW();
printf( "Filling TABLES.<br />\n" );
fill_TABLES();
// print_TABLES();
printf( "Filling TRIE.<br />\n" );
fill_TRIE( token_types );
printf( "Initializing NODE_HASH.<br />\n" );
init_NODE_HASH();
printf( "Initializing NAMESPACE.<br />\n" );
NAMESPACE = new_hash();
// Test parsing function.
//char * test_string = "a:(b,c);";
//char * test_string = "a:(\\x.y,z);\nb:(a,x);";
//char * test_string = "first:(\\x.y,z);";
char * test_string = "two:((plus,one),one);\none:\\f\\x(f,x);\nplus:\\m\\n\\f\\x((m,f),((n,f),x));";
char * error;
struct lambda_expr * parse_tree = parse( test_string, &error );
printf( "The result of the parse is: <br />\n" );
print_NAMESPACE();
printf( "The error is:<br />\n" );
printf( "%s<br />\n", error );
printf( "About to try evaluating the namespace.<br />\n" );
set_global_V( hashlookup( get_NODE_HASH(), "VAR" )->data );
evaluate_namespace( NAMESPACE );
printf( "After evaluating the namespace, it looks like:<br />\n" );
print_NAMESPACE();
}