commit be4c41c762566514c0daff828a27f593cbedd265 Author: Michael Uleysky Date: Fri Aug 21 13:15:58 2015 +1000 Lexical parser diff --git a/src/Makefile b/src/Makefile new file mode 100644 index 0000000..04aa6ef --- /dev/null +++ b/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 \ No newline at end of file diff --git a/src/debug.cpp b/src/debug.cpp new file mode 100644 index 0000000..0c5579b --- /dev/null +++ b/src/debug.cpp @@ -0,0 +1,19 @@ +#include +#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; +} diff --git a/src/debug.h b/src/debug.h new file mode 100644 index 0000000..3bfdf3c --- /dev/null +++ b/src/debug.h @@ -0,0 +1,11 @@ +#ifndef DEBUG_H +#define DEBUG_H +#include + +enum debug_level {INTERNALREQUEST,DEBUG,INFO,WARNING,ERROR}; + +std::ostream& COUT(debug_level dl); + +debug_level SetDebugLevel(debug_level dl=INTERNALREQUEST); + +#endif diff --git a/src/init.cpp b/src/init.cpp new file mode 100644 index 0000000..d5879c9 --- /dev/null +++ b/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 "< +#include "../debug.h" +#include "parser.h" +int nc; +%} + +%% +include\(\".+\"\); { + if(yyextra->inclevel>=yyextra->maxinclevel) { COUT(ERROR)<<"Max include level reached in file "<filename<<" at line "<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); +[+-]?[0-9]+ printf("INTEGER\n"); +[+-]?[0-9]+(\.[0-9]*)?([eE][+-][0-9]+)? printf("REAL\n"); +\( printf("OBRACE\n"); +\) printf("CBRACE\n"); +\; printf("ENDL\n"); BEGIN(0); += printf("ASSIGN\n"); +[a-zA-Z][a-zA-Z0-9_]* printf("IDENTIFIER\n"); +[ ,\n\t] +\#.* +\" BEGIN(STRING); nc=0; +. COUT(ERROR)<<"Unknown symbol "<filename<<" at line "<\\\\ nc++; +\\\" nc++; +\" BEGIN(PARSE); printf("STRING%d\n",nc); +. nc++; +<> COUT(ERROR)<<"Unclosed quote!"<> yyterminate(); +%% diff --git a/src/parser/parser.h b/src/parser/parser.h new file mode 100644 index 0000000..b5960ab --- /dev/null +++ b/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