Could someone help me figure out how to put my project together. I
can't get my mind wrapped around the creation of the 4 double Linked
Lists. Thank your for your insight.
1. Create 4 double linked lists as follows:
(a) A double linked list called NAMES which will contain all C like
identifiers of less than
256 characters long identified in the input file F. Each identifier
will be represented by
a triple (I, length, string) where I is used to identify the type of
the object as being an
identifier, length is an integer representing the length of the
identifier in characters, and
string is a string of characters representing the identifier itself.
(b) A double linked list called NUMBERS which will contain all C like
numbers of less than
256 characters long identified in the input file F. Each number will be
represented by a
triple (N, length, string) where N is used to identify the type of the
objects as being a
number, length is an integer representing the length of the number in
characters, and
string is the string of characters representing the number itself.
(c) A double linked list called SEPARATORS which will contain all C
language separators
and operators identified in the input file F. Each separator will be
represented by a triple
(X, length, string) where X is S for separators, O for operators, R for
symbols denoting
relations, L for new line and E for end of file. The length is an
integer representing the
length of the symbol and string is the string of characters
representing the symbol itself.
(d) A double linked list called UNKNOWN which will contain all symbols
you discovered in
the file F which could not be classi ed in the lists NAMES, NUMBERS,
SEPARATORS.
Each object in this list will be represented by a triple (Y, length,
string) where Y is used
to signify that an unknown symbol was discovered, length is an integer
representing the
length in characters of the unknown symbol, and string is the string of
characters that
make up the unknown symbol itself.
The input file F will be an arbitrary text file in your directory. The
actions of list creation and list printing will be programmed as
functions which will be called from a main program. Use in your program
the functions list(H, T), to create an empty list L whose header is H
and tail is T, and append(L, OBJ), to append the object OBJ to the list
L.
Here are the three files that i have created:
main.c
#include <stdio.h>
#include <stdlib.h>
#include scan.h
#define null 0
#define true 1
#define false 0
struct NAMES{
char Name[256];
struct NAMES *Next;
struct NAMES *Prev;
};
typedef struct NAMES NAMES;
typedef struct NAMES header;
typdef header *headpt;
header *list(h,t)
header *h, *t;
{
h->Prev=null;
h->Next=t;
h->Name=null;
t->Prev=h;
t->Next=null;
t->Name=null;
return(h);
}
append (l,obj)
header *l;
NAME *obj;
{
NAME *q=l->Next;
while(q->Next != null)
q=q->Next;
obj->Prev=q->Prev;
obj->Next=q;
q->Prev=obj;
q=obj->Prev;
q->Next=obj;
}
printlist(pt)
header *pt;
{
object *q;
int i;
printf("\n->")
q=pt->Next;
while(q->Next != null)
{
i=q->Prev;
printf("%d->",i);
q=q->Next;
}
printf("\n");
}
main (argc, argv)
int argc;
char *argv[];
{
extern TKN get_token(FILE *);
TKN Token;
FILE *Input;
int TokenNr = 1, LineNr = 1, Done = 0, k;
Input = fopen(argv[1], "r");
while (!Done)
{
Token = get_token( Input );
switch (Token.Code)
{
case 'I':
{
/* process identifier */
printf("(%d,%d) Symbol: Identifier %s\n",TokenNr,
LineNr,Token.String);
TokenNr = TokenNr+1;
break;
}
case 'N':
{
/* process integer number */
printf("(%d,%d) Symbol: Integer number
%s\n",TokenNr,LineNr,Token.String);
TokenNr = TokenNr+1;
break;
}
case 'F':
{
/* process real number */
printf("(%d,%d) Symbol: Real number %s\n",TokenNr,LineNr,
Token.String);
TokenNr = TokenNr+1;
break;
}
case 'W':
{
printf("White symbol received\n");
break;
}
case 'L':
{
printf("New line symbol received\n");
LineNr = LineNr+1;
TokenNr = 1;
}
case 'U':
{
if (Token.String[0] == 'Z')
Done = 1;
else
printf("Unprintable character
discovered\n");
break;
}
case 'O':
{
printf("(%d,%d) Symbol: Separator
%s\n",TokenNr,LineNr,Token.String);
TokenNr = TokenNr+1;
break;
}
case 'E':
{
printf("Error condition: %s\n",
Token.String);
break;
}
}
} /* end while */
}
scan.c
/* This scanner recognizes identifiers returning the token I */
/* integers returing the token N, reals returning the token F, */
/* white spaces (blancs and tabs mapped into just one white */
/* space) returning the token W, unprintable characters returning */
/* the token U, and other characters, returning the token O. */
/* In all casses the recognized lexeme is stored in the string */
/* variable Toke.String. */
#include "scan.h"
TKN get_token (FILE *Input )
{
typedef struct pt_entry
{
int Action,
Data;
} pt_node;
static pt_node PT[ 11 ][ 8 ] = {
{{ 'S', 9 }, { 'S', 8 }, { 'S', 2 }, { 'S', 1 },
{ 'S', 1 }, { 'S', 8 }, { 'S', 8 }, { 'S', 10 }},
{{ 'A', 'I' }, { 'A', 'I' }, { 'S', 1 }, { 'S', 1 },
{ 'S', 1 }, { 'A', 'I' }, { 'A', 'I' }, { 'A', 'I' }},
{{ 'A', 'N' }, { 'A', 'N' }, { 'S', 2 }, { 'S', 5 },
{ 'A', 'N' }, { 'S', 3 }, { 'A', 'N' }, { 'A', 'N' }},
{{ 'A', 'E' }, { 'A', 'E' }, { 'S', 4 }, { 'A', 'E' },
{ 'A', 'E' }, { 'A', 'E' }, { 'A', 'E' }, { 'A', 'E' }},
{{ 'A', 'F' }, { 'A', 'F' }, { 'S', 4 }, { 'S', 5 },
{ 'A', 'F' }, { 'A', 'F' }, { 'A', 'F' }, { 'A', 'F' }},
{{ 'A', 'E' }, { 'A', 'E' }, { 'S', 7 }, { 'A', 'E' },
{ 'A', 'E' }, { 'A', 'E' }, { 'S', 6 }, { 'A', 'E' }},
{{ 'A', 'E' }, { 'A', 'E' }, { 'S', 7 }, { 'A', 'E' },
{ 'A', 'E' }, { 'A', 'E' }, { 'A', 'E' }, { 'A', 'E' }},
{{ 'A', 'F' }, { 'A', 'F' }, { 'S', 7 }, { 'A', 'F' },
{ 'A', 'F' }, { 'A', 'F' }, { 'A', 'F' }, { 'A', 'F' }},
{{ 'A', 'O' }, { 'A', 'O' }, { 'A', 'O' }, { 'A', 'O' },
{ 'A', 'O' }, { 'A', 'O' }, { 'A', 'O' }, { 'A', 'O' }},
{{ 'A', 'U' }, { 'A', 'U' }, { 'A', 'U' }, { 'A', 'U' },
{ 'A', 'U' }, { 'A', 'U' }, { 'A', 'U' }, { 'A', 'U' }},
{{ 'A', 'W' }, { 'A', 'W' }, { 'A', 'W' }, { 'A', 'W' },
{ 'A', 'W' }, { 'A', 'W' }, { 'A', 'W' }, { 'S', 10 }} };
static int TOKENS[ ] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
7, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 6, 1, 6, 5, 1,
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1,
1, 4, 4, 4, 4, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 1, 1, 1, 1, 1,
1, 4, 4, 4, 4, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 1, 1, 1, 1, 0 };
int C, R = 0, S = 0, ch;
TKN Tkn;
ch = getc(Input);
if (( ch != EOF ) && (ch != '\n'))
C = TOKENS[ ch ];
else if (ch == EOF)
{
Tkn.Code = 'U';
Tkn.String[ 0 ] = 'Z';
Tkn.String[ 1 ] = '\0';
return ( Tkn );
}
else
{
Tkn.Code = 'L';
Tkn.String[0] = '\n';
Tkn.String[1] = '\0';
return (Tkn);
}
while (1)
if ( PT[ R ][ C ].Action == 'S' )
{
Tkn.String[ S++ ] = ch;
ch = getc(Input);
if (ch == '\n')
{
Tkn.String[ S ] = '\0';
R = PT[ R ][ C ].Data;
C = TOKENS[ ch ];
Tkn.Code = PT[ R ][ C ].Data;
ungetc(ch, Input);
return (Tkn);
}
else
{
R = PT[ R ][ C ].Data;
C = TOKENS[ ch ];
}
}
else
{
Tkn.String[ S ] = '\0';
Tkn.Code = PT[ R ][ C ].Data;
ungetc(ch, Input);
return (Tkn);
}
}
scan.h
#include <stdio.h>
#define MAX_STRING 32
typedef struct t_node
{
char Code;
char String[ MAX_STRING ];
} TKN, *TKN_PTR;