Home » Recent acquisitions » Acorn ADFS disks » adfs_AcornUser_199512_2.adf » !Regulars » Regulars/StarInfo/Seery/c/lex
Regulars/StarInfo/Seery/c/lex
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_AcornUser_199512_2.adf » !Regulars |
Filename: | Regulars/StarInfo/Seery/c/lex |
Read OK: | ✔ |
File size: | 15BE bytes |
Load address: | 0000 |
Exec address: | 0000 |
File contents
/** lex.c ******************************************************************** * * Lexical analyser function for 'fittapes' tape fitting system. * Splits input into tokens and passes these to the parser for grammatical * analysis. Recognizes keywords, identifiers, string literals, and * numerical constants. * * (c) David Seery 1995 * *****************************************************************************/ #include <stdlib.h> #include <stdio.h> #include <ctype.h> #include <string.h> #include "fittapes.h" /** Globals ******************************************************************/ void lex(char *file); /** Locals *******************************************************************/ static void getLexeme(FILE *ifp); static int getChar(FILE *ifp); static int nextChar(FILE *ifp); static void rebuffer(FILE *ifp); #define MAX_LINE_SIZE 1024 #define MAX_LVAL_SIZE 256 #define MAX_FILENAME_LENGTH 256 static char sourceFile[MAX_FILENAME_LENGTH] = {0}; static int sourceLine = 0; static char inputLine[MAX_LINE_SIZE] = {0}; static int inputPos = 0; static char yylval[MAX_LVAL_SIZE] = {0}; static boolean atEof = false; static char keywords[17][11] = { "begin","end","video","e480","e360","e240","e180","e120","e60","categories", "programme","from","starts","ends","category","precedes","follows" }; /*****************************************************************************/ void lex(char *file) { FILE *ifp; Lexeme ll; if ((ifp = fopen(file,"r")) == NULL) { printf("[error]: file '%s' won't open\n",file); exit(EXIT_FAILURE); } strcpy(sourceFile,file); sourceLine = 0; while (feof(ifp) == 0) { getLexeme(ifp); } fclose(ifp); ll = malloc(sizeof(lexeme_instance)); ll->lexeme_type = endOfStream; ll->source_file = malloc(strlen(sourceFile) + 1); strcpy(ll->source_file,sourceFile); ll->source_line = sourceLine; ll->next = NULL; if (lexList_head == NULL) { ll->prev = NULL; lexList_head = lexList_tail = ll; } else { ll->prev = lexList_tail; lexList_tail->next = ll; lexList_tail = ll; } } /*****************************************************************************/ static void getLexeme(FILE *ifp) { int ch = 0; char *pch; Lexeme ll; int i; boolean link = true; ll = malloc(sizeof(lexeme_instance)); yylval[0] = '\0'; while (feof(ifp) == 0 && (ch = nextChar(ifp),ch != EOF && ch <= ' ')) { getChar(ifp); } if (feof(ifp) != 0 || ch == EOF) { free(ll); return; } ch = getChar(ifp); if (isalpha(ch) || ch == '_') { /* identifier or keyword */ *(pch = yylval) = ch; pch++; while (isalpha(nextChar(ifp)) || isdigit(nextChar(ifp)) || nextChar(ifp) == '_') { *pch++ = getChar(ifp); } *pch = '\0'; ll->lexeme_type = -1; for (i = begin; i <= follows; i++) { if (strcmp(keywords[i],yylval) == 0) { ll->lexeme_type = i; break; } } if (ll->lexeme_type == -1) { ll->lexeme_type = identifier; ll->lexeme_value.identifierValue = malloc(strlen(yylval) + 1); strcpy(ll->lexeme_value.identifierValue,yylval); } } else if (isdigit(ch)) { /* numerical constant */ *(pch = yylval) = ch; pch++; while (isdigit(nextChar(ifp))) { *pch++ = getChar(ifp); } *pch = '\0'; ll->lexeme_type = constant; ll->lexeme_value.constantValue = atoi(yylval); } else if (ch == '\"') { /* string literal */ pch = yylval; while (nextChar(ifp) != '\"') { *pch++ = getChar(ifp); } *pch = '\0'; getChar(ifp); ll->lexeme_type = stringLiteral; ll->lexeme_value.stringLiteralValue = malloc(strlen(yylval) + 1); strcpy(ll->lexeme_value.stringLiteralValue,yylval); } else { /* punctutator or illegal */ switch (ch) { case '{' : ll->lexeme_type = leftBrace; break; case '}' : ll->lexeme_type = rightBrace; break; case ',' : ll->lexeme_type = comma; break; case ';' : ll->lexeme_type = semiColon; break; default : printf("[warning]: invalid character in input, hex value = 0x%.2x\n", ch); link = false; break; } } if (link) { ll->source_file = malloc(strlen(sourceFile) + 1); strcpy(ll->source_file,sourceFile); ll->source_line = sourceLine; ll->next = NULL; if (lexList_head == NULL) { ll->prev = NULL; lexList_head = lexList_tail = ll; } else { ll->prev = lexList_tail; lexList_tail->next = ll; lexList_tail = ll; } } else { free(ll); } } static int nextChar(FILE *ifp) { if (inputLine[inputPos] == '\0') { rebuffer(ifp); } if (atEof) { return (EOF); } return (inputLine[inputPos]); } static int getChar(FILE *ifp) { char rval = nextChar(ifp); inputPos++; return (rval); } static void rebuffer(FILE *ifp) { char *ch; if (fgets(inputLine,MAX_LINE_SIZE,ifp) == NULL) { if (atEof) { printf("[error]: premature end of file in '%s'\n",sourceFile); exit(EXIT_FAILURE); } else { atEof = true; } } else { sourceLine++; inputPos = '\0'; inputLine[MAX_LINE_SIZE - 1] = '\0'; ch = inputLine; while (*ch != '\n' && *ch != '\0') { ch++; } if (*ch != '\n') { printf("[warning]: line %d too long\n",sourceLine); } } }
00000000 2f 2a 2a 20 6c 65 78 2e 63 20 2a 2a 2a 2a 2a 2a |/** lex.c ******| 00000010 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a |****************| * 00000040 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 0a 20 |**************. | 00000050 2a 0a 20 2a 20 20 4c 65 78 69 63 61 6c 20 61 6e |*. * Lexical an| 00000060 61 6c 79 73 65 72 20 66 75 6e 63 74 69 6f 6e 20 |alyser function | 00000070 66 6f 72 20 27 66 69 74 74 61 70 65 73 27 20 74 |for 'fittapes' t| 00000080 61 70 65 20 66 69 74 74 69 6e 67 20 73 79 73 74 |ape fitting syst| 00000090 65 6d 2e 0a 20 2a 20 20 53 70 6c 69 74 73 20 69 |em.. * Splits i| 000000a0 6e 70 75 74 20 69 6e 74 6f 20 74 6f 6b 65 6e 73 |nput into tokens| 000000b0 20 61 6e 64 20 70 61 73 73 65 73 20 74 68 65 73 | and passes thes| 000000c0 65 20 74 6f 20 74 68 65 20 70 61 72 73 65 72 20 |e to the parser | 000000d0 66 6f 72 20 67 72 61 6d 6d 61 74 69 63 61 6c 0a |for grammatical.| 000000e0 20 2a 20 20 61 6e 61 6c 79 73 69 73 2e 20 20 52 | * analysis. R| 000000f0 65 63 6f 67 6e 69 7a 65 73 20 6b 65 79 77 6f 72 |ecognizes keywor| 00000100 64 73 2c 20 69 64 65 6e 74 69 66 69 65 72 73 2c |ds, identifiers,| 00000110 20 73 74 72 69 6e 67 20 6c 69 74 65 72 61 6c 73 | string literals| 00000120 2c 20 61 6e 64 0a 20 2a 20 20 6e 75 6d 65 72 69 |, and. * numeri| 00000130 63 61 6c 20 63 6f 6e 73 74 61 6e 74 73 2e 0a 20 |cal constants.. | 00000140 2a 0a 20 2a 20 20 28 63 29 20 44 61 76 69 64 20 |*. * (c) David | 00000150 53 65 65 72 79 20 31 39 39 35 0a 20 2a 0a 20 2a |Seery 1995. *. *| 00000160 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a |****************| * 000001a0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 23 |************/..#| 000001b0 69 6e 63 6c 75 64 65 20 3c 73 74 64 6c 69 62 2e |include <stdlib.| 000001c0 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 73 74 64 |h>.#include <std| 000001d0 69 6f 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c |io.h>.#include <| 000001e0 63 74 79 70 65 2e 68 3e 0a 23 69 6e 63 6c 75 64 |ctype.h>.#includ| 000001f0 65 20 3c 73 74 72 69 6e 67 2e 68 3e 0a 0a 23 69 |e <string.h>..#i| 00000200 6e 63 6c 75 64 65 20 22 66 69 74 74 61 70 65 73 |nclude "fittapes| 00000210 2e 68 22 0a 0a 2f 2a 2a 20 47 6c 6f 62 61 6c 73 |.h"../** Globals| 00000220 20 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a | ***************| 00000230 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a |****************| * 00000260 2a 2a 2a 2f 0a 0a 76 6f 69 64 20 6c 65 78 28 63 |***/..void lex(c| 00000270 68 61 72 20 2a 66 69 6c 65 29 3b 0a 0a 2f 2a 2a |har *file);../**| 00000280 20 4c 6f 63 61 6c 73 20 2a 2a 2a 2a 2a 2a 2a 2a | Locals ********| 00000290 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a |****************| * 000002c0 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 73 74 |***********/..st| 000002d0 61 74 69 63 20 76 6f 69 64 20 67 65 74 4c 65 78 |atic void getLex| 000002e0 65 6d 65 28 46 49 4c 45 20 2a 69 66 70 29 3b 0a |eme(FILE *ifp);.| 000002f0 73 74 61 74 69 63 20 69 6e 74 20 67 65 74 43 68 |static int getCh| 00000300 61 72 28 46 49 4c 45 20 2a 69 66 70 29 3b 0a 73 |ar(FILE *ifp);.s| 00000310 74 61 74 69 63 20 69 6e 74 20 6e 65 78 74 43 68 |tatic int nextCh| 00000320 61 72 28 46 49 4c 45 20 2a 69 66 70 29 3b 0a 73 |ar(FILE *ifp);.s| 00000330 74 61 74 69 63 20 76 6f 69 64 20 72 65 62 75 66 |tatic void rebuf| 00000340 66 65 72 28 46 49 4c 45 20 2a 69 66 70 29 3b 0a |fer(FILE *ifp);.| 00000350 0a 23 64 65 66 69 6e 65 20 4d 41 58 5f 4c 49 4e |.#define MAX_LIN| 00000360 45 5f 53 49 5a 45 20 31 30 32 34 0a 23 64 65 66 |E_SIZE 1024.#def| 00000370 69 6e 65 20 4d 41 58 5f 4c 56 41 4c 5f 53 49 5a |ine MAX_LVAL_SIZ| 00000380 45 20 32 35 36 0a 0a 23 64 65 66 69 6e 65 20 4d |E 256..#define M| 00000390 41 58 5f 46 49 4c 45 4e 41 4d 45 5f 4c 45 4e 47 |AX_FILENAME_LENG| 000003a0 54 48 20 32 35 36 0a 0a 73 74 61 74 69 63 20 63 |TH 256..static c| 000003b0 68 61 72 20 73 6f 75 72 63 65 46 69 6c 65 5b 4d |har sourceFile[M| 000003c0 41 58 5f 46 49 4c 45 4e 41 4d 45 5f 4c 45 4e 47 |AX_FILENAME_LENG| 000003d0 54 48 5d 20 3d 20 7b 30 7d 3b 0a 73 74 61 74 69 |TH] = {0};.stati| 000003e0 63 20 69 6e 74 20 73 6f 75 72 63 65 4c 69 6e 65 |c int sourceLine| 000003f0 20 3d 20 30 3b 0a 0a 73 74 61 74 69 63 20 63 68 | = 0;..static ch| 00000400 61 72 20 69 6e 70 75 74 4c 69 6e 65 5b 4d 41 58 |ar inputLine[MAX| 00000410 5f 4c 49 4e 45 5f 53 49 5a 45 5d 20 3d 20 7b 30 |_LINE_SIZE] = {0| 00000420 7d 3b 0a 73 74 61 74 69 63 20 69 6e 74 20 69 6e |};.static int in| 00000430 70 75 74 50 6f 73 20 3d 20 30 3b 0a 0a 73 74 61 |putPos = 0;..sta| 00000440 74 69 63 20 63 68 61 72 20 79 79 6c 76 61 6c 5b |tic char yylval[| 00000450 4d 41 58 5f 4c 56 41 4c 5f 53 49 5a 45 5d 20 3d |MAX_LVAL_SIZE] =| 00000460 20 7b 30 7d 3b 0a 0a 73 74 61 74 69 63 20 62 6f | {0};..static bo| 00000470 6f 6c 65 61 6e 20 61 74 45 6f 66 20 3d 20 66 61 |olean atEof = fa| 00000480 6c 73 65 3b 0a 0a 73 74 61 74 69 63 20 63 68 61 |lse;..static cha| 00000490 72 20 6b 65 79 77 6f 72 64 73 5b 31 37 5d 5b 31 |r keywords[17][1| 000004a0 31 5d 20 3d 20 7b 0a 20 20 22 62 65 67 69 6e 22 |1] = {. "begin"| 000004b0 2c 22 65 6e 64 22 2c 22 76 69 64 65 6f 22 2c 22 |,"end","video","| 000004c0 65 34 38 30 22 2c 22 65 33 36 30 22 2c 22 65 32 |e480","e360","e2| 000004d0 34 30 22 2c 22 65 31 38 30 22 2c 22 65 31 32 30 |40","e180","e120| 000004e0 22 2c 22 65 36 30 22 2c 22 63 61 74 65 67 6f 72 |","e60","categor| 000004f0 69 65 73 22 2c 0a 20 20 22 70 72 6f 67 72 61 6d |ies",. "program| 00000500 6d 65 22 2c 22 66 72 6f 6d 22 2c 22 73 74 61 72 |me","from","star| 00000510 74 73 22 2c 22 65 6e 64 73 22 2c 22 63 61 74 65 |ts","ends","cate| 00000520 67 6f 72 79 22 2c 22 70 72 65 63 65 64 65 73 22 |gory","precedes"| 00000530 2c 22 66 6f 6c 6c 6f 77 73 22 0a 7d 3b 0a 0a 2f |,"follows".};../| 00000540 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a |****************| * 00000580 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a |*************/..| 00000590 76 6f 69 64 20 6c 65 78 28 63 68 61 72 20 2a 66 |void lex(char *f| 000005a0 69 6c 65 29 0a 7b 0a 20 20 46 49 4c 45 20 2a 69 |ile).{. FILE *i| 000005b0 66 70 3b 0a 20 20 4c 65 78 65 6d 65 20 6c 6c 3b |fp;. Lexeme ll;| 000005c0 0a 0a 20 20 69 66 20 28 28 69 66 70 20 3d 20 66 |.. if ((ifp = f| 000005d0 6f 70 65 6e 28 66 69 6c 65 2c 22 72 22 29 29 20 |open(file,"r")) | 000005e0 3d 3d 20 4e 55 4c 4c 29 0a 20 20 7b 0a 20 20 20 |== NULL). {. | 000005f0 20 70 72 69 6e 74 66 28 22 5b 65 72 72 6f 72 5d | printf("[error]| 00000600 3a 20 66 69 6c 65 20 27 25 73 27 20 77 6f 6e 27 |: file '%s' won'| 00000610 74 20 6f 70 65 6e 5c 6e 22 2c 66 69 6c 65 29 3b |t open\n",file);| 00000620 0a 20 20 20 20 65 78 69 74 28 45 58 49 54 5f 46 |. exit(EXIT_F| 00000630 41 49 4c 55 52 45 29 3b 0a 20 20 7d 0a 0a 20 20 |AILURE);. }.. | 00000640 73 74 72 63 70 79 28 73 6f 75 72 63 65 46 69 6c |strcpy(sourceFil| 00000650 65 2c 66 69 6c 65 29 3b 0a 20 20 73 6f 75 72 63 |e,file);. sourc| 00000660 65 4c 69 6e 65 20 3d 20 30 3b 0a 0a 20 20 77 68 |eLine = 0;.. wh| 00000670 69 6c 65 20 28 66 65 6f 66 28 69 66 70 29 20 3d |ile (feof(ifp) =| 00000680 3d 20 30 29 0a 20 20 7b 0a 20 20 20 20 67 65 74 |= 0). {. get| 00000690 4c 65 78 65 6d 65 28 69 66 70 29 3b 0a 20 20 7d |Lexeme(ifp);. }| 000006a0 0a 0a 20 20 66 63 6c 6f 73 65 28 69 66 70 29 3b |.. fclose(ifp);| 000006b0 0a 0a 20 20 6c 6c 20 3d 20 6d 61 6c 6c 6f 63 28 |.. ll = malloc(| 000006c0 73 69 7a 65 6f 66 28 6c 65 78 65 6d 65 5f 69 6e |sizeof(lexeme_in| 000006d0 73 74 61 6e 63 65 29 29 3b 0a 20 20 6c 6c 2d 3e |stance));. ll->| 000006e0 6c 65 78 65 6d 65 5f 74 79 70 65 20 3d 20 65 6e |lexeme_type = en| 000006f0 64 4f 66 53 74 72 65 61 6d 3b 0a 20 20 6c 6c 2d |dOfStream;. ll-| 00000700 3e 73 6f 75 72 63 65 5f 66 69 6c 65 20 3d 20 6d |>source_file = m| 00000710 61 6c 6c 6f 63 28 73 74 72 6c 65 6e 28 73 6f 75 |alloc(strlen(sou| 00000720 72 63 65 46 69 6c 65 29 20 2b 20 31 29 3b 0a 20 |rceFile) + 1);. | 00000730 20 73 74 72 63 70 79 28 6c 6c 2d 3e 73 6f 75 72 | strcpy(ll->sour| 00000740 63 65 5f 66 69 6c 65 2c 73 6f 75 72 63 65 46 69 |ce_file,sourceFi| 00000750 6c 65 29 3b 0a 20 20 6c 6c 2d 3e 73 6f 75 72 63 |le);. ll->sourc| 00000760 65 5f 6c 69 6e 65 20 3d 20 73 6f 75 72 63 65 4c |e_line = sourceL| 00000770 69 6e 65 3b 0a 20 20 6c 6c 2d 3e 6e 65 78 74 20 |ine;. ll->next | 00000780 3d 20 4e 55 4c 4c 3b 0a 0a 20 20 69 66 20 28 6c |= NULL;.. if (l| 00000790 65 78 4c 69 73 74 5f 68 65 61 64 20 3d 3d 20 4e |exList_head == N| 000007a0 55 4c 4c 29 0a 20 20 7b 0a 20 20 20 20 6c 6c 2d |ULL). {. ll-| 000007b0 3e 70 72 65 76 20 3d 20 4e 55 4c 4c 3b 0a 20 20 |>prev = NULL;. | 000007c0 20 20 6c 65 78 4c 69 73 74 5f 68 65 61 64 20 3d | lexList_head =| 000007d0 20 6c 65 78 4c 69 73 74 5f 74 61 69 6c 20 3d 20 | lexList_tail = | 000007e0 6c 6c 3b 0a 20 20 7d 0a 20 20 65 6c 73 65 0a 20 |ll;. }. else. | 000007f0 20 7b 0a 20 20 20 20 6c 6c 2d 3e 70 72 65 76 20 | {. ll->prev | 00000800 3d 20 6c 65 78 4c 69 73 74 5f 74 61 69 6c 3b 0a |= lexList_tail;.| 00000810 20 20 20 20 6c 65 78 4c 69 73 74 5f 74 61 69 6c | lexList_tail| 00000820 2d 3e 6e 65 78 74 20 3d 20 6c 6c 3b 0a 20 20 20 |->next = ll;. | 00000830 20 6c 65 78 4c 69 73 74 5f 74 61 69 6c 20 3d 20 | lexList_tail = | 00000840 6c 6c 3b 0a 20 20 7d 0a 7d 0a 0a 2f 2a 2a 2a 2a |ll;. }.}../****| 00000850 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a |****************| * 00000890 2a 2a 2a 2a 2a 2a 2a 2a 2a 2f 0a 0a 73 74 61 74 |*********/..stat| 000008a0 69 63 20 76 6f 69 64 20 67 65 74 4c 65 78 65 6d |ic void getLexem| 000008b0 65 28 46 49 4c 45 20 2a 69 66 70 29 0a 7b 0a 20 |e(FILE *ifp).{. | 000008c0 20 69 6e 74 20 63 68 20 3d 20 30 3b 0a 20 20 63 | int ch = 0;. c| 000008d0 68 61 72 20 2a 70 63 68 3b 0a 20 20 4c 65 78 65 |har *pch;. Lexe| 000008e0 6d 65 20 6c 6c 3b 0a 20 20 69 6e 74 20 69 3b 0a |me ll;. int i;.| 000008f0 20 20 62 6f 6f 6c 65 61 6e 20 6c 69 6e 6b 20 3d | boolean link =| 00000900 20 74 72 75 65 3b 0a 0a 20 20 6c 6c 20 3d 20 6d | true;.. ll = m| 00000910 61 6c 6c 6f 63 28 73 69 7a 65 6f 66 28 6c 65 78 |alloc(sizeof(lex| 00000920 65 6d 65 5f 69 6e 73 74 61 6e 63 65 29 29 3b 0a |eme_instance));.| 00000930 20 20 79 79 6c 76 61 6c 5b 30 5d 20 3d 20 27 5c | yylval[0] = '\| 00000940 30 27 3b 0a 0a 20 20 77 68 69 6c 65 20 28 66 65 |0';.. while (fe| 00000950 6f 66 28 69 66 70 29 20 3d 3d 20 30 0a 20 20 09 |of(ifp) == 0. .| 00000960 20 26 26 20 28 63 68 20 3d 20 6e 65 78 74 43 68 | && (ch = nextCh| 00000970 61 72 28 69 66 70 29 2c 63 68 20 21 3d 20 45 4f |ar(ifp),ch != EO| 00000980 46 20 26 26 20 63 68 20 3c 3d 20 27 20 27 29 29 |F && ch <= ' '))| 00000990 0a 20 20 7b 0a 20 20 20 20 67 65 74 43 68 61 72 |. {. getChar| 000009a0 28 69 66 70 29 3b 0a 20 20 7d 0a 0a 20 20 69 66 |(ifp);. }.. if| 000009b0 20 28 66 65 6f 66 28 69 66 70 29 20 21 3d 20 30 | (feof(ifp) != 0| 000009c0 20 7c 7c 20 63 68 20 3d 3d 20 45 4f 46 29 0a 20 | || ch == EOF). | 000009d0 20 7b 0a 20 20 20 20 66 72 65 65 28 6c 6c 29 3b | {. free(ll);| 000009e0 0a 20 20 20 20 72 65 74 75 72 6e 3b 0a 20 20 7d |. return;. }| 000009f0 0a 0a 20 20 63 68 20 3d 20 67 65 74 43 68 61 72 |.. ch = getChar| 00000a00 28 69 66 70 29 3b 0a 0a 20 20 69 66 20 28 69 73 |(ifp);.. if (is| 00000a10 61 6c 70 68 61 28 63 68 29 20 7c 7c 20 63 68 20 |alpha(ch) || ch | 00000a20 3d 3d 20 27 5f 27 29 0a 20 20 7b 0a 20 20 20 20 |== '_'). {. | 00000a30 2f 2a 20 69 64 65 6e 74 69 66 69 65 72 20 6f 72 |/* identifier or| 00000a40 20 6b 65 79 77 6f 72 64 20 2a 2f 0a 20 20 20 20 | keyword */. | 00000a50 2a 28 70 63 68 20 3d 20 79 79 6c 76 61 6c 29 20 |*(pch = yylval) | 00000a60 3d 20 63 68 3b 0a 20 20 20 20 70 63 68 2b 2b 3b |= ch;. pch++;| 00000a70 0a 0a 20 20 20 20 77 68 69 6c 65 20 28 69 73 61 |.. while (isa| 00000a80 6c 70 68 61 28 6e 65 78 74 43 68 61 72 28 69 66 |lpha(nextChar(if| 00000a90 70 29 29 20 7c 7c 20 69 73 64 69 67 69 74 28 6e |p)) || isdigit(n| 00000aa0 65 78 74 43 68 61 72 28 69 66 70 29 29 0a 20 20 |extChar(ifp)). | 00000ab0 20 20 09 20 20 20 7c 7c 20 6e 65 78 74 43 68 61 | . || nextCha| 00000ac0 72 28 69 66 70 29 20 3d 3d 20 27 5f 27 29 0a 20 |r(ifp) == '_'). | 00000ad0 20 20 20 7b 0a 20 20 20 20 20 20 2a 70 63 68 2b | {. *pch+| 00000ae0 2b 20 3d 20 67 65 74 43 68 61 72 28 69 66 70 29 |+ = getChar(ifp)| 00000af0 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2a 70 63 68 |;. }. *pch| 00000b00 20 3d 20 27 5c 30 27 3b 0a 0a 20 20 20 20 6c 6c | = '\0';.. ll| 00000b10 2d 3e 6c 65 78 65 6d 65 5f 74 79 70 65 20 3d 20 |->lexeme_type = | 00000b20 2d 31 3b 0a 20 20 20 20 66 6f 72 20 28 69 20 3d |-1;. for (i =| 00000b30 20 62 65 67 69 6e 3b 20 69 20 3c 3d 20 66 6f 6c | begin; i <= fol| 00000b40 6c 6f 77 73 3b 20 69 2b 2b 29 0a 20 20 20 20 7b |lows; i++). {| 00000b50 0a 20 20 20 20 20 20 69 66 20 28 73 74 72 63 6d |. if (strcm| 00000b60 70 28 6b 65 79 77 6f 72 64 73 5b 69 5d 2c 79 79 |p(keywords[i],yy| 00000b70 6c 76 61 6c 29 20 3d 3d 20 30 29 0a 20 20 20 20 |lval) == 0). | 00000b80 20 20 7b 0a 20 20 20 20 20 20 20 20 6c 6c 2d 3e | {. ll->| 00000b90 6c 65 78 65 6d 65 5f 74 79 70 65 20 3d 20 69 3b |lexeme_type = i;| 00000ba0 0a 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a |. break;.| 00000bb0 20 20 20 20 20 20 7d 0a 20 20 20 20 7d 0a 0a 20 | }. }.. | 00000bc0 20 20 20 69 66 20 28 6c 6c 2d 3e 6c 65 78 65 6d | if (ll->lexem| 00000bd0 65 5f 74 79 70 65 20 3d 3d 20 2d 31 29 0a 20 20 |e_type == -1). | 00000be0 20 20 7b 0a 20 20 20 20 20 20 6c 6c 2d 3e 6c 65 | {. ll->le| 00000bf0 78 65 6d 65 5f 74 79 70 65 20 3d 20 69 64 65 6e |xeme_type = iden| 00000c00 74 69 66 69 65 72 3b 0a 20 20 20 20 20 20 6c 6c |tifier;. ll| 00000c10 2d 3e 6c 65 78 65 6d 65 5f 76 61 6c 75 65 2e 69 |->lexeme_value.i| 00000c20 64 65 6e 74 69 66 69 65 72 56 61 6c 75 65 20 3d |dentifierValue =| 00000c30 20 6d 61 6c 6c 6f 63 28 73 74 72 6c 65 6e 28 79 | malloc(strlen(y| 00000c40 79 6c 76 61 6c 29 20 2b 20 31 29 3b 0a 20 20 20 |ylval) + 1);. | 00000c50 20 20 20 73 74 72 63 70 79 28 6c 6c 2d 3e 6c 65 | strcpy(ll->le| 00000c60 78 65 6d 65 5f 76 61 6c 75 65 2e 69 64 65 6e 74 |xeme_value.ident| 00000c70 69 66 69 65 72 56 61 6c 75 65 2c 79 79 6c 76 61 |ifierValue,yylva| 00000c80 6c 29 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 |l);. }. }. | 00000c90 65 6c 73 65 20 69 66 20 28 69 73 64 69 67 69 74 |else if (isdigit| 00000ca0 28 63 68 29 29 0a 20 20 7b 0a 20 20 20 20 2f 2a |(ch)). {. /*| 00000cb0 20 6e 75 6d 65 72 69 63 61 6c 20 63 6f 6e 73 74 | numerical const| 00000cc0 61 6e 74 20 2a 2f 0a 0a 20 20 20 20 2a 28 70 63 |ant */.. *(pc| 00000cd0 68 20 3d 20 79 79 6c 76 61 6c 29 20 3d 20 63 68 |h = yylval) = ch| 00000ce0 3b 0a 20 20 20 20 70 63 68 2b 2b 3b 0a 0a 20 20 |;. pch++;.. | 00000cf0 20 20 77 68 69 6c 65 20 28 69 73 64 69 67 69 74 | while (isdigit| 00000d00 28 6e 65 78 74 43 68 61 72 28 69 66 70 29 29 29 |(nextChar(ifp)))| 00000d10 0a 20 20 20 20 7b 0a 20 20 20 20 20 20 2a 70 63 |. {. *pc| 00000d20 68 2b 2b 20 3d 20 67 65 74 43 68 61 72 28 69 66 |h++ = getChar(if| 00000d30 70 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2a 70 |p);. }. *p| 00000d40 63 68 20 3d 20 27 5c 30 27 3b 0a 0a 20 20 20 20 |ch = '\0';.. | 00000d50 6c 6c 2d 3e 6c 65 78 65 6d 65 5f 74 79 70 65 20 |ll->lexeme_type | 00000d60 3d 20 63 6f 6e 73 74 61 6e 74 3b 0a 20 20 20 20 |= constant;. | 00000d70 6c 6c 2d 3e 6c 65 78 65 6d 65 5f 76 61 6c 75 65 |ll->lexeme_value| 00000d80 2e 63 6f 6e 73 74 61 6e 74 56 61 6c 75 65 20 3d |.constantValue =| 00000d90 20 61 74 6f 69 28 79 79 6c 76 61 6c 29 3b 0a 20 | atoi(yylval);. | 00000da0 20 7d 0a 20 20 65 6c 73 65 20 69 66 20 28 63 68 | }. else if (ch| 00000db0 20 3d 3d 20 27 5c 22 27 29 0a 20 20 7b 0a 20 20 | == '\"'). {. | 00000dc0 20 20 2f 2a 20 73 74 72 69 6e 67 20 6c 69 74 65 | /* string lite| 00000dd0 72 61 6c 20 2a 2f 0a 0a 20 20 20 20 70 63 68 20 |ral */.. pch | 00000de0 3d 20 79 79 6c 76 61 6c 3b 0a 0a 20 20 20 20 77 |= yylval;.. w| 00000df0 68 69 6c 65 20 28 6e 65 78 74 43 68 61 72 28 69 |hile (nextChar(i| 00000e00 66 70 29 20 21 3d 20 27 5c 22 27 29 0a 20 20 20 |fp) != '\"'). | 00000e10 20 7b 0a 20 20 20 20 20 20 2a 70 63 68 2b 2b 20 | {. *pch++ | 00000e20 3d 20 67 65 74 43 68 61 72 28 69 66 70 29 3b 0a |= getChar(ifp);.| 00000e30 20 20 20 20 7d 0a 20 20 20 20 2a 70 63 68 20 3d | }. *pch =| 00000e40 20 27 5c 30 27 3b 0a 20 20 20 20 67 65 74 43 68 | '\0';. getCh| 00000e50 61 72 28 69 66 70 29 3b 0a 0a 20 20 20 20 6c 6c |ar(ifp);.. ll| 00000e60 2d 3e 6c 65 78 65 6d 65 5f 74 79 70 65 20 3d 20 |->lexeme_type = | 00000e70 73 74 72 69 6e 67 4c 69 74 65 72 61 6c 3b 0a 20 |stringLiteral;. | 00000e80 20 20 20 6c 6c 2d 3e 6c 65 78 65 6d 65 5f 76 61 | ll->lexeme_va| 00000e90 6c 75 65 2e 73 74 72 69 6e 67 4c 69 74 65 72 61 |lue.stringLitera| 00000ea0 6c 56 61 6c 75 65 20 3d 20 6d 61 6c 6c 6f 63 28 |lValue = malloc(| 00000eb0 73 74 72 6c 65 6e 28 79 79 6c 76 61 6c 29 20 2b |strlen(yylval) +| 00000ec0 20 31 29 3b 0a 20 20 20 20 73 74 72 63 70 79 28 | 1);. strcpy(| 00000ed0 6c 6c 2d 3e 6c 65 78 65 6d 65 5f 76 61 6c 75 65 |ll->lexeme_value| 00000ee0 2e 73 74 72 69 6e 67 4c 69 74 65 72 61 6c 56 61 |.stringLiteralVa| 00000ef0 6c 75 65 2c 79 79 6c 76 61 6c 29 3b 0a 20 20 7d |lue,yylval);. }| 00000f00 0a 20 20 65 6c 73 65 0a 20 20 7b 0a 20 20 20 20 |. else. {. | 00000f10 2f 2a 20 70 75 6e 63 74 75 74 61 74 6f 72 20 6f |/* punctutator o| 00000f20 72 20 69 6c 6c 65 67 61 6c 20 2a 2f 0a 20 20 20 |r illegal */. | 00000f30 20 73 77 69 74 63 68 20 28 63 68 29 0a 20 20 20 | switch (ch). | 00000f40 20 7b 0a 20 20 20 20 20 20 63 61 73 65 20 27 7b | {. case '{| 00000f50 27 20 3a 0a 20 20 20 20 20 20 20 20 6c 6c 2d 3e |' :. ll->| 00000f60 6c 65 78 65 6d 65 5f 74 79 70 65 20 3d 20 6c 65 |lexeme_type = le| 00000f70 66 74 42 72 61 63 65 3b 0a 20 20 20 20 20 20 20 |ftBrace;. | 00000f80 20 62 72 65 61 6b 3b 0a 20 20 20 20 20 20 63 61 | break;. ca| 00000f90 73 65 20 27 7d 27 20 3a 0a 20 20 20 20 20 20 20 |se '}' :. | 00000fa0 20 6c 6c 2d 3e 6c 65 78 65 6d 65 5f 74 79 70 65 | ll->lexeme_type| 00000fb0 20 3d 20 72 69 67 68 74 42 72 61 63 65 3b 0a 20 | = rightBrace;. | 00000fc0 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 | break;. | 00000fd0 20 20 20 20 63 61 73 65 20 27 2c 27 20 3a 0a 20 | case ',' :. | 00000fe0 20 20 20 20 20 20 20 6c 6c 2d 3e 6c 65 78 65 6d | ll->lexem| 00000ff0 65 5f 74 79 70 65 20 3d 20 63 6f 6d 6d 61 3b 0a |e_type = comma;.| 00001000 20 20 20 20 20 20 20 20 62 72 65 61 6b 3b 0a 20 | break;. | 00001010 20 20 20 20 20 63 61 73 65 20 27 3b 27 20 3a 0a | case ';' :.| 00001020 20 20 20 20 20 20 20 20 6c 6c 2d 3e 6c 65 78 65 | ll->lexe| 00001030 6d 65 5f 74 79 70 65 20 3d 20 73 65 6d 69 43 6f |me_type = semiCo| 00001040 6c 6f 6e 3b 0a 20 20 20 20 20 20 20 20 62 72 65 |lon;. bre| 00001050 61 6b 3b 0a 20 20 20 20 20 20 64 65 66 61 75 6c |ak;. defaul| 00001060 74 20 3a 0a 20 20 20 20 20 20 20 20 70 72 69 6e |t :. prin| 00001070 74 66 28 22 5b 77 61 72 6e 69 6e 67 5d 3a 20 69 |tf("[warning]: i| 00001080 6e 76 61 6c 69 64 20 63 68 61 72 61 63 74 65 72 |nvalid character| 00001090 20 69 6e 20 69 6e 70 75 74 2c 20 68 65 78 20 76 | in input, hex v| 000010a0 61 6c 75 65 20 3d 20 30 78 25 2e 32 78 5c 6e 22 |alue = 0x%.2x\n"| 000010b0 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 20 20 |,. | 000010c0 20 63 68 29 3b 0a 20 20 20 20 20 20 20 20 6c 69 | ch);. li| 000010d0 6e 6b 20 3d 20 66 61 6c 73 65 3b 0a 20 20 20 20 |nk = false;. | 000010e0 20 20 20 20 62 72 65 61 6b 3b 0a 20 20 20 20 7d | break;. }| 000010f0 0a 20 20 7d 0a 0a 20 20 69 66 20 28 6c 69 6e 6b |. }.. if (link| 00001100 29 0a 20 20 7b 0a 20 20 20 20 6c 6c 2d 3e 73 6f |). {. ll->so| 00001110 75 72 63 65 5f 66 69 6c 65 20 3d 20 6d 61 6c 6c |urce_file = mall| 00001120 6f 63 28 73 74 72 6c 65 6e 28 73 6f 75 72 63 65 |oc(strlen(source| 00001130 46 69 6c 65 29 20 2b 20 31 29 3b 0a 20 20 20 20 |File) + 1);. | 00001140 73 74 72 63 70 79 28 6c 6c 2d 3e 73 6f 75 72 63 |strcpy(ll->sourc| 00001150 65 5f 66 69 6c 65 2c 73 6f 75 72 63 65 46 69 6c |e_file,sourceFil| 00001160 65 29 3b 0a 20 20 20 20 6c 6c 2d 3e 73 6f 75 72 |e);. ll->sour| 00001170 63 65 5f 6c 69 6e 65 20 3d 20 73 6f 75 72 63 65 |ce_line = source| 00001180 4c 69 6e 65 3b 0a 0a 20 20 20 20 6c 6c 2d 3e 6e |Line;.. ll->n| 00001190 65 78 74 20 3d 20 4e 55 4c 4c 3b 0a 20 20 20 20 |ext = NULL;. | 000011a0 69 66 20 28 6c 65 78 4c 69 73 74 5f 68 65 61 64 |if (lexList_head| 000011b0 20 3d 3d 20 4e 55 4c 4c 29 0a 20 20 20 20 7b 0a | == NULL). {.| 000011c0 20 20 20 20 20 20 6c 6c 2d 3e 70 72 65 76 20 3d | ll->prev =| 000011d0 20 4e 55 4c 4c 3b 0a 20 20 20 20 20 20 6c 65 78 | NULL;. lex| 000011e0 4c 69 73 74 5f 68 65 61 64 20 3d 20 6c 65 78 4c |List_head = lexL| 000011f0 69 73 74 5f 74 61 69 6c 20 3d 20 6c 6c 3b 0a 20 |ist_tail = ll;. | 00001200 20 20 20 7d 0a 20 20 20 20 65 6c 73 65 0a 20 20 | }. else. | 00001210 20 20 7b 0a 20 20 20 20 20 20 6c 6c 2d 3e 70 72 | {. ll->pr| 00001220 65 76 20 3d 20 6c 65 78 4c 69 73 74 5f 74 61 69 |ev = lexList_tai| 00001230 6c 3b 0a 20 20 20 20 20 20 6c 65 78 4c 69 73 74 |l;. lexList| 00001240 5f 74 61 69 6c 2d 3e 6e 65 78 74 20 3d 20 6c 6c |_tail->next = ll| 00001250 3b 0a 20 20 20 20 20 20 6c 65 78 4c 69 73 74 5f |;. lexList_| 00001260 74 61 69 6c 20 3d 20 6c 6c 3b 0a 20 20 20 20 7d |tail = ll;. }| 00001270 0a 20 20 7d 0a 20 20 65 6c 73 65 0a 20 20 7b 0a |. }. else. {.| 00001280 20 20 20 20 66 72 65 65 28 6c 6c 29 3b 0a 20 20 | free(ll);. | 00001290 7d 0a 7d 0a 0a 73 74 61 74 69 63 20 69 6e 74 20 |}.}..static int | 000012a0 6e 65 78 74 43 68 61 72 28 46 49 4c 45 20 2a 69 |nextChar(FILE *i| 000012b0 66 70 29 0a 7b 0a 20 20 69 66 20 28 69 6e 70 75 |fp).{. if (inpu| 000012c0 74 4c 69 6e 65 5b 69 6e 70 75 74 50 6f 73 5d 20 |tLine[inputPos] | 000012d0 3d 3d 20 27 5c 30 27 29 0a 20 20 7b 0a 20 20 20 |== '\0'). {. | 000012e0 20 72 65 62 75 66 66 65 72 28 69 66 70 29 3b 0a | rebuffer(ifp);.| 000012f0 20 20 7d 0a 0a 20 20 69 66 20 28 61 74 45 6f 66 | }.. if (atEof| 00001300 29 0a 20 20 7b 0a 20 20 20 20 72 65 74 75 72 6e |). {. return| 00001310 20 28 45 4f 46 29 3b 0a 20 20 7d 0a 0a 20 20 72 | (EOF);. }.. r| 00001320 65 74 75 72 6e 20 28 69 6e 70 75 74 4c 69 6e 65 |eturn (inputLine| 00001330 5b 69 6e 70 75 74 50 6f 73 5d 29 3b 0a 7d 0a 0a |[inputPos]);.}..| 00001340 73 74 61 74 69 63 20 69 6e 74 20 67 65 74 43 68 |static int getCh| 00001350 61 72 28 46 49 4c 45 20 2a 69 66 70 29 0a 7b 0a |ar(FILE *ifp).{.| 00001360 20 20 63 68 61 72 20 72 76 61 6c 20 3d 20 6e 65 | char rval = ne| 00001370 78 74 43 68 61 72 28 69 66 70 29 3b 0a 0a 20 20 |xtChar(ifp);.. | 00001380 69 6e 70 75 74 50 6f 73 2b 2b 3b 0a 0a 20 20 72 |inputPos++;.. r| 00001390 65 74 75 72 6e 20 28 72 76 61 6c 29 3b 0a 7d 0a |eturn (rval);.}.| 000013a0 0a 73 74 61 74 69 63 20 76 6f 69 64 20 72 65 62 |.static void reb| 000013b0 75 66 66 65 72 28 46 49 4c 45 20 2a 69 66 70 29 |uffer(FILE *ifp)| 000013c0 0a 7b 0a 20 20 63 68 61 72 20 2a 63 68 3b 0a 0a |.{. char *ch;..| 000013d0 20 20 69 66 20 28 66 67 65 74 73 28 69 6e 70 75 | if (fgets(inpu| 000013e0 74 4c 69 6e 65 2c 4d 41 58 5f 4c 49 4e 45 5f 53 |tLine,MAX_LINE_S| 000013f0 49 5a 45 2c 69 66 70 29 20 3d 3d 20 4e 55 4c 4c |IZE,ifp) == NULL| 00001400 29 0a 20 20 7b 0a 20 20 20 20 69 66 20 28 61 74 |). {. if (at| 00001410 45 6f 66 29 0a 20 20 20 20 7b 0a 20 20 20 20 20 |Eof). {. | 00001420 20 70 72 69 6e 74 66 28 22 5b 65 72 72 6f 72 5d | printf("[error]| 00001430 3a 20 70 72 65 6d 61 74 75 72 65 20 65 6e 64 20 |: premature end | 00001440 6f 66 20 66 69 6c 65 20 69 6e 20 27 25 73 27 5c |of file in '%s'\| 00001450 6e 22 2c 73 6f 75 72 63 65 46 69 6c 65 29 3b 0a |n",sourceFile);.| 00001460 20 20 20 20 20 20 65 78 69 74 28 45 58 49 54 5f | exit(EXIT_| 00001470 46 41 49 4c 55 52 45 29 3b 0a 20 20 20 20 7d 0a |FAILURE);. }.| 00001480 20 20 20 20 65 6c 73 65 0a 20 20 20 20 7b 0a 20 | else. {. | 00001490 20 20 20 20 20 61 74 45 6f 66 20 3d 20 74 72 75 | atEof = tru| 000014a0 65 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 20 20 65 |e;. }. }. e| 000014b0 6c 73 65 0a 20 20 7b 0a 20 20 20 20 73 6f 75 72 |lse. {. sour| 000014c0 63 65 4c 69 6e 65 2b 2b 3b 0a 20 20 20 20 69 6e |ceLine++;. in| 000014d0 70 75 74 50 6f 73 20 3d 20 27 5c 30 27 3b 0a 20 |putPos = '\0';. | 000014e0 20 20 20 69 6e 70 75 74 4c 69 6e 65 5b 4d 41 58 | inputLine[MAX| 000014f0 5f 4c 49 4e 45 5f 53 49 5a 45 20 2d 20 31 5d 20 |_LINE_SIZE - 1] | 00001500 3d 20 27 5c 30 27 3b 0a 0a 20 20 20 20 63 68 20 |= '\0';.. ch | 00001510 3d 20 69 6e 70 75 74 4c 69 6e 65 3b 0a 20 20 20 |= inputLine;. | 00001520 20 77 68 69 6c 65 20 28 2a 63 68 20 21 3d 20 27 | while (*ch != '| 00001530 5c 6e 27 20 26 26 20 2a 63 68 20 21 3d 20 27 5c |\n' && *ch != '\| 00001540 30 27 29 0a 20 20 20 20 7b 0a 20 20 20 20 20 20 |0'). {. | 00001550 63 68 2b 2b 3b 0a 20 20 20 20 7d 0a 0a 20 20 20 |ch++;. }.. | 00001560 20 69 66 20 28 2a 63 68 20 21 3d 20 27 5c 6e 27 | if (*ch != '\n'| 00001570 29 0a 20 20 20 20 7b 0a 20 20 20 20 20 20 70 72 |). {. pr| 00001580 69 6e 74 66 28 22 5b 77 61 72 6e 69 6e 67 5d 3a |intf("[warning]:| 00001590 20 6c 69 6e 65 20 25 64 20 74 6f 6f 20 6c 6f 6e | line %d too lon| 000015a0 67 5c 6e 22 2c 73 6f 75 72 63 65 4c 69 6e 65 29 |g\n",sourceLine)| 000015b0 3b 0a 20 20 20 20 7d 0a 20 20 7d 0a 7d 0a |;. }. }.}.| 000015be