⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 awkc++.ps

📁 c programming pearls answer
💻 PS
📖 第 1 页 / 共 5 页
字号:
10 CW f
(printf)1708 5430 w
10 R f
(and)2110 5430 w
10 CW f
(sprintf)2296 5430 w
10 R f
( first argument is scanned for format conversion)7 2047( the)1 164(? Here,)1 329 3 2716 5430 t
( the type of the expression doesn't)6 1382( If)1 118( list.)1 175(characters that are used to infer the type of each expression in the)12 2645 4 936 5550 t
( for)1 156( So,)1 196( possible.)1 393(match the conversion character, a cast is generated, avoiding redundant parentheses if)11 3575 4 936 5670 t
(example, the)1 510 1 936 5790 t
8 R f
(AWK)1471 5790 w
10 R f
(statement)1687 5790 w
9 CW f
(printf\("%s %d %f\\n", $1, $2+1, 123.4\))5 1998 1 1224 5960 t
10 R f
(generates)936 6140 w
9 CW f
(printf\("%s %d %f\\n", \(cchar *\) F1, \(int\)\(F2 + 1\), 123.4\);)9 3078 1 1224 6310 t
10 R f
(The casts are unattractive but there seems to be no better solution.)11 2634 1 936 6490 t
10 B f
(7. Library)1 464 1 936 6730 t
10 R f
( to implement the operators, or)5 1322(Most of the run-time library comprises either functions necessary)8 2748 2 1186 6860 t
( the interpreter for regular-expression matching, input and output, field)9 2957(transliterations of functions from)3 1363 2 936 6980 t
( Field)1 258( in interfaces.)2 544( of these are much the same, although there are some changes)11 2471( Most)1 257(splitting, and so on.)3 790 5 936 7100 t
( most different, since it now has to interface to the different field-handling)12 3288(splitting is probably the)3 1032 2 936 7220 t
cleartomark
showpage
saveobj restore
end
%%PageBoundingBox: 83 54 548 728
%%EndPage: 8 8
%%Page: 9 9
%%PageBoundingBox: (atend)
DpostDict begin
/saveobj save def
mark
9 pagesetup
10 R f
( library also includes definitions for built-in variables like)8 2365( The)1 212(implementation described above.)2 1337 3 936 840 t
10 CW f
(NR)4882 840 w
10 R f
(and a)1 221 1 5035 840 t
(routine)936 960 w
10 CW f
(Awkinit)1244 960 w
10 R f
(to set up the)3 486 1 1689 960 t
10 CW f
(ARGC)2200 960 w
10 R f
(and)2465 960 w
10 CW f
(ARGV)2634 960 w
10 R f
(variables.)2899 960 w
( defining the)2 511( Consider)1 414( the library echoes a previous problem.)6 1573(One problem arising with)3 1030 4 1186 1090 t
8 R f
(AWK)4742 1090 w
10 R f
(built-in)4961 1090 w
(function)936 1210 w
10 CW f
(length)1304 1210 w
10 R f
( obvious)1 351( The)1 214( value of a variable.)4 825(, which returns the number of characters in the string)9 2202 4 1664 1210 t
(implementation is)1 720 1 936 1330 t
9 CW f
(inline int length\(const Awk &a\) { return strlen\(a\); })8 2862 1 1224 1500 t
10 R f
(Thus)936 1680 w
10 CW f
(length)1173 1680 w
10 R f
( type; a constructor will convert this to an)8 1769(may be called with any)4 974 2 1570 1680 t
10 CW f
(Awk)4351 1680 w
10 R f
(, and)1 207 1 4531 1680 t
10 CW f
(operator)4776 1680 w
(const char*)1 625 1 936 1800 t
10 R f
(will be called implicitly to coerce the string value for)9 2122 1 1586 1800 t
10 CW f
(strlen)3733 1800 w
10 R f
(.)4093 1800 w
( that calling a constructor is more costly than might be expected; in fact, even)14 3364(One drawback is)2 706 2 1186 1930 t
(though my constructors are all declared)5 1597 1 936 2050 t
10 CW f
(inline)2562 2050 w
10 R f
( The)1 208( are too complex.)3 702(, some are not inlined because they)6 1424 3 2922 2050 t
( obviates the problems)3 920( This)1 233(alternative implementation is again to write out every possible type explicitly.)10 3167 3 936 2170 t
( generalizes poorly to functions that have more than one)9 2279(of constructors and unimplemented features, but it)6 2041 2 936 2290 t
(argument, such as)2 718 1 936 2410 t
10 CW f
(cat)1679 2410 w
10 R f
(or)1884 2410 w
10 CW f
(substr)1992 2410 w
10 R f
(.)2352 2410 w
10 B f
(8. Status)1 392 1 936 2650 t
10 R f
(Most of)1 325 1 1186 2780 t
8 R f
(AWK)1547 2780 w
10 R f
( known bugs and constructions that)5 1473( are a handful of)4 697( There)1 293(can be handled properly.)3 1019 4 1774 2780 t
(may never work; some of these are intrinsic to the way that I have made trade-offs.)15 3314 1 936 2900 t
( the program)2 510( Consider)1 411(Type inference creates some problems.)4 1559 3 1186 3030 t
9 CW f
($1 > $2 { i = NR })7 972 1 1224 3200 t
(END { print i })4 810 1 1224 3310 t
10 R f
(This generates)1 579 1 936 3490 t
9 CW f
(int i;)1 540 1 1224 3660 t
(void END\(\);)1 756 1 1224 3770 t
(main\(int argc, char *argv[]\))3 1512 1 1224 3935 t
({)1224 4045 w
(Awkinit\(argc, argv\);)1 1080 1 1656 4155 t
(while \(getline\(\) > 0\) {)4 1242 1 1656 4265 t
(if \(F1 > F2\))3 648 1 2088 4375 t
(i = NR;)2 378 1 2520 4485 t
(})1656 4595 w
(END\(\);)1656 4705 w
(})1224 4815 w
(void END\(\))1 540 1 1224 4980 t
({)1224 5090 w
(print\(i\);)1656 5200 w
(})1224 5310 w
10 R f
(Notice that the type of)4 897 1 936 5490 t
10 CW f
(i)1859 5490 w
10 R f
(has been inferred as)3 797 1 1945 5490 t
10 CW f
(int)2768 5490 w
10 R f
( however, that)2 570(. Suppose,)1 440 2 2948 5490 t
10 CW f
($1)3985 5490 w
10 R f
(is never greater than)3 817 1 4132 5490 t
10 CW f
($2)4976 5490 w
10 R f
(. In)1 160 1 5096 5490 t
(the interpreter, the variable)3 1089 1 936 5610 t
10 CW f
(i)2053 5610 w
10 R f
( the compiled code, how-)4 1020( In)1 135( will be null.)3 512(will have a null value, so the output)7 1448 4 2141 5610 t
(ever, since)1 431 1 936 5730 t
10 CW f
(i)1398 5730 w
10 R f
(is an)1 192 1 1489 5730 t
10 CW f
(int)1712 5730 w
10 R f
(, the output will be a literal)6 1116 1 1892 5730 t
10 CW f
(0)3039 5730 w
10 R f
( it better to do)4 585( Is)1 128( is an example of a trade-off.)6 1185(. This)1 259 4 3099 5730 t
(type inference and get this one wrong, or not to do it and produce less readable code that runs more slowly?)20 4300 1 936 5850 t
( example, the standard version of)5 1335( For)1 191(There are minor problems with name clashes.)6 1833 3 1186 5980 t
10 CW f
(rand)4573 5980 w
10 R f
(has differ-)1 415 1 4841 5980 t
(ent properties from the)3 975 1 936 6100 t
8 R f
(AWK)1957 6100 w
10 R f
(version, which uses the name)4 1257 1 2194 6100 t
10 CW f
(Arand)3497 6100 w
10 R f
( problems with)2 640( are similar)2 491(. There)1 328 3 3797 6100 t
10 CW f
(system)936 6220 w
10 R f
(,)1296 6220 w
10 CW f
(sprintf)1346 6220 w
10 R f
(, C++ keywords, and probably others that I haven't stumbled into yet.)11 2787 1 1766 6220 t
(The header file)2 635 1 1186 6350 t
10 CW f
(Awk.h)1862 6350 w
10 R f
( and the library is 1275, including comments and some)9 2346(is 625 lines long)3 707 2 2203 6350 t
( comparison,)1 520( For)1 194( translator is 3700 lines.)4 978( The)1 210(debugging code but excluding regular expression matching.)6 2418 5 936 6470 t
(the interpreter in C is 4900 lines.)6 1313 1 936 6590 t
( On)1 183( which comparisons are most representative.)5 1829(Performance is mixed, and it is difficult to decide)8 2058 3 1186 6720 t
( while on some others, it is)6 1135(some test cases, the compiled code is significantly faster than the interpreter,)11 3185 2 936 6840 t
( example, the prototypical)3 1043( For)1 189(somewhat slower.)1 721 3 936 6960 t
8 R f
(AWK)2914 6960 w
10 R f
(program is to compute a word-frequency count:)6 1907 1 3130 6960 t
cleartomark
showpage
saveobj restore
end
%%PageBoundingBox: 83 80 536 728
%%EndPage: 9 9
%%Page: 10 10
%%PageBoundingBox: (atend)
DpostDict begin
/saveobj save def
mark
10 pagesetup
9 CW f
({ for \(i = 1; i <= NF; i++\) count[$i]++ })10 2214 1 1224 830 t
(END { for \(i in count\) printf\("%4d %s\\n", count[i], i\) })10 3024 1 1224 940 t
10 R f
(The code generated for this is)5 1183 1 936 1120 t
9 CW f
(#include <stdio.h>)1 972 1 1224 1290 t
(#include "Awk.h")1 864 1 1224 1400 t
(Array count;)1 756 1 1224 1620 t
(Index i;)1 540 1 1224 1730 t
(void END\(\);)1 756 1 1224 1840 t
(main\(int argc, char *argv[]\))3 1512 1 1224 2005 t
({)1224 2115 w
(Awkinit\(argc, argv\);)1 1080 1 1656 2225 t
(while \(getline\(\) > 0\) {)4 1242 1 1656 2335 t
(for \(i = 1; i <= NF; i++\))7 1350 1 2088 2445 t
(count[F\(i\)]++;)2520 2555 w
(})1656 2665 w
(END\(\);)1656 2775 w
(})1224 2885 w
(void END\(\))1 540 1 1224 3050 t
({)1224 3160 w
(For \(i, count\))2 756 1 1656 3270 t
(printf\("%4d %s\\n", \(int\) count[i], \(char *\) i\);)6 2538 1 1872 3380 t
(})1224 3490 w
10 R f
(This can also be expressed in C++ with standard libraries:)9 2317 1 936 3670 t
9 CW f
(#include <String.h>)1 1026 1 1224 3840 t
(#include <Map.h>)1 864 1 1224 3950 t
(#include <stream.h>)1 1026 1 1224 4060 t
(Mapdeclare\(String,int\))1224 4225 w
(Mapimplement\(String,int\))1224 4335 w
(main\(\))1224 4500 w
({)1224 4610 w
(Map\(String,int\) count;)1 1188 1 1656 4720 t
(String word;)1 648 1 1656 4830 t
(while \(cin >> word\))3 1026 1 1656 4995 t
(count[word]++;)2088 5105 w
(Mapiter\(String,int\) p \(count\);)2 1620 1 1656 5215 t
(while \(++p\))1 594 1 1656 5325 t
(cout << dec\(p.value\(\),4\) << " " << p.key\(\) << "\\n";)9 2754 1 2088 5435 t
(})1224 5545 w
10 R f
( of 320,000 bytes with)4 925(The following table shows running times for four implementations, on an input file)12 3395 2 936 5725 t
(about 600 distinct words:)3 1014 1 936 5845 t
10 CW f
(Map)1296 6025 w
10 R f
( sec.)1 177(class 16.9)1 1050 2 1501 6025 t
8 R f
(AWK)1296 6145 w
10 R f
(interpreter 11.5)1 1039 1 1512 6145 t
8 R f
(AWK)1296 6265 w
10 R f
(-C++ 8.1)1 1064 1 1487 6265 t
8 R f
(AWKCC)1296 6385 w
10 R f
(5.7)2426 6385 w
(On another test, a large program that includes representative)8 2408 1 936 6565 t
8 R f
(AWK)3369 6565 w
10 R f
(statements, the results are less favorable:)5 1630 1 3585 6565 t
8 R f
(AWK)1296 6745 w
10 R f
( sec.)1 177(interpreter 117)1 1014 2 1512 6745 t
8 R f
(AWK)1296 6865 w
10 R f
(-C++ 71)1 1039 1 1487 6865 t
8 R f
(AWKCC)1296 6985 w
10 R f
(39)2426 6985 w
( values of Ackermann's func-)4 1197( Computing)1 504( uniformly faster, this is not true.)6 1334(Although it might appear that is)5 1285 4 936 7165 t
(tion up to Ack\(3,5\) shows quite a different picture:)8 2033 1 936 7285 t
cleartomark
showpage
saveobj restore
end
%%PageBoundingBox: 83 48 536 728
%%EndPage: 10 10
%%Page: 11 11
%%PageBoundingBox: (atend)
DpostDict begin
/saveobj save def
mark
11 pagesetup
8 R f
(AWK)1296 900 w
10 R f
( sec.)1 177(interpreter 21.1)1 1039 2 1512 900 t
8 R f
(AWK)1296 1020 w
10 R f
(-C++ 1.8)1 1064 1 1487 1020 t
8 R f
(AWKCC)1296 1140 w
10 R f
(44.5)2376 1140 w
(This stress test of the function calling mechanism appears to show the)11 2794 1 936 1320 t
8 R f
(AWK)3755 1320 w
10 R f
(-C++ translator in its best light.)5 1257 1 3946 1320 t
10 B f
(9. Observations)1 692 1 936 1560 t
10 R f
( even allowing for my learning curve and the fact)9 2024(The job has turned out to be harder than expected,)9 2046 2 1186 1690 t
( part this is because it is difficult to match exactly)10 2007( In)1 135( an oft-interrupted back-burner project.)4 1567(that it has been)3 611 4 936 1810 t
( all, in a new medium; the task would be far easier if I were free to adjust the)18 3086(an existing program, warts and)4 1234 2 936 1930 t
(problem to the solution, rather than being constrained in all directions.)10 2814 1 936 2050 t
( it)1 85( retrospect,)1 446( In)1 136(I spent too much time stumbling around trying to get the overloaded operators right.)13 3403 4 1186 2180 t
( of operands)2 503(is quite trivial, but I kept hoping for some alternative to writing out all possible combinations)15 3817 2 936 2300 t
( be easier if one needed conversions only in one direction, which is the)13 2854( would of course)3 671( This)1 229(and operators.)1 566 4 936 2420 t
( studied)1 315( may also be easier with templates, but I have not)10 1992( It)1 113(only situation that textbooks typically mention.)5 1900 4 936 2540 t
(the issue.)1 372 1 936 2660 t
( as)1 109(There was a similar problem with fields, and a lot of trouble getting the semantics exactly the same)17 3961 2 1186 2790 t
( the)1 156( Until)1 265(the interpreter.)1 596 3 936 2910 t
10 CW f
(lvalue)1987 2910 w
10 R f
(and)2381 2910 w
10 CW f
(rvalue)2559 2910 w
10 R f
(functions were properly in place \(in all places!\) this just)9 2304 1 2952 2910 t
(didn't work.)1 494 1 936 3030 t
( thought through so that one does not incur unnecessary over-)10 2483(Reference parameters must be carefully)4 1587 2 1186 3160 t
( inadvertently modify)2 892(head, create unwanted temporaries \(current C++ implementations warn of this\), or)10 3428 2 936 3280 t
( meaning of)2 479( The)1 206(something that should be untouched.)4 1469 3 936 3400 t
10 CW f
(const)3116 3400 w
10 R f
(for reference arguments is in the hands of the)8 1814 1 3442 3400 t
( desirable to change the value of a)7 1465(implementer; it is quite possible and sometimes)6 2002 2 936 3520 t
10 CW f
(const)4443 3520 w
10 R f
(object. For)1 473 1 4783 3520 t
(example, updating the string or numeric state of an)8 2081 1 936 3640 t
10 CW f
(Awk)3048 3640 w
10 R f
( as the rest of the)5 714(does not change its value as far)6 1283 2 3259 3640 t
(program is concerned, so this is done even for)8 1838 1 936 3760 t
10 CW f
(const)2799 3760 w
10 R f
(reference arguments.)1 835 1 3124 3760 t
( Several)1 360( be careful to respect the levels of abstraction when one is building a data type.)15 3314(One must)1 396 3 1186 3890 t
( caused)1 300(times I inadvertently used an operation on a type from deep within its implementation; this usually)15 4020 2 936 4010 t
( a type in some function that is)7 1244( typical example is using the assignment operator for)8 2122( A)1 123(an infinite recursion.)2 831 4 936 4130 t
(indirectly part of the implementation of that type.)7 1976 1 936 4250 t
( also uncovered perhaps a dozen)5 1313(Although most of the bugs I encountered were of my own making, I)12 2757 2 1186 4380 t
( general, these)2 585( In)1 139(bugs in C++ \(using a development version of cfront\) and a handful of C compiler bugs.)15 3596 3 936 4500 t
( and conversion functions,)3 1069(were in areas where I was pushing hard on type matching, operator overloading,)12 3251 2 936 4620 t
(exercising them in unusual ways.)4 1329 1 936 4740 t
( the game that would be terri-)6 1211( checking finds lots of errors early in)7 1500( Type)1 259(C++ has major advantages.)3 1100 4 1186 4870 t
( linkage extends this checking to separately compiled)7 2196( Type-safe)1 457(ble to find in a conventional C program.)7 1667 3 936 4990 t
( they are obviously mandatory for an)6 1541( and function-name overloading are often a help;)7 2018(routines. Operator)1 761 3 936 5110 t
(exercise like this one.)3 865 1 936 5230 t
( Compilation)1 562(C++ diagnostics are very good, pinpointing errors and often suggesting correct code.)11 3508 2 1186 5360 t
(and loading are slow, but not a serious problem, at least on a fast machine.)14 2976 1 936 5480 t
( of the old mistakes and some)6 1248(As others have observed, C++ is not a panacea: one can make many)12 2822 2 1186 5610 t
( particular, because the meaning of names and

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -