📄 awkc++.ps
字号:
( pri-)1 171( The)1 207( to work with.)3 567(Both of these uses require that generated code be especially readable and easy)12 3125 4 1186 7020 t
( as possible to)3 585(mary goal throughout the experiment has been that the translated output should be as close)14 3735 2 936 7140 t
(the original)1 476 1 936 7260 t
8 R f
(AWK)1455 7260 w
10 R f
( means that we want to define variables that have the semantics of)12 2855(input. This)1 477 2 1689 7260 t
8 R f
(AWK)5065 7260 w
cleartomark
showpage
saveobj restore
end
%%PageBoundingBox: 83 50 536 691
%%EndPage: 1 1
%%Page: 2 2
%%PageBoundingBox: (atend)
DpostDict begin
/saveobj save def
mark
2 pagesetup
10 R f
( in the natural syntax in expressions with the usual C operators, C and)13 2909(variables and use them)3 942 2 936 840 t
8 R f
(AWK)4821 840 w
10 R f
(func-)5046 840 w
(tions, and C built-in data types.)5 1268 1 936 960 t
8 R f
(AWK)2247 960 w
10 R f
( in many)2 358(syntax is close enough to C to make an exact match feasible)11 2432 2 2466 960 t
(places, and provide a reasonable mapping in others.)7 2065 1 936 1080 t
(It is necessary to duplicate)4 1095 1 1186 1210 t
8 R f
(AWK)2315 1210 w
10 R f
( is already an excellent)4 954( There)1 292(semantics, but that is not sufficient.)5 1469 3 2541 1210 t
8 R f
(AWK)936 1330 w
10 R f
( that exactly matches the semantics of)6 1574(to C converter, that generates C)5 1319 2 1163 1330 t
8 R f
(AWK)4091 1330 w
10 R f
( speaking, an)2 544(. Roughly)1 430 2 4282 1330 t
( about twice as fast as the corresponding)7 1644(program will run)2 683 2 936 1450 t
8 R f
(AWK)3292 1450 w
10 R f
(program, so if one wishes only to speed up)8 1744 1 3512 1450 t
(an existing)1 445 1 936 1570 t
8 R f
(AWK)1415 1570 w
10 R f
( output of however, was never meant for human con-)9 2189( The)1 213(program, is quite satisfactory.)3 1214 3 1640 1570 t
(sumption, and thus it is not appropriate for augmenting or extending)10 2729 1 936 1690 t
8 R f
(AWK)3690 1690 w
10 R f
(code.)3906 1690 w
( development was carried on in parallel, since they are)9 2271( Their)1 277(This project has several components.)4 1522 3 1186 1820 t
( separate these as much as possible)6 1439( have tried to)3 548( I)1 91(related and activities in one area affect the other areas.)9 2242 4 936 1940 t
( is a sketch of the pieces and events.)8 1444( Here)1 243(but there is still room for confusion.)6 1441 3 936 2060 t
(The)1186 2190 w
8 I f
(AWK)1375 2190 w
10 I f
(interpreter)1578 2190 w
10 R f
( For)1 199( originally written in 1977 and much modified since then.)9 2394(is a C program)3 618 3 2045 2190 t
( the interpreter)2 595(most people,)1 516 2 936 2310 t
10 I f
(is)2076 2310 w
8 R f
(AWK)2167 2310 w
10 R f
( first step was to translate the interpreter into the C subset of C++)13 2664(. The)1 234 2 2358 2310 t
( version of the interpreter)4 1026( This)1 231( make some minor changes in implementation to use C++ better.)10 2615(and then to)2 448 4 936 2430 t
( also no need to make this a C++ program)9 1728( was)1 187( There)1 289(could \(but does not\) replace the standard C version.)8 2116 4 936 2550 t
(but it was good practice.)4 979 1 936 2670 t
( modify the interpreter so that instead of interpreting)8 2137(The second step was to)4 942 2 1186 2800 t
8 R f
(AWK)4290 2800 w
10 R f
(programs, it trans-)2 745 1 4511 2800 t
(lates them into C++; this program is called the)8 1856 1 936 2920 t
10 I f
(translator)2817 2920 w
10 R f
(and the C++ it produces is called)6 1316 1 3243 2920 t
10 I f
(generated code.)1 637 1 4584 2920 t
10 R f
( compiler\))1 425(The generated code does not stand alone; it assumes that it will be compiled \(by a C++)16 3645 2 1186 3050 t
(with a)1 248 1 936 3170 t
10 I f
(header file)1 431 1 1210 3170 t
10 R f
(that contains class declarations for)4 1375 1 1666 3170 t
8 R f
(AWK)3066 3170 w
10 R f
(data types and loaded with a)5 1134 1 3282 3170 t
10 I f
(library)4441 3170 w
10 R f
(of separately)1 512 1 4744 3170 t
( the third step is)4 651( Thus)1 253( regular-expression matching, etc.)3 1364(compiled functions for input-output, field splitting,)5 2052 4 936 3290 t
( are written in C++.)4 786( These)1 288(the development of this header file and library.)7 1874 3 936 3410 t
( actions or provides)3 824(The translator, header file, and library are interdependent, since each performs)10 3246 2 1186 3540 t
( are often trade-offs among them, since one can do more work in)12 2633( There)1 287(services that the others depend on.)5 1400 3 936 3660 t
( be traded off are complexity, efficiency,)6 1682( the issues that can)4 781( Among)1 358(one place to simplify life in another.)6 1499 4 936 3780 t
(and readability of generated code.)4 1354 1 936 3900 t
10 B f
(2. Translation)1 626 1 936 4140 t
10 R f
(The translator parses an)3 977 1 1186 4270 t
8 R f
(AWK)2197 4270 w
10 R f
( it)1 91(program creates a parse tree and walks it recursively; at each node)11 2743 2 2422 4270 t
( simple use of C++ facilities;)5 1184( translator makes only relatively)4 1311( The)1 211(calls a routine that generates C++ code.)6 1614 4 936 4390 t
(its origin as a C program is very clear.)8 1528 1 936 4510 t
(The generated code is meant to be compiled with a header file)11 2579 1 1186 4640 t
10 CW f
(Awk.h)3799 4640 w
10 R f
( next)1 207(, to be discussed in the)5 950 2 4099 4640 t
( generated code is based on this template:)7 1661( The)1 205(section, and loaded with a library of run-time routines.)8 2180 3 936 4760 t
9 CW f
(#include "Awk.h")1 864 1 1224 4930 t
(// declarations of user variables and functions, if any)8 2970 1 1224 5095 t
(main\(\))1224 5260 w
({)1224 5370 w
( if there is a BEGIN block)6 1404(BEGIN\(\); //)1 972 2 1656 5480 t
(while \(getline\(\) > 0\) {)4 1242 1 1656 5590 t
(// code for pattern-action statement 1)5 2052 1 2088 5700 t
(// code for pattern-action statement 2)5 2052 1 2088 5810 t
(// ...)1 324 1 2088 5920 t
(})1656 6030 w
( if there is an END block)6 1350(END\(\); //)1 972 2 1656 6140 t
(})1224 6250 w
(// user function definitions, if any)5 1944 1 1224 6415 t
10 R f
( example, no redundant paren-)4 1228( For)1 192(Considerable effort has been devoted to generating readable code.)8 2650 3 1186 6605 t
( produced around non-compound state-)4 1617(theses are produced for expressions, and no redundant braces are)9 2703 2 936 6725 t
( output is piped through the C beautifier)7 1623(ments. The)1 472 2 936 6845 t
10 CW f
(cb)3059 6845 w
10 R f
( external)1 350( of)1 111( Declarations)1 557(so it is properly indented.)4 1031 4 3207 6845 t
( arguments and multiple)3 999( Default)1 359( so that variables are easy to find in large programs.)10 2165(variables are sorted)2 797 4 936 6965 t
( like)1 183(declarations are used for functions)4 1402 2 936 7085 t
10 CW f
(substr)2554 7085 w
10 R f
(and)2947 7085 w
10 CW f
(split)3124 7085 w
10 R f
(that can be called with different numbers of)7 1799 1 3457 7085 t
(arguments.)936 7205 w
cleartomark
showpage
saveobj restore
end
%%PageBoundingBox: 83 56 536 728
%%EndPage: 2 2
%%Page: 3 3
%%PageBoundingBox: (atend)
DpostDict begin
/saveobj save def
mark
3 pagesetup
10 R f
(There are some)2 628 1 1186 840 t
8 R f
(AWK)1846 840 w
10 R f
( example,)1 396( For)1 197(notations that simply cannot be made to look the same in C++.)11 2594 3 2069 840 t
(there is no C exponentiation operator, so)6 1647 1 936 960 t
10 CW f
(x\303y)2612 960 w
10 R f
(becomes)2821 960 w
10 CW f
(pow\(x,y\))3198 960 w
10 R f
( alternative, overloading the C)4 1224(. \(The)1 266 2 3678 960 t
10 CW f
(\303)5196 960 w
10 R f
(exclusive operator, must be rejected because its precedence does not match the)11 3213 1 936 1080 t
8 R f
(AWK)4180 1080 w
10 R f
(precedence for expo-)2 854 1 4402 1080 t
( is no explicit operator for concatenation in)7 1757(nentiation.\) There)1 745 2 936 1200 t
8 R f
(AWK)3467 1200 w
10 R f
(; the translator generates a function call)6 1598 1 3658 1200 t
( right)1 230(instead of overloading some operator, because there is no suitably mnemonic C operator with the)14 4090 2 936 1320 t
( with)1 211( common cases of concatenation of two or three strings are handled)11 2790(precedence. The)1 686 3 936 1440 t
10 CW f
(cat\(s1,s2\))4656 1440 w
10 R f
(and)936 1560 w
10 CW f
(cat\(s1,s2,s3\))1126 1560 w
10 R f
( regular expressions)2 842( Constant)1 427(; longer concatenations use nested function calls.)6 2081 3 1906 1560 t
( slashes, and an extra layer of backslashes must be added to protect)12 2740(must be delimited by quotes instead of)6 1580 2 936 1680 t
(embedded backslashes.)1 931 1 936 1800 t
(The notation for fields is a problem, since there is no way to use `)14 2635 1 1186 1930 t
10 CW f
($)3821 1930 w
10 R f
(' as)1 142 1 3881 1930 t
8 R f
(AWK)4049 1930 w
10 R f
( some exper-)2 519(does. After)1 470 2 4267 1930 t
(imentation, I decided to use the variable)6 1642 1 936 2050 t
10 CW f
(F)2610 2050 w
10 R f
(so that fields are called)4 948 1 2702 2050 t
10 CW f
(F\(0\))3682 2050 w
10 R f
(,)3922 2050 w
10 CW f
(F\(i+1\))3979 2050 w
10 R f
( are)1 152( There)1 288( so on.)2 276(, and)1 201 4 4339 2050 t
(also definitions for)2 755 1 936 2170 t
10 CW f
(F0)1716 2170 w
10 R f
(,)1836 2170 w
10 CW f
(F1)1886 2170 w
10 R f
(, etc., so that an expression like)6 1251 1 2006 2170 t
10 CW f
($1 > $2)2 350 1 3282 2170 t
10 R f
(can be expressed as)3 783 1 3657 2170 t
10 CW f
(F1 > F2)2 350 1 4465 2170 t
10 R f
(.)4815 2170 w
( This)1 231(A small example will illustrate many of these points.)8 2137 2 1186 2300 t
8 R f
(AWK)3582 2300 w
10 R f
( and)1 173(program reads a list of numbers)5 1282 2 3801 2300 t
(prints the list with serial numbers and percentage of the total:)10 2447 1 936 2420 t
9 CW f
({ x[NR] = $1; sum += $1 })7 1350 1 1440 2590 t
(END { if \(sum != 0\))5 1026 1 1224 2810 t
(for \(i = 1; i <= NR; i++\))7 1350 1 1764 2920 t
(printf\("%2d %10.2f %5.1f\\n", i, x[i], 100*x[i]/sum\))5 2754 1 1980 3030 t
(})1440 3140 w
10 R f
(The translator generates this code:)4 1369 1 936 3320 t
9 CW f
(#include <stdio.h>)1 972 1 1224 3490 t
(#include "Awk.h")1 864 1 1224 3600 t
(int i;)1 540 1 1224 3765 t
(double sum;)1 648 1 1224 3875 t
(Array x;)1 540 1 1224 3985 t
(void END\(\);)1 756 1 1224 4095 t
(main\(int argc, char *argv[]\))3 1512 1 1224 4260 t
({)1224 4370 w
(Awkinit\(argc, argv\);)1 1080 1 1440 4480 t
(while \(getline\(\) > 0\) {)4 1242 1 1440 4590 t
(x[NR] = F1;)2 594 1 1656 4700 t
(sum += F1;)2 540 1 1656 4810 t
(})1440 4920 w
(END\(\);)1440 5030 w
(})1224 5140 w
(void END\(\))1 540 1 1224 5305 t
({)1224 5415 w
(if \(sum != 0\))3 702 1 1440 5525 t
(for \(i = 1; i <= NR; i++\))7 1350 1 1656 5635 t
(printf\("%2d %10.2f %5.1f\\n", i, \(double\) x[i], 100 * x[i] / sum\);)10 3510 1 1872 5745 t
(})1224 5855 w
10 R f
( an)1 125(The basic type is)3 689 2 1186 6045 t
10 CW f
(Awk)2031 6045 w
10 R f
(, which captures the semantics of)5 1355 1 2211 6045 t
8 R f
(AWK)3597 6045 w
10 R f
(variables mentioned in Section 1: a)5 1437 1 3819 6045 t
( general, most variables in an)5 1190( In)1 138(string value, a numeric value, or both, depending on usage.)9 2405 3 936 6165 t
8 R f
(AWK)4698 6165 w
10 R f
(program)4918 6165 w
(would be translated into)3 963 1 936 6285 t
10 CW f
(Awk)1924 6285 w
10 R f
(s.)2104 6285 w
( translator attempts to infer the simplest)6 1636(Since it is more efficient to use built-in types, however, the)10 2434 2 1186 6415 t
( example, since the variable)4 1119( For)1 191( will serve for each variable.)5 1145(type that)1 350 4 936 6535 t
10 CW f
(i)3768 6535 w
10 R f
(is used only as the index of a loop,)8 1401 1 3855 6535 t
(it can be an)3 472 1 936 6655 t
10 CW f
(int)1438 6655 w
10 R f
(, while)1 277 1 1618 6655 t
10 CW f
(sum)1925 6655 w
10 R f
(is a)1 141 1 2135 6655 t
10 CW f
(double)2306 6655 w
10 R f
(, and)1 199 1 2666 6655 t
10 CW f
(x)2895 6655 w
10 R f
(is an)1 192 1 2985 6655 t
10 CW f
(Array)3208 6655 w
10 R f
(, a type that captures the notion of an)8 1526 1 3508 6655 t
8 R f
(AWK)5065 6655 w
10 R f
(array, i.e., an indexable collection of)5 1485 1 936 6775 t
10 CW f
(Awk)2450 6775 w
10 R f
( coercion)1 372(s. The)1 273 2 2630 6775 t
10 CW f
(\(double\))3304 6775 w
10 R f
(in the)1 229 1 3813 6775 t
10 CW f
(printf)4071 6775 w
10 R f
(is necessary to con-)3 796 1 4460 6775 t
(vert the array element, of type)5 1227 1 936 6895 t
10 CW f
(Awk)2193 6895 w
10 R f
( is no need for a coercion for)7 1184( There)1 287(, to a number for printing.)5 1060 3 2373 6895 t
10 CW f
(i)4935 6895 w
10 R f
(, how-)1 261 1 4995 6895 t
( to more readable code and poten-)6 1387(ever, because its type already matches; in this case, type inference leads)11 2933 2 936 7015 t
(tially more efficient code.)3 1031 1 936 7135 t
(Type inference in the translator is fairly)6 1615 1 1186 7265 t
10 I f
(ad hoc.)1 299 1 2831 7265 t
10 R f
( are)1 152( Types)1 300(A type is associated with each constant.)6 1619 3 3185 7265 t
cleartomark
showpage
saveobj restore
end
%%PageBoundingBox: 83 50 548 728
%%EndPage: 3 3
%%Page: 4 4
%%PageBoundingBox: (atend)
DpostDict begin
/saveobj save def
mark
4 pagesetup
10 R f
(combined at operators to produce a result type; for instance, the result of an addition is an)16 3788 1 936 840 t
10 CW f
(int)4762 840 w
10 R f
(if both)1 276 1 4980 840 t
(operands are integer; otherwise it is)5 1452 1 936 960 t
10 CW f
(double.)2419 960 w
10 R f
(Relational operators always produce)3 1473 1 2870 960 t
10 CW f
(int)4374 960 w
10 R f
(regardless of the)2 671 1 4585 960 t
( of variables are set by assignment statements and also by usage in expres-)13 3092( Types)1 303(type of their operands.)3 925 3 936 1080 t
( based on the oper-)4 771( operands of an operator are assigned a tentative type)9 2137( The)1 207(sions and function arguments.)3 1205 4 936 1200 t
(ator; for example, the expression)4 1375 1 936 1320 t
10 CW f
(x+y)2352 1320 w
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -