Home » Recent acquisitions » Acorn ADFS disks » adfs_ArchimedesWorld_199206b.adf » June92 » !AWJune92/Goodies/AllAtC/work/c/lexical

!AWJune92/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_199206b.adf » June92
Filename: !AWJune92/Goodies/AllAtC/work/c/lexical
Read OK:
File size: 0881 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, 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 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 2c 20 69 6e 74 20 72  |word list, int r|
00000640  65 70 65 61 74 73 20 29  0a 7b 0a 20 20 20 20 69  |epeats ).{.    i|
00000650  66 20 28 20 6c 69 73 74  20 29 0a 20 20 20 20 7b  |f ( list ).    {|
00000660  0a 20 20 20 20 20 20 20  20 4c 69 73 74 57 6f 72  |.        ListWor|
00000670  64 73 28 20 66 2c 20 6c  69 73 74 2d 3e 6c 6f 77  |ds( f, list->low|
00000680  65 72 2c 20 72 65 70 65  61 74 73 20 29 3b 0a 0a  |er, repeats );..|
00000690  20 20 20 20 20 20 20 20  69 66 20 28 20 72 65 70  |        if ( rep|
000006a0  65 61 74 73 20 29 0a 20  20 20 20 20 20 20 20 20  |eats ).         |
000006b0  20 20 20 66 70 72 69 6e  74 66 28 20 66 2c 20 22  |   fprintf( f, "|
000006c0  25 34 69 20 20 25 73 5c  6e 22 2c 20 6c 69 73 74  |%4i  %s\n", list|
000006d0  2d 3e 63 6f 75 6e 74 2c  20 6c 69 73 74 2d 3e 73  |->count, list->s|
000006e0  74 72 20 29 3b 0a 20 20  20 20 20 20 20 20 65 6c  |tr );.        el|
000006f0  73 65 0a 20 20 20 20 20  20 20 20 20 20 20 20 66  |se.            f|
00000700  70 72 69 6e 74 66 28 20  66 2c 20 22 25 73 5c 6e  |printf( f, "%s\n|
00000710  22 2c 20 6c 69 73 74 2d  3e 73 74 72 20 29 3b 0a  |", list->str );.|
00000720  0a 20 20 20 20 20 20 20  20 4c 69 73 74 57 6f 72  |.        ListWor|
00000730  64 73 28 20 66 2c 20 6c  69 73 74 2d 3e 75 70 70  |ds( f, list->upp|
00000740  65 72 2c 20 72 65 70 65  61 74 73 20 29 3b 0a 20  |er, repeats );. |
00000750  20 20 20 7d 0a 7d 0a 0a  0a 69 6e 74 20 47 65 74  |   }.}...int Get|
00000760  57 6f 72 64 28 20 63 68  61 72 20 2a 62 75 66 66  |Word( char *buff|
00000770  65 72 2c 20 69 6e 74 20  6c 65 6e 67 74 68 2c 20  |er, int length, |
00000780  46 49 4c 45 20 2a 73 74  72 65 61 6d 20 29 0a 7b  |FILE *stream ).{|
00000790  0a 20 20 20 20 69 6e 74  20 63 2c 20 69 20 3d 20  |.    int c, i = |
000007a0  30 3b 0a 0a 20 20 20 20  77 68 69 6c 65 20 28 20  |0;..    while ( |
000007b0  63 20 3d 20 66 67 65 74  63 28 73 74 72 65 61 6d  |c = fgetc(stream|
000007c0  29 2c 20 63 20 21 3d 20  45 4f 46 20 26 26 20 21  |), c != EOF && !|
000007d0  69 73 61 6c 70 68 61 28  63 29 20 29 3b 0a 0a 20  |isalpha(c) );.. |
000007e0  20 20 20 77 68 69 6c 65  20 28 20 63 20 21 3d 20  |   while ( c != |
000007f0  45 4f 46 20 26 26 20 69  73 61 6c 70 68 61 28 63  |EOF && isalpha(c|
00000800  29 20 29 0a 20 20 20 20  7b 0a 20 20 20 20 20 20  |) ).    {.      |
00000810  20 20 69 66 20 28 20 69  20 3c 20 6c 65 6e 67 74  |  if ( i < lengt|
00000820  68 20 29 20 20 62 75 66  66 65 72 5b 69 2b 2b 5d  |h )  buffer[i++]|
00000830  20 3d 20 63 3b 0a 20 20  20 20 20 20 20 20 63 20  | = c;.        c |
00000840  3d 20 66 67 65 74 63 28  20 73 74 72 65 61 6d 20  |= fgetc( stream |
00000850  29 3b 0a 20 20 20 20 7d  0a 0a 20 20 20 20 62 75  |);.    }..    bu|
00000860  66 66 65 72 5b 69 5d 20  3d 20 4e 55 4c 4c 3b 0a  |ffer[i] = NULL;.|
00000870  0a 20 20 20 20 72 65 74  75 72 6e 20 69 3b 0a 7d  |.    return i;.}|
00000880  0a                                                |.|
00000881