Tcl Library Source Code

Check-in [f71a1db4a8]
Login
Bounty program for improvements to Tcl and certain Tcl packages.
Tcl 2018 Conference, Houston/TX, US, Oct 15-19
Send your abstracts to tclconference@googlegroups.com
or submit via the online form by Aug 20.

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:* pt_peg_op.tcl: Started work on a proper dechain transformation (dechain2); the current dechain logic is entirely broken; added tests
Timelines: family | ancestors | descendants | both | pt-peg_op-tests-ssoberni
Files: files | file ages | folders
SHA3-256:f71a1db4a83cadcf257d44dc7fb5ce577cb7009aa66419f713a9b87639968d8a
User & Date: ssoberni 2018-08-08 14:28:14
Context
2018-08-08
14:42
* pt_peg_op.tcl: Sort out correct order of minimization transformations: dechaining must precede unrealizable and unreachable transformations Leaf check-in: cfca679384 user: ssoberni tags: pt-peg_op-tests-ssoberni
14:28
* pt_peg_op.tcl: Started work on a proper dechain transformation (dechain2); the current dechain logic is entirely broken; added tests check-in: f71a1db4a8 user: ssoberni tags: pt-peg_op-tests-ssoberni
2018-07-25
12:26
Adding tests for pt::peg::op realizable and reachable check-in: be24b48161 user: ssoberni tags: pt-peg_op-tests-ssoberni
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to modules/pt/pt_peg_op.tcl.

13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
..
56
57
58
59
60
61
62









































63
64
65
66
67
68
69
...
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204

# # ## ### ##### ######## ############# #####################
##

namespace eval ::pt::peg::op {
    namespace export \
	flatten called reachable realizable \
	dechain drop modeopt minimize

    namespace ensemble create

    namespace eval ::pt::peg::op::drop {
	namespace export \
	    unreachable unrealizable

................................................................................
    foreach {symbol rule} [$container rules] {
	lappend dict $symbol [pt::pe::op called $rule]
    }

