Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.compilers > #1602

Re: lex - how to get current file position of parser file

From Jens Kallup <jkallup@web.de>
Newsgroups comp.compilers
Subject Re: lex - how to get current file position of parser file
Date 2015-09-06 21:53 +0200
Organization Aioe.org NNTP Server
Message-ID <15-09-007@comp.compilers> (permalink)
References <15-08-019@comp.compilers> <15-09-001@comp.compilers> <15-09-003@comp.compilers>

Show all headers | View raw


Hello,

I start a testcase Project - see attachment(s).
Most of the source comes from John's book.
But I collect his code, and using the Qt5 framework,
to make an executable.
It can be created by "qmake", which makes a "Makefile".
And with "make", it can be compiled into exec file.
It will be run fine, except "set procedure to test.prg".
The applications display the content of initial code, as
given by "argv[1]" at start up.
But, if I try to include "test.prg", the application
display me a blank line, and jumps to next line in initial
file.

Have someone a glue?
Thanks
Jens

// ---%<--- main.cpp:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

extern int newfile(char*);
extern int yylex(void);

int main(int argc, char *argv[])
{
     if (argc < 2) {
         fprintf(stderr, "need filename\n");
         return 1;
     }


     if (newfile(argv[1]))
         return yylex();
}
// --->%--- EOF - main.cpp

// ---%<--- flex.l:
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include "y.tab.h"

#include <QString>
#include <QVector>

QVector<QString> paths_vector;

struct bufstack {
     struct bufstack *prev;  /* previous entry */
     YY_BUFFER_STATE bs;     /* saved buffer */
     int lineno;             /* saved line number */
     int parse_mode;         /* compiler pass */
     char *filename;         /* name of this file */
     FILE *f;                /* current file */
} *curbs = 0;

char *curfilename; /* name of current input file */

int newfile(char *fn)
{
     FILE *f = fopen(fn, "r");
     struct bufstack *bs = (bufstack*) malloc(sizeof(struct bufstack));

     /* die if no file or no room */
     if (!f)  { perror(fn); return 0;   }
     if (!bs) { perror("mallocas"); return 0; }

     /* remember state */
     if (curbs) curbs->lineno = yylineno;
     bs->prev = curbs;

     /* set up current entry */
     bs->bs = yy_create_buffer(f, YY_BUF_SIZE);
     bs->f = f;
     bs->filename = fn;

     yy_switch_to_buffer(bs->bs);

     curbs = bs;
     yylineno = 1;
     curfilename = fn;

     return 1;
}

int popfile(void)
{
     struct bufstack *bs = curbs;
     struct bufstack *prevbs;

     if(!bs) return 0;

     /* get rid of current entry */
     fclose(bs->f);

     yy_delete_buffer(bs->bs);

     /* switch back to previous */
     prevbs = bs->prev;
     free(bs);

     if (!prevbs) return 0;

     yy_switch_to_buffer(prevbs->bs);
     curbs = prevbs;
     yylineno = curbs->lineno;
     curfilename = curbs->filename;

     return 1;
}

void yyerror(char* message)
{
     printf("Error: '%s' in line: %d",message,yylineno);
     exit(1);
}

extern "C" int yywrap(void) { return 1; }
%}

%%

"set"[ \t\n]*"path"[ \t\n]*"to"[ \t\n]*\"([^\"]*)\" {
     QString str = yytext;
     str = str.replace("set","");
     str = str.replace("path","");
     str = str.replace("to","");
     str = str.replace("\"","");
     str = str.replace("\n","");
     str = str.replace("\t","");
     str = str.replace(" ","");

     paths_vector << QString(str.toStdString().c_str());
     BEGIN(INITIAL);
}

