diff options
author | Yann Herklotz <ymherklotz@gmail.com> | 2017-02-07 18:10:15 +0000 |
---|---|---|
committer | Yann Herklotz <ymherklotz@gmail.com> | 2017-02-07 18:10:15 +0000 |
commit | d1bb4f42bd6ab8e8f0d0dfe7acb45183e4629760 (patch) | |
tree | bed66cf902367f0d28e152e0d5612480e873ca52 /c_lexer | |
parent | 1a89a34dfc04a32ec5c25f6bb6d91173acf31679 (diff) | |
download | Compiler-d1bb4f42bd6ab8e8f0d0dfe7acb45183e4629760.tar.gz Compiler-d1bb4f42bd6ab8e8f0d0dfe7acb45183e4629760.zip |
Went about the issue with yyleng being of weird type
Diffstat (limited to 'c_lexer')
-rw-r--r-- | c_lexer/include/c_lexer.hpp | 4 | ||||
-rw-r--r-- | c_lexer/src/c_lexer.flex | 13 | ||||
-rw-r--r-- | c_lexer/src/main.cpp | 2 | ||||
-rw-r--r-- | c_lexer/test/test_lex.c | 8 |
4 files changed, 17 insertions, 10 deletions
diff --git a/c_lexer/include/c_lexer.hpp b/c_lexer/include/c_lexer.hpp index 54b1b81..90c4d53 100644 --- a/c_lexer/include/c_lexer.hpp +++ b/c_lexer/include/c_lexer.hpp @@ -19,12 +19,12 @@ extern std::string *yylval; // flex function to run on input extern int yylex(); -extern size_t yyleng; - extern int lineCount; extern int spaceCount; extern int sourceLineCount; +extern int charLength; + extern std::string fileName; // get the correct output diff --git a/c_lexer/src/c_lexer.flex b/c_lexer/src/c_lexer.flex index c2a42c4..6c7a424 100644 --- a/c_lexer/src/c_lexer.flex +++ b/c_lexer/src/c_lexer.flex @@ -11,6 +11,8 @@ int lineCount = 1; int spaceCount = 1; int sourceLineCount = 1; +int charLength = 0; + std::string fileName; %} @@ -49,53 +51,63 @@ ALL . {KEYWORD} { yylval = new std::string(yytext); + charLength = (int)yyleng; return Keyword; } {IDENTIFIER} { yylval = new std::string(yytext); + charLength = (int)yyleng; return Identifier; } {OPERATOR} { yylval = new std::string(yytext); + charLength = (int)yyleng; return Operator; } {FRACTIONALCONSTANT}{EXPONENTPART}?{FLOATINGSUFFIX}? { yylval = new std::string(yytext); + charLength = (int)yyleng; return Constant; } ([0-9]+){EXPONENTPART}{FLOATINGSUFFIX}? { yylval = new std::string(yytext); + charLength = (int)yyleng; return Constant; } {HEXCONSTANT}{INTEGERSUFFIX}? { yylval = new std::string(yytext); + charLength = (int)yyleng; return Constant; } {DECIMALCONSTANT}{INTEGERSUFFIX}? { yylval = new std::string(yytext); + charLength = (int)yyleng; return Constant; } {OCTALCONSTANT}{INTEGERSUFFIX}? { yylval = new std::string(yytext); + charLength = (int)yyleng; return Constant; } {CHARCONSTANT} { std::string tmp(yytext); yylval = new std::string(tmp.substr(1, tmp.length()-2)); + charLength = (int)yyleng; return Constant; } {STRINGLITERAL} { std::string tmp(yytext); yylval = new std::string(tmp.substr(1, tmp.length()-2)); + charLength = (int)yyleng; return StringLiteral; } @@ -125,6 +137,7 @@ ALL . {ALL} { yylval = new std::string(yytext); + charLength = (int)yyleng; return Invalid; } diff --git a/c_lexer/src/main.cpp b/c_lexer/src/main.cpp index 8b20098..1e45bbd 100644 --- a/c_lexer/src/main.cpp +++ b/c_lexer/src/main.cpp @@ -65,7 +65,7 @@ int main() { printf("%s,\n", toJson(classType, text, streamLine, sourceCol, sourceLine, fileName).c_str()); - spaceCount += yyleng; + spaceCount += charLength; } printf("{}\n]\n"); diff --git a/c_lexer/test/test_lex.c b/c_lexer/test/test_lex.c index 9db9054..e962c60 100644 --- a/c_lexer/test/test_lex.c +++ b/c_lexer/test/test_lex.c @@ -1,14 +1,8 @@ -#include <stdlib.h> -#include <stdio.h> -#include <assert.h> -#include <math.h> - -#define POTATO 5 - int main(int argc, char* argv[]) { char* h = "Hello World\""; int j = POTATO; int u = 2398uL; float rt = 23.238e-283; + char x = 'a'; return 0; } |