diff options
Diffstat (limited to 'c_parser/src/c_lexer.flex')
-rw-r--r-- | c_parser/src/c_lexer.flex | 137 |
1 files changed, 21 insertions, 116 deletions
diff --git a/c_parser/src/c_lexer.flex b/c_parser/src/c_lexer.flex index c2a42c4..b8a7e8a 100644 --- a/c_parser/src/c_lexer.flex +++ b/c_parser/src/c_lexer.flex @@ -1,131 +1,36 @@ %option noyywrap %{ +// Avoid error "error: �fileno� was not declared in this scope" +extern "C" int fileno(FILE *stream); -#include "c_lexer.hpp" - -#include <sstream> -#include <stdlib.h> - -int lineCount = 1; -int spaceCount = 1; -int sourceLineCount = 1; - -std::string fileName; +#include "c_parser.tab.hpp" %} -KEYWORD auto|double|int|struct|break|else|long|switch|case|enum|register|typedef|char|extern|return|union|const|float|short|unsigned|continue|for|signed|void|default|goto|sizeof|volatile|do|if|static|while - -IDENTIFIER [_a-zA-Z][_a-zA-Z0-9]* - -OPERATOR [.][.][.]|[<>][<>][=]|[-][-]|[+][+]|[|][|]|[#][#]|[&][&]|[+\-*\/<>=!%^|&][=]|[<][<]|[->][>]|[<>&=+\/\-*(){}\[\]\.,%~!?:|^;] - -FRACTIONALCONSTANT (([0-9]*\.[0-9]+)|([0-9]+\.)) -EXPONENTPART ([eE][+-]?[0-9]+) - -FLOATINGSUFFIX ([flFL]) -INTEGERSUFFIX ([uU][lL]|[lL][uU]|[uUlL]) - -DECIMALCONSTANT ([1-9][0-9]*) -OCTALCONSTANT ([0][0-7]*) -HEXCONSTANT ([0][xX][0-9A-Fa-f]+) - -CHARCONSTANT ('(([\\]['])|([^']))+') - -STRINGLITERAL ["](([\\]["])|([^"]))*["] - -NEWLINE (\r\n?|\n) - -WHITESPACE [ ] - -TAB \t - -PREPROC [#][ ][0-9]+[ ]{STRINGLITERAL}[ 0-9]* - -ALL . - %% +[*] { return T_TIMES; } +[+] { return T_PLUS; } +[/] { return T_DIVIDE; } +[-] { return T_MINUS; } -{KEYWORD} { - yylval = new std::string(yytext); - return Keyword; -} +[(] { return T_LBRACKET; } +[)] { return T_RBRACKET; } -{IDENTIFIER} { - yylval = new std::string(yytext); - return Identifier; -} +log { return T_LOG; } +exp { return T_EXP; } +sqrt { return T_SQRT; } -{OPERATOR} { - yylval = new std::string(yytext); - return Operator; -} +[-]?[0-9]+([.][0-9]*)? { yylval.number=strtod(yytext, 0); return T_NUMBER; } +[a-z]+ { yylval.string=new std::string(yytext); return T_VARIABLE; } -{FRACTIONALCONSTANT}{EXPONENTPART}?{FLOATINGSUFFIX}? { - yylval = new std::string(yytext); - return Constant; -} - -([0-9]+){EXPONENTPART}{FLOATINGSUFFIX}? { - yylval = new std::string(yytext); - return Constant; -} +[ \t\r\n]+ {;} -{HEXCONSTANT}{INTEGERSUFFIX}? { - yylval = new std::string(yytext); - return Constant; -} - -{DECIMALCONSTANT}{INTEGERSUFFIX}? { - yylval = new std::string(yytext); - return Constant; -} - -{OCTALCONSTANT}{INTEGERSUFFIX}? { - yylval = new std::string(yytext); - return Constant; -} - -{CHARCONSTANT} { - std::string tmp(yytext); - yylval = new std::string(tmp.substr(1, tmp.length()-2)); - return Constant; -} - -{STRINGLITERAL} { - std::string tmp(yytext); - yylval = new std::string(tmp.substr(1, tmp.length()-2)); - return StringLiteral; -} - -{NEWLINE} { - spaceCount = 1; - lineCount++; - sourceLineCount++; -} - -{WHITESPACE} { - spaceCount++; -} - -{PREPROC} { - int srcLineInt; - - yylval = new std::string(yytext); - std::stringstream preProcLine((*yylval).substr(1, (*yylval).length())); - preProcLine >> srcLineInt >> fileName; - sourceLineCount = srcLineInt - 1; - fileName = fileName.substr(1, fileName.length() - 2); -} - -{TAB} { - spaceCount += 8; -} +. { fprintf(stderr, "Invalid token\n"); exit(1); } +%% -{ALL} { - yylval = new std::string(yytext); - return Invalid; +void yyerror (char const *s) +{ + fprintf (stderr, "Parse error : %s\n", s); + exit(1); } - -%% |