-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlyutils.cpp
97 lines (81 loc) · 2.37 KB
/
lyutils.cpp
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
90
91
92
93
94
95
96
// $Id: lyutils.cpp,v 1.11 2016-10-06 16:42:53-07 - - $
// Chenxing Ji
// cji13
// Nelson Yeap
// nyeap
#include <assert.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "auxlib.h"
#include "lyutils.h"
#include "astree.h"
bool lexer::interactive = true;
location lexer::lloc = {0, 1, 0};
size_t lexer::last_yyleng = 0;
vector<string> lexer::filenames;
astree* parser::root = nullptr;
FILE* tok_file;
int yylval_token (int symbol){
yylval = new astree (symbol, lexer::lloc, yytext);
fprintf(tok_file, " %lu %lu.%03lu %-5d %-13s (%s)\n", \
yylval->lloc.filenr, yylval->lloc.linenr, \
yylval->lloc.offset, \
yylval->symbol, parser::get_tname (yylval->symbol), \
yylval->lexinfo->c_str());
return symbol;
}
const string* lexer::filename (int filenr) {
return &lexer::filenames.at(filenr);
}
void lexer::newfilename (const string& filename) {
lexer::lloc.filenr = lexer::filenames.size();
lexer::filenames.push_back (filename);
}
void lexer::advance() {
if (not interactive) {
if (lexer::lloc.offset == 0) {
printf (";%2zd.%3zd: ",
lexer::lloc.filenr, lexer::lloc.linenr);
}
printf ("%s", yytext);
}
lexer::lloc.offset += last_yyleng;
last_yyleng = yyleng;
}
void lexer::newline() {
++lexer::lloc.linenr;
lexer::lloc.offset = 0;
}
void lexer::badchar (unsigned char bad) {
char buffer[16];
snprintf (buffer, sizeof buffer,
isgraph (bad) ? "%c" : "\\%03o", bad);
errllocprintf (lexer::lloc, "invalid source character (%s)\n",
buffer);
}
void lexer::badtoken (char* lexeme) {
errllocprintf (lexer::lloc, "invalid token (%s)\n", lexeme);
}
void lexer::include() {
size_t linenr;
static char filename[0x1000];
assert (sizeof filename > strlen (yytext));
int scan_rc = sscanf (yytext, "# %zd \"%[^\"]\"", &linenr, filename);
if (scan_rc != 2) {
errprintf ("%s: invalid directive, ignored\n", yytext);
}else {
if (yy_flex_debug) {
fprintf (stderr, "--included # %zd \"%s\"\n",
linenr, filename);
}
lexer::lloc.linenr = linenr - 1;
fprintf(tok_file, "%s\n", yytext);
lexer::newfilename (filename);
}
}
void yyerror (const char* message) {
assert (not lexer::filenames.empty());
errllocprintf (lexer::lloc, "%s\n", message);
}