-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathpleac.expected.raydebug
More file actions
2044 lines (1540 loc) · 114 KB
/
pleac.expected.raydebug
File metadata and controls
2044 lines (1540 loc) · 114 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
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
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
comment(// -*- c++ -*-)
comment(// @@PLEAC@@_NAME)
comment(// @@SKIP@@ C++/STL/Boost)
comment(// @@PLEAC@@_WEB)
comment(// @@SKIP@@ http://www.research.att.com/~bs/C++.html)
comment(// @@SKIP@@ http://www.boost.org/)
comment(// @@PLEAC@@_1.0)
comment(// NOTE: Whilst it is perfectly valid to use Standard C Library, or GNU)
comment(// C Library, routines in C++ programs, the code examples here will, as)
comment(// far as possible, avoid doing so, instead using C++-specific functionality)
comment(// and idioms. In general:)
comment(// * I/O will be iostream-based [i.e. no 'scanf', 'printf', 'fgets' etc])
comment(// * Container / iterator idioms based on the Standard Template Library [STL])
comment(// will replace the built-in array / raw pointer idioms typically used in C)
comment(// * Boost Library functionality utilised wherever possible [the reason for)
comment(// this is that much of this functionality is likely to appear in the next)
comment(// C++ standard])
comment(// * Error detection/handling will generally be exception-based [this is done)
comment(// to keep examples simple. Exception use is optional in C++, and is not as)
comment(// pervasive as it is in other languages like Java or C#])
comment(// C-based solution(s\) to problem(s\) will be found in the corresponding section)
comment(// of PLEAC-C/Posix/GNU.)
comment(// In C++, one can use the builtin 'char *' type or the 'string' type)
comment(// to represent strings. In this section, we will work with the C++)
comment(// library 'string' class.)
comment(// Characteristics of 'string' types:)
comment(// - may be of any length)
comment(// - are defined within the std namespace)
comment(// - can be converted to a 'const char *' using std::string::c_str(\))
comment(// - can be subscripted to access individual characters (e.g., str[3])
comment(// returns the 4th character of the string)
comment(// - memory associated with strings is reclaimed automatically as strings)
comment(// go out of scope)
comment(// - strings cannot be used as true/false values (i.e., the following is not)
comment(// allowed: string s; if (s\) {}\))
comment(//-----------------------------)
comment(// Before using strings, you must include the <string> header file)
preprocessor(#include) include(<string>)
comment(//-----------------------------)
comment(// To create a literal strings, you must use double quotes ("\). You cannot)
comment(// use single quotes. )
comment(//-----------------------------)
comment(// String variables must be declared -- if no value is given it's)
comment(// value is the empty string (""\). )
ident(std)operator(::)predefined_type(string) ident(s)operator(;)
comment(//-----------------------------)
comment(// To insert special characters, quote the character with \\
std::string s1 = "\\\\n"; // Two characters, \\ and n)
ident(std)operator(::)predefined_type(string) ident(s2) operator(=) string<delimiter(")content(Jon )char(\\")content(Maddog)char(\\")content( Orwant)delimiter(")>operator(;) comment(// Literal double quotes)
comment(//-----------------------------)
comment(// Strings can be declared in one of two ways)
ident(std)operator(::)predefined_type(string) ident(s1) operator(=) string<delimiter(")content(assignment syntax)delimiter(")>operator(;)
ident(std)operator(::)predefined_type(string) ident(s2)operator(()string<delimiter(")content(constructor syntax)delimiter(")>operator(\);)
comment(//-----------------------------)
comment(// Multi-line strings.)
comment(// There is no equivalent to perl's "here" documents in c++)
ident(std)operator(::)predefined_type(string) ident(s1) operator(=) string<delimiter(")content(
This is a multiline string started and finished with double
quotes that spans 4 lines (it contains 3 newline characters\).
)delimiter(")>operator(;)
ident(std)operator(::)predefined_type(string) ident(s2) operator(=) string<delimiter(")content(This is a multiline string started and finished with double
quotes that spans 2 lines (it contains 1 newline character\).)delimiter(")>operator(;)
comment(//-----------------------------)
comment(// @@PLEAC@@_1.1)
ident(std)operator(::)predefined_type(string) ident(s) operator(=) string<delimiter(")content(some string)delimiter(")>operator(;)
comment(//-----------------------------)
ident(std)operator(::)predefined_type(string) ident(value1) operator(=) ident(s)operator(.)ident(substr)operator(()ident(offset)operator(,) ident(length)operator(\);)
ident(std)operator(::)predefined_type(string) ident(value2) operator(=) ident(s)operator(.)ident(substr)operator(()ident(offset)operator(\);)
comment(// Unlike perl, the substr function returns a copy of the substring)
comment(// rather than a reference to the existing substring, thus using substr)
comment(// on the left hand side of an assignment statement will not modify )
comment(// the original string. To get this functionality, you can use the)
comment(// std::string::replace function.)
comment(// Using offsets and lengths)
ident(s)operator(.)ident(replace)operator(()ident(offset)operator(,) ident(length)operator(,) ident(newstring)operator(\);)
ident(s)operator(.)ident(replace)operator(()ident(offset)operator(,) ident(s)operator(.)ident(size)operator((\)-)ident(offset)operator(,) ident(newtail)operator(\);)
comment(//-----------------------------)
comment(// The C++ string class doesn't have anything equivalent to perl's unpack.)
comment(// Instead, one can use C structures to import/export binary data)
comment(//-----------------------------)
preprocessor(#include) include(<string>)
predefined_type(string) ident(s) operator(=) string<delimiter(")content(This is what you have)delimiter(")>operator(;)
ident(std)operator(::)predefined_type(string) ident(first) operator(=) ident(s)operator(.)ident(substr)operator(()integer(0)operator(,) integer(1)operator(\);) comment(// "T")
ident(std)operator(::)predefined_type(string) ident(second) operator(=) ident(s)operator(.)ident(substr)operator(()integer(5)operator(,) integer(2)operator(\);) comment(// "is")
ident(std)operator(::)predefined_type(string) ident(rest) operator(=) ident(s)operator(.)ident(substr)operator(()integer(13)operator(\);) comment(// "you have")
comment(// C++ strings do not support backwards indexing as perl does but )
comment(// you can fake it out by subtracting the negative index from the)
comment(// string length)
ident(std)operator(::)predefined_type(string) ident(last) operator(=) ident(s)operator(.)ident(substr)operator(()ident(s)operator(.)ident(size)operator((\)-)integer(1)operator(\);) comment(// "e")
ident(std)operator(::)predefined_type(string) ident(end) operator(=) ident(s)operator(.)ident(substr)operator(()ident(s)operator(.)ident(size)operator((\)-)integer(4)operator(\);) comment(// "have")
ident(std)operator(::)predefined_type(string) ident(piece) operator(=) ident(s)operator(.)ident(substr)operator(()ident(s)operator(.)ident(size)operator((\)-)integer(8)operator(,) integer(3)operator(\);) comment(// "you")
comment(//-----------------------------)
preprocessor(#include) include(<string>)
preprocessor(#include) include(<iostream>)
predefined_type(string) ident(s)operator(()string<delimiter(")content(This is what you have)delimiter(")>operator(\);)
ident(std)operator(::)ident(cout) operator(<<) ident(s) operator(<<) ident(std)operator(::)ident(endl)operator(;)
comment(// This is what you have)
ident(s)operator(.)ident(replace)operator(()integer(5)operator(,)integer(2)operator(,)string<delimiter(")content(wasn't)delimiter(")>operator(\);) comment(// change "is to "wasn't")
comment(// This wasn't what you have)
ident(s)operator(.)ident(replace)operator(()ident(s)operator(.)ident(size)operator((\)-)integer(12)operator(,) integer(12)operator(,) string<delimiter(")content(ondrous)delimiter(")>operator(\);) comment(// "This wasn't wondrous")
comment(// This wasn't wonderous)
ident(s)operator(.)ident(replace)operator(()integer(0)operator(,) integer(1)operator(,) string<delimiter(")delimiter(")>operator(\);) comment(// delete first character)
comment(// his wasn't wondrous)
ident(s)operator(.)ident(replace)operator(()ident(s)operator(.)ident(size)operator((\)-)integer(10)operator(,) integer(10)operator(,) string<delimiter(")delimiter(")>operator(\);) comment(// delete last 10 characters)
comment(// his wasn')
comment(//-----------------------------)
comment(// C++ does not have built-in support for the perl s///, m//, and tr/// )
comment(// operators; however, similar results can be achieved in at least )
comment(// two ways:)
comment(// - string operations such as string::find, string::rfind, etc.)
comment(// - the boost regular expression library (regex++\) supports perl)
comment(// regular expression syntax.)
comment(// TODO: Add examples of each.)
comment(// MISSING: if (substr($string, -10\) =~ /pattern/\) {)
comment(// print "Pattern matches in last 10 characters\\n";)
comment(// })
comment(// MISSING: substr($string, 0, 5\) =~ s/is/at/g;)
comment(//-----------------------------)
comment(// exchange the first and last letters in a string using substr and replace)
predefined_type(string) ident(a) operator(=) string<delimiter(")content(make a hat)delimiter(")>operator(;)
ident(std)operator(::)predefined_type(string) ident(first) operator(=) ident(a)operator(.)ident(substr)operator(()integer(0)operator(,)integer(1)operator(\);)
ident(std)operator(::)predefined_type(string) ident(last) operator(=) ident(a)operator(.)ident(substr)operator(()ident(a)operator(.)ident(size)operator((\)-)integer(1)operator(\);)
ident(a)operator(.)ident(replace)operator(()integer(0)operator(,)integer(1)operator(,) ident(last)operator(\);)
ident(a)operator(.)ident(replace)operator(()ident(a)operator(.)ident(size)operator((\)-)integer(1)operator(,) integer(1)operator(,) ident(first)operator(\);)
comment(// exchange the first and last letters in a string using indexing and swap)
preprocessor(#include) include(<algorithm>)
ident(std)operator(::)ident(swap)operator(()ident(a)operator([)integer(0)operator(],) ident(a)operator([)ident(a)operator(.)ident(size)operator((\)-)integer(1)operator(]\);)
comment(//-----------------------------)
comment(// @@PLEAC@@_1.2)
comment(//-----------------------------)
comment(// C++ doesn't have functionality equivalent to the || and ||=. )
comment(// If statements and trigraphs can be used instead.)
comment(//-----------------------------)
comment(// C++ doesn't have anything equivalent "defined". C++ variables)
comment(// cannot be used at all if they have not previously been defined.)
comment(//-----------------------------)
comment(// Use b if b is not empty, else c)
ident(a) operator(=) ident(b)operator(.)ident(size)operator((\)) operator(?) ident(b) operator(:) ident(c)operator(;)
comment(// Set x to y unless x is not empty)
keyword(if) operator(()ident(x)operator(.)ident(is_empty)operator((\)\)) ident(x) operator(=) ident(y)operator(;)
comment(//-----------------------------)
ident(foo) operator(=) operator((!)ident(bar)operator(.)ident(is_empty)operator((\)\)) operator(?) ident(bar) operator(:) string<delimiter(")content(DEFAULT VALUE)delimiter(")>operator(;)
comment(//-----------------------------)
comment(// NOTE: argv is declared as char *argv[] in C/C++. We assume)
comment(// the following code surrounds the following examples that deal)
comment(// with argv. Also, arguments to a program start at argv[1] -- argv[0])
comment(// is the name of the executable that's running.)
preprocessor(#include) include(<string.h>)
predefined_type(int) ident(main)operator(()predefined_type(int) ident(argc)operator(,) predefined_type(char) operator(*)ident(argv)operator([]\)) operator({)
predefined_type(char) operator(**)ident(args) operator(=) ident(argv)operator(+)integer(1)operator(;) comment(// +1 skips argv[0], the name of the executable)
comment(// examples)
operator(})
comment(//-----------------------------)
ident(std)operator(::)predefined_type(string) ident(dir) operator(=) operator((*)ident(args)operator(\)) operator(?) operator(*)ident(argv)operator(++) operator(:) string<delimiter(")content(/tmp)delimiter(")>operator(;)
comment(//-----------------------------)
ident(std)operator(::)predefined_type(string) ident(dir) operator(=) ident(argv)operator([)integer(1)operator(]) operator(?) ident(argv)operator([)integer(1)operator(]) operator(:) string<delimiter(")content(/tmp)delimiter(")>operator(;)
comment(//-----------------------------)
ident(std)operator(::)predefined_type(string) ident(dir) operator(=) operator(()ident(argc)operator(-)integer(1)operator(\)) operator(?) ident(argv)operator([)integer(1)operator(]) operator(:) string<delimiter(")content(/tmp)delimiter(")>operator(;)
comment(//-----------------------------)
preprocessor(#include) include(<map>)
ident(std)operator(::)ident(map)operator(<)ident(std)operator(::)predefined_type(string)operator(,)predefined_type(int)operator(>) ident(count)operator(;)
ident(count)operator([)ident(shell)operator(.)ident(size)operator((\)) operator(?) ident(shell) operator(:) string<delimiter(")content(/bin/sh)delimiter(")>operator(]++;)
comment(//-----------------------------)
comment(// find the user name on Unix systems)
comment(// TODO: Simplify. This is too ugly and complex)
preprocessor(#include) include(<sys/types.h>)
preprocessor(#include) include(<unistd.h>)
preprocessor(#include) include(<pwd.h>)
preprocessor(#include) include("boost/lexical_cast.hpp")
ident(std)operator(::)predefined_type(string) ident(user)operator(;)
predefined_type(char) operator(*)ident(msg) operator(=) integer(0)operator(;)
ident(passwd) operator(*)ident(pwd) operator(=) predefined_constant(nullptr)operator(;)
keyword(if) operator(() operator(()ident(msg) operator(=) ident(getenv)operator(()string<delimiter(")content(USER)delimiter(")>operator(\)\)) operator(||)
operator(()ident(msg) operator(=) ident(getenv)operator(()string<delimiter(")content(LOGNAME)delimiter(")>operator(\)\)) operator(||)
operator(()ident(msg) operator(=) ident(getlogin)operator((\)\)) operator(\))
ident(user) operator(=) ident(msg)operator(;)
keyword(else) keyword(if) operator(()ident(pwd) operator(=) ident(getpwuid)operator(()ident(getuid)operator((\)\)\))
ident(user) operator(=) ident(pwd)operator(->)ident(pw_name)operator(;)
keyword(else)
ident(user) operator(=) string<delimiter(")content(Unknown uid number )delimiter(")> operator(+) ident(boost)operator(::)ident(lexical_cast)operator(<)ident(std)operator(::)predefined_type(string)operator(>()ident(getuid)operator((\)\);)
comment(//-----------------------------)
keyword(if) operator(()ident(starting_point)operator(.)ident(is_empty)operator((\)\)) ident(starting_point) operator(=) string<delimiter(")content(Greenwich)delimiter(")>operator(;)
comment(//-----------------------------)
comment(// Example using list. Other C++ STL containers work similarly.)
preprocessor(#include) include(<list>)
ident(list)operator(<)predefined_type(int)operator(>) ident(a)operator(,) ident(b)operator(;)
keyword(if) operator(()ident(a)operator(.)ident(is_empty)operator((\)\)) ident(a) operator(=) ident(b)operator(;) comment(// copy only if a is empty)
ident(a) operator(=) operator((!)ident(b)operator(.)ident(is_empty)operator((\)\)) operator(?) ident(b) operator(:) ident(c)operator(;) comment(// asign b if b nonempty, else c)
comment(//-----------------------------)
comment(// @@PLEAC@@_1.3)
comment(//-----------------------------)
preprocessor(#include) include(<algorithm>)
ident(std)operator(::)ident(swap)operator(()ident(a)operator(,) ident(b)operator(\);)
comment(//-----------------------------)
ident(temp) operator(=) ident(a)operator(;)
ident(a) operator(=) ident(b)operator(;)
ident(b) operator(=) ident(temp)operator(;)
comment(//-----------------------------)
ident(std)operator(::)predefined_type(string) ident(a)operator(()string<delimiter(")content(alpha)delimiter(")>operator(\);)
ident(std)operator(::)predefined_type(string) ident(b)operator(()string<delimiter(")content(omega)delimiter(")>operator(\);)
ident(std)operator(::)ident(swap)operator(()ident(a)operator(,)ident(b)operator(\);)
comment(//-----------------------------)
comment(// The ability to exchange more than two variables at once is not )
comment(// built into the C++ language or C++ standard libraries. However, you)
comment(// can use the boost tuple library to accomplish this.)
preprocessor(#include) include(<boost/tuple/tuple.hpp>)
ident(boost)operator(::)ident(tie)operator(()ident(alpha)operator(,)ident(beta)operator(,)ident(production)operator(\))
operator(=) ident(boost)operator(::)ident(make_tuple)operator(()string<delimiter(")content(January)delimiter(")>operator(,) string<delimiter(")content(March)delimiter(")>operator(,) string<delimiter(")content(August)delimiter(")>operator(\);)
comment(// move beta to alpha,)
comment(// move production to beta,)
comment(// move alpha to production)
ident(boost)operator(::)ident(tie)operator(()ident(alpha)operator(,) ident(beta)operator(,) ident(production)operator(\))
operator(=) ident(boost)operator(::)ident(make_tuple)operator(()ident(beta)operator(,) ident(production)operator(,) ident(alpha)operator(\);)
comment(//-----------------------------)
comment(// @@PLEAC@@_1.4)
comment(//-----------------------------)
comment(// There are several ways to convert between characters)
comment(// and integers. The examples assume the following declarations:)
predefined_type(char) ident(ch)operator(;)
predefined_type(int) ident(num)operator(;)
comment(//-----------------------------)
comment(// Using implicit conversion)
ident(num) operator(=) ident(ch)operator(;)
ident(ch) operator(=) ident(num)operator(;)
comment(//-----------------------------)
comment(// New-style C++ casts)
ident(ch) operator(=) keyword(static_cast)operator(<)predefined_type(char)operator(>()ident(num)operator(\);)
ident(num) operator(=) keyword(static_cast)operator(<)predefined_type(int)operator(>()ident(ch)operator(\);)
comment(//-----------------------------)
comment(// Old-style C casts)
ident(ch) operator(=) operator(()predefined_type(char)operator(\))ident(num)operator(;)
ident(num) operator(=) operator(()predefined_type(int)operator(\))ident(ch)operator(;)
comment(//-----------------------------)
comment(// Using the C++ stringstream class)
preprocessor(#include) include(<sstream>) comment(// On some older compilers, use <strstream>)
ident(std)operator(::)ident(stringstream) ident(a)operator(;) comment(// On some older compilers, use std::strstream)
ident(a) operator(<<) ident(ch)operator(;) comment(// Append character to a string)
ident(a) operator(>>) ident(num)operator(;) comment(// Output character as a number)
ident(a) operator(<<) ident(num)operator(;) comment(// Append number to a string)
ident(a) operator(>>) ident(ch)operator(;) comment(// Output number as a character)
comment(//-----------------------------)
comment(// Using sprintf, printf)
predefined_type(char) ident(str)operator([)integer(2)operator(];) comment(// Has to be length 2 to have room for NULL character)
ident(sprintf)operator(()ident(str)operator(,) string<delimiter(")content(%c)delimiter(")>operator(,) ident(num)operator(\);)
ident(printf)operator(()string<delimiter(")content(Number %d is character %c)char(\\n)delimiter(")>operator(,) ident(num)operator(,) ident(num)operator(\);)
comment(//-----------------------------)
predefined_type(int) ident(ascii_value) operator(=) char('e')operator(;) comment(// now 101)
predefined_type(char) ident(character) operator(=) integer(101)operator(;) comment(// now 'e')
predefined_type(char16_t) ident(ch1) operator({) ident(L)char('a') operator(};)
predefined_type(char32_t) ident(ch2) operator({) ident(L)char('a') operator(};)
comment(//-----------------------------)
ident(printf)operator(()string<delimiter(")content(Number %d is character %c)char(\\n)delimiter(")>operator(,) integer(101)operator(,) integer(101)operator(\);)
comment(//-----------------------------)
comment(// Convert from HAL to IBM, character by character)
preprocessor(#include) include(<string>)
preprocessor(#include) include(<iostream>)
ident(std)operator(::)predefined_type(string) ident(ibm)operator(,) ident(hal) operator(=) string<delimiter(")content(HAL)delimiter(")>operator(;)
keyword(for) operator(()predefined_type(unsigned) predefined_type(int) ident(i)operator(=)integer(0)operator(;) ident(i)operator(<)ident(hal)operator(.)ident(size)operator((\);) operator(++)ident(i)operator(\))
ident(ibm) operator(+=) ident(hal)operator([)ident(i)operator(]+)integer(1)operator(;) comment(// Add one to each ascii value)
ident(std)operator(::)ident(cout) operator(<<) ident(ibm) operator(<<) ident(std)operator(::)ident(endl)operator(;) comment(// prints "IBM")
comment(//-----------------------------)
comment(// Convert hal from HAL to IBM)
preprocessor(#include) include(<string>)
preprocessor(#include) include(<iostream>)
preprocessor(#include) include(<functional>) comment(// For bind1st and plus<>)
preprocessor(#include) include(<algorithm>) comment(// For transform )
ident(std)operator(::)predefined_type(string) ident(hal) operator(=) string<delimiter(")content(HAL)delimiter(")>operator(;)
ident(transform)operator(()ident(hal)operator(.)ident(begin)operator((\),) ident(hal)operator(.)ident(end)operator((\),) ident(hal)operator(.)ident(begin)operator((\),)
ident(bind1st)operator(()ident(plus)operator(<)predefined_type(char)operator(>(\),)integer(1)operator(\)\);)
ident(std)operator(::)ident(cout) operator(<<) ident(hal) operator(<<) ident(std)operator(::)ident(endl)operator(;) comment(// prints "IBM")
comment(//-----------------------------)
comment(// @@PLEAC@@_1.5)
comment(//-----------------------------)
comment(// Since C++ strings can be accessed one character at a time,)
comment(// there's no need to do any processing on the string to convert)
comment(// it into an array of characters. )
preprocessor(#include) include(<string>)
ident(std)operator(::)predefined_type(string) ident(s)operator(;)
comment(// Accessing characters using for loop and integer offsets)
keyword(for) operator(()predefined_type(unsigned) predefined_type(int) ident(i)operator(=)integer(0)operator(;) ident(i)operator(<)ident(s)operator(.)ident(size)operator((\);) operator(++)ident(i)operator(\)) operator({)
comment(// do something with s[i])
operator(})
comment(// Accessing characters using iterators)
keyword(for) operator(()ident(std)operator(::)predefined_type(string)operator(::)ident(iterator) ident(i)operator(=)ident(s)operator(.)ident(begin)operator((\);) ident(i)operator(!=)ident(s)operator(.)ident(end)operator((\);) operator(++)ident(i)operator(\)) operator({)
comment(// do something with *i)
operator(})
comment(//-----------------------------)
ident(std)operator(::)predefined_type(string) ident(str) operator(=) string<delimiter(")content(an apple a day)delimiter(")>operator(;)
ident(std)operator(::)ident(map)operator(<)predefined_type(char)operator(,)predefined_type(int)operator(>) ident(seen)operator(;)
keyword(for) operator(()ident(std)operator(::)predefined_type(string)operator(::)ident(iterator) ident(i)operator(=)ident(str)operator(.)ident(begin)operator((\);) ident(i)operator(!=)ident(str)operator(.)ident(end)operator((\);) operator(++)ident(i)operator(\))
ident(seen)operator([*)ident(i)operator(]++;)
ident(std)operator(::)ident(cout) operator(<<) string<delimiter(")content(unique chars are: )delimiter(")>operator(;)
keyword(for) operator(()ident(std)operator(::)ident(map)operator(<)predefined_type(char)operator(,)predefined_type(int)operator(>::)ident(iterator) ident(i)operator(=)ident(seen)operator(.)ident(begin)operator((\);) ident(i)operator(!=)ident(seen)operator(.)ident(end)operator((\);) operator(++)ident(i)operator(\))
ident(std)operator(::)ident(cout) operator(<<) ident(i)operator(->)ident(first)operator(;)
ident(std)operator(::)ident(cout) operator(<<) ident(std)operator(::)ident(endl)operator(;)
comment(// unique chars are: adelnpy)
comment(//-----------------------------)
predefined_type(int) ident(sum) operator(=) integer(0)operator(;)
keyword(for) operator(()ident(std)operator(::)predefined_type(string)operator(::)ident(iterator) ident(i)operator(=)ident(str)operator(.)ident(begin)operator((\);) ident(i)operator(!=)ident(str)operator(.)ident(end)operator((\);) operator(++)ident(i)operator(\))
ident(sum) operator(+=) operator(*)ident(i)operator(;)
ident(std)operator(::)ident(cout) operator(<<) string<delimiter(")content(sum is )delimiter(")> operator(<<) ident(sum) operator(<<) ident(std)operator(::)ident(endl)operator(;)
comment(// prints "sum is 1248" if str was "an appla a day")
comment(//-----------------------------)
comment(// MISSING: sysv-like checksum program)
comment(//-----------------------------)
comment(// slowcat, emulate a slow line printer)
preprocessor(#include) include(<sys/time.h>)
preprocessor(#include) include(<iostream>)
preprocessor(#include) include(<fstream>)
predefined_type(int) ident(main)operator(()predefined_type(int) ident(argc)operator(,) predefined_type(char) operator(*)ident(argv)operator([]\)) operator({)
ident(timeval) ident(delay) operator(=) operator({) integer(0)operator(,) integer(50000) operator(};) comment(// Delay in { seconds, nanoseconds })
predefined_type(char) operator(**)ident(arg) operator(=) ident(argv)operator(+)integer(1)operator(;)
keyword(while) operator((*)ident(arg)operator(\)) operator({) comment(// For each file)
ident(std)operator(::)ident(ifstream) ident(file)operator((*)ident(arg)operator(++\);)
predefined_type(char) ident(c)operator(;)
keyword(while) operator(()ident(file)operator(.)ident(get)operator(()ident(c)operator(\)\)) operator({)
ident(std)operator(::)ident(cout)operator(.)ident(put)operator(()ident(c)operator(\);)
ident(std)operator(::)ident(cout)operator(.)ident(flush)operator((\);)
ident(select)operator(()integer(0)operator(,) integer(0)operator(,) integer(0)operator(,) integer(0)operator(,) operator(&)ident(delay)operator(\);)
operator(})
operator(})
operator(})
comment(//-----------------------------)
comment(// @@PLEAC@@_1.6)
comment(//-----------------------------)
preprocessor(#include) include(<string>)
preprocessor(#include) include(<algorithm>) comment(// For reverse)
ident(std)operator(::)predefined_type(string) ident(s)operator(;)
ident(reverse)operator(()ident(s)operator(.)ident(begin)operator((\),) ident(s)operator(.)ident(end)operator((\)\);)
comment(//-----------------------------)
preprocessor(#include) include(<vector>) comment(// For std::vector)
preprocessor(#include) include(<sstream>) comment(// On older compilers, use <strstream>)
preprocessor(#include) include("boost/regex.hpp") comment(// For boost::regex_split)
ident(std)operator(::)predefined_type(string) ident(str)operator(;)
ident(std)operator(::)ident(vector)operator(<)ident(std)operator(::)predefined_type(string)operator(>) ident(words)operator(;)
ident(boost)operator(::)ident(regex_split)operator(()ident(std)operator(::)ident(back_inserter)operator(()ident(words)operator(\),) ident(str)operator(\);)
ident(reverse)operator(()ident(words)operator(.)ident(begin)operator((\),) ident(words)operator(.)ident(end)operator((\)\);) comment(// Reverse the order of the words)
ident(std)operator(::)ident(stringstream) ident(revwords)operator(;) comment(// On older compilers, use strstream)
ident(copy)operator(()ident(words)operator(.)ident(begin)operator((\),) ident(words)operator(.)ident(end)operator((\),) ident(ostream_inserter)operator(<)predefined_type(string)operator(>()ident(revwords)operator(,)string<delimiter(")content( )delimiter(")>operator(\);)
ident(std)operator(::)ident(cout) operator(<<) ident(revwards)operator(.)ident(str)operator((\)) operator(<<) ident(std)operator(::)ident(endl)operator(;)
comment(//-----------------------------)
ident(std)operator(::)predefined_type(string) ident(rts) operator(=) ident(str)operator(;)
ident(reverse)operator(()ident(rts)operator(.)ident(begin)operator((\),) ident(rts)operator(.)ident(end)operator((\)\);) comment(// Reverses letters in rts)
comment(//-----------------------------)
ident(std)operator(::)ident(vector)operator(<)predefined_type(string)operator(>) ident(words)operator(;)
ident(reverse)operator(()ident(words)operator(.)ident(begin)operator((\),) ident(words)operator(.)ident(end)operator((\)\);) comment(// Reverses words in container)
comment(//-----------------------------)
comment(// Reverse word order)
ident(std)operator(::)predefined_type(string) ident(s) operator(=) string<delimiter(")content(Yoda said, 'can you see this?')delimiter(")>operator(;)
ident(std)operator(::)ident(vector)operator(<)ident(std)operator(::)predefined_type(string)operator(>) ident(allwords)operator(;)
ident(boost)operator(::)ident(regex_split)operator(()ident(std)operator(::)ident(back_inserter)operator(()ident(allwords)operator(\),) ident(s)operator(\);)
ident(reverse)operator(()ident(allwords)operator(.)ident(begin)operator((\),) ident(allwords)operator(.)ident(end)operator((\)\);)
ident(std)operator(::)ident(stringstream) ident(revwords)operator(;) comment(// On older compilers, use strstream)
ident(copy)operator(()ident(allwords)operator(.)ident(begin)operator((\),) ident(allwords)operator(.)ident(end)operator((\),) ident(ostream_inserter)operator(<)predefined_type(string)operator(>()ident(revwords)operator(,)string<delimiter(")content( )delimiter(")>operator(\)\);)
ident(std)operator(::)ident(cout) operator(<<) ident(revwards)operator(.)ident(str)operator((\)) operator(<<) ident(std)operator(::)ident(endl)operator(;)
comment(// this?' see you 'can said, Yoda)
comment(//-----------------------------)
ident(std)operator(::)predefined_type(string) ident(word) operator(=) string<delimiter(")content(reviver)delimiter(")>operator(;)
predefined_type(bool) ident(is_palindrome) operator(=) ident(equal)operator(()ident(word)operator(.)ident(begin)operator((\),) ident(word)operator(.)ident(end)operator((\),) ident(word)operator(.)ident(rbegin)operator((\)\);)
comment(//-----------------------------)
preprocessor(#include) include(<ifstream>)
ident(std)operator(::)ident(ifstream) ident(dict)operator(()string<delimiter(")content(/usr/dict/words)delimiter(")>operator(\);)
ident(std)operator(::)predefined_type(string) ident(word)operator(;)
keyword(while)operator(()ident(getline)operator(()ident(dict)operator(,)ident(word)operator(\)\)) operator({)
keyword(if) operator(()ident(equal)operator(()ident(word)operator(.)ident(begin)operator((\),) ident(word)operator(.)ident(end)operator((\),) ident(word)operator(.)ident(rbegin)operator((\)\)) operator(&&)
ident(word)operator(.)ident(size)operator((\)) operator(>) integer(5)operator(\))
ident(std)operator(::)ident(cout) operator(<<) ident(word) operator(<<) ident(std)operator(::)ident(endl)operator(;)
operator(})
comment(//-----------------------------)
comment(// @@PLEAC@@_1.7)
comment(//-----------------------------)
preprocessor(#include) include(<string>)
ident(std)operator(::)predefined_type(string)operator(::)ident(size_type) ident(pos)operator(;)
keyword(while) operator((()ident(pos) operator(=) ident(str)operator(.)ident(find)operator(()string<delimiter(")char(\\t)delimiter(")>operator(\)\)) operator(!=) ident(std)operator(::)predefined_type(string)operator(::)ident(npos)operator(\))
ident(str)operator(.)ident(replace)operator(()ident(pos)operator(,) integer(1)operator(,) predefined_type(string)operator(()char(' ')operator(,)integer(8)operator(-)ident(pos)operator(%)integer(8)operator(\)\);)
comment(//-----------------------------)
comment(// @@PLEAC@@_1.8)
comment(//-----------------------------)
comment(// Not applicable to C++)
comment(//-----------------------------)
comment(// @@PLEAC@@_1.9)
comment(//-----------------------------)
comment(// TODO: Fix to be more like cookbook)
comment(// TODO: Modify/add code to do this with locales)
preprocessor(#include) include(<string>)
preprocessor(#include) include(<algorithm>)
ident(std)operator(::)predefined_type(string) ident(phrase) operator(=) string<delimiter(")content(bo peep)delimiter(")>operator(;)
ident(transform)operator(()ident(phrase)operator(.)ident(begin)operator((\),) ident(phrase)operator(.)ident(end)operator((\),) ident(phrase)operator(.)ident(begin)operator((\),) ident(toupper)operator(\);)
comment(// "BO PEEP")
ident(transform)operator(()ident(phrase)operator(.)ident(begin)operator((\),) ident(phrase)operator(.)ident(end)operator((\),) ident(phrase)operator(.)ident(begin)operator((\),) ident(tolower)operator(\);)
comment(// "bo peep")
comment(//-----------------------------)
comment(// @@PLEAC@@_1.10)
comment(//-----------------------------)
comment(// C++ does not provide support for perl-like in-string interpolation,)
comment(// concatenation must be used instead.)
preprocessor(#include) include(<string>)
ident(std)operator(::)predefined_type(string) ident(var1)operator(,) ident(var2)operator(;)
ident(std)operator(::)predefined_type(string) ident(answer) operator(=) ident(var1) operator(+) ident(func)operator((\)) operator(+) ident(var2)operator(;) comment(// func returns string or char *)
comment(//-----------------------------)
preprocessor(#include) include("boost/lexical_cast.hpp")
predefined_type(int) ident(n) operator(=) integer(4)operator(;)
ident(std)operator(::)predefined_type(string) ident(phrase) operator(=) string<delimiter(")content(I have )delimiter(")> operator(+) ident(boost)operator(::)ident(lexical_cast)operator(<)predefined_type(string)operator(>()ident(n)operator(+)integer(1)operator(\)) operator(+) string<delimiter(")content( guanacos.)delimiter(")>operator(;)
comment(//-----------------------------)
ident(std)operator(::)ident(cout) operator(<<) string<delimiter(")content(I have )delimiter(")> operator(+) ident(boost)operator(::)ident(lexical_cast)operator(<)predefined_type(string)operator(>()ident(n)operator(+)integer(1)operator(\)) operator(+) string<delimiter(")content( guanacos.)delimiter(")> operator(<<) ident(std)operator(::)ident(endl)operator(;)
comment(// @@PLEAC@@_1.11)
comment(//-----------------------------)
comment(// C++ does not have "here documents".)
comment(// TODO: Lots more.)
preprocessor(#include) include(<string>)
preprocessor(#include) include("boost/regex.hpp")
ident(std)operator(::)predefined_type(string) ident(var) operator(=) string<delimiter(")content(
your text
goes here.
)delimiter(")>operator(;)
ident(boost)operator(::)ident(regex) ident(ex)operator(()string<delimiter(")content(^)char(\\\\)content(s+)delimiter(")>operator(\);)
ident(var) operator(=) ident(boost)operator(::)ident(regex_merge)operator(()ident(var)operator(,) ident(ex)operator(,) string<delimiter(")delimiter(")>operator(\);)
comment(// @@PLEAC@@_10.0)
comment(// NOTE: Whilst it is perfectly valid to use Standard C Library, or GNU C Library, routines in)
comment(// C++ programs, the code examples here will, as far as possible, avoid doing so, instead using)
comment(// C++-specific functionality and idioms. In general:)
comment(// * I/O will be iostream-based [i.e. no 'scanf', 'printf', 'fgets' etc])
comment(// * Container / iterator idioms based on the Standard Template Library [STL])
comment(// will replace the built-in array / raw pointer idioms typically used in C)
comment(// * Boost Library functionality utilised wherever possible [the reason for)
comment(// this is that much of this functionality is likely to appear in the next)
comment(// C++ standard])
comment(// * Error detection/handling will generally be exception-based [this is done)
comment(// to keep examples simple. Exception use is optional in C++, and is not as)
comment(// pervasive as it is in other languages like Java or C#])
comment(// C-based solution(s\) to problem(s\) will be found in the corresponding section of PLEAC-C/Posix/GNU.)
preprocessor(#include) include(<iostream>)
comment(// 'greeted' defined outside of any namespace, class or function, so is part of the)
comment(// global namespace, and will be visible throughout the entire executable. Should it)
comment(// be necessary to restrict the visibility of this global identifier to the current)
comment(// 'compilation unit' [i.e. current source file] then the following may be used:)
comment(//)
comment(// namespace { int greeted = 0; })
comment(//)
comment(// The effect is similar to using the 'static' keyword, in this same context, in the C)
comment(// language.)
predefined_type(int) ident(greeted) operator(=) integer(0)operator(;)
predefined_type(int) ident(howManyGreetings)operator((\);)
directive(void) ident(hello)operator((\);)
comment(// ----)
predefined_type(int) ident(main)operator((\))
operator({)
ident(hello)operator((\);)
predefined_type(int) ident(greetings) operator(=) ident(howManyGreetings)operator((\);)
ident(std)operator(::)ident(cout) operator(<<) string<delimiter(")content(bye there!, there have been )delimiter(")>
operator(<<) ident(greetings)
operator(<<) string<delimiter(")content( greetings so far)delimiter(")>
operator(<<) ident(std)operator(::)ident(endl)operator(;)
operator(})
comment(// ----)
predefined_type(int) ident(howManyGreetings)operator((\))
operator({)
comment(// Access 'greeted' identifier in the global namespace using the scope resolution)
comment(// operator. Use of this operator is only necessary if a similarly-named identifier)
comment(// exists in a )
keyword(return) operator(::)ident(greeted)operator(;)
operator(})
directive(void) ident(hello)operator((\))
operator({)
comment(// Here 'greeted' is accessed without additional qualification. Since a 'greeted' identifier)
comment(// exists only in the global namespace, it is that identifier that is used)
ident(std)operator(::)ident(cout) operator(<<) string<delimiter(")content(high there!, this function has been called )delimiter(")>
operator(<<) operator(++)ident(greeted)
operator(<<) string<delimiter(")content( times)delimiter(")>
operator(<<) ident(std)operator(::)ident(endl)operator(;)
operator(})
comment(// @@PLEAC@@_10.1)
comment(// Standard C++ requires that a function be prototyped, hence the name and type of parameters)
comment(// must be specified, and the argumemt list in any calls to that function must match the)
comment(// parameter list, as shown here )
preprocessor(#include) include(<cmath>)
predefined_type(double) ident(hypotenuse)operator(()predefined_type(double) ident(side1)operator(,) predefined_type(double) ident(side2)operator(\);)
comment(// ----)
predefined_type(int) ident(main)operator((\))
operator({)
predefined_type(double) ident(diag) operator(=) ident(hypotenuse)operator(()float(3)float(.0)operator(,) float(4)float(.0)operator(\);)
operator(})
comment(// ----)
predefined_type(double) ident(hypotenuse)operator(()predefined_type(double) ident(side1)operator(,) predefined_type(double) ident(side2)operator(\))
operator({)
keyword(return) ident(std)operator(::)ident(sqrt)operator(()ident(std)operator(::)ident(pow)operator(()ident(side1)operator(,) float(2)float(.0)operator(\)) operator(+) ident(std)operator(::)ident(pow)operator(()ident(side2)operator(,) float(2)float(.0)operator(\)\);)
operator(})
comment(// ----------------------------)
comment(// Variable length argument list functions, via the C Language derived 'va_...' macros,)
comment(// are also supported. However use of this facility is particularly discouraged in C++)
comment(// because:)
comment(// * It is an inherently type-unsafe facility; type safety is a core C++ concern)
comment(// * Other facilities, such as overloaded functions, and default arguments [neither of which)
comment(// are available in C] can sometimes obviate the need for variable length argument lists)
comment(// * OOP techniques can also lessen the need for variable length argument lists. The most)
comment(// obvious example here is the Iostream library where repeated calls of I/O operators replace)
comment(// the format string / variable arguments of 'printf')
preprocessor(#include) include(<cmath>)
preprocessor(#include) include(<cstdarg>)
predefined_type(double) ident(hypotenuse)operator(()predefined_type(double) ident(side1)operator(,) operator(.)operator(.)operator(.)operator(\);)
comment(// ----)
predefined_type(int) ident(main)operator((\))
operator({)
predefined_type(double) ident(diag) operator(=) ident(hypotenuse)operator(()float(3)float(.0)operator(,) float(4)float(.0)operator(\);)
operator(})
comment(// ----)
predefined_type(double) ident(hypotenuse)operator(()predefined_type(double) ident(side1)operator(,) operator(.)operator(.)operator(.)operator(\))
operator({)
comment(// More details available in the corresponding section of PLEAC-C/Posix/GNU)
ident(va_list) ident(ap)operator(;)
ident(va_start)operator(()ident(ap)operator(,) ident(side1)operator(\);)
predefined_type(double) ident(side2) operator(=) ident(va_arg)operator(()ident(ap)operator(,) predefined_type(double)operator(\);)
ident(va_end)operator(()ident(ap)operator(\);)
keyword(return) ident(std)operator(::)ident(sqrt)operator(()ident(std)operator(::)ident(pow)operator(()ident(side1)operator(,) float(2)float(.0)operator(\)) operator(+) ident(std)operator(::)ident(pow)operator(()ident(side2)operator(,) float(2)float(.0)operator(\)\);)
operator(})
comment(// ----------------------------)
comment(// An example using default arguments appears below)
preprocessor(#include) include(<cmath>)
comment(// Specify default argument values in declaration)
comment(// Note: This may be done in either of the declaration or the definition [not both], but it)
comment(// makes more sense to do so in the declaration since these are usually placed in header files)
comment(// which may be included in several source files. The default argument values would need to be)
comment(// known in all those locations)
predefined_type(double) ident(hypotenuse)operator(()predefined_type(double) ident(side1) operator(=) float(3)float(.0)operator(,) predefined_type(double) ident(side2) operator(=) float(4)float(.0)operator(\);)
comment(// ----)
predefined_type(int) ident(main)operator((\))
operator({)
comment(// All arguments specified)
predefined_type(double) ident(diag) operator(=) ident(hypotenuse)operator(()float(3)float(.0)operator(,) float(4)float(.0)operator(\);)
comment(// Both calls utilise default argument value(s\))
ident(diag) operator(=) ident(hypotenuse)operator(()float(3)float(.0)operator(\);)
ident(diag) operator(=) ident(hypotenuse)operator((\);)
operator(})
comment(// ----)
predefined_type(double) ident(hypotenuse)operator(()predefined_type(double) ident(side1)operator(,) predefined_type(double) ident(side2)operator(\))
operator({)
keyword(return) ident(std)operator(::)ident(sqrt)operator(()ident(std)operator(::)ident(pow)operator(()ident(side1)operator(,) float(2)float(.0)operator(\)) operator(+) ident(std)operator(::)ident(pow)operator(()ident(side2)operator(,) float(2)float(.0)operator(\)\);)
operator(})
comment(// ----------------------------)
comment(// A [very contrived, not very practical] example using function overloading appears below)
preprocessor(#include) include(<cmath>)
predefined_type(double) ident(hypotenuse)operator(()predefined_type(double) ident(side1)operator(,) predefined_type(double) ident(side2)operator(\);)
predefined_type(double) ident(hypotenuse)operator(()predefined_type(double) ident(side1)operator(\);)
predefined_type(double) ident(hypotenuse)operator((\);)
comment(// ----)
predefined_type(int) ident(main)operator((\))
operator({)
comment(// Call version (1\))
predefined_type(double) ident(diag) operator(=) ident(hypotenuse)operator(()float(3)float(.0)operator(,) float(4)float(.0)operator(\);)
comment(// Call version (2\))
ident(diag) operator(=) ident(hypotenuse)operator(()float(3)float(.0)operator(\);)
comment(// Call version (3\))
ident(diag) operator(=) ident(hypotenuse)operator((\);)
operator(})
comment(// ----)
comment(// (1\))
predefined_type(double) ident(hypotenuse)operator(()predefined_type(double) ident(side1)operator(,) predefined_type(double) ident(side2)operator(\))
operator({)
keyword(return) ident(std)operator(::)ident(sqrt)operator(()ident(std)operator(::)ident(pow)operator(()ident(side1)operator(,) float(2)float(.0)operator(\)) operator(+) ident(std)operator(::)ident(pow)operator(()ident(side2)operator(,) float(2)float(.0)operator(\)\);)
operator(})
comment(// (2\))
predefined_type(double) ident(hypotenuse)operator(()predefined_type(double) ident(side1)operator(\))
operator({)
keyword(return) ident(std)operator(::)ident(sqrt)operator(()ident(std)operator(::)ident(pow)operator(()ident(side1)operator(,) float(2)float(.0)operator(\)) operator(+) ident(std)operator(::)ident(pow)operator(()float(4)float(.0)operator(,) float(2)float(.0)operator(\)\);)
operator(})
comment(// (3\))
predefined_type(double) ident(hypotenuse)operator((\))
operator({)
keyword(return) ident(std)operator(::)ident(sqrt)operator(()ident(std)operator(::)ident(pow)operator(()float(3)float(.0)operator(,) float(2)float(.0)operator(\)) operator(+) ident(std)operator(::)ident(pow)operator(()float(4)float(.0)operator(,) float(2)float(.0)operator(\)\);)
operator(})
comment(// ----------------------------)
preprocessor(#include) include(<cstddef>)
preprocessor(#include) include(<vector>)
ident(std)operator(::)ident(vector)operator(<)predefined_type(int)operator(>) ident(int_all)operator(()directive(const) predefined_type(double) ident(arr)operator([],) ident(size_t) ident(arrsize)operator(\);)
ident(std)operator(::)ident(vector)operator(<)predefined_type(int)operator(>) ident(int_all)operator(()directive(const) ident(std)operator(::)ident(vector)operator(<)predefined_type(double)operator(>&) ident(arr)operator(\);)
comment(// ----)
predefined_type(int) ident(main)operator((\))
operator({)
comment(// Load vectors from built-in arrays, or use Boost 'assign' library)
directive(const) predefined_type(double) ident(nums)operator([]) operator(=) operator({)float(1)float(.4)operator(,) float(3)float(.5)operator(,) float(6)float(.7)operator(};)
directive(const) ident(size_t) ident(arrsize) operator(=) keyword(sizeof)operator(()ident(nums)operator(\)) operator(/) keyword(sizeof)operator(()ident(nums)operator([)integer(0)operator(]\);)
comment(// Conversion effected at vector creation time)
ident(std)operator(::)ident(vector)operator(<)predefined_type(int)operator(>) ident(ints) operator(=) ident(int_all)operator(()ident(nums)operator(,) ident(arrsize)operator(\);)
comment(// Vector -> vector copy / conversion )
ident(ints) operator(=) ident(int_all)operator(()ident(std)operator(::)ident(vector)operator(<)predefined_type(double)operator(>()ident(nums)operator(,) ident(nums) operator(+) ident(arrsize)operator(\)\);)
operator(})
comment(// ----)
ident(std)operator(::)ident(vector)operator(<)predefined_type(int)operator(>) ident(int_all)operator(()directive(const) predefined_type(double) ident(arr)operator([],) ident(size_t) ident(arrsize)operator(\))
operator({)
keyword(return) ident(std)operator(::)ident(vector)operator(<)predefined_type(int)operator(>()ident(arr)operator(,) ident(arr) operator(+) ident(arrsize)operator(\);)
operator(})
ident(std)operator(::)ident(vector)operator(<)predefined_type(int)operator(>) ident(int_all)operator(()directive(const) ident(std)operator(::)ident(vector)operator(<)predefined_type(double)operator(>&) ident(arr)operator(\))
operator({)
ident(std)operator(::)ident(vector)operator(<)predefined_type(int)operator(>) ident(r)operator(;)
ident(r)operator(.)ident(assign)operator(()ident(arr)operator(.)ident(begin)operator((\),) ident(arr)operator(.)ident(end)operator((\)\);) comment(// Type safe element copying )
keyword(return) ident(r)operator(;)
operator(})
comment(// ----------------------------)
preprocessor(#include) include(<algorithm>)
preprocessor(#include) include(<vector>)
preprocessor(#include) include(<cmath>)
preprocessor(#include) include(<cstddef>)
directive(void) ident(trunc_em)operator(()ident(std)operator(::)ident(vector)operator(<)predefined_type(double)operator(>&) ident(arr)operator(\);)
comment(// ----)
predefined_type(int) ident(main)operator((\))
operator({)
comment(// Load vectors from built-in arrays, or use Boost 'assign' library)
directive(const) predefined_type(double) ident(nums)operator([]) operator(=) operator({)float(1)float(.4)operator(,) float(3)float(.5)operator(,) float(6)float(.7)operator(};)
directive(const) ident(size_t) ident(arrsize) operator(=) keyword(sizeof)operator(()ident(nums)operator(\)) operator(/) keyword(sizeof)operator(()ident(nums)operator([)integer(0)operator(]\);)
ident(std)operator(::)ident(vector)operator(<)predefined_type(double)operator(>) ident(numsv)operator(()ident(nums)operator(,) ident(nums) operator(+) ident(arrsize)operator(\);)
ident(trunc_em)operator(()ident(numsv)operator(\);)
operator(})
comment(// ----)
directive(void) ident(trunc_em)operator(()ident(std)operator(::)ident(vector)operator(<)predefined_type(double)operator(>&) ident(arr)operator(\))
operator({)
comment(// Replace each element with the value returned by applying 'floor' to that element)
ident(std)operator(::)ident(transform)operator(()ident(arr)operator(.)ident(begin)operator((\),) ident(arr)operator(.)ident(end)operator((\),) ident(arr)operator(.)ident(begin)operator((\),) ident(floor)operator(\);)
operator(})
comment(// @@PLEAC@@_10.2)
comment(// Variables declared within a function body are local to that function, and those declared)
comment(// outside a function body [and not as part of a class / struct definition, or enclosed within)
comment(// a namespace] are global, that is, are visible throughout the executable unless their)
comment(// visibility has been restricted to the source file in which they are defined via enclosing)
comment(// them within an anonymous namespace [which has the same effect as using the 'static' keyword,)
comment(// in this same context, in the C language])
preprocessor(#include) include(<vector>)
directive(void) ident(somefunc)operator((\))
operator({)
comment(// All these variables are local to this function)
predefined_type(int) ident(variable)operator(,) ident(another)operator(;)
ident(std)operator(::)ident(vector)operator(<)predefined_type(int)operator(>) ident(vec)operator(()integer(5)operator(\);)
operator(;) comment(// ...)
operator(})
comment(// ----------------------------)
comment(// A couple of generic, type-safe type conversion helpers. The Boost Library sports a conversion)
comment(// library at: http://www.boost.org/libs/conversion/index.html)
preprocessor(#include) include(<sstream>)
preprocessor(#include) include(<string>)
keyword(class) class(bad_conversion) operator({};)
keyword(template)operator(<)keyword(typename) ident(T)operator(>) ident(T) ident(fromString)operator(()directive(const) ident(std)operator(::)predefined_type(string)operator(&) ident(s)operator(\))
operator({)
ident(std)operator(::)ident(istringstream) ident(iss)operator(()ident(s)operator(\);)
ident(T) ident(t)operator(;) ident(iss) operator(>>) ident(t)operator(;)
keyword(if) operator((!)ident(iss)operator(\)) keyword(throw) ident(bad_conversion)operator((\);)
keyword(return) ident(t)operator(;)
operator(})
keyword(template)operator(<)keyword(typename) ident(T)operator(>) ident(std)operator(::)predefined_type(string) ident(toString)operator(()directive(const) ident(T)operator(&) ident(t)operator(\))
operator({)
ident(std)operator(::)ident(ostringstream) ident(oss)operator(;)
ident(oss) operator(<<) ident(t) operator(<<) ident(std)operator(::)ident(ends)operator(;)
keyword(if) operator((!)ident(oss)operator(\)) keyword(throw) ident(bad_conversion)operator((\);)
keyword(return) ident(std)operator(::)predefined_type(string)operator(()ident(oss)operator(.)ident(str)operator((\)\);)
operator(})
comment(// ------------)
preprocessor(#include) include(<string>)
comment(// File scope variables)
keyword(namespace)
operator({)
ident(std)operator(::)predefined_type(string) ident(name)operator(;)
predefined_type(int) ident(age)operator(,) ident(c)operator(,) ident(condition)operator(;)
operator(})
directive(void) ident(run_check)operator((\);)
directive(void) ident(check_x)operator(()predefined_type(int) ident(x)operator(\);)
comment(// ----)
comment(// An alternative, C++-specific approach, to command-line handling and type conversion)
comment(// may be seen at: http://www.boost.org/libs/conversion/lexical_cast.htm)
predefined_type(int) ident(main)operator(()predefined_type(int) ident(argc)operator(,) predefined_type(char)operator(*) ident(argv)operator([]\))
operator({)
ident(name)operator(.)ident(assign)operator(()ident(argv)operator([)integer(1)operator(]\);)
keyword(try)
operator({)
ident(age) operator(=) ident(fromString)operator(<)predefined_type(int)operator(>()ident(argv)operator([)integer(2)operator(]\);)
operator(})
keyword(catch) operator(()directive(const) ident(bad_conversion)operator(&) ident(e)operator(\))
operator({)
operator(;) comment(// ... handle conversion error ...)
operator(})
ident(check_x)operator(()ident(age)operator(\);)
operator(})
comment(// ------------)
directive(void) ident(run_check)operator((\))
operator({)
comment(// Full access to file scope variables)
ident(condition) operator(=) integer(1)operator(;)
comment(// ...)
operator(})
directive(void) ident(check_x)operator(()predefined_type(int) ident(x)operator(\))
operator({)
comment(// Full access to file scope variables)
ident(std)operator(::)predefined_type(string) ident(y)operator(()string<delimiter(")content(whatever)delimiter(")>operator(\);)
ident(run_check)operator((\);)
comment(// 'condition' updated by 'run_check')
keyword(if) operator(()ident(condition)operator(\))
operator({)
operator(;) comment(// ...)
operator(})
operator(})
comment(// @@PLEAC@@_10.3)
comment(// Standard C++, owing to its C heritage, allows the creation of 'persistent private variables',)
comment(// via use of the 'static' keyword. For more details about this, and illustrative code examples,)
comment(// refer to this same section in PLEAC-C/Posix/GNU. Standard C++-specific methods of perfoming)
comment(// this task involve use of the 'namespace' facility, or creating a class containing 'static')
comment(// members and using access specifiers to restrict access)
comment(// This example replaces the 'static' keyword with use of an anonymous namespace to force)
comment(// 'variable' to have file scope, and be visible only within the 'mysubs.cpp file. It is)
comment(// therefore both persistant [because it is a global variable] and private [because it is)
comment(// visible only to functions defined within the same source file])
comment(// File: 'mysubs.h')
directive(void) ident(mysub)operator(()directive(void)operator(\);)
directive(void) ident(reset)operator(()directive(void)operator(\);)
comment(// ----)
comment(// File: 'mysubs.cpp')
keyword(namespace)
operator({)
predefined_type(int) ident(variable) operator(=) integer(1)operator(;)
operator(})
directive(void) ident(mysub)operator(()directive(void)operator(\))
operator({)
operator(;) comment(// ... do something with 'variable' ...)
operator(})
directive(void) ident(reset)operator(()directive(void)operator(\)) operator({) ident(variable) operator(=) integer(1)operator(;) operator(})
comment(// ----)
comment(// File: 'test.cpp')
preprocessor(#include) include("mysubs.h")
predefined_type(int) ident(main)operator((\))
operator({)
comment(// 'variable' is not accessable here)
comment(// Call 'mysub', which can access 'variable')
ident(mysub)operator((\);)
comment(// Call 'reset' which sets 'variable' to 1 )
ident(reset)operator((\);)
operator(})
comment(// ------------)
comment(// This example is similar to the previous one in using an anonymous namespace to restrict)
comment(// variable visibility. It goes further, hoewever, grouping logically related items within)
comment(// a named namespace, thus ensuring access to those items is controlled [i.e. requires)
comment(// qualification, or a 'using' declaration or directive])
comment(// File: 'counter.h')
keyword(namespace) ident(cnt)
operator({)
predefined_type(int) ident(increment)operator((\);)
predefined_type(int) ident(decrement)operator((\);)
operator(})
comment(// ----)
comment(// File: 'counter.cpp')
keyword(namespace) ident(cnt)
operator({)
comment(// Ensures 'counter' is visible only within the current source file)
keyword(namespace) operator({) predefined_type(int) ident(counter) operator(=) integer(0)operator(;) operator(})
directive(void) ident(reset)operator(()predefined_type(int) ident(v) operator(=) integer(0)operator(\)) operator({) ident(counter) operator(=) ident(v)operator(;) operator(})
predefined_type(int) ident(increment)operator((\)) operator({) keyword(return) operator(++)ident(counter)operator(;) operator(})
predefined_type(int) ident(decrement)operator((\)) operator({) keyword(return) operator(--)ident(counter)operator(;) operator(})
operator(})
comment(// ----)
comment(// File: 'test.cpp')
preprocessor(#include) include(<iostream>)
preprocessor(#include) include("counter.h")
predefined_type(int) ident(main)operator((\))
operator({)
comment(// Following line is illegal because 'cnt::counter' is private to the 'counter.cpp' file)
comment(// int c = cnt::counter;)
predefined_type(int) ident(a) operator(=) ident(cnt)operator(::)ident(increment)operator((\);)