"set"[ \t\n]*"procedure"[ \t\n]*"to"[ \t\n]*([0-9a-zA-Z_]+[0-9a-zA-Z_\.]*) {

     QString str = yytext;

     str = str.replace("procedure","");
     str = str.replace("set","");
     str = str.replace("to","");
     str = str.replace("\n","");
     str = str.replace("\t","");
     str = str.replace(" ","");

     QString s;
     if (paths_vector.size() < 0) {
     printf("hhh\n");
         s = QString("./" + str);
         if (!newfile((char*)s.toStdString().c_str())) {
             printf("file not found!\n");
             yyterminate();
         }
     }
     else {
         for (int i = 0; i < paths_vector.size(); i++)
         {
             s = paths_vector.at(i);
             s = s + QString("/") + str;

             if ((newfile((char*)s.toStdString().c_str()))) {
                 break;
             }
         }
     }

     BEGIN(INITIAL);
}

<<EOF>> { if(!popfile()) yyterminate(); }

^.      { fprintf(yyout, "%4d %s", yylineno, yytext); }
^\n     { fprintf(yyout, "%4d %s", yylineno++, yytext); }
\n      { ECHO; yylineno++; }
.       { ECHO; }

%%
// --->%--- EOF - flex.l

// ---%<--- yacc.y
%{
extern int  yylex();
extern void yyerror(char*);
%}
%start program

%%

program : stmt_seq;
stmt_seq
     : stmt_seq stmt { }
     | stmt
     ;

stmt
     : { /* empty */ }
     ;

%%

// --->%--- EOF - yacc.y


// ---%<--- qmake.pro
QT       += core

QT       -= gui

TARGET = predbase
CONFIG   += console
CONFIG   -= app_bundle

TEMPLATE = app


SOURCES += \
     pre_main.cpp

OTHER_FILES += \
     pre_pcode.l\
     pre_pcode.y

FLEXSOURCES  = pre_pcode.l
BISONSOURCES = pre_pcode.y

flex.commands = flex -i ${QMAKE_FILE_IN} && mv lex.yy.c lex.yy.cpp
flex.input = FLEXSOURCES
flex.output = lex.yy.cpp
flex.variable_out = SOURCES
flex.depends = y.tab.h
flex.name = flex
QMAKE_EXTRA_COMPILERS += flex

bison.commands = bison -d -t -y ${QMAKE_FILE_IN} && mv y.tab.c y.tab.cpp
bison.input = BISONSOURCES
bison.output = y.tab.cpp
bison.variable_out = SOURCES
bison.name = bison
QMAKE_EXTRA_COMPILERS += bison

bisonheader.commands = @true
bisonheader.input = BISONSOURCES
bisonheader.output = y.tab.h
bisonheader.variable_out = HEADERS
bisonheader.name = bison header
bisonheader.depends = y.tab.cpp
QMAKE_EXTRA_COMPILERS += bisonheader

QMAKE_EXTRA_TARGETS += flex bison

// --->%--- EOF - qmake.pro

[I haven't a clue.  What ever happened to using debuggers
and breakpoints to debug your code? -John]

Back to comp.compilers | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

lex - how to get current file position of parser file Jens Kallup <jkallup@web.de> - 2015-09-01 04:20 +0200
  Re: lex - how to get current file position of parser file Kaz Kylheku <kaz@kylheku.com> - 2015-09-01 14:19 +0000
    Re: lex - how to get current file position of parser file federation2005@netzero.com - 2015-09-03 15:47 -0700
      Re: include files, was lex - how to get current file position of parser file Kaz Kylheku <kaz@kylheku.com> - 2015-09-04 05:26 +0000
    Re: lex - how to get current file position of parser file Gene Wirchenko <genew@telus.net> - 2015-09-03 18:19 -0700
      Re: lex - how to get current file position of parser file Jens Kallup <jkallup@web.de> - 2015-09-06 21:53 +0200
        Re: lex - how to get current file position of parser file Kaz Kylheku <kaz@kylheku.com> - 2015-09-07 05:59 +0000
    Re: lex - how to get current file position of parser file Jens Kallup <jkallup@web.de> - 2015-09-07 17:11 +0200

csiph-web