Line 47... |
Line 47... |
#include
|
#include
|
#include
|
#include
|
|
|
using namespace std;
|
using namespace std;
|
|
|
extern "C" int yylex();
|
int yylex();
|
|
void mark_line(void);
|
|
int end_of_file(void);
|
|
void pushf(const char *fname);
|
|
|
// #include "zprepr.tab.h"
|
// #include "zprepr.tab.h"
|
int ndef = 0;
|
int ndef = 0, structno = 0;
|
|
char *structid = NULL;
|
void stb_define(const char *str);
|
void stb_define(const char *str);
|
void stb_args(const char *str);
|
void stb_args(const char *str);
|
void stb_macro(const char *value);
|
void stb_macro(const char *value);
|
void stb_addmacro(const char *value);
|
void stb_addmacro(const char *value);
|
bool stb_isdefined(const char *str);
|
bool stb_isdefined(const char *str);
|
const char *stb_getdefn(const char *str);
|
const char *stb_getdefn(const char *str);
|
%}
|
%}
|
%x DEF DFA DFV INDEF IFDEFV INNOTDEF NODEF NVRDEF COMMENT
|
%x DEF DFA DFV DFV_EOL INDEF IFDEFV INNOTDEF NODEF NVRDEF COMMENT INSTRUCT
|
%x INDEF_EOL INNOTDEF_EOL
|
%x INDEF_EOL INNOTDEF_EOL GETSTRUCTID
|
%option noyywrap
|
%option noyywrap
|
%option stack
|
%option stack
|
ID [_:A-Za-z][_:A-Za-z0-9]*
|
ID [_:A-Za-z][_:A-Za-z0-9]*
|
|
IDDOT {ID}("."{ID})*
|
|
|
%%
|
%%
|
"*/" { yy_pop_state(); }
|
"*/" { yy_pop_state(); }
|
[^*]+ { /* Ignore comments */ }
|
[^*]+ { /* Ignore comments */ }
|
"*"+[^/] { /* Ignore comments */ }
|
"*"+[^/] { /* Ignore comments */ }
|
^"#include"[ \t]+\"[^\"]+\" {
|
^"#include"[ \t]+\"[^\"]+\" {
|
char *ptr = &yytext[9], *start, *end, *str;
|
char *ptr = &yytext[9], *start, *end, *str;
|
while(isspace(*ptr))
|
while(isspace(*ptr))
|
ptr++;
|
ptr++;
|
start = ptr;
|
start = ++ptr;
|
ptr++;
|
ptr++;
|
while((*ptr)&&(*ptr != '\"'))
|
while((*ptr)&&(*ptr != '\"'))
|
ptr++;
|
ptr++;
|
*ptr = '\0';
|
*ptr = '\0';
|
yypush_buffer_state(yy_create_buffer(
|
pushf(start);
|
fopen(start, "r"),
|
|
YY_BUF_SIZE));
|
|
// push_file_state(yylineno); // and filename ...
|
// push_file_state(yylineno); // and filename ...
|
fprintf(yyout, "#line 0 \"%s\"\n", start);
|
mark_line();
|
}
|
}
|
^"#define"[ \t]+ { yy_push_state(DEF); }
|
^"#define"[ \t]+ { yy_push_state(DEF); }
|
/* <*>^"#line"[ \t]+(0-9)+[ \t]+["][^"]*["][ \t]*\n { } */
|
/* <*>^"#line"[ \t]+(0-9)+[ \t]+["][^"]*["][ \t]*\n { } */
|
[_A-Za-z][_:A-Za-z0-9]*/[^(] {
|
{IDDOT}/[^(] {
|
stb_define(yytext);
|
stb_define(yytext);
|
BEGIN DFV;
|
BEGIN DFV;
|
}
|
}
|
{ID}/[(] { fprintf(stderr, "DEF::Found MACRO\n");
|
{IDDOT}/[(] { stb_define(yytext); BEGIN DFA; }
|
stb_define(yytext);
|
"("[^)]+")" {
|
BEGIN DFA; }
|
/* Process macro arguments */
|
([^)]+) {
|
|
/* Process arguments */
|
|
stb_args(yytext);
|
stb_args(yytext);
|
BEGIN DFV;
|
BEGIN DFV;
|
}
|
}
|
[ \t]+ { /* Ignore initial spaces */ }
|
[ \t]+ { /* Ignore initial spaces */ }
|
[.]*$ {/* Parse to end of line, get value for our define */
|
[^ \t\n]* {/* Parse to end of line, get value for our define */
|
// printf("End of define, value = \'%s\'\n", yytext);
|
|
stb_macro(yytext);
|
stb_macro(yytext);
|
|
}
|
|
[ \t]*((;|"//").*)?$ {/* Parse to end of line, get value for our define */
|
yy_pop_state();
|
yy_pop_state();
|
}
|
}
|
|
[ \t]*"\\"[ \t]*((;|"//").*)?\n {/* Continue onto next line */
|
|
fprintf(yyout, "\n"); mark_line(); yylineno++;
|
|
stb_macro("\n");
|
|
}
|
^[ \t]+.[dD][aA][tT][aA]? { fprintf(yyout, "\tWORD"); }
|
^[ \t]+.[dD][aA][tT][aA]? { fprintf(yyout, "\tWORD"); }
|
^"#defcont"[ \t]+ { yy_push_state(DFV); }
|
^"#defcont"[ \t]+ { yy_push_state(DFV); }
|
^"#ifdef"[ \t]* { ndef = 0; yy_push_state(IFDEFV); }
|
^"#ifdef"[ \t]* { ndef = 0; yy_push_state(IFDEFV); }
|
^"#ifndef"[ \t]* { ndef = 1; yy_push_state(IFDEFV); }
|
^"#ifndef"[ \t]* { ndef = 1; yy_push_state(IFDEFV); }
|
{ID} {
|
{IDDOT} {
|
bool known = stb_isdefined(yytext);
|
bool known = stb_isdefined(yytext);
|
if ( ((known)&&(ndef==0)) || ((!known)&&(ndef!=0)) ) {
|
if ( ((known)&&(ndef==0)) || ((!known)&&(ndef!=0)) ) {
|
BEGIN INDEF_EOL;
|
BEGIN INDEF_EOL;
|
} else {
|
} else {
|
BEGIN INNOTDEF_EOL;
|
BEGIN INNOTDEF_EOL;
|
Line 122... |
Line 129... |
/* ^"#elsif"[ \t]* { yy_pop_state(); yy_push_state(IFDEFE); } */
|
/* ^"#elsif"[ \t]* { yy_pop_state(); yy_push_state(IFDEFE); } */
|
[ \t]*$ { BEGIN INDEF; }
|
[ \t]*$ { BEGIN INDEF; }
|
(;|"//").*$ { BEGIN INDEF; }
|
(;|"//").*$ { BEGIN INDEF; }
|
[ \t]*$ { BEGIN INNOTDEF; }
|
[ \t]*$ { BEGIN INNOTDEF; }
|
(;|"//").*$ { BEGIN INNOTDEF; }
|
(;|"//").*$ { BEGIN INNOTDEF; }
|
[^ \t\n].*$ { BEGIN INDEF; fprintf(stderr, "WARNING! Unexpected characters on IFDEF line, \'%s\'\n", yytext); }
|
[^ \t\n].*$ { BEGIN INDEF; fprintf(stderr, "WARNING! Unexpected characters on IFDEF line, \'%s\'\n", yytext); mark_line(); }
|
[^ \t\n].*$ { BEGIN INNOTDEF; fprintf(stderr, "WARNING! Unexpected characters on IFNDEF line, %s\n", yytext); }
|
[^ \t\n].*$ { BEGIN INNOTDEF; fprintf(stderr, "WARNING! Unexpected characters on IFNDEF line, %s\n", yytext); mark_line(); }
|
^"#else"[ \t]*((;|"//").*)?$ { BEGIN NODEF; }
|
^"#else"[ \t]*((;|"//").*)?$ { BEGIN NODEF; }
|
^"#else"[ \t]*((;|"//").*)?$ { BEGIN INDEF; }
|
^"#else"[ \t]*((;|"//").*)?$ { BEGIN INDEF; }
|
(.*) { }
|
(.*) { }
|
^"#elsif"[ \t]* { BEGIN NVRDEF; }
|
^"#elsif"[ \t]* { BEGIN NVRDEF; }
|
^"#endif"[ \t]*((;|"//").*)?$ { yy_pop_state(); }
|
^"#endif"[ \t]*((;|"//").*)?$ { yy_pop_state(); }
|
^"#endif"[ \t]*"/*" { BEGIN COMMENT; }
|
^"#endif"[ \t]*"/*" { BEGIN COMMENT; }
|
<*>^"#endif"[ \t]* { fprintf(stderr, "ERR: Unknown endif!!\n");}
|
<*>^"#endif"[ \t]* { fprintf(stderr, "ERR: Unknown endif!!\n");}
|
|
^"#struct"[ \t]* {
|
|
yy_push_state(GETSTRUCTID); structno = 0; }
|
|
{ID}/[ \t\n;/] { BEGIN INSTRUCT;
|
|
structid = strdup(yytext);
|
|
}
|
|
{ID}("."{ID})* {
|
|
fprintf(yyout, "\t%s.%s\tequ\t%d", structid, yytext, structno++); }
|
|
^"#endstruct".*$ { yy_pop_state(); }
|
/* Not yet: ^"#struct"[ \t]* {} */
|
/* Not yet: ^"#struct"[ \t]* {} */
|
/* Not yet: ^"#endstruct"[ \t]* {} */
|
/* Not yet: ^"#endstruct"[ \t]* {} */
|
/* Not yet: ^"#seg"[ \t]* {} */
|
/* Not yet: ^"#seg"[ \t]* {} */
|
.* { /* Ignore everything in these states*/ }
|
.* { /* Ignore everything in these states*/ }
|
{ID}/[^(] {
|
{ID}/[^(] {
|
if (stb_isdefined(yytext))
|
if (stb_isdefined(yytext))
|
fprintf(yyout, "%s", stb_getdefn(yytext));
|
fprintf(yyout, "%s", stb_getdefn(yytext));
|
else
|
else
|
fprintf(yyout, "%s", yytext);
|
fprintf(yyout, "%s", yytext);
|
}
|
}
|
<*>^[ \t]*"//".*$ { /* Ignore comment only lines */ }
|
<*>[ \t]*"//".*$ { /* Ignore (trailing) comment only lines */ }
|
<*>^[ \t]*";".*$ { /* Ignore comment only lines */ }
|
<*>[ \t]*";".*$ { /* Ignore (trailing) comment only lines */ }
|
<*>"//".* { /* Ignore trailing comments */ }
|
|
<*>";".* { /* Ignore trailing comments */ }
|
|
<*>"/*" { yy_push_state(COMMENT); }
|
<*>"/*" { yy_push_state(COMMENT); }
|
<*>[ \t]+ { ECHO; }
|
<*>[ \t]+ { ECHO; }
|
[.]* { ECHO; }
|
<*>\n { ECHO; yylineno++; mark_line(); }
|
<*>\n { ECHO; }
|
|
/* <*>. { printf("Unmatched \'%c\'\n", yytext[0]); } */
|
/* <*>. { printf("Unmatched \'%c\'\n", yytext[0]); } */
|
/* <> { printf("EOF!\n"); } */
|
<> { if (end_of_file()) yyterminate(); }
|
|
|
%%
|
%%
|
|
|
class SYMTABLE_ENTRY {
|
class SYMTABLE_ENTRY {
|
private:
|
private:
|
Line 193... |
Line 205... |
SYMTABLE_ENTRY &setargs(const char *str) {
|
SYMTABLE_ENTRY &setargs(const char *str) {
|
m_args += str;
|
m_args += str;
|
return *this;
|
return *this;
|
}
|
}
|
|
|
std::string getdefn(void) {
|
const std::string &getdefn(void) {
|
return m_value;
|
return m_value;
|
}
|
}
|
};
|
};
|
|
|
class SYMBOL_TABLE {
|
class SYMBOL_TABLE {
|
Line 301... |
Line 313... |
|
|
const char *stb_getdefn(const char *str) {
|
const char *stb_getdefn(const char *str) {
|
return syms.getdefn(str);
|
return syms.getdefn(str);
|
}
|
}
|
|
|
|
class BUFSTACK {
|
|
public:
|
|
FILE *m_fp;
|
|
char *m_fname;
|
|
int m_lineno;
|
|
BUFSTACK *m_prev;
|
|
YY_BUFFER_STATE m_bs;
|
|
|
|
static BUFSTACK *curbs;
|
|
static const char *curfilename;
|
|
|
|
BUFSTACK(void) {
|
|
m_fp = stdin;
|
|
|
|
if (curbs)
|
|
curbs->m_lineno = yylineno;
|
|
m_prev = curbs;
|
|
// m_bs = yy_create_buffer(fp, YY_BUF_SIZE);
|
|
m_fname = strdup("(stdin)");
|
|
// yy_switch_to_buffer(m_bs);
|
|
m_bs = NULL;
|
|
curbs = this;
|
|
m_lineno = 1;
|
|
curfilename = m_fname;
|
|
|
|
yyrestart(m_fp);
|
|
yylineno = 1;
|
|
}
|
|
|
|
BUFSTACK(const char *fname) {
|
|
m_fp = fopen(fname, "r");
|
|
if (!m_fp) {
|
|
fprintf(stderr, "Cannot open %s\n", fname);
|
|
perror("O/S Err:");
|
|
exit(-1);
|
|
}
|
|
|
|
if (curbs)
|
|
curbs->m_lineno = yylineno;
|
|
m_prev = curbs;
|
|
m_bs = yy_create_buffer(m_fp, YY_BUF_SIZE);
|
|
m_fname = strdup(fname);
|
|
yy_switch_to_buffer(m_bs);
|
|
curbs = this;
|
|
m_lineno = 1;
|
|
curfilename = m_fname;
|
|
|
|
yyrestart(m_fp);
|
|
yylineno = 1;
|
|
}
|
|
|
|
~BUFSTACK(void) {
|
|
// fprintf(stderr, "DELETING(%s)\n", m_fname);
|
|
fclose(m_fp);
|
|
free(m_fname);
|
|
if (m_bs)
|
|
yy_delete_buffer(m_bs);
|
|
curbs = m_prev;
|
|
|
|
if (curbs) {
|
|
yy_switch_to_buffer(curbs->m_bs);
|
|
yylineno = curbs->m_lineno;
|
|
curfilename = curbs->m_fname;
|
|
}
|
|
}
|
|
|
|
void mark(void) {
|
|
FILE *fp = yyout;
|
|
if (!fp) fp = stdout;
|
|
fprintf(fp, "#line %d \"%s\"\n", yylineno, m_fname);
|
|
}
|
|
|
|
static void pop(void) {
|
|
// fprintf(stderr, "POP! (%s)\n", curbs->m_fname);
|
|
if (curbs)
|
|
delete curbs;
|
|
}
|
|
};
|
|
|
|
BUFSTACK *BUFSTACK::curbs = NULL;
|
|
const char *BUFSTACK::curfilename = NULL;
|
|
|
|
int last_marked_line = -1;
|
|
const char *last_marked_file = NULL;
|
|
void mark_line(void) {
|
|
if ((yylineno != last_marked_line+1)||(BUFSTACK::curfilename != last_marked_file))
|
|
BUFSTACK::curbs->mark();
|
|
last_marked_line = yylineno;
|
|
last_marked_file = BUFSTACK::curfilename;
|
|
}
|
|
|
|
int end_of_file(void) {
|
|
BUFSTACK::pop();
|
|
return (BUFSTACK::curbs == NULL);
|
|
}
|
|
|
|
void pushf(const char *fname) {
|
|
BUFSTACK *bs = new BUFSTACK(fname);
|
|
}
|
|
|
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
|
yylineno = 1;
|
|
if (argc < 2) { // Stdin only
|
|
BUFSTACK::curbs = new BUFSTACK();
|
yylex();
|
yylex();
|
|
} else {
|
|
for(int argn=1; argn
|
|
BUFSTACK *bs = new BUFSTACK(argv[argn]);
|
|
mark_line();
|
|
yylex();
|
|
// delete bs;
|
|
}
|
|
}
|
}
|
}
|
|
|