Home » Recent acquisitions » Acorn ADFS disks » adfs_ArchimedesWorld_199201.adf » January92 » !AWJan92/Goodies/AllAtC/Work/c/lexical

!AWJan92/Goodies/AllAtC/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_199201.adf » January92
Filename: !AWJan92/Goodies/AllAtC/Work/c/lexical
Read OK:
File size: 07E5 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>


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( word list )
{
    if ( list )
    {
        ListWords( list->lower );
        printf( "%4i  %s\n", list->count, list->str );
        ListWords( 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 2a 2f 0a 2f 2a 20  |          */./* |
00000030  20 41 72 63 68 69 6d 65  64 65 73 20 57 6f 72 6c  | Archimedes Worl|
00000040  64 2c 20 4a 61 6e 75 61  72 79 20 31 39 39 32 2e  |d, January 1992.|
00000050  20 20 20 20 20 20 20 20  2a 2f 0a 0a 23 69 6e 63  |        */..#inc|
00000060  6c 75 64 65 20 22 6c 65  78 69 63 61 6c 2e 68 22  |lude "lexical.h"|
00000070  0a 23 69 6e 63 6c 75 64  65 20 3c 63 74 79 70 65  |.#include <ctype|
00000080  2e 68 3e 0a 23 69 6e 63  6c 75 64 65 20 3c 73 74  |.h>.#include <st|
00000090  64 6c 69 62 2e 68 3e 0a  23 69 6e 63 6c 75 64 65  |dlib.h>.#include|
000000a0  20 3c 73 74 72 69 6e 67  2e 68 3e 0a 0a 0a 73 74  | <string.h>...st|
000000b0  72 75 63 74 20 77 6f 72  64 5f 73 74 72 75 63 74  |ruct word_struct|
000000c0  0a 7b 0a 20 20 20 20 73  74 72 69 6e 67 20 73 74  |.{.    string st|
000000d0  72 3b 0a 20 20 20 20 77  6f 72 64 20 20 20 75 70  |r;.    word   up|
000000e0  70 65 72 3b 0a 20 20 20  20 77 6f 72 64 20 20 20  |per;.    word   |
000000f0  6c 6f 77 65 72 3b 0a 20  20 20 20 69 6e 74 20 20  |lower;.    int  |
00000100  20 20 63 6f 75 6e 74 3b  0a 7d 3b 0a 0a 0a 73 74  |  count;.};...st|
00000110  61 74 69 63 20 77 6f 72  64 20 4e 65 77 57 6f 72  |atic word NewWor|
00000120  64 28 20 63 68 61 72 20  2a 73 74 72 20 29 0a 7b  |d( char *str ).{|
00000130  0a 20 20 20 20 77 6f 72  64 20 6e 65 77 20 3d 20  |.    word new = |
00000140  6d 61 6c 6c 6f 63 28 20  73 69 7a 65 6f 66 28 20  |malloc( sizeof( |
00000150  73 74 72 75 63 74 20 77  6f 72 64 5f 73 74 72 75  |struct word_stru|
00000160  63 74 20 29 20 29 3b 0a  0a 20 20 20 20 69 66 20  |ct ) );..    if |
00000170  28 20 6e 65 77 20 29 0a  20 20 20 20 7b 0a 20 20  |( new ).    {.  |
00000180  20 20 20 20 20 20 73 74  72 6e 63 70 79 28 20 6e  |      strncpy( n|
00000190  65 77 2d 3e 73 74 72 2c  20 73 74 72 2c 20 53 54  |ew->str, str, ST|
000001a0  52 4c 45 4e 20 29 3b 0a  0a 20 20 20 20 20 20 20  |RLEN );..       |
000001b0  20 6e 65 77 2d 3e 73 74  72 5b 53 54 52 4c 45 4e  | new->str[STRLEN|
000001c0  5d 20 3d 20 4e 55 4c 4c  3b 0a 20 20 20 20 20 20  |] = NULL;.      |
000001d0  20 20 6e 65 77 2d 3e 6c  6f 77 65 72 20 20 20 20  |  new->lower    |
000001e0  20 20 20 3d 20 4e 55 4c  4c 3b 0a 20 20 20 20 20  |   = NULL;.     |
000001f0  20 20 20 6e 65 77 2d 3e  75 70 70 65 72 20 20 20  |   new->upper   |
00000200  20 20 20 20 3d 20 4e 55  4c 4c 3b 0a 20 20 20 20  |    = NULL;.    |
00000210  20 20 20 20 6e 65 77 2d  3e 63 6f 75 6e 74 20 20  |    new->count  |
00000220  20 20 20 20 20 3d 20 31  3b 0a 20 20 20 20 7d 0a  |     = 1;.    }.|
00000230  20 20 20 20 65 6c 73 65  0a 20 20 20 20 7b 0a 20  |    else.    {. |
00000240  20 20 20 20 20 20 20 70  75 74 73 28 20 22 4f 75  |       puts( "Ou|
00000250  74 20 6f 66 20 6d 65 6d  6f 72 79 21 5c 6e 50 72  |t of memory!\nPr|
00000260  6f 67 72 61 6d 20 74 65  72 6d 69 6e 61 74 65 64  |ogram terminated|
00000270  2e 5c 6e 22 20 29 3b 0a  20 20 20 20 20 20 20 20  |.\n" );.        |
00000280  65 78 69 74 28 20 31 20  29 3b 0a 20 20 20 20 7d  |exit( 1 );.    }|
00000290  0a 0a 20 20 20 20 72 65  74 75 72 6e 20 6e 65 77  |..    return new|
000002a0  3b 0a 7d 0a 0a 0a 76 6f  69 64 20 54 6f 55 70 70  |;.}...void ToUpp|
000002b0  65 72 43 61 73 65 28 20  63 68 61 72 20 2a 77 6f  |erCase( char *wo|
000002c0  72 64 20 29 0a 7b 0a 2f  2a 20 20 43 6f 6e 76 65  |rd ).{./*  Conve|
000002d0  72 74 73 20 74 68 65 20  67 69 76 65 6e 20 77 6f  |rts the given wo|
000002e0  72 64 20 74 6f 20 75 70  70 65 72 20 63 61 73 65  |rd to upper case|
000002f0  2e 20 2a 2f 0a 0a 20 20  20 20 63 68 61 72 20 2a  |. */..    char *|
00000300  70 74 72 20 3d 20 77 6f  72 64 3b 0a 0a 20 20 20  |ptr = word;..   |
00000310  20 77 68 69 6c 65 20 28  20 2a 70 74 72 20 29 0a  | while ( *ptr ).|
00000320  20 20 20 20 7b 0a 20 20  20 20 20 20 20 2a 70 74  |    {.       *pt|
00000330  72 20 3d 20 74 6f 75 70  70 65 72 28 20 2a 70 74  |r = toupper( *pt|
00000340  72 20 29 3b 0a 20 20 20  20 20 20 20 20 70 74 72  |r );.        ptr|
00000350  2b 2b 3b 0a 20 20 20 20  7d 0a 7d 0a 0a 0a 76 6f  |++;.    }.}...vo|
00000360  69 64 20 54 6f 4c 6f 77  65 72 43 61 73 65 28 20  |id ToLowerCase( |
00000370  63 68 61 72 20 2a 77 6f  72 64 20 29 0a 7b 0a 2f  |char *word ).{./|
00000380  2a 20 20 43 6f 6e 76 65  72 74 73 20 74 68 65 20  |*  Converts the |
00000390  67 69 76 65 6e 20 77 6f  72 64 20 74 6f 20 6c 6f  |given word to lo|
000003a0  77 65 72 20 63 61 73 65  2e 20 2a 2f 0a 0a 20 20  |wer case. */..  |
000003b0  20 20 63 68 61 72 20 2a  70 74 72 20 3d 20 77 6f  |  char *ptr = wo|
000003c0  72 64 3b 0a 0a 20 20 20  20 77 68 69 6c 65 20 28  |rd;..    while (|
000003d0  20 2a 70 74 72 20 29 0a  20 20 20 20 7b 0a 20 20  | *ptr ).    {.  |
000003e0  20 20 20 20 20 2a 70 74  72 20 3d 20 74 6f 6c 6f  |     *ptr = tolo|
000003f0  77 65 72 28 20 2a 70 74  72 20 29 3b 0a 20 20 20  |wer( *ptr );.   |
00000400  20 20 20 20 20 70 74 72  2b 2b 3b 0a 20 20 20 20  |     ptr++;.    |
00000410  7d 0a 7d 0a 0a 0a 76 6f  69 64 20 43 61 70 69 74  |}.}...void Capit|
00000420  61 6c 69 73 65 28 20 63  68 61 72 20 2a 73 74 72  |alise( char *str|
00000430  69 6e 67 20 29 0a 7b 0a  2f 2a 20 20 46 6f 72 63  |ing ).{./*  Forc|
00000440  65 73 20 74 68 65 20 66  69 72 73 74 20 63 68 61  |es the first cha|
00000450  72 61 63 74 65 72 20 6f  66 20 74 68 65 20 73 75  |racter of the su|
00000460  70 70 6c 69 65 64 20 73  74 72 69 6e 67 20 74 6f  |pplied string to|
00000470  20 75 70 70 65 72 20 63  61 73 65 2e 20 2a 2f 0a  | upper case. */.|
00000480  0a 20 20 20 20 74 6f 75  70 70 65 72 28 20 2a 73  |.    toupper( *s|
00000490  74 72 69 6e 67 20 29 3b  0a 7d 0a 0a 0a 76 6f 69  |tring );.}...voi|
000004a0  64 20 53 74 6f 72 65 57  6f 72 64 28 20 63 68 61  |d StoreWord( cha|
000004b0  72 20 2a 73 74 72 2c 20  77 6f 72 64 20 2a 6c 69  |r *str, word *li|
000004c0  73 74 20 29 0a 7b 0a 2f  2a 20 20 53 74 6f 72 65  |st ).{./*  Store|
000004d0  73 20 27 73 74 72 27 20  6f 6e 20 74 68 65 20 67  |s 'str' on the g|
000004e0  69 76 65 6e 20 77 6f 72  64 20 6c 69 73 74 2e 20  |iven word list. |
000004f0  20 2a 2f 0a 0a 20 20 20  20 69 66 20 28 20 21 2a  | */..    if ( !*|
00000500  6c 69 73 74 20 29 0a 20  20 20 20 20 20 20 20 2a  |list ).        *|
00000510  6c 69 73 74 20 3d 20 4e  65 77 57 6f 72 64 28 20  |list = NewWord( |
00000520  73 74 72 20 29 3b 0a 20  20 20 20 65 6c 73 65 20  |str );.    else |
00000530  69 66 20 28 20 73 74 72  63 6d 70 28 20 73 74 72  |if ( strcmp( str|
00000540  2c 20 28 2a 6c 69 73 74  29 2d 3e 73 74 72 20 29  |, (*list)->str )|
00000550  20 3e 20 30 20 29 0a 20  20 20 20 20 20 20 20 53  | > 0 ).        S|
00000560  74 6f 72 65 57 6f 72 64  28 20 73 74 72 2c 20 26  |toreWord( str, &|
00000570  28 2a 6c 69 73 74 29 2d  3e 75 70 70 65 72 20 29  |(*list)->upper )|
00000580  3b 0a 20 20 20 20 65 6c  73 65 20 69 66 20 28 20  |;.    else if ( |
00000590  73 74 72 63 6d 70 28 20  73 74 72 2c 20 28 2a 6c  |strcmp( str, (*l|
000005a0  69 73 74 29 2d 3e 73 74  72 20 29 20 3c 20 30 20  |ist)->str ) < 0 |
000005b0  29 0a 20 20 20 20 20 20  20 20 53 74 6f 72 65 57  |).        StoreW|
000005c0  6f 72 64 28 20 73 74 72  2c 20 26 28 2a 6c 69 73  |ord( str, &(*lis|
000005d0  74 29 2d 3e 6c 6f 77 65  72 20 29 3b 0a 20 20 20  |t)->lower );.   |
000005e0  20 65 6c 73 65 0a 20 20  20 20 20 20 20 20 28 2a  | else.        (*|
000005f0  6c 69 73 74 29 2d 3e 63  6f 75 6e 74 2b 2b 3b 0a  |list)->count++;.|
00000600  7d 0a 0a 0a 76 6f 69 64  20 4c 69 73 74 57 6f 72  |}...void ListWor|
00000610  64 73 28 20 77 6f 72 64  20 6c 69 73 74 20 29 0a  |ds( word list ).|
00000620  7b 0a 20 20 20 20 69 66  20 28 20 6c 69 73 74 20  |{.    if ( list |
00000630  29 0a 20 20 20 20 7b 0a  20 20 20 20 20 20 20 20  |).    {.        |
00000640  4c 69 73 74 57 6f 72 64  73 28 20 6c 69 73 74 2d  |ListWords( list-|
00000650  3e 6c 6f 77 65 72 20 29  3b 0a 20 20 20 20 20 20  |>lower );.      |
00000660  20 20 70 72 69 6e 74 66  28 20 22 25 34 69 20 20  |  printf( "%4i  |
00000670  25 73 5c 6e 22 2c 20 6c  69 73 74 2d 3e 63 6f 75  |%s\n", list->cou|
00000680  6e 74 2c 20 6c 69 73 74  2d 3e 73 74 72 20 29 3b  |nt, list->str );|
00000690  0a 20 20 20 20 20 20 20  20 4c 69 73 74 57 6f 72  |.        ListWor|
000006a0  64 73 28 20 6c 69 73 74  2d 3e 75 70 70 65 72 20  |ds( list->upper |
000006b0  29 3b 0a 20 20 20 20 7d  0a 7d 0a 0a 0a 69 6e 74  |);.    }.}...int|
000006c0  20 47 65 74 57 6f 72 64  28 20 63 68 61 72 20 2a  | GetWord( char *|
000006d0  62 75 66 66 65 72 2c 20  69 6e 74 20 6c 65 6e 67  |buffer, int leng|
000006e0  74 68 2c 20 46 49 4c 45  20 2a 73 74 72 65 61 6d  |th, FILE *stream|
000006f0  20 29 0a 7b 0a 20 20 20  20 69 6e 74 20 63 2c 20  | ).{.    int c, |
00000700  69 20 3d 20 30 3b 0a 0a  20 20 20 20 77 68 69 6c  |i = 0;..    whil|
00000710  65 20 28 20 63 20 3d 20  66 67 65 74 63 28 73 74  |e ( c = fgetc(st|
00000720  72 65 61 6d 29 2c 20 63  20 21 3d 20 45 4f 46 20  |ream), c != EOF |
00000730  26 26 20 21 69 73 61 6c  70 68 61 28 63 29 20 29  |&& !isalpha(c) )|
00000740  3b 0a 0a 20 20 20 20 77  68 69 6c 65 20 28 20 63  |;..    while ( c|
00000750  20 21 3d 20 45 4f 46 20  26 26 20 69 73 61 6c 70  | != EOF && isalp|
00000760  68 61 28 63 29 20 29 0a  20 20 20 20 7b 0a 20 20  |ha(c) ).    {.  |
00000770  20 20 20 20 20 20 69 66  20 28 20 69 20 3c 20 6c  |      if ( i < l|
00000780  65 6e 67 74 68 20 29 20  20 62 75 66 66 65 72 5b  |ength )  buffer[|
00000790  69 2b 2b 5d 20 3d 20 63  3b 0a 20 20 20 20 20 20  |i++] = c;.      |
000007a0  20 20 63 20 3d 20 66 67  65 74 63 28 20 73 74 72  |  c = fgetc( str|
000007b0  65 61 6d 20 29 3b 0a 20  20 20 20 7d 0a 0a 20 20  |eam );.    }..  |
000007c0  20 20 62 75 66 66 65 72  5b 69 5d 20 3d 20 4e 55  |  buffer[i] = NU|
000007d0  4c 4c 3b 0a 0a 20 20 20  20 72 65 74 75 72 6e 20  |LL;..    return |
000007e0  69 3b 0a 7d 0a                                    |i;.}.|
000007e5