Browse Source

Lexical parser

test
Michael Uleysky 9 years ago
commit
be4c41c762
  1. 21
      src/Makefile
  2. 19
      src/debug.cpp
  3. 11
      src/debug.h
  4. 26
      src/init.cpp
  5. 6
      src/init.h
  6. 13
      src/main.cpp
  7. 58
      src/parser/lexical.l
  8. 9
      src/parser/parser.h

21
src/Makefile

@ -0,0 +1,21 @@
CFLAGS=-O2 -g
OBJECTS=main.o debug.o init.o parser/lexical.o
makemap: $(OBJECTS)
g++ $(CFLAGS) -o $@ $(OBJECTS)
main.o: debug.h init.h
init.o: parser/lexical.h parser/parser.h debug.h init.h
parser/lexical.o: parser/parser.h debug.h
parser/lexical.h parser/lexical.cpp: parser/lexical.l
cd parser && flex lexical.l
clean:
rm -f *.o parser/*.o parser/lexical.{cpp,h}
distclean: clean
rm -f makemap

19
src/debug.cpp

@ -0,0 +1,19 @@
#include <iostream>
#include "debug.h"
std::ostream& COUT(debug_level dl)
{
if(dl==ERROR) return std::cerr;
if(dl>=SetDebugLevel(INTERNALREQUEST)) std::cout.clear();
else std::cout.setstate(std::cout.failbit);
return std::cout;
}
debug_level SetDebugLevel(debug_level dl)
{
static debug_level DEBUG_LEVEL=INFO;
if(dl==INTERNALREQUEST) return DEBUG_LEVEL;
debug_level s=DEBUG_LEVEL;
DEBUG_LEVEL=dl;
return s;
}

11
src/debug.h

@ -0,0 +1,11 @@
#ifndef DEBUG_H
#define DEBUG_H
#include <iostream>
enum debug_level {INTERNALREQUEST,DEBUG,INFO,WARNING,ERROR};
std::ostream& COUT(debug_level dl);
debug_level SetDebugLevel(debug_level dl=INTERNALREQUEST);
#endif

26
src/init.cpp

@ -0,0 +1,26 @@
#include "init.h"
#include "debug.h"
#include "parser/lexical.h"
#include "parser/parser.h"
int ParseConfigFile(char* config)
{
yyscan_t scanner;
struct lexical_extra extra;
FILE* conffd;
conffd=fopen(config,"r");
if(conffd==0)
{
COUT(ERROR)<<"Can't open file "<<config<<std::endl;
return 1;
}
extra.filename=config;
extra.inclevel=0;
extra.maxinclevel=10;
conflex_init_extra(&extra,&scanner);
confset_in(conffd,scanner);
conflex(scanner);
conflex_destroy(scanner);
fclose(conffd);
}

6
src/init.h

@ -0,0 +1,6 @@
#ifndef INIT_H
#define INIT_H
int ParseConfigFile(char* config);
#endif

13
src/main.cpp

@ -0,0 +1,13 @@
#include "debug.h"
#include "init.h"
int main(int argc, char** argv)
{
if(argc!=2) return 1;
SetDebugLevel(DEBUG);
ParseConfigFile(argv[1]);
return 0;
}

58
src/parser/lexical.l

@ -0,0 +1,58 @@
%option 8bit reentrant
%option warn
%option yylineno
%option noyywrap
%option yylineno
%option header-file="lexical.h"
%option outfile="lexical.cpp"
%option prefix="conf"
%option extra-type="const struct lexical_extra*"
%x STRING
%x PARSE
%{
#include <string>
#include "../debug.h"
#include "parser.h"
int nc;
%}
%%
include\(\".+\"\); {
if(yyextra->inclevel>=yyextra->maxinclevel) { COUT(ERROR)<<"Max include level reached in file "<<yyextra->filename<<" at line "<<yylineno<<std::endl; return 1; }
yyscan_t scanner;
struct lexical_extra extra;
std::string fname(yytext+9,yyleng-12);
FILE* fd;
fd=fopen(fname.c_str(),"r");
if(fd==0) { COUT(ERROR)<<"Can't open file "<<fname<<std::endl; return 1; }
COUT(DEBUG)<<"Include "<<fname<<std::endl;
extra.filename=fname.c_str();
extra.inclevel=yyextra->inclevel+1;
extra.maxinclevel=yyextra->maxinclevel;
yylex_init_extra(&extra,&scanner);
yyset_in(fd,scanner);
yylex(scanner);
yylex_destroy(scanner);
fclose(fd);
}
[a-zA-Z][a-zA-Z0-9_]* printf("NAME\n"); BEGIN(PARSE);
<PARSE>[+-]?[0-9]+ printf("INTEGER\n");
<PARSE>[+-]?[0-9]+(\.[0-9]*)?([eE][+-][0-9]+)? printf("REAL\n");
<PARSE>\( printf("OBRACE\n");
<PARSE>\) printf("CBRACE\n");
<PARSE>\; printf("ENDL\n"); BEGIN(0);
<PARSE>= printf("ASSIGN\n");
<PARSE>[a-zA-Z][a-zA-Z0-9_]* printf("IDENTIFIER\n");
<PARSE,INITIAL>[ ,\n\t]
<PARSE,INITIAL>\#.*
<PARSE>\" BEGIN(STRING); nc=0;
<PARSE,INITIAL>. COUT(ERROR)<<"Unknown symbol "<<yytext<<" in file "<<yyextra->filename<<" at line "<<yylineno<<std::endl;
<STRING>\\\\ nc++;
<STRING>\\\" nc++;
<STRING>\" BEGIN(PARSE); printf("STRING%d\n",nc);
<STRING>. nc++;
<STRING><<EOF>> COUT(ERROR)<<"Unclosed quote!"<<std::endl; yyterminate();
<<EOF>> yyterminate();
%%

9
src/parser/parser.h

@ -0,0 +1,9 @@
#ifndef PARSER_PARSER_H
#define PARSER_PARSER_H
struct lexical_extra
{
const char* filename;
unsigned int inclevel,maxinclevel;
};
#endif
Loading…
Cancel
Save