Home » Archimedes archive » Archimedes World » AW-1991-10.adf » October91 » !AWOct91/Goodies/AllAtC/work/c/lexical
!AWOct91/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 » Archimedes archive » Archimedes World » AW-1991-10.adf » October91 |
Filename: | !AWOct91/Goodies/AllAtC/work/c/lexical |
Read OK: | ✔ |
File size: | 06A8 bytes |
Load address: | 0000 |
Exec address: | 0000 |
File contents
/* Lexical Functions. */ /* Archimedes World, August 1991. */ #include "lexical.h" #include <ctype.h> #include <stdlib.h> #include <string.h> struct word_struct { string str; word upper; word lower; }; 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; } 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 ) { if ( !*list ) *list = NewWord( str ); else if ( strcmp( str, (*list)->str ) > 0 ) StoreWord( str, &(*list)->upper ); else StoreWord( str, &(*list)->lower ); } void ListWords( word list ) { if ( !list ) return; ListWords( list->lower ); puts( list->str ); ListWords( list->upper ); } void GetWord( char *buffer, int length, FILE *stream ) { char c, i = 0; while ( c = fgetc( stream ), c != '\n' ) if ( i < length ) buffer[i++] = c; buffer[i] = NULL; }
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 41 75 67 75 73 74 20 31 39 39 31 2e 20 |d, August 1991. | 00000050 20 20 20 20 20 20 20 2a 2f 0a 0a 23 69 6e 63 6c | */..#incl| 00000060 75 64 65 20 22 6c 65 78 69 63 61 6c 2e 68 22 0a |ude "lexical.h".| 00000070 23 69 6e 63 6c 75 64 65 20 3c 63 74 79 70 65 2e |#include <ctype.| 00000080 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 73 74 64 |h>.#include <std| 00000090 6c 69 62 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 |lib.h>.#include | 000000a0 3c 73 74 72 69 6e 67 2e 68 3e 0a 0a 0a 73 74 72 |<string.h>...str| 000000b0 75 63 74 20 77 6f 72 64 5f 73 74 72 75 63 74 0a |uct word_struct.| 000000c0 7b 0a 20 20 20 20 73 74 72 69 6e 67 20 73 74 72 |{. string str| 000000d0 3b 0a 20 20 20 20 77 6f 72 64 20 20 20 75 70 70 |;. word upp| 000000e0 65 72 3b 0a 20 20 20 20 77 6f 72 64 20 20 20 6c |er;. word l| 000000f0 6f 77 65 72 3b 0a 7d 3b 0a 0a 0a 73 74 61 74 69 |ower;.};...stati| 00000100 63 20 77 6f 72 64 20 4e 65 77 57 6f 72 64 28 20 |c word NewWord( | 00000110 63 68 61 72 20 2a 73 74 72 20 29 0a 7b 0a 20 20 |char *str ).{. | 00000120 20 20 77 6f 72 64 20 6e 65 77 20 3d 20 6d 61 6c | word new = mal| 00000130 6c 6f 63 28 20 73 69 7a 65 6f 66 28 20 73 74 72 |loc( sizeof( str| 00000140 75 63 74 20 77 6f 72 64 5f 73 74 72 75 63 74 20 |uct word_struct | 00000150 29 20 29 3b 0a 0a 20 20 20 20 69 66 20 28 20 6e |) );.. if ( n| 00000160 65 77 20 29 0a 20 20 20 20 7b 0a 20 20 20 20 20 |ew ). {. | 00000170 20 20 20 73 74 72 6e 63 70 79 28 20 6e 65 77 2d | strncpy( new-| 00000180 3e 73 74 72 2c 20 73 74 72 2c 20 53 54 52 4c 45 |>str, str, STRLE| 00000190 4e 20 29 3b 0a 0a 20 20 20 20 20 20 20 20 6e 65 |N );.. ne| 000001a0 77 2d 3e 73 74 72 5b 53 54 52 4c 45 4e 5d 20 3d |w->str[STRLEN] =| 000001b0 20 4e 55 4c 4c 3b 0a 20 20 20 20 20 20 20 20 6e | NULL;. n| 000001c0 65 77 2d 3e 6c 6f 77 65 72 20 20 20 20 20 20 20 |ew->lower | 000001d0 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 20 20 20 20 |= NULL;. | 000001e0 6e 65 77 2d 3e 75 70 70 65 72 20 20 20 20 20 20 |new->upper | 000001f0 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 7d 0a 20 | = NULL;. }. | 00000200 20 20 20 65 6c 73 65 0a 20 20 20 20 7b 0a 20 20 | else. {. | 00000210 20 20 20 20 20 20 70 75 74 73 28 20 22 4f 75 74 | puts( "Out| 00000220 20 6f 66 20 6d 65 6d 6f 72 79 21 5c 6e 50 72 6f | of memory!\nPro| 00000230 67 72 61 6d 20 74 65 72 6d 69 6e 61 74 65 64 2e |gram terminated.| 00000240 5c 6e 22 20 29 3b 0a 20 20 20 20 20 20 20 20 65 |\n" );. e| 00000250 78 69 74 28 20 31 20 29 3b 0a 20 20 20 20 7d 0a |xit( 1 );. }.| 00000260 0a 20 20 20 20 72 65 74 75 72 6e 20 6e 65 77 3b |. return new;| 00000270 0a 7d 0a 0a 0a 76 6f 69 64 20 54 6f 55 70 70 65 |.}...void ToUppe| 00000280 72 43 61 73 65 28 20 63 68 61 72 20 2a 77 6f 72 |rCase( char *wor| 00000290 64 20 29 0a 7b 0a 2f 2a 20 20 43 6f 6e 76 65 72 |d ).{./* Conver| 000002a0 74 73 20 74 68 65 20 67 69 76 65 6e 20 77 6f 72 |ts the given wor| 000002b0 64 20 74 6f 20 75 70 70 65 72 20 63 61 73 65 2e |d to upper case.| 000002c0 20 2a 2f 0a 0a 20 20 20 20 63 68 61 72 20 2a 70 | */.. char *p| 000002d0 74 72 20 3d 20 77 6f 72 64 3b 0a 0a 20 20 20 20 |tr = word;.. | 000002e0 77 68 69 6c 65 20 28 20 2a 70 74 72 20 29 0a 20 |while ( *ptr ). | 000002f0 20 20 20 7b 0a 20 20 20 20 20 20 20 2a 70 74 72 | {. *ptr| 00000300 20 3d 20 74 6f 75 70 70 65 72 28 20 2a 70 74 72 | = toupper( *ptr| 00000310 20 29 3b 0a 20 20 20 20 20 20 20 20 70 74 72 2b | );. ptr+| 00000320 2b 3b 0a 20 20 20 20 7d 0a 7d 0a 0a 0a 76 6f 69 |+;. }.}...voi| 00000330 64 20 54 6f 4c 6f 77 65 72 43 61 73 65 28 20 63 |d ToLowerCase( c| 00000340 68 61 72 20 2a 77 6f 72 64 20 29 0a 7b 0a 2f 2a |har *word ).{./*| 00000350 20 20 43 6f 6e 76 65 72 74 73 20 74 68 65 20 67 | Converts the g| 00000360 69 76 65 6e 20 77 6f 72 64 20 74 6f 20 6c 6f 77 |iven word to low| 00000370 65 72 20 63 61 73 65 2e 20 2a 2f 0a 0a 20 20 20 |er case. */.. | 00000380 20 63 68 61 72 20 2a 70 74 72 20 3d 20 77 6f 72 | char *ptr = wor| 00000390 64 3b 0a 0a 20 20 20 20 77 68 69 6c 65 20 28 20 |d;.. while ( | 000003a0 2a 70 74 72 20 29 0a 20 20 20 20 7b 0a 20 20 20 |*ptr ). {. | 000003b0 20 20 20 20 2a 70 74 72 20 3d 20 74 6f 6c 6f 77 | *ptr = tolow| 000003c0 65 72 28 20 2a 70 74 72 20 29 3b 0a 20 20 20 20 |er( *ptr );. | 000003d0 20 20 20 20 70 74 72 2b 2b 3b 0a 20 20 20 20 7d | ptr++;. }| 000003e0 0a 7d 0a 0a 0a 76 6f 69 64 20 43 61 70 69 74 61 |.}...void Capita| 000003f0 6c 69 73 65 28 20 63 68 61 72 20 2a 73 74 72 69 |lise( char *stri| 00000400 6e 67 20 29 0a 7b 0a 2f 2a 20 20 46 6f 72 63 65 |ng ).{./* Force| 00000410 73 20 74 68 65 20 66 69 72 73 74 20 63 68 61 72 |s the first char| 00000420 61 63 74 65 72 20 6f 66 20 74 68 65 20 73 75 70 |acter of the sup| 00000430 70 6c 69 65 64 20 73 74 72 69 6e 67 20 74 6f 20 |plied string to | 00000440 75 70 70 65 72 20 63 61 73 65 2e 20 2a 2f 0a 0a |upper case. */..| 00000450 20 20 20 20 74 6f 75 70 70 65 72 28 20 2a 73 74 | toupper( *st| 00000460 72 69 6e 67 20 29 3b 0a 7d 0a 0a 0a 76 6f 69 64 |ring );.}...void| 00000470 20 53 74 6f 72 65 57 6f 72 64 28 20 63 68 61 72 | StoreWord( char| 00000480 20 2a 73 74 72 2c 20 77 6f 72 64 20 2a 6c 69 73 | *str, word *lis| 00000490 74 20 29 0a 7b 0a 20 20 20 20 69 66 20 28 20 21 |t ).{. if ( !| 000004a0 2a 6c 69 73 74 20 29 0a 20 20 20 20 20 20 20 20 |*list ). | 000004b0 2a 6c 69 73 74 20 3d 20 4e 65 77 57 6f 72 64 28 |*list = NewWord(| 000004c0 20 73 74 72 20 29 3b 0a 20 20 20 20 65 6c 73 65 | str );. else| 000004d0 20 69 66 20 28 20 73 74 72 63 6d 70 28 20 73 74 | if ( strcmp( st| 000004e0 72 2c 20 28 2a 6c 69 73 74 29 2d 3e 73 74 72 20 |r, (*list)->str | 000004f0 29 20 3e 20 30 20 29 0a 20 20 20 20 20 20 20 20 |) > 0 ). | 00000500 53 74 6f 72 65 57 6f 72 64 28 20 73 74 72 2c 20 |StoreWord( str, | 00000510 26 28 2a 6c 69 73 74 29 2d 3e 75 70 70 65 72 20 |&(*list)->upper | 00000520 29 3b 0a 20 20 20 20 65 6c 73 65 0a 20 20 20 20 |);. else. | 00000530 20 20 20 20 53 74 6f 72 65 57 6f 72 64 28 20 73 | StoreWord( s| 00000540 74 72 2c 20 26 28 2a 6c 69 73 74 29 2d 3e 6c 6f |tr, &(*list)->lo| 00000550 77 65 72 20 29 3b 0a 7d 0a 0a 0a 76 6f 69 64 20 |wer );.}...void | 00000560 4c 69 73 74 57 6f 72 64 73 28 20 77 6f 72 64 20 |ListWords( word | 00000570 6c 69 73 74 20 29 0a 7b 0a 20 20 20 20 69 66 20 |list ).{. if | 00000580 28 20 21 6c 69 73 74 20 29 20 72 65 74 75 72 6e |( !list ) return| 00000590 3b 0a 0a 20 20 20 20 4c 69 73 74 57 6f 72 64 73 |;.. ListWords| 000005a0 28 20 6c 69 73 74 2d 3e 6c 6f 77 65 72 20 29 3b |( list->lower );| 000005b0 0a 20 20 20 20 70 75 74 73 28 20 6c 69 73 74 2d |. puts( list-| 000005c0 3e 73 74 72 20 29 3b 0a 20 20 20 20 4c 69 73 74 |>str );. List| 000005d0 57 6f 72 64 73 28 20 6c 69 73 74 2d 3e 75 70 70 |Words( list->upp| 000005e0 65 72 20 29 3b 0a 7d 0a 0a 0a 76 6f 69 64 20 47 |er );.}...void G| 000005f0 65 74 57 6f 72 64 28 20 63 68 61 72 20 2a 62 75 |etWord( char *bu| 00000600 66 66 65 72 2c 20 69 6e 74 20 6c 65 6e 67 74 68 |ffer, int length| 00000610 2c 20 46 49 4c 45 20 2a 73 74 72 65 61 6d 20 29 |, FILE *stream )| 00000620 0a 7b 0a 20 20 20 20 63 68 61 72 20 63 2c 20 69 |.{. char c, i| 00000630 20 3d 20 30 3b 0a 0a 20 20 20 20 77 68 69 6c 65 | = 0;.. while| 00000640 20 28 20 63 20 3d 20 66 67 65 74 63 28 20 73 74 | ( c = fgetc( st| 00000650 72 65 61 6d 20 29 2c 20 63 20 21 3d 20 27 5c 6e |ream ), c != '\n| 00000660 27 20 29 0a 20 20 20 20 20 20 20 20 69 66 20 28 |' ). if (| 00000670 20 69 20 3c 20 6c 65 6e 67 74 68 20 29 20 62 75 | i < length ) bu| 00000680 66 66 65 72 5b 69 2b 2b 5d 20 3d 20 63 3b 0a 0a |ffer[i++] = c;..| 00000690 20 20 20 20 62 75 66 66 65 72 5b 69 5d 20 3d 20 | buffer[i] = | 000006a0 4e 55 4c 4c 3b 0a 7d 0a |NULL;.}.| 000006a8