    return $dict
}










































proc ::pt::peg::op::dechain {container} {

    # Simplify all symbols which just chain to a different symbol by
    # inlining the called symbol in its callers. This works if and
    # only the modes match properly.

    # X     Z      dechain notes
................................................................................

proc ::pt::peg::op::minimize {container} {
    flatten           $container
    drop unrealizable $container
    drop unreachable  $container
    flatten           $container
    modeopt           $container
    dechain           $container
    return
}

# # ## ### ##### ######## #############

proc ::pt::peg::op::reachable {container} {








|







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







|







13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
..
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
...
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245

# # ## ### ##### ######## ############# #####################
##

namespace eval ::pt::peg::op {
    namespace export \
	flatten called reachable realizable \
	dechain drop modeopt minimize dechain2

    namespace ensemble create

    namespace eval ::pt::peg::op::drop {
	namespace export \
	    unreachable unrealizable

................................................................................
    foreach {symbol rule} [$container rules] {
	lappend dict $symbol [pt::pe::op called $rule]
    }

    return $dict
}


proc ::pt::peg::op::dechain2 {container} {

    set changed 1
    while {$changed} {
	
	set chainPairs [dict create]
	set rules [$container rules]
	array set modes [$container modes]
	
	set changed 0
	foreach {symbol rule} $rules {
	    lassign $rule op caller
	    if {$op ne "n"} continue
	    if {$caller eq $symbol} continue; # self-recursive: A <- A
	    dict set chainPairs $caller $symbol
	}
	
	set changed [dict size $chainPairs]
	if {$changed} {
	    dict for {called caller} $chainPairs {
		# TODO: check the modes interaction ...
		if {$called in [$container nonterminals] &&
		    !(($modes($caller) ne "value") &&
		      (($modes($caller) ne "void") ||
		       ($modes($called) ne "void")))} {
		    
		    $container rule $caller \
			[pt::pe::op rename $called $caller \
			     [$container rule $called]]
		    
		} else {
		    incr changed -1
		}
	    }
	}
    }
    
    return
}

proc ::pt::peg::op::dechain {container} {

    # Simplify all symbols which just chain to a different symbol by
    # inlining the called symbol in its callers. This works if and
    # only the modes match properly.

    # X     Z      dechain notes
................................................................................

proc ::pt::peg::op::minimize {container} {
    flatten           $container
    drop unrealizable $container
    drop unreachable  $container
    flatten           $container
    modeopt           $container
    dechain2          $container
    return
}

# # ## ### ##### ######## #############

proc ::pt::peg::op::reachable {container} {

Changes to modules/pt/tests/pt_peg_op.tests.

15
16
17
18
19
20
21

22
23
24
25
26
27
28
...
352
353
354
355
356
357
358



















































































































































































359
360
361
362
363
364
365
...
386
387
388
389
390
391
392












393
394
395
396
397
398

# -------------------------------------------------------------------------
# Basic syntax

foreach op {
    called
    dechain

    flatten
    minimize
    modeopt
    reachable
    realizable
    {drop unreachable}
    {drop unrealizable}
................................................................................
     	A {is {n B} mode void}
     	B {is {t a} mode void}
    }
    {n S} {
     	S {is {t a} mode leaf}
    }
} $setimpl




















































































































































































# -------------------------------------------------------------------------
# op: minimize

TestTransformation minimize {
    # --- stays as-is
    epsilon {}
................................................................................
     	S {is {/ {x {n A} {n B}} {t a}} mode value}
     	A {is {x {t a} {n A}} mode value}
	B {is {t a} mode leaf}
    }
    {n S} {
	S {is {t a} mode leaf}
    }












} $setimpl

# -------------------------------------------------------------------------
rename sl {}
rename g {}
rename TestTransformation {}







>







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







>
>
>
>
>
>
>
>
>
>
>
>






15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
...
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
...
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

# -------------------------------------------------------------------------
# Basic syntax

foreach op {
    called
    dechain
    dechain2
    flatten
    minimize
    modeopt
    reachable
    realizable
    {drop unreachable}
    {drop unrealizable}
................................................................................
     	A {is {n B} mode void}
     	B {is {t a} mode void}
    }
    {n S} {
     	S {is {t a} mode leaf}
    }
} $setimpl

# -------------------------------------------------------------------------
# op: dechain2

TestTransformation dechain2 {
    # --- stays as-is
    epsilon {}
    epsilon {}
    # --- basic chain:  A <- B <- C (leaf) <- c |> A <- c
    {n S} {
    	S {is {x {n A} {t s}} mode value}
	A {is {n B} mode value}
	B {is {n C} mode value}
	C {is {t c} mode value}
    }
    {n S} {
    	S {is {x {n A} {t s}} mode value}
	A {is {t c} mode value}
	B {is {t c} mode value}
	C {is {t c} mode value}
    }
    # --- basic cycle:  A <- B <- A
    {n S} {
    	S {is {x {n A} {t s}} mode value}
	A {is {n B} mode value}
	B {is {n A} mode value}
    }
    {n S} {
    	S {is {x {n A} {t s}} mode value}
	A {is {n A} mode value}
	B {is {n B} mode value}
    }
    # --- basic (indirect) cycle:  A <- B <- C <- A
    {n S} {
    	S {is {x {n A} {t s}} mode value}
	A {is {n B} mode value}
	B {is {n C} mode value}
	C {is {n A} mode value}
    }
    {n S} {
    	S {is {x {n A} {t s}} mode value}
	A {is {n A} mode value}
	B {is {n B} mode value}
	C {is {n C} mode value}
    }
    # --- basic chain plus renaming for self-recursive leaves
    {n S} {
    	S {is {x {n A} {t s}} mode value}
	A {is {n B} mode value}
	B {is {n C} mode value}
	C {is {x {n C} {t c}} mode value}
    }
    {n S} {
    	S {is {x {n A} {t s}} mode value}
	A {is {x {n A} {t c}} mode value}
	B {is {x {n B} {t c}} mode value}
	C {is {x {n C} {t c}} mode value}
    }
    # --- start expression: {} <- Z <- S <- s |> {} <- Z <- s
    {n Z} {
	Z {is {n S} mode value}
    	S {is {t s} mode value}
    }
    {n Z} {
	Z {is {t s} mode value}
    	S {is {t s} mode value}
    }
    # --- broken chain #1 (undefined leaves?)
    {n S} {
    	S {is {x {n A} {t s}} mode value}
	A {is {n B} mode value}
    }
    {n S} {
	S {is {x {n A} {t s}} mode value}
	A {is {n B} mode value}
    }
    # --- broken chain #2 (undefined leaves?)
    {n S} {
    	S {is {x {n A} {t s}} mode value}
	A {is {n B} mode value}
	B {is {n C} mode value}
    }
    {n S} {
	S {is {x {n A} {t s}} mode value}
	A {is {n C} mode value}
	B {is {n C} mode value}
    }
    # --- intermittents
    {n S} {
    	S {is {x {n A} {t s}} mode value}
	A {is {n B} mode value}
	B {is {n C} mode value}
	C {is {x {n X} {t c}} mode value}
	X {is {n Y} mode value}
	Y {is {n Z} mode value}
	Z {is {x {n Z} {t z}} mode value}
    }
    {n S} {
	S {is {x {n A} {t s}} mode value}
	A {is {x {n X} {t c}} mode value}
	B {is {x {n X} {t c}} mode value}
	C {is {x {n X} {t c}} mode value}
	X {is {x {n X} {t z}} mode value}
	Y {is {x {n Y} {t z}} mode value}
	Z {is {x {n Z} {t z}} mode value}
    }
    # --- incompat modes #1 
    {n S} {
    	S {is {x {n A} {t s}} mode value}
	A {is {n B} mode void}
	B {is {n C} mode void}
	C {is {t c} mode value}
    }
    {n S} {
	S {is {x {n A} {t s}} mode value}
	A {is {n C} mode void}
	B {is {n C} mode void}
	C {is {t c} mode value}
    }

} $setimpl

# exit

# -------------------------------------------------------------------------
# op: dechain (legacy; broken)

TestTransformation dechain {
    # --- direct cycles (untouched)
    # {n A} {
    # 	A {is {n A} mode value}
    # }
    # {n A} {
    # 	A {is {n A} mode value}
    # }
    # --- indirect cycles (turn into a unit-recursive, but unrealizable rule)
    # {n A} {
    # 	A {is {n B} mode value}
    # 	B {is {n C} mode value}
    # 	C {is {n A} mode value}
    # }
    # {n A} {
    # 	A {is {n A} mode value}
    # }
    # --- (multi-)chain rules are not processed
    {n A} {
	A {is {/ {n B} {t a}} mode value}
	B {is {/ {n C} {t b}} mode value}
	C {is {/ {n A} {t c}} mode value}
    }
    {n A} {
	A {is {/ {n B} {t a}} mode value}
	B {is {/ {n C} {t b}} mode value}
	C {is {/ {n A} {t c}} mode value}
    }
    # --- stays as-is
    epsilon {}
    epsilon {}
    # ---
    # X     Z      dechain notes
    # value value| yes    | value is passed
    # {n S} {
    # 	S {is {n Z} mode value}
    # 	Z {is {t z} mode value}
    # }
    # {n Z} {
    # 	S {is {n Z} mode value}
    # 	Z {is {t z} mode value}
    # }
    # ---
    # {x {n S} {t s}} {
    # 	S {is {n Z} mode value}
    # 	Z {is {t z} mode value}
    # }
    # {x {n S} {t s}} {
    # 	S {is {t z} mode value}
    # 	Z {is {t z} mode value}
    # }
} $setimpl

# -------------------------------------------------------------------------
# op: minimize

TestTransformation minimize {
    # --- stays as-is
    epsilon {}
................................................................................
     	S {is {/ {x {n A} {n B}} {t a}} mode value}
     	A {is {x {t a} {n A}} mode value}
	B {is {t a} mode leaf}
    }
    {n S} {
	S {is {t a} mode leaf}
    }
    # --- direct cycle
    {n A} {
	A {is {n A} mode value}
    }
    epsilon {}
    # --- indirect cycle
    {n A} {
	A {is {n B} mode value}
	B {is {n C} mode value}
	C {is {n A} mode value}
    }
    epsilon {}
} $setimpl

# -------------------------------------------------------------------------
rename sl {}
rename g {}
rename TestTransformation {}