0% found this document useful (0 votes)
47 views2 pages

Int Int Return Void Void Void Int Int Return: #Include #Include #Include #Include #Include

This document contains C code for tokenizing input. It defines functions for scanning input characters, clearing and accessing the current lexeme, and mapping characters and keywords to symbol codes. Arrays store the character mappings and keyword mappings. Functions return the name or code for a given symbol or check if a symbol matches the current token. This allows lexical analysis of C code by identifying tokens from the input stream.

Uploaded by

Khải Trần
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views2 pages

Int Int Return Void Void Void Int Int Return: #Include #Include #Include #Include #Include

This document contains C code for tokenizing input. It defines functions for scanning input characters, clearing and accessing the current lexeme, and mapping characters and keywords to symbol codes. Arrays store the character mappings and keyword mappings. Functions return the name or code for a given symbol or check if a symbol matches the current token. This allows lexical analysis of C code by identifying tokens from the input stream.

Uploaded by

Khải Trần
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

token.

c 6/16/17, 9:48 PM

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include "token.h"

Token token;

int istoken(int sym) { return token.sym == sym; }

void clear_lexeme() { token.index = 0; }


void delete_prev() { --token.index; }
void outch(int ch) { token.text[token.index++] = ch; }
int prevch() { return (token.index>0) ? (token.text[token.index-1]) : '\n';
}

static char tmp[4];

static char *tokenname[] = {


"id", "lit", "op", "cmt",
"if", "else", "switch", "case", "default",
"while", "break", "continue", "return",
"do", "for",
0
};

char *nameof(int sym) {


if (sym < ID)
{ tmp[0] = sym; tmp[1] = 0; return tmp; }
else if (sym < tEND)
return tokenname[sym-ID];
else
return "";
}

#define SQ ('\'')
#define DQ ('\"')

static int scancode[] = {


/* 0x20 - 0x2f */
' ', '!', DQ, -1, -1, '%', '&', SQ, '(', ')', '*', '+', -1, '-', '.', '/
',
/* 0x30 - 0x3f */
'0', '9', '9', '9', '9', '9', '9', '9', '9', '9', -1, -1, '<', '=', '>
', -1,
/* 0x40 - 0x4f */
'@', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z
', 'Z',
/* 0x50 - 0x5f */
'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', '{', -1, '}', -1,
'Z',
/* 0x60 - 0x6f */
-1 , 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z
', 'Z',
/* 0x70 - 0x7f */
'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', 'Z', '[', '|', ']', -1
, -1
};
#undef SQ
#undef DQ

Page 1 of 2
token.c 6/16/17, 9:48 PM

int codeof(int ch) {


if (ch < 0x20 || ch > 0x7f) return ch;
return scancode[ch - 0x20];
}

static kwentry kwtable[] = {


"if", tIF,
"else", tELSE,
"switch", tSWITCH,
"case", tCASE,
"default", tDEFAULT,
"while", tWHILE,
"break", tBREAK,
"continue", tCONTINUE,
"return", tRETURN,
"do", tDO,
"for", tFOR,
0, 0
};

kwentry *kwlookup(const char *t) {


kwentry *e;
for (e=kwtable;e->text;e++)
if (strcmp(e->text,t)==0) return e;
return 0;
}

Page 2 of 2

You might also like