<commit_before>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#include "cc.h"
#include "tokens.h"
#include "symbol.h"


static FILE *yyin;
union yyval yyval;
static unsigned char aheadtok = NOTOK;
unsigned char yytoken;
char yytext[TOKSIZ_MAX + 1];
unsigned linenum;
unsigned columnum;
const char *filename;


static char number(void)
{
	register char *bp;
	register char ch;
	register struct symbol *sym;

	for (bp = yytext; bp < yytext + TOKSIZ_MAX; *bp++ = ch) {
		if (!isdigit(ch = getc(yyin)))
			break;
	}
	if (bp == yytext + TOKSIZ_MAX)
		error("identifier too long %s", yytext);
	*bp = '\0';
	ungetc(ch, yyin);
	yyval.sym = sym = install(NULL);
	sym->val = atoi(yytext);

	return CONSTANT;
}

static unsigned char iden(void)
{
	register char ch, *bp;
	register struct symbol *sym;

	for (bp = yytext; bp < yytext + TOKSIZ_MAX; *bp++ = ch) {
		if (!isalnum(ch = getc(yyin)) && ch != '_')
			break;
	}
	if (bp == yytext + TOKSIZ_MAX)
		error("identifier too long %s", yytext);
	*bp = '\0';
	ungetc(ch, yyin);
	if ((sym = lookup(yytext)) && sym->ns == NS_KEYWORD)
		return sym->tok;
	yyval.sym = sym;
	return IDEN;
}

static unsigned char skip(void)
{
	register int c;
	extern char parser_out_home;

	while (isspace(c = getc(yyin))) {
		if (c == '\n')
			++linenum, columnum = 1;
		else
			++columnum;
	}
	if (c == EOF) {
		if (parser_out_home)
			error("Find EOF while parsing");
		return 0;
	}
	ungetc(c, yyin);
	return 1;
}

static unsigned char
follow(unsigned char op, unsigned char eq, unsigned char rep)
{
	register char c;

	if ((c = getc(yyin)) == '=')
		return eq;
	else if (c == op && rep)
		return rep;
	ungetc(c, yyin);
	return op;
}

static unsigned char rel_shift(unsigned char op)
{
	static char tokens[2][3] = {
		{GE, SHL, SHL_EQ},
		{LE, SHR, SHR_EQ}};
	register char c;
	register char *tp = tokens[op == '>'];

	if ((c = getc(yyin)) == '=') {
		return tp[0];
	} else if (c == op) {
		if ((c = getc(yyin)) == '=')
			return tp[2];
		op = tp[1];
	}
	ungetc(c, yyin);
	return c;
}

static unsigned char minus(void)
{
	register int c;

	switch (c = getc(yyin)) {
	case '-': return DEC;
	case '>': return INDIR;
	case '=': return SUB_EQ;
	default:
		ungetc(c, yyin);
		return '-';
	}
}

void next(void)
{
	register unsigned char c;

	if (aheadtok != NOTOK) {
		yytoken = aheadtok;
		aheadtok = NOTOK;
	} else if (!skip()) {
		yytoken = EOFTOK;
	} else if (isalpha(c = getc(yyin)) || c == '_') {
		ungetc(c, yyin);
		yytoken = iden();
	} else if (isdigit(c)) {
		ungetc(c, yyin);
		yytoken = number();
	} else {
		switch (c) {
		case '=': yytoken = follow('=', EQ, 0); break;
		case '^': yytoken = follow('^', XOR_EQ, 0); break;
		case '*': yytoken = follow('*', MUL_EQ, 0); break;
		case '!': yytoken = follow('!', NE, 0); break;
		case '+': yytoken = follow('+', ADD_EQ, INC); break;
		case '&': yytoken = follow('&', AND_EQ, AND); break;
		case '|': yytoken = follow('|', OR_EQ, OR); break;
		case '<': yytoken = rel_shift('<'); break;
		case '>': yytoken = rel_shift('>'); break;
		case '-': yytoken = minus(); break;
		default: yytoken = c;
		}
	}
}

unsigned char ahead(void)
{
	static unsigned char oldtok;

	oldtok = yytoken;
	next();
	aheadtok = yytoken;
	yytoken = oldtok;
	return aheadtok;
}

char accept(register unsigned char tok)
{
	if (yytoken == tok) {
		next();
		return 1;
	}
	return 0;
}

void expect(register unsigned char tok)
{
	if (yytoken != tok)
		error("unexpected %s", yytext);
	next();
}

void open_file(register const char *file)
{
	if (yyin != NULL)
		fclose(yyin);
	if (file == NULL) {
		yyin = stdin;
		filename = "(stdin)";
		return;
	}
	if ((yyin = fopen(file, "r")) == NULL)
		die("file '%s' not found", file);
	filename = file;
	columnum = linenum = 1;
}
<commit_msg>New function operator in lexical analysis<commit_after>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#include "cc.h"
#include "tokens.h"
#include "symbol.h"


