Π‘ΠΈΠ½ΡΠ°ΠΊΡΠΈΡ ΠΎΠΏΡΠ΅Π΄Π΅Π»Π΅Π½ΠΈΡ ΡΠ°ΠΊΠΎΠ²:
function: func ΠΈΠΌΡ () ΠΎΠΏΠ΅ΡΠ°ΡΠΎΡ
procedure: proc ΠΈΠΌΡ() ΠΎΠΏΠ΅ΡΠ°ΡΠΎΡ
ΠΠ΄Π΅ΡΡ ΠΈΠΌΡ ΠΌΠΎΠΆΠ΅Ρ Π±ΡΡΡ ΠΈΠΌΠ΅Π½Π΅ΠΌ Π½Π΅ΠΊΠΎΡΠΎΡΠΎΠΉ ΠΏΠ΅ΡΠ΅ΠΌΠ΅Π½Π½ΠΎΠΉ Π²ΡΡΡΠΎΠ΅Π½Π½ΡΠ΅ ΡΡΠ½ΠΊΡΠΈΠΈ ΠΈΡΠΊΠ»ΡΡΠ°ΡΡΡΡ. ΠΠΏΡΠ΅Π΄Π΅Π»Π΅Π½ΠΈΠ΅, Π²ΠΏΠ»ΠΎΡΡ Π΄ΠΎ ΠΎΡΠΊΡΡΠ²Π°ΡΡΠ΅ΠΉΡΡ ΡΠΊΠΎΠ±ΠΊΠΈ ΠΎΠΏΠ΅ΡΠ°ΡΠΎΡΠ°, Π΄ΠΎΠ»ΠΆΠ½ΠΎ ΠΏΠΎΠΌΠ΅ΡΠ°ΡΡΡΡ Π½Π° ΠΎΠ΄Π½ΠΎΠΉ ΡΡΡΠΎΠΊΠ΅, ΠΊΠ°ΠΊ Π² ΠΏΡΠΈΠ²Π΅Π΄Π΅Π½Π½ΠΎΠΌ Π²ΡΡΠ΅ ΠΎΠΏΠ΅ΡΠ°ΡΠΎΡΠ΅ if.
Π ΠΎΡΠ»ΠΈΡΠΈΠ΅ ΠΎΡ Π‘ΠΈ ΡΠ΅Π»ΠΎ ΡΡΠ½ΠΊΡΠΈΠΈ ΠΈΠ»ΠΈ ΠΏΡΠΎΡΠ΅Π΄ΡΡΡ ΠΌΠΎΠΆΠ΅Ρ Π±ΡΡΡ Π»ΡΠ±ΡΠΌ ΠΎΠΏΠ΅ΡΠ°ΡΠΎΡΠΎΠΌ, Π½Π΅ ΠΎΠ±ΡΠ·Π°ΡΠ΅Π»ΡΠ½ΠΎ ΡΠΎΡΡΠ°Π²Π½ΡΠΌ (Π² ΡΠΊΠΎΠ±ΠΊΠ°Ρ ). ΠΠΎΡΠΊΠΎΠ»ΡΠΊΡ ΡΠΈΠΌΠ²ΠΎΠ» ; Π½Π΅ ΠΈΠΌΠ΅Π΅Ρ ΡΠ²ΠΎΠ΅Π³ΠΎ Π·Π½Π°ΡΠ΅Π½ΠΈΡ Π² hoc, ΠΏΡΡΡΠΎΠ΅ ΡΠ΅Π»ΠΎ ΠΏΡΠΎΡΠ΅Π΄ΡΡΡ ΡΠΎΡΠΌΠΈΡΡΠ΅ΡΡΡ ΠΏΡΡΡΠΎΠΉ ΠΏΠ°ΡΠΎΠΉ ΡΠΊΠΎΠ±ΠΎΠΊ.
Π€ΡΠ½ΠΊΡΠΈΠΈ ΠΈ ΠΏΡΠΎΡΠ΅Π΄ΡΡΡ ΠΏΡΠΈ Π²ΡΠ·ΠΎΠ²Π°Ρ ΠΌΠΎΠ³ΡΡ ΠΏΠΎΠ»ΡΡΠ°ΡΡ Π°ΡΠ³ΡΠΌΠ΅Π½ΡΡ, ΠΎΡΠ΄Π΅Π»Π΅Π½Π½ΡΠ΅ Π·Π°ΠΏΡΡΡΠΌΠΈ. ΠΠ° Π°ΡΠ³ΡΠΌΠ΅Π½ΡΡ ΡΡΡΠ»Π°ΡΡΡΡ ΡΠ°ΠΊ ΠΆΠ΅, ΠΊΠ°ΠΊ Π² shell: $3 ΠΎΡΠ½ΠΎΡΠΈΡΡΡ ΠΊ ΡΡΠ΅ΡΡΠ΅ΠΌΡ, ΠΈΠ½Π΄Π΅ΠΊΡΠΈΡΡΠ΅ΠΌΠΎΠΌΡ, Π½Π°ΡΠΈΠ½Π°Ρ Ρ Π΅Π΄ΠΈΠ½ΠΈΡΡ, Π°ΡΠ³ΡΠΌΠ΅Π½ΡΡ. ΠΠ½ΠΈ ΠΏΠ΅ΡΠ΅Π΄Π°ΡΡΡΡ Π·Π½Π°ΡΠ΅Π½ΠΈΡΠΌΠΈ ΠΈ Π²Π½ΡΡΡΠΈ ΡΡΠ½ΠΊΡΠΈΠΉ ΡΠ΅ΠΌΠ°Π½ΡΠΈΡΠ΅ΡΠΊΠΈ ΡΠΊΠ²ΠΈΠ²Π°Π»Π΅Π½ΡΠ½Ρ ΠΏΠ΅ΡΠ΅ΠΌΠ΅Π½Π½ΡΠΌ. Π‘ΡΡΠ»ΠΊΠ° Π½Π° Π°ΡΠ³ΡΠΌΠ΅Π½Ρ Ρ ΠΏΠΎΠΌΠΎΡΡΡ ΡΠΈΡΠ»Π°, ΠΏΡΠ΅Π²ΡΡΠ°ΡΡΠ΅Π³ΠΎ ΡΠΈΡΠ»ΠΎ Π°ΡΠ³ΡΠΌΠ΅Π½ΡΠΎΠ², ΠΏΠ΅ΡΠ΅Π΄Π°Π½Π½ΡΡ ΠΏΡΠΎΡΠ΅Π΄ΡΡΠ΅, ΡΡΠΈΡΠ°Π΅ΡΡΡ ΠΎΡΠΈΠ±ΠΊΠΎΠΉ. ΠΠΎΠ½ΡΡΠΎΠ»Ρ ΠΎΡΠΈΠ±ΠΎΠΊ Π΄ΠΈΠ½Π°ΠΌΠΈΡΠ΅ΡΠΊΠΈΠΉ, ΠΏΠΎΡΠΊΠΎΠ»ΡΠΊΡ ΠΏΠΎΠ΄ΠΏΡΠΎΠ³ΡΠ°ΠΌΠΌΠ° ΠΌΠΎΠΆΠ΅Ρ ΠΈΠΌΠ΅ΡΡ ΠΏΠ΅ΡΠ΅ΠΌΠ΅Π½Π½ΠΎΠ΅ ΡΠΈΡΠ»ΠΎ ΠΏΠ°ΡΠ°ΠΌΠ΅ΡΡΠΎΠ², Π΅ΡΠ»ΠΈ Π΅Π΅ Π½Π°ΡΠ°Π»ΡΠ½ΡΠ΅ Π°ΡΠ³ΡΠΌΠ΅Π½ΡΡ Π²Π»ΠΈΡΡΡ Π½Π° ΡΡΠΎ ΡΠΈΡΠ»ΠΎ (ΡΠΌ. ΡΡΠ½ΠΊΡΠΈΡ printf Π² Π‘ΠΈ).
Π€ΡΠ½ΠΊΡΠΈΠΈ ΠΈ ΠΏΡΠΎΡΠ΅Π΄ΡΡΡ ΠΌΠΎΠ³ΡΡ Π±ΡΡΡ ΡΠ΅ΠΊΡΡΡΠΈΠ²Π½ΡΠΌΠΈ, Π½ΠΎ ΡΡΠ΅ΠΊ ΠΈΠΌΠ΅Π΅Ρ ΠΎΠ³ΡΠ°Π½ΠΈΡΠ΅Π½Π½ΡΡ Π³Π»ΡΠ±ΠΈΠ½Ρ (ΠΎΠΊΠΎΠ»ΠΎ ΡΠΎΡΠ½ΠΈ Π²ΡΠ·ΠΎΠ²ΠΎΠ²).
ΠΡΠΈΠΌΠ΅ΡΡβ’ ΠΠΈΠΆΠ΅ ΠΏΠΎΠΊΠ°Π·Π°Π½ΠΎ ΠΎΠΏΡΠ΅Π΄Π΅Π»Π΅Π½ΠΈΠ΅ ΡΡΠ½ΠΊΡΠΈΠΈ ΠΠΊΠΊΠ΅ΡΠΌΠ°Π½Π½Π° Π² hoc:
$ hoc
time ack() {
if ($1 == 0) return $2+1
if ($2 ==0) return ack($1 - 1, 1)
return ack($1 - 1, ack($1, $2 - 1))
}
ack(3,2)
29
ack(3,3)
61
ack(3,4)
hoc: ΡΡΠ΅ΠΊ ΡΠ»ΠΈΡΠΊΠΎΠΌ Π²Π΅Π»ΠΈΠΊ (ΡΡΡΠΎΠΊΠ° 8)
...
β’ Π€ΠΎΡΠΌΡΠ»Π° Π‘ΡΠΈΡΠ»ΠΈΠ½Π³Π°:
$ hoc
func stirl() {
return sqrt(2 * $1 * PI) * ($1 / E) ^ $1 * (1 + 1 / (12 * $1))
}
stirl(10) 3628684.7
stirl(20) 2.4328818e+18
β’ Π€ΡΠ½ΠΊΡΠΈΡ ΡΠ°ΠΊΡΠΎΡΠΈΠ°Π» n!
func fac() if ($1 <= 0) return 1 else return $1 * fac($1 - 1)
ΠΡΠ½ΠΎΡΠ΅Π½ΠΈΠ΅ ΡΠ°ΠΊΡΠΎΡΠΈΠ°Π»Π° ΠΊ ΠΏΡΠΈΠ±Π»ΠΈΠΆΠ΅Π½ΠΈΡ Π‘ΡΠΈΡΠ»ΠΈΠ½Π³Π°:
i = 9
while ((i = i + 1) <= 20) {
print i, " ", fac(i)/stirl(i), "\n"
}
10 1.0000318
11 1.0000265
12 1.0000224
13 1.0000192
14 1.0000166
15 1.0000146
16 1.0000128
17 1.0000114
18 1.0000102
19 1.0000092
20 1.0000083
ΠΡΠΈΠ»ΠΎΠΆΠ΅Π½ΠΈΠ΅ 3
ΠΡΡ ΠΎΠ΄Π½ΡΠ΅ ΡΠ΅ΠΊΡΡΡ ΠΊΠ°Π»ΡΠΊΡΠ»ΡΡΠΎΡΠ° hoc
These files contain all the code from "The Unix Programming Environment", by Brian Kernighan and Rob Pike (Prentice Hall, 1984, ISBN 0-13-937681-X). A separate hoc6 distribution contains any fixes that we have applied to that; the version in this file is from the book.
Copyright Β© Lucent Technologies, 1997. All Rights Reserved
Permission to use, copy, modify, and distribute this software and its documentation for any purpose and without fee is hereby granted, provided that the above copyright notice appear in all copies and that both the copyright notice and this permission notice and warranty disclaimer appear in supporting documentation, and that the name of Lucent Technologies or any of its entities not be used in advertising or publicity pertaining to distribution of the software without specific, written prior permission.
LUCENT TECHNOLOGIES DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
3.1 hoc1
3.1.1 makefile
hoc1: hoc.o
cc hoc.o -o hoc1
3.1.2 hoc.y
%{
#define YYSTYPE double /* data type of yacc stack */
%}
%token NUMBER
%left '+' /* left associative, same precedence */
%left '*' '/' /* left assoc., higher precedence */
%%
list : /* nothing */
| list '\n'
| list expr '\n' { printf("\t%.8g\n", $2); }
;
expr: NUMBER { $$ = $1; }
| expr '+' expr { $$ = $1 + $3; }
| expr '-' expr { $$ = $1 - $3; }
| expr '*' expr { $$ = $1 * $3; }
| expr '/' expr { $$ = $1 / $3; }
| '(' expr ')' { $$ = $2; }
;
%%
/* end of grammar */
#include <stdio.h>
#include <ctype.h>
char *progname; /* for error messages */
int lineno = 1;
main(argc, argv) /* hoc1 */
char *argv[];
{
progname = argv[0];
yyparse();
}
yylex() /* hoc1 */
{
int c;
while ((c=getchar()) == ' ' || c == '\t')
;
if (c == EOF)
return 0;
if (c == '.' || isdigit(c)) { /* number */
ungetc(c, stdin);
scanf("%lf", &yylval);
return NUMBER;
if (c == '\n')
lineno++;
return c;
}
yyerror(s) /* called for yacc syntax error */
char *s;
{
warning(s, (char*)0);
}
warning(s, t) /* print warning message */
char *s, *t;
{
fprintf(stderr, "%s: %s", progname, s);
if (t)
fprintf(stderr, " %s", t);
fprintf(stderr, " near line %d\n", lineno);
}
3.1.3 hoc1.y, Π²Π΅ΡΡΠΈΡ 1.5
%{
#define YYSTYPE double /* data type of yacc stack */
%}
%token NUMBER
%left '-' '+'
%left '*' '/'
%left UNARYMINUS
%%
list: /* nothing */
| list '\n'
| list expr '\n' { printf("\t%.8g\n", $2); }
;
expr: NUMBER { $$ = $1; }
| expr '+' expr { $$ = $1 + $3; }
| expr '-' expr { $$ = $1 - $3; }
| expr '*' expr { $$ = $1 * $3; }
| expr '/' expr { $$ = $1 / $3; }
| '-' expr %prec UNARYMINUS { $$ = -$2; } /* new */
| '(' expr ')' { $$ = $2; }
;
%%
/* end of grammar */
#include <stdio.h>
#include <ctype.h>
char *progname; /* for error messages */
int lineno = 1;
main(argc, argv) /* hoc1 */
char *argv[];
{
progname = argv[0];
yyparse();
}
yylex() /* hoc1 */
{
int c;
while ((c=getchar()) == ' ' || Ρ == '\t')
;
if (c == EOF)
return 0;
if (c == '.' || isdigit(c)) { /* number */
ungetc(c, stdin);
scanf("%lf", &yylval);
return NUMBER;
}
if (c == '\n')
lineno++;
return c;
}
yyerror(s)
char *s;
{
warning(s, (char *)0);
}
warning(s, t)
char *s, *t;
{
fprintf(stderr, "%s: %s", progname, s);
if (t && *t)
fprintf(stderr, "%s", t);
fprintf(stderr, " near line %d\n", lineno);
}
3.2 hoc2
3.2.1 hoc.y
%{
double mem[26]; /* memory for variables 'a'..'z' */
%}
%union { /* stack type */
double val; /* actual value */
int index; /* index into mem[] */
}
%token <val> NUMBER
%token <index> VAR
%type <val> expr
%right '='
%left '+' '-'
%left '*' '/'
%left UNARYMINUS
%%
list: /* nothing */
| list '\n'
| list expr '\n' { printf("\t%.8g\n\ $2); }
| list error '\n' { yyerrok; }
;
expr: NUMBER
| VAR { $$ = mem[$1]; }
| VAR '=' expr { $$ = mem[$1] = $3; }
| expr '+' expr { $$ = $1 + $3; }
| expr '-' expr { $$ = $1 - $3; }
| expr '*' expr { $$ = $1 * $3; }
| expr '/' expr {
if ($3 == 0.0)
execerror("division by zero", "");
$$ = $1 / $3;
}
| '(' expr ')' { $$ = $2; }
| '-' expr %prec UNARYMINUS { $$ = -$2; }
;
%%
/* end of grammar */
#include <stdio.h>
#include <ctype.h>
char *progname;
int lineno = 1;
#include <signal.h>
#include <setjmp.h>
jmp_buf begin;
main(argc, argv) /* hoc2 */
char *argv[];
{
int fpecatch();
progname = argv[0];
setjmp(begin);
signal(SIGFPE, fpecatch);
yyparse();
}
yylex() /* hoc2 */
{
int c;
while ((c=getchar()) == ' ' || c == '\t')
;
if (c == EOF)
return 0;
if (c == '.' || isdigit(c)) { /* number */
ungetc(c, stdin);
scanf("%lf", &yylval.val);
return NUMBER;
}
if (islower(c)) {
yylval.index = c - 'a'; /* ASCII only */
return VAR;
}
if (c == '\n')
lineno++;
return c;
}
yyerror(s) /* report compile-time error */
char *s;
{
warning(s, (char*)0);
}
execerror(s, t) /* recover from run-time error */
char *s, *t;
{
warning(s, t);
longjmp(begin, 0);
}
fpecatch() /* catch floating point exceptions */