Home » Recent acquisitions » Acorn ADFS disks » adfs_ArchimedesWorld_199207.adf » !AWJULY92 » !AWJUL92/Goodies/work/c/lexical
!AWJUL92/Goodies/work/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_199207.adf » !AWJULY92 |
Filename: | !AWJUL92/Goodies/work/c/lexical |
Read OK: | ✔ |
File size: | 0896 bytes |
Load address: | 0000 |
Exec address: | 0000 |
File contents
/* Lexical Functions. */ /* Archimedes World, January 1992. */ #include "h.global" #include "h.lexical" #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, int repeats ) { if ( list ) { ListWords( f, list->lower, repeats ); if ( repeats ) fprintf( f, "%4i %s\n", list->count, list->str ); else fprintf( f, "%s\n", list->str ); ListWords( f, list->upper, repeats ); } } 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 68 2e 67 6c 6f 62 61 6c 22 |clude "h.global"| 00000070 0a 23 69 6e 63 6c 75 64 65 20 22 68 2e 6c 65 78 |.#include "h.lex| 00000080 69 63 61 6c 22 0a 0a 23 69 6e 63 6c 75 64 65 20 |ical"..#include | 00000090 3c 63 74 79 70 65 2e 68 3e 0a 23 69 6e 63 6c 75 |<ctype.h>.#inclu| 000000a0 64 65 20 3c 73 74 64 6c 69 62 2e 68 3e 0a 23 69 |de <stdlib.h>.#i| 000000b0 6e 63 6c 75 64 65 20 3c 73 74 72 69 6e 67 2e 68 |nclude <string.h| 000000c0 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 73 74 64 69 |>.#include <stdi| 000000d0 6f 2e 68 3e 0a 0a 73 74 72 75 63 74 20 77 6f 72 |o.h>..struct wor| 000000e0 64 5f 73 74 72 75 63 74 0a 7b 0a 20 20 20 20 73 |d_struct.{. s| 000000f0 74 72 69 6e 67 20 73 74 72 3b 0a 20 20 20 20 77 |tring str;. w| 00000100 6f 72 64 20 20 20 75 70 70 65 72 3b 0a 20 20 20 |ord upper;. | 00000110 20 77 6f 72 64 20 20 20 6c 6f 77 65 72 3b 0a 20 | word lower;. | 00000120 20 20 20 69 6e 74 20 20 20 20 63 6f 75 6e 74 3b | int count;| 00000130 0a 7d 3b 0a 0a 0a 73 74 61 74 69 63 20 77 6f 72 |.};...static wor| 00000140 64 20 4e 65 77 57 6f 72 64 28 20 63 68 61 72 20 |d NewWord( char | 00000150 2a 73 74 72 20 29 0a 7b 0a 20 20 20 20 77 6f 72 |*str ).{. wor| 00000160 64 20 6e 65 77 20 3d 20 6d 61 6c 6c 6f 63 28 20 |d new = malloc( | 00000170 73 69 7a 65 6f 66 28 20 73 74 72 75 63 74 20 77 |sizeof( struct w| 00000180 6f 72 64 5f 73 74 72 75 63 74 20 29 20 29 3b 0a |ord_struct ) );.| 00000190 0a 20 20 20 20 69 66 20 28 20 6e 65 77 20 29 0a |. if ( new ).| 000001a0 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20 73 74 | {. st| 000001b0 72 6e 63 70 79 28 20 6e 65 77 2d 3e 73 74 72 2c |rncpy( new->str,| 000001c0 20 73 74 72 2c 20 53 54 52 4c 45 4e 20 29 3b 0a | str, STRLEN );.| 000001d0 0a 20 20 20 20 20 20 20 20 6e 65 77 2d 3e 73 74 |. new->st| 000001e0 72 5b 53 54 52 4c 45 4e 5d 20 3d 20 4e 55 4c 4c |r[STRLEN] = NULL| 000001f0 3b 0a 20 20 20 20 20 20 20 20 6e 65 77 2d 3e 6c |;. new->l| 00000200 6f 77 65 72 20 20 20 20 20 20 20 3d 20 4e 55 4c |ower = NUL| 00000210 4c 3b 0a 20 20 20 20 20 20 20 20 6e 65 77 2d 3e |L;. new->| 00000220 75 70 70 65 72 20 20 20 20 20 20 20 3d 20 4e 55 |upper = NU| 00000230 4c 4c 3b 0a 20 20 20 20 20 20 20 20 6e 65 77 2d |LL;. new-| 00000240 3e 63 6f 75 6e 74 20 20 20 20 20 20 20 3d 20 31 |>count = 1| 00000250 3b 0a 20 20 20 20 7d 0a 20 20 20 20 65 6c 73 65 |;. }. else| 00000260 0a 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20 70 |. {. p| 00000270 75 74 73 28 20 22 4f 75 74 20 6f 66 20 6d 65 6d |uts( "Out of mem| 00000280 6f 72 79 21 5c 6e 50 72 6f 67 72 61 6d 20 74 65 |ory!\nProgram te| 00000290 72 6d 69 6e 61 74 65 64 2e 5c 6e 22 20 29 3b 0a |rminated.\n" );.| 000002a0 20 20 20 20 20 20 20 20 65 78 69 74 28 20 31 20 | exit( 1 | 000002b0 29 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 20 72 65 |);. }.. re| 000002c0 74 75 72 6e 20 6e 65 77 3b 0a 7d 0a 0a 0a 76 6f |turn new;.}...vo| 000002d0 69 64 20 54 6f 55 70 70 65 72 43 61 73 65 28 20 |id ToUpperCase( | 000002e0 63 68 61 72 20 2a 77 6f 72 64 20 29 0a 7b 0a 2f |char *word ).{./| 000002f0 2a 20 20 43 6f 6e 76 65 72 74 73 20 74 68 65 20 |* Converts the | 00000300 67 69 76 65 6e 20 77 6f 72 64 20 74 6f 20 75 70 |given word to up| 00000310 70 65 72 20 63 61 73 65 2e 20 2a 2f 0a 0a 20 20 |per case. */.. | 00000320 20 20 63 68 61 72 20 2a 70 74 72 20 3d 20 77 6f | char *ptr = wo| 00000330 72 64 3b 0a 0a 20 20 20 20 77 68 69 6c 65 20 28 |rd;.. while (| 00000340 20 2a 70 74 72 20 29 0a 20 20 20 20 7b 0a 20 20 | *ptr ). {. | 00000350 20 20 20 20 20 2a 70 74 72 20 3d 20 74 6f 75 70 | *ptr = toup| 00000360 70 65 72 28 20 2a 70 74 72 20 29 3b 0a 20 20 20 |per( *ptr );. | 00000370 20 20 20 20 20 70 74 72 2b 2b 3b 0a 20 20 20 20 | ptr++;. | 00000380 7d 0a 7d 0a 0a 0a 76 6f 69 64 20 54 6f 4c 6f 77 |}.}...void ToLow| 00000390 65 72 43 61 73 65 28 20 63 68 61 72 20 2a 77 6f |erCase( char *wo| 000003a0 72 64 20 29 0a 7b 0a 2f 2a 20 20 43 6f 6e 76 65 |rd ).{./* Conve| 000003b0 72 74 73 20 74 68 65 20 67 69 76 65 6e 20 77 6f |rts the given wo| 000003c0 72 64 20 74 6f 20 6c 6f 77 65 72 20 63 61 73 65 |rd to lower case| 000003d0 2e 20 2a 2f 0a 0a 20 20 20 20 63 68 61 72 20 2a |. */.. char *| 000003e0 70 74 72 20 3d 20 77 6f 72 64 3b 0a 0a 20 20 20 |ptr = word;.. | 000003f0 20 77 68 69 6c 65 20 28 20 2a 70 74 72 20 29 0a | while ( *ptr ).| 00000400 20 20 20 20 7b 0a 20 20 20 20 20 20 20 2a 70 74 | {. *pt| 00000410 72 20 3d 20 74 6f 6c 6f 77 65 72 28 20 2a 70 74 |r = tolower( *pt| 00000420 72 20 29 3b 0a 20 20 20 20 20 20 20 20 70 74 72 |r );. ptr| 00000430 2b 2b 3b 0a 20 20 20 20 7d 0a 7d 0a 0a 0a 76 6f |++;. }.}...vo| 00000440 69 64 20 43 61 70 69 74 61 6c 69 73 65 28 20 63 |id Capitalise( c| 00000450 68 61 72 20 2a 73 74 72 69 6e 67 20 29 0a 7b 0a |har *string ).{.| 00000460 2f 2a 20 20 46 6f 72 63 65 73 20 74 68 65 20 66 |/* Forces the f| 00000470 69 72 73 74 20 63 68 61 72 61 63 74 65 72 20 6f |irst character o| 00000480 66 20 74 68 65 20 73 75 70 70 6c 69 65 64 20 73 |f the supplied s| 00000490 74 72 69 6e 67 20 74 6f 20 75 70 70 65 72 20 63 |tring to upper c| 000004a0 61 73 65 2e 20 2a 2f 0a 0a 20 20 20 20 74 6f 75 |ase. */.. tou| 000004b0 70 70 65 72 28 20 2a 73 74 72 69 6e 67 20 29 3b |pper( *string );| 000004c0 0a 7d 0a 0a 0a 76 6f 69 64 20 53 74 6f 72 65 57 |.}...void StoreW| 000004d0 6f 72 64 28 20 63 68 61 72 20 2a 73 74 72 2c 20 |ord( char *str, | 000004e0 77 6f 72 64 20 2a 6c 69 73 74 20 29 0a 7b 0a 2f |word *list ).{./| 000004f0 2a 20 20 53 74 6f 72 65 73 20 27 73 74 72 27 20 |* Stores 'str' | 00000500 6f 6e 20 74 68 65 20 67 69 76 65 6e 20 77 6f 72 |on the given wor| 00000510 64 20 6c 69 73 74 2e 20 20 2a 2f 0a 0a 20 20 20 |d list. */.. | 00000520 20 69 66 20 28 20 21 2a 6c 69 73 74 20 29 0a 20 | if ( !*list ). | 00000530 20 20 20 20 20 20 20 2a 6c 69 73 74 20 3d 20 4e | *list = N| 00000540 65 77 57 6f 72 64 28 20 73 74 72 20 29 3b 0a 20 |ewWord( str );. | 00000550 20 20 20 65 6c 73 65 20 69 66 20 28 20 73 74 72 | else if ( str| 00000560 63 6d 70 28 20 73 74 72 2c 20 28 2a 6c 69 73 74 |cmp( str, (*list| 00000570 29 2d 3e 73 74 72 20 29 20 3e 20 30 20 29 0a 20 |)->str ) > 0 ). | 00000580 20 20 20 20 20 20 20 53 74 6f 72 65 57 6f 72 64 | StoreWord| 00000590 28 20 73 74 72 2c 20 26 28 2a 6c 69 73 74 29 2d |( str, &(*list)-| 000005a0 3e 75 70 70 65 72 20 29 3b 0a 20 20 20 20 65 6c |>upper );. el| 000005b0 73 65 20 69 66 20 28 20 73 74 72 63 6d 70 28 20 |se if ( strcmp( | 000005c0 73 74 72 2c 20 28 2a 6c 69 73 74 29 2d 3e 73 74 |str, (*list)->st| 000005d0 72 20 29 20 3c 20 30 20 29 0a 20 20 20 20 20 20 |r ) < 0 ). | 000005e0 20 20 53 74 6f 72 65 57 6f 72 64 28 20 73 74 72 | StoreWord( str| 000005f0 2c 20 26 28 2a 6c 69 73 74 29 2d 3e 6c 6f 77 65 |, &(*list)->lowe| 00000600 72 20 29 3b 0a 20 20 20 20 65 6c 73 65 0a 20 20 |r );. else. | 00000610 20 20 20 20 20 20 28 2a 6c 69 73 74 29 2d 3e 63 | (*list)->c| 00000620 6f 75 6e 74 2b 2b 3b 0a 7d 0a 0a 0a 76 6f 69 64 |ount++;.}...void| 00000630 20 4c 69 73 74 57 6f 72 64 73 28 20 46 49 4c 45 | ListWords( FILE| 00000640 20 2a 66 2c 20 77 6f 72 64 20 6c 69 73 74 2c 20 | *f, word list, | 00000650 69 6e 74 20 72 65 70 65 61 74 73 20 29 0a 7b 0a |int repeats ).{.| 00000660 20 20 20 20 69 66 20 28 20 6c 69 73 74 20 29 0a | if ( list ).| 00000670 20 20 20 20 7b 0a 20 20 20 20 20 20 20 20 4c 69 | {. Li| 00000680 73 74 57 6f 72 64 73 28 20 66 2c 20 6c 69 73 74 |stWords( f, list| 00000690 2d 3e 6c 6f 77 65 72 2c 20 72 65 70 65 61 74 73 |->lower, repeats| 000006a0 20 29 3b 0a 0a 20 20 20 20 20 20 20 20 69 66 20 | );.. if | 000006b0 28 20 72 65 70 65 61 74 73 20 29 0a 20 20 20 20 |( repeats ). | 000006c0 20 20 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 | fprintf(| 000006d0 20 66 2c 20 22 25 34 69 20 20 25 73 5c 6e 22 2c | f, "%4i %s\n",| 000006e0 20 6c 69 73 74 2d 3e 63 6f 75 6e 74 2c 20 6c 69 | list->count, li| 000006f0 73 74 2d 3e 73 74 72 20 29 3b 0a 20 20 20 20 20 |st->str );. | 00000700 20 20 20 65 6c 73 65 0a 20 20 20 20 20 20 20 20 | else. | 00000710 20 20 20 20 66 70 72 69 6e 74 66 28 20 66 2c 20 | fprintf( f, | 00000720 22 25 73 5c 6e 22 2c 20 6c 69 73 74 2d 3e 73 74 |"%s\n", list->st| 00000730 72 20 29 3b 0a 0a 20 20 20 20 20 20 20 20 4c 69 |r );.. Li| 00000740 73 74 57 6f 72 64 73 28 20 66 2c 20 6c 69 73 74 |stWords( f, list| 00000750 2d 3e 75 70 70 65 72 2c 20 72 65 70 65 61 74 73 |->upper, repeats| 00000760 20 29 3b 0a 20 20 20 20 7d 0a 7d 0a 0a 0a 69 6e | );. }.}...in| 00000770 74 20 47 65 74 57 6f 72 64 28 20 63 68 61 72 20 |t GetWord( char | 00000780 2a 62 75 66 66 65 72 2c 20 69 6e 74 20 6c 65 6e |*buffer, int len| 00000790 67 74 68 2c 20 46 49 4c 45 20 2a 73 74 72 65 61 |gth, FILE *strea| 000007a0 6d 20 29 0a 7b 0a 20 20 20 20 69 6e 74 20 63 2c |m ).{. int c,| 000007b0 20 69 20 3d 20 30 3b 0a 0a 20 20 20 20 77 68 69 | i = 0;.. whi| 000007c0 6c 65 20 28 20 63 20 3d 20 66 67 65 74 63 28 73 |le ( c = fgetc(s| 000007d0 74 72 65 61 6d 29 2c 20 63 20 21 3d 20 45 4f 46 |tream), c != EOF| 000007e0 20 26 26 20 21 69 73 61 6c 70 68 61 28 63 29 20 | && !isalpha(c) | 000007f0 29 3b 0a 0a 20 20 20 20 77 68 69 6c 65 20 28 20 |);.. while ( | 00000800 63 20 21 3d 20 45 4f 46 20 26 26 20 69 73 61 6c |c != EOF && isal| 00000810 70 68 61 28 63 29 20 29 0a 20 20 20 20 7b 0a 20 |pha(c) ). {. | 00000820 20 20 20 20 20 20 20 69 66 20 28 20 69 20 3c 20 | if ( i < | 00000830 6c 65 6e 67 74 68 20 29 20 20 62 75 66 66 65 72 |length ) buffer| 00000840 5b 69 2b 2b 5d 20 3d 20 63 3b 0a 20 20 20 20 20 |[i++] = c;. | 00000850 20 20 20 63 20 3d 20 66 67 65 74 63 28 20 73 74 | c = fgetc( st| 00000860 72 65 61 6d 20 29 3b 0a 20 20 20 20 7d 0a 0a 20 |ream );. }.. | 00000870 20 20 20 62 75 66 66 65 72 5b 69 5d 20 3d 20 4e | buffer[i] = N| 00000880 55 4c 4c 3b 0a 0a 20 20 20 20 72 65 74 75 72 6e |ULL;.. return| 00000890 20 69 3b 0a 7d 0a | i;.}.| 00000896