static FILE *yyin;
union yyval yyval;
static unsigned char aheadtok = NOTOK;
unsigned char yytoken;
char yytext[TOKSIZ_MAX + 1];
unsigned linenum;
unsigned columnum;
const char *filename;


static char number(void)
{
	register char *bp;
	register char ch;
	register struct symbol *sym;

	for (bp = yytext; bp < yytext + TOKSIZ_MAX; *bp++ = ch) {
		if (!isdigit(ch = getc(yyin)))
			break;
	}
	if (bp == yytext + TOKSIZ_MAX)
		error("identifier too long %s", yytext);
	*bp = '\0';
	ungetc(ch, yyin);
	yyval.sym = sym = install(NULL);
	sym->val = atoi(yytext);

	return CONSTANT;
}

static unsigned char iden(void)
{
	register char ch, *bp;
	register struct symbol *sym;

	for (bp = yytext; bp < yytext + TOKSIZ_MAX; *bp++ = ch) {
		if (!isalnum(ch = getc(yyin)) && ch != '_')
			break;
	}
	if (bp == yytext + TOKSIZ_MAX)
		error("identifier too long %s", yytext);
	*bp = '\0';
	ungetc(ch, yyin);
	if ((sym = lookup(yytext)) && sym->ns == NS_KEYWORD)
		return sym->tok;
	yyval.sym = sym;
	return IDEN;
}

static unsigned char skip(void)
{
	register int c;
	extern char parser_out_home;

	while (isspace(c = getc(yyin))) {
		if (c == '\n')
			++linenum, columnum = 1;
		else
			++columnum;
	}
	if (c == EOF) {
		if (parser_out_home)
			error("Find EOF while parsing");
		return 0;
	}
	ungetc(c, yyin);
	return 1;
}

static unsigned char
follow(unsigned char op, unsigned char eq, unsigned char rep)
{
	register char c;

	if ((c = getc(yyin)) == '=')
		return eq;
	else if (c == op && rep)
		return rep;
	ungetc(c, yyin);
	return op;
}

static unsigned char rel_shift(unsigned char op)
{
	static char tokens[2][3] = {
		{GE, SHL, SHL_EQ},
		{LE, SHR, SHR_EQ}};
	register char c;
	register char *tp = tokens[op == '>'];

	if ((c = getc(yyin)) == '=') {
		return tp[0];
	} else if (c == op) {
		if ((c = getc(yyin)) == '=')
			return tp[2];
		op = tp[1];
	}
	ungetc(c, yyin);
	return c;
}

static unsigned char minus(void)
{
	register int c;

	switch (c = getc(yyin)) {
	case '-': return DEC;
	case '>': return INDIR;
	case '=': return SUB_EQ;
	default:
		ungetc(c, yyin);
		return '-';
	}
}

static unsigned char operator(void)
{
	register unsigned char c;

	switch (c = getc(yyin)) {
	case '=': return follow('=', EQ, 0);
	case '^': return follow('^', XOR_EQ, 0);
	case '*': return follow('*', MUL_EQ, 0);
	case '!': return follow('!', NE, 0);
	case '+': return follow('+', ADD_EQ, INC);
	case '&': return follow('&', AND_EQ, AND);
	case '|': return follow('|', OR_EQ, OR);
	case '<': return rel_shift('<');
	case '>': return rel_shift('>');
	case '-': return minus();
	default: return c;
	}
}

void next(void)
{
	register unsigned char c;

	if (aheadtok != NOTOK) {
		yytoken = aheadtok;
		aheadtok = NOTOK;
	} else if (!skip()) {
		yytoken = EOFTOK;
	} else if (isalpha(c = getc(yyin)) || c == '_') {
		ungetc(c, yyin);
		yytoken = iden();
	} else if (isdigit(c)) {
		ungetc(c, yyin);
		yytoken = number();
	} else {
		ungetc(c, yyin);
		yytoken = operator();
	}
}

unsigned char ahead(void)
{
	static unsigned char oldtok;

	oldtok = yytoken;
	next();
	aheadtok = yytoken;
	yytoken = oldtok;
	return aheadtok;
}

char accept(register unsigned char tok)
{
	if (yytoken == tok) {
		next();
		return 1;
	}
	return 0;
}

void expect(register unsigned char tok)
{
	if (yytoken != tok)
		error("unexpected %s", yytext);
	next();
}

void open_file(register const char *file)
{
	if (yyin != NULL)
		fclose(yyin);
	if (file == NULL) {
		yyin = stdin;
		filename = "(stdin)";
		return;
	}
	if ((yyin = fopen(file, "r")) == NULL)
		die("file '%s' not found", file);
	filename = file;
	columnum = linenum = 1;
}

{'fraction_non_alphanumeric': 0.18166158738571617, 'fraction_numerical': 0.004770107327414867, 'mean_word_length': 3.676579925650558, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6547640', 'n_tokens_mistral': 3282, 'n_tokens_neox': 3141, 'n_words': 872}