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 "tokens.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);
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);
return resultToken;
}
else
panic("lexer error");
return NULL;
}
|