File: lexer.c

package info (click to toggle)
magnus 20060324-5.1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 19,436 kB
  • ctags: 20,462
  • sloc: cpp: 130,217; ansic: 37,090; tcl: 10,970; perl: 1,109; makefile: 966; sh: 403; yacc: 372; csh: 57; awk: 33; asm: 10
file content (89 lines) | stat: -rw-r--r-- 2,191 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include "tcyacc.h" 
#include "y.tab.h" 
#include "string.h"
#include "parser.h"
#include "stdio.h"
#include <stdlib.h>
extern int lineno;
extern char *eemalloc(); 
extern	Keyword keywords[];
tclex()  /* tc lexical analyzer  */
{
/* recognize comments started with #.  3/2/93 */
extern FILE *fin;
int 	c;
int	s;
	while ((c=getc(fin)) == ' ' || c == '\t');
/* skip comments which are started with # */
	if (c == '#')
		do {
		while ((c=getc(fin)) != EOF && c != '\n');
		lineno++;
		if (c == EOF)
                	return 0;
		} while ((c=getc(fin)) == '#'); 
	if (c == EOF)
		return 0;
	if (isdigit(c)) {
		ungetc(c, fin);
		fscanf(fin, "%d", &yylval.vali);
		return (TOKEN_INT);
	}
	if (isalpha(c)) { /* keywords */
		char sbuf[BUF_LEN], *p = sbuf;
		do {
			if (p >= sbuf + sizeof(sbuf) - 1) {
				*p = '\0';
				execerror("name too long", sbuf);
			}
			*p++ = c;
		} while ((c=getc(fin)) != EOF && c != ':' && c != '\n');
		if (c == '\n')
			ungetc(c, fin);	
		*p  = '\0';
		if ((s = lookup(sbuf)) == 0) 
			execerror("invalid key word", sbuf);
		if (s == ENUM || s == SUBG || s == RES || s == DI){
			p = sbuf;
			while ((c=getc(fin)) != EOF && c != '\n') {
				if (p >= sbuf + sizeof(sbuf) - 1) {
                                	*p = '\0';
                                	execerror("name too long", sbuf);
                        	}
                        	*p++ = c; 
			}
			*p  = '\0';
			lineno++;
			yylval.words = (char *) eemalloc(100);
			strcpy(yylval.words, sbuf);
		}
		return (s);
	}
	switch (c) {
		case ',':	return c;
		case ';':	return c;
		case '\n': 	lineno++; return c;
	
		default:	fprintf(fout," lexical error in line %d near %c\n",lineno, c);
				exit(1);
	}
}
lookup(s)
char *s;
{
	int	i;
	s[4] = '\0';
	for (i = 0; i < KEY_LEN; i++)
		if (strcmp(keywords[i].name, s) == 0)
                        return (keywords[i].kval);
	s[3] = '\0';
	for (i = 0; i < KEY_LEN; i++)
                if (strcmp(keywords[i].name, s) == 0)
                        return (keywords[i].kval);
	s[2] = '\0';
	for (i = 0; i < KEY_LEN; i++)
                if (strcmp(keywords[i].name, s) == 0)
                        return (keywords[i].kval);

	return 0; /* 0 ==> not found  */
}