Home » Recent acquisitions » Acorn ADFS disks » adfs_ArchimedesWorld_199204.adf » April92 » !AWApr92/Goodies/All_At_C/c/lexical
!AWApr92/Goodies/All_At_C/c/lexical
This website contains an archive of files for the Acorn Electron, BBC Micro, Acorn Archimedes, Commodore 16 and Commodore 64 computers, which Dominic Ford has rescued from his private collection of floppy disks and cassettes.
Some of these files were originally commercial releases in the 1980s and 1990s, but they are now widely available online. I assume that copyright over them is no longer being asserted. If you own the copyright and would like files to be removed, please contact me.
Tape/disk: | Home » Recent acquisitions » Acorn ADFS disks » adfs_ArchimedesWorld_199204.adf » April92 |
Filename: | !AWApr92/Goodies/All_At_C/c/lexical |
Read OK: | ✔ |
File size: | 080B bytes |
Load address: | 0000 |
Exec address: | 0000 |
File contents
/* Lexical Functions. */ /* Archimedes World, January 1992. */ #include "lexical.h" #include <ctype.h> #include <stdlib.h> #include <string.h> #include <stdio.h> struct word_struct { string str; word upper; word lower; int count; }; static word NewWord( char *str ) { word new = malloc( sizeof( struct word_struct ) ); if ( new ) { strncpy( new->str, str, STRLEN ); new->str[STRLEN] = NULL; new->lower = NULL; new->upper = NULL; new->count = 1; } else { puts( "Out of memory!\nProgram terminated.\n" ); exit( 1 ); } return new; } void ToUpperCase( char *word ) { /* Converts the given word to upper case. */ char *ptr = word; while ( *ptr ) { *ptr = toupper( *ptr ); ptr++; } } void ToLowerCase( char *word ) { /* Converts the given word to lower case. */ char *ptr = word; while ( *ptr ) { *ptr = tolower( *ptr ); ptr++; } } void Capitalise( char *string ) { /* Forces the first character of the supplied string to upper case. */ toupper( *string ); } void StoreWord( char *str, word *list ) { /* Stores 'str' on the given word list. */ if ( !*list ) *list = NewWord( str ); else if ( strcmp( str, (*list)->str ) > 0 ) StoreWord( str, &(*list)->upper ); else if ( strcmp( str, (*list)->str ) < 0 ) StoreWord( str, &(*list)->lower ); else (*list)->count++; } void ListWords( FILE *f, word list ) { if ( list ) { ListWords( f, list->lower ); fprintf( f, "%4i %s\n", list->count, list->str ); ListWords( f, list->upper ); } } int GetWord( char *buffer, int length, FILE *stream ) { int c, i = 0; while ( c = fgetc(stream), c != EOF && !isalpha(c) ); while ( c != EOF && isalpha(c) ) { if ( i < length ) buffer[i++] = c; c = fgetc( stream ); } buffer[i] = NULL; return i; }
00000000 2f 2a 20 20 4c 65 78 69 63 61 6c 20 46 75 6e 63 |/* Lexical Func| 00000010 74 69 6f 6e 73 2e 20 20 20 20 20 20 20 20 20 20 |tions. | 00000020 20 20 20 20 20 20 20 20 20 20 20 2a 2f 0a 2f 2a | */./*| 00000030 20 20 41 72 63 68 69 6d 65 64 65 73 20 57 6f 72 | Archimedes Wor| 00000040 6c 64 2c 20 4a 61 6e 75 61 72 79 20 31 39 39 32 |ld, January 1992| 00000050 2e 20 20 20 20 20 20 20 20 2a 2f 0a 0a 23 69 6e |. */..#in| 00000060 63 6c 75 64 65 20 22 6c 65 78 69 63 61 6c 2e 68 |clude "lexical.h| 00000070 22 0a 23 69 6e 63 6c 75 64 65 20 3c 63 74 79 70 |".#include <ctyp| 00000080 65 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 73 |e.h>.#include <s| 00000090 74 64 6c 69 62 2e 68 3e 0a 23 69 6e 63 6c 75 64 |tdlib.h>.#includ| 000000a0 65 20 3c 73 74 72 69 6e 67 2e 68 3e 0a 23 69 6e |e <string.h>.#in| 000000b0 63 6c 75 64 65 20 3c 73 74 64 69 6f 2e 68 3e 0a |clude <stdio.h>.| 000000c0 0a 73 74 72 75 63 74 20 77 6f 72 64 5f 73 74 72 |.struct word_str| 000000d0 75 63 74 0a 7b 0a 20 20 20 20 73 74 72 69 6e 67 |uct.{. string| 000000e0 20 73 74 72 3b 0a 20 20 20 20 77 6f 72 64 20 20 | str;. word | 000000f0 20 75 70 70 65 72 3b 0a 20 20 20 20 77 6f 72 64 | upper;. word| 00000100 20 20 20 6c 6f 77 65 72 3b 0a 20 20 20 20 69 6e | lower;. in| 00000110 74 20 20 20 20 63 6f 75 6e 74 3b 0a 7d 3b 0a 0a |t count;.};..| 00000120 0a 73 74 61 74 69 63 20 77 6f 72 64 20 4e 65 77 |.static word New| 00000130 57 6f 72 64 28 20 63 68 61 72 20 2a 73 74 72 20 |Word( char *str | 00000140 29 0a 7b 0a 20 20 20 20 77 6f 72 64 20 6e 65 77 |).{. word new| 00000150 20 3d 20 6d 61 6c 6c 6f 63 28 20 73 69 7a 65 6f | = malloc( sizeo| 00000160 66 28 20 73 74 72 75 63 74 20 77 6f 72 64 5f 73 |f( struct word_s| 00000170 74 72 75 63 74 20 29 20 29 3b 0a 0a 20 20 20 20 |truct ) );.. | 00000180 69 66 20 28 20 6e 65 77 20 29 0a 20 20 20 20 7b |if ( new ). {| 00000190 0a 20 20 20 20 20 20 20 20 73 74 72 6e 63 70 79 |. strncpy| 000001a0 28 20 6e 65 77 2d 3e 73 74 72 2c 20 73 74 72 2c |( new->str, str,| 000001b0 20 53 54 52 4c 45 4e 20 29 3b 0a 0a 20 20 20 20 | STRLEN );.. | 000001c0 20 20 20 20 6e 65 77 2d 3e 73 74 72 5b 53 54 52 | new->str[STR| 000001d0 4c 45 4e 5d 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 |LEN] = NULL;. | 000001e0 20 20 20 20 20 6e 65 77 2d 3e 6c 6f 77 65 72 20 | new->lower | 000001f0 20 20 20 20 20 20 3d 20 4e 55 4c 4c 3b 0a 20 20 | = NULL;. | 00000200 20 20 20 20 20 20 6e 65 77 2d 3e 75 70 70 65 72 | new->upper| 00000210 20 20 20 20 20 20 20 3d 20 4e 55 4c 4c 3b 0a 20 | = NULL;. | 00000220 20 20 20 20 20 20 20 6e 65 77 2d 3e 63 6f 75 6e | new->coun| 00000230 74 20 20 20 20 20 20 20 3d 20 31 3b 0a 20 20 20 |t = 1;. | 00000240 20 7d 0a 20 20 20 20 65 6c 73 65 0a 20 20 20 20 | }. else. | 00000250 7b 0a 20 20 20 20 20 20 20 20 70 75 74 73 28 20 |{. puts( | 00000260 22 4f 75 74 20 6f 66 20 6d 65 6d 6f 72 79 21 5c |"Out of memory!\| 00000270 6e 50 72 6f 67 72 61 6d 20 74 65 72 6d 69 6e 61 |nProgram termina| 00000280 74 65 64 2e 5c 6e 22 20 29 3b 0a 20 20 20 20 20 |ted.\n" );. | 00000290 20 20 20 65 78 69 74 28 20 31 20 29 3b 0a 20 20 | exit( 1 );. | 000002a0 20 20 7d 0a 0a 20 20 20 20 72 65 74 75 72 6e 20 | }.. return | 000002b0 6e 65 77 3b 0a 7d 0a 0a 0a 76 6f 69 64 20 54 6f |new;.}...void To| 000002c0 55 70 70 65 72 43 61 73 65 28 20 63 68 61 72 20 |UpperCase( char | 000002d0 2a 77 6f 72 64 20 29 0a 7b 0a 2f 2a 20 20 43 6f |*word ).{./* Co| 000002e0 6e 76 65 72 74 73 20 74 68 65 20 67 69 76 65 6e |nverts the given| 000002f0 20 77 6f 72 64 20 74 6f 20 75 70 70 65 72 20 63 | word to upper c| 00000300 61 73 65 2e 20 2a 2f 0a 0a 20 20 20 20 63 68 61 |ase. */.. cha| 00000310 72 20 2a 70 74 72 20 3d 20 77 6f 72 64 3b 0a 0a |r *ptr = word;..| 00000320 20 20 20 20 77 68 69 6c 65 20 28 20 2a 70 74 72 | while ( *ptr| 00000330 20 29 0a 20 20 20 20 7b 0a 20 20 20 20 20 20 20 | ). {. | 00000340 2a 70 74 72 20 3d 20 74 6f 75 70 70 65 72 28 20 |*ptr = toupper( | 00000350 2a 70 74 72 20 29 3b 0a 20 20 20 20 20 20 20 20 |*ptr );. | 00000360 70 74 72 2b 2b 3b 0a 20 20 20 20 7d 0a 7d 0a 0a |ptr++;. }.}..| 00000370 0a 76 6f 69 64 20 54 6f 4c 6f 77 65 72 43 61 73 |.void ToLowerCas| 00000380 65 28 20 63 68 61 72 20 2a 77 6f 72 64 20 29 0a |e( char *word ).| 00000390 7b 0a 2f 2a 20 20 43 6f 6e 76 65 72 74 73 20 74 |{./* Converts t| 000003a0 68 65 20 67 69 76 65 6e 20 77 6f 72 64 20 74 6f |he given word to| 000003b0 20 6c 6f 77 65 72 20 63 61 73 65 2e 20 2a 2f 0a | lower case. */.| 000003c0 0a 20 20 20 20 63 68 61 72 20 2a 70 74 72 20 3d |. char *ptr =| 000003d0 20 77 6f 72 64 3b 0a 0a 20 20 20 20 77 68 69 6c | word;.. whil| 000003e0 65 20 28 20 2a 70 74 72 20 29 0a 20 20 20 20 7b |e ( *ptr ). {| 000003f0 0a 20 20 20 20 20 20 20 2a 70 74 72 20 3d 20 74 |. *ptr = t| 00000400 6f 6c 6f 77 65 72 28 20 2a 70 74 72 20 29 3b 0a |olower( *ptr );.| 00000410 20 20 20 20 20 20 20 20 70 74 72 2b 2b 3b 0a 20 | ptr++;. | 00000420 20 20 20 7d 0a 7d 0a 0a 0a 76 6f 69 64 20 43 61 | }.}...void Ca| 00000430 70 69 74 61 6c 69 73 65 28 20 63 68 61 72 20 2a |pitalise( char *| 00000440 73 74 72 69 6e 67 20 29 0a 7b 0a 2f 2a 20 20 46 |string ).{./* F| 00000450 6f 72 63 65 73 20 74 68 65 20 66 69 72 73 74 20 |orces the first | 00000460 63 68 61 72 61 63 74 65 72 20 6f 66 20 74 68 65 |character of the| 00000470 20 73 75 70 70 6c 69 65 64 20 73 74 72 69 6e 67 | supplied string| 00000480 20 74 6f 20 75 70 70 65 72 20 63 61 73 65 2e 20 | to upper case. | 00000490 2a 2f 0a 0a 20 20 20 20 74 6f 75 70 70 65 72 28 |*/.. toupper(| 000004a0 20 2a 73 74 72 69 6e 67 20 29 3b 0a 7d 0a 0a 0a | *string );.}...| 000004b0 76 6f 69 64 20 53 74 6f 72 65 57 6f 72 64 28 20 |void StoreWord( | 000004c0 63 68 61 72 20 2a 73 74 72 2c 20 77 6f 72 64 20 |char *str, word | 000004d0 2a 6c 69 73 74 20 29 0a 7b 0a 2f 2a 20 20 53 74 |*list ).{./* St| 000004e0 6f 72 65 73 20 27 73 74 72 27 20 6f 6e 20 74 68 |ores 'str' on th| 000004f0 65 20 67 69 76 65 6e 20 77 6f 72 64 20 6c 69 73 |e given word lis| 00000500 74 2e 20 20 2a 2f 0a 0a 20 20 20 20 69 66 20 28 |t. */.. if (| 00000510 20 21 2a 6c 69 73 74 20 29 0a 20 20 20 20 20 20 | !*list ). | 00000520 20 20 2a 6c 69 73 74 20 3d 20 4e 65 77 57 6f 72 | *list = NewWor| 00000530 64 28 20 73 74 72 20 29 3b 0a 20 20 20 20 65 6c |d( str );. el| 00000540 73 65 20 69 66 20 28 20 73 74 72 63 6d 70 28 20 |se if ( strcmp( | 00000550 73 74 72 2c 20 28 2a 6c 69 73 74 29 2d 3e 73 74 |str, (*list)->st| 00000560 72 20 29 20 3e 20 30 20 29 0a 20 20 20 20 20 20 |r ) > 0 ). | 00000570 20 20 53 74 6f 72 65 57 6f 72 64 28 20 73 74 72 | StoreWord( str| 00000580 2c 20 26 28 2a 6c 69 73 74 29 2d 3e 75 70 70 65 |, &(*list)->uppe| 00000590 72 20 29 3b 0a 20 20 20 20 65 6c 73 65 20 69 66 |r );. else if| 000005a0 20 28 20 73 74 72 63 6d 70 28 20 73 74 72 2c 20 | ( strcmp( str, | 000005b0 28 2a 6c 69 73 74 29 2d 3e 73 74 72 20 29 20 3c |(*list)->str ) <| 000005c0 20 30 20 29 0a 20 20 20 20 20 20 20 20 53 74 6f | 0 ). Sto| 000005d0 72 65 57 6f 72 64 28 20 73 74 72 2c 20 26 28 2a |reWord( str, &(*| 000005e0 6c 69 73 74 29 2d 3e 6c 6f 77 65 72 20 29 3b 0a |list)->lower );.| 000005f0 20 20 20 20 65 6c 73 65 0a 20 20 20 20 20 20 20 | else. | 00000600 20 28 2a 6c 69 73 74 29 2d 3e 63 6f 75 6e 74 2b | (*list)->count+| 00000610 2b 3b 0a 7d 0a 0a 0a 76 6f 69 64 20 4c 69 73 74 |+;.}...void List| 00000620 57 6f 72 64 73 28 20 46 49 4c 45 20 2a 66 2c 20 |Words( FILE *f, | 00000630 77 6f 72 64 20 6c 69 73 74 20 29 0a 7b 0a 20 20 |word list ).{. | 00000640 20 20 69 66 20 28 20 6c 69 73 74 20 29 0a 20 20 | if ( list ). | 00000650 20 20 7b 0a 20 20 20 20 20 20 20 20 4c 69 73 74 | {. List| 00000660 57 6f 72 64 73 28 20 66 2c 20 6c 69 73 74 2d 3e |Words( f, list->| 00000670 6c 6f 77 65 72 20 29 3b 0a 20 20 20 20 20 20 20 |lower );. | 00000680 20 66 70 72 69 6e 74 66 28 20 66 2c 20 22 25 34 | fprintf( f, "%4| 00000690 69 20 20 25 73 5c 6e 22 2c 20 6c 69 73 74 2d 3e |i %s\n", list->| 000006a0 63 6f 75 6e 74 2c 20 6c 69 73 74 2d 3e 73 74 72 |count, list->str| 000006b0 20 29 3b 0a 20 20 20 20 20 20 20 20 4c 69 73 74 | );. List| 000006c0 57 6f 72 64 73 28 20 66 2c 20 6c 69 73 74 2d 3e |Words( f, list->| 000006d0 75 70 70 65 72 20 29 3b 0a 20 20 20 20 7d 0a 7d |upper );. }.}| 000006e0 0a 0a 0a 69 6e 74 20 47 65 74 57 6f 72 64 28 20 |...int GetWord( | 000006f0 63 68 61 72 20 2a 62 75 66 66 65 72 2c 20 69 6e |char *buffer, in| 00000700 74 20 6c 65 6e 67 74 68 2c 20 46 49 4c 45 20 2a |t length, FILE *| 00000710 73 74 72 65 61 6d 20 29 0a 7b 0a 20 20 20 20 69 |stream ).{. i| 00000720 6e 74 20 63 2c 20 69 20 3d 20 30 3b 0a 0a 20 20 |nt c, i = 0;.. | 00000730 20 20 77 68 69 6c 65 20 28 20 63 20 3d 20 66 67 | while ( c = fg| 00000740 65 74 63 28 73 74 72 65 61 6d 29 2c 20 63 20 21 |etc(stream), c !| 00000750 3d 20 45 4f 46 20 26 26 20 21 69 73 61 6c 70 68 |= EOF && !isalph| 00000760 61 28 63 29 20 29 3b 0a 0a 20 20 20 20 77 68 69 |a(c) );.. whi| 00000770 6c 65 20 28 20 63 20 21 3d 20 45 4f 46 20 26 26 |le ( c != EOF &&| 00000780 20 69 73 61 6c 70 68 61 28 63 29 20 29 0a 20 20 | isalpha(c) ). | 00000790 20 20 7b 0a 20 20 20 20 20 20 20 20 69 66 20 28 | {. if (| 000007a0 20 69 20 3c 20 6c 65 6e 67 74 68 20 29 20 20 62 | i < length ) b| 000007b0 75 66 66 65 72 5b 69 2b 2b 5d 20 3d 20 63 3b 0a |uffer[i++] = c;.| 000007c0 20 20 20 20 20 20 20 20 63 20 3d 20 66 67 65 74 | c = fget| 000007d0 63 28 20 73 74 72 65 61 6d 20 29 3b 0a 20 20 20 |c( stream );. | 000007e0 20 7d 0a 0a 20 20 20 20 62 75 66 66 65 72 5b 69 | }.. buffer[i| 000007f0 5d 20 3d 20 4e 55 4c 4c 3b 0a 0a 20 20 20 20 72 |] = NULL;.. r| 00000800 65 74 75 72 6e 20 69 3b 0a 7d 0a |eturn i;.}.| 0000080b