File: MyLexer.cpp

package info (click to toggle)
pccts 1.33MR33-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 3,212 kB
  • ctags: 5,280
  • sloc: ansic: 46,051; cpp: 3,234; makefile: 957
file content (76 lines) | stat: -rw-r--r-- 1,813 bytes parent folder | download | duplicates (19)
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
/* MyTokenBuffer.c */
/* Sample replacement for DLGLexer */
/* Shows how to override DLG with your own lexer */

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <iostream.h>

#include "config.h"					/* include token defs */
#include "mytokens.h"				/* include token defs */
#include APARSER_H					/* include all the ANTLR yuck */
#include "MyLexer.h"				/* define your lexer */
typedef ANTLRCommonToken ANTLRToken;/* use a predefined Token class */

void panic(char *s) {
   cerr << s << '\n';
   exit(5);
}

MyLexer::MyLexer()
{
	c = getchar();
}

/* Recognizes Tokens IDENTIFIER and NUMBER */
ANTLRAbstractToken *MyLexer::
getToken()
{
	/* we will return a pointer to this next guy */
	ANTLRToken *resultToken = new ANTLRToken;

	ANTLRChar TokenBuffer[100];
						/* This routine will just crash if tokens become
                           more than 99 chars; your code must of course
						   gracefully recover/adapt */
   int  index=0;

   while ( c==' ' || c=='\n' ) c=getchar();

   if (c==EOF) {resultToken->setType(Eof); return resultToken;}

   if (isdigit(c)) {
      /* Looks like we have ourselves a number token */
      while (isdigit(c)) {
         TokenBuffer[index++]=c;
		 c = getchar();
	  }
      TokenBuffer[index]='\0';

      resultToken->setType(NUMBER);
      resultToken->setText(TokenBuffer); /* not exactly swift, unfortunately... */

      return resultToken;
   }

   if (isalpha(c)) {
      /* We have ourselves an IDENTIFIER token */
      while (isalpha(c)) {
         TokenBuffer[index++]=c;
		 c = getchar();
	  }
      TokenBuffer[index]='\0';

      resultToken->setType(IDENTIFIER);
      resultToken->setText(TokenBuffer); /* not exactly swift, unfortunately... */

      return resultToken;
   }

   else
      panic("lexer error");

   return NULL;
}