59 lines
1.5 KiB
Plaintext
59 lines
1.5 KiB
Plaintext
%{
|
|
#include <iostream>
|
|
#include <string>
|
|
|
|
#include "Token.hpp"
|
|
|
|
using namespace std;
|
|
|
|
string val;
|
|
int nbLines = 0;
|
|
int nbChars = 0;
|
|
%}
|
|
|
|
%option noyywrap
|
|
|
|
%x strEnv
|
|
|
|
integer [0-9]+
|
|
real [0-9]+\.[0-9]*|\.[0-9]+
|
|
value {integer}|{real}
|
|
operator "+"|"-"|"/"|"*"|"=>"|"<="|">="|"="|"->"
|
|
keyword "+"|"-"|"/"|"*"|"=>"|"<="|">="|"="|"->"
|
|
ident [a-zA-Z_][0-9a-zA-Z_]*
|
|
|
|
%%
|
|
|
|
{value} { val=yytext; return(Token::Token_NumberLiteral); }
|
|
{operator} { val=yytext; return(Token::Token_Operator); }
|
|
{keyword} { val=yytext; return(Token::Token_Keyword); }
|
|
{ident} { val=yytext; return(Token::Token_Identifier); }
|
|
"\"" { val.clear(); BEGIN(strEnv); }
|
|
<strEnv>"\"" { BEGIN(INITIAL); return(Token::Token_StringLiteral); }
|
|
<strEnv>"\n" { cerr << "multi-line strings not allowed" << endl; ++nbLines; }
|
|
<strEnv>"\\\n" { val+='\n'; ++nbLines; } // line cut by \
|
|
<strEnv>"\\". { val+=yytext[1]; }
|
|
<strEnv><<EOF>> { BEGIN(INITIAL); return(STRING); }
|
|
<strEnv>. { val+=yytext[0]; }
|
|
[ \t]+ { /* nothing to be done */ }
|
|
"\n" { ++nbLines; }
|
|
. { val=yytext; return(Token::Token_Unknown); }
|
|
|
|
%%
|
|
int main(int argc,char ** argv)
|
|
{
|
|
int token;
|
|
if(argc>1)
|
|
yyin=fopen(argv[1],"r"); // check result !!!
|
|
TODO
|
|
do {
|
|
token=yylex();
|
|
switch(token) {
|
|
case STRING: cerr << "STRING[" << val << "]" << endl; break;
|
|
case INTEGER: cerr << "INTEGER[" << val << "]" << endl; break;
|
|
}
|
|
} while(token);
|
|
|
|
cerr << nbLines << " lines" << endl;
|
|
return(0);
|
|
} |