lsd10.l
Description du code
Fichier Lex du langage LSD010 Compilateur LSD010Code source ou contenu du fichier
Code Lex (lsd10.l) (312 lignes)
%{ /* * lsd10.l : lexical parsing file for Lex * @see http://www.gaudry.be/langages-flex.html * Part of the compiler Project for LSD10 language */ #include <stdio.h> #include <stdlib.h> #include <string.h> #include "y.tab.h" /* #include "ast.h" */ #include "common.h" /** * Count of lines read */ int lexLinesCount=0; /** * Count of chars read */ int lexTotalCharsCount=0; extern AstNode *rootNode; /* * internal business ************************************************************* */ extern DebugInfo *psnBeforeToken; extern DebugInfo *psnAfterToken; int processToken(int tokenType); void incrementNumChar(); void addLineRead(); /* * Lex overriding ************************************************************* */ int yyTempColumn=1; //define YY_USER_ACTION yylloc.first_line=yylloc.last_line=yylineno;yylloc.first_column=yylloc.last_column=yyTempColumn+yyleng-1;yyTempColumn+=yyleng; %} %option noyywrap %option yylineno %x COMMENT_LINE COMMENT_BLOC /*nbr 0|[-][1-9][0-9]*|[1-9][0-9]* */ nbr 0|[1-9][0-9]* id [a-zA-Z]|([a-zA-Z]([a-zA-Z]|[0-9])*) blank [ \t]+ %% %{ //we specify that we begin with the rules of the state INITIAL BEGIN INITIAL; %} "boolean" { incrementNumChar(); return processToken(LEXICAL_BOOLEAN_TYPE);} "integer" { incrementNumChar(); return processToken(LEXICAL_INTEGER_TYPE);} "=" { incrementNumChar(); return processToken(LEXICAL_AFFECTATION); } "&&" { incrementNumChar(); return processToken(LEXICAL_AND); } "||" { incrementNumChar(); return processToken(LEXICAL_OR); } "&" { incrementNumChar(); return processToken(LEXICAL_ANDLAZY); } "|" { incrementNumChar(); return processToken(LEXICAL_ORLAZY); } "!" { incrementNumChar(); return processToken(LEXICAL_NOT); } "==" { incrementNumChar(); return processToken(LEXICAL_EQUALS); } "<=" { incrementNumChar(); return processToken(LEXICAL_LESS_EQUALS); } "<" { incrementNumChar(); return processToken(LEXICAL_LESS); } "+" { incrementNumChar(); return processToken(LEXICAL_PLUS); } "-" { incrementNumChar(); return processToken(LEXICAL_MINUS); } "*" { incrementNumChar(); return processToken(LEXICAL_MULT); } "div" { incrementNumChar(); return processToken(LEXICAL_DIV); } "mod" { incrementNumChar(); return processToken(LEXICAL_MOD); } "(" { incrementNumChar(); return processToken(L_PARENTHESIS); } "{" { incrementNumChar(); return processToken(LSQUI_BRACKET); } "." { incrementNumChar(); return processToken(POINT); } ")" { incrementNumChar(); return processToken(R_PARENTHESIS); } "}" { incrementNumChar(); return processToken(RSQUI_BRACKET); } "[" { incrementNumChar(); return processToken(LSQUA_BRACKET); } "]" { incrementNumChar(); return processToken(RSQUA_BRACKET); } "," { incrementNumChar(); return processToken(COMMA); } ":" { incrementNumChar(); return processToken(COLON); } ";" { incrementNumChar(); return processToken(SEMICOLON); } "true" { incrementNumChar(); return processToken(LEXICAL_TRUE_VAL); } "false" { return processToken(LEXICAL_FALSE_VAL); } "void" { incrementNumChar(); return processToken(LEXICAL_VOID_TYPE); } "var" { incrementNumChar(); return processToken(LEXICAL_VAR); } "get" { return processToken(LEXICAL_GET_OPS); } "isempty" { incrementNumChar(); return processToken(LEXICAL_ISEMPTY_OPS); } "write" { incrementNumChar(); return processToken(LEXICAL_WRITE_OPS); } "read" { incrementNumChar(); return processToken(LEXICAL_READ_OPS); } "put" { incrementNumChar(); return processToken(LEXICAL_PUT_OPS); } "return" { incrementNumChar(); return processToken(LEXICAL_RETURN_STMT); } "for" { incrementNumChar(); return processToken(LEXICAL_FOR_STMT); } "if" { incrementNumChar(); return processToken(LEXICAL_IF_STMT); } "else" { incrementNumChar(); return processToken(LEXICAL_ELSE_STMT); } "while" { incrementNumChar(); return processToken(LEXICAL_WHILE_STMT); } "intstack" { incrementNumChar(); return processToken(LEXICAL_INSTACK_TYPE); } {blank} { incrementNumChar(); } "\n" { addLineRead(); } {nbr} { yylval.text=yytext; incrementNumChar(); return processToken(NUMBER); } {id} { incrementNumChar(); return processToken(ID); } "//" { BEGIN(COMMENT_LINE); incrementNumChar(); } "/*" { BEGIN(COMMENT_BLOC); incrementNumChar(); } <COMMENT_BLOC>"*/" { BEGIN(INITIAL); incrementNumChar(); } <COMMENT_BLOC>"\n" { addLineRead(); } <COMMENT_BLOC>. { incrementNumChar();/*Ignore comment*/} <COMMENT_LINE>"\n" { BEGIN(INITIAL); addLineRead(); } <COMMENT_LINE>. { incrementNumChar();/*Ignore comment*/} . { char errorStr[1024];//todo: minimize length sprintf(errorStr, "Error : invalid '%s' On parsed file, Line %d col %d\n",yytext, yylloc.first_line, yylloc.first_column); yyerror(errorStr); } %% int processToken(int tokenType) { yylloc.first_line=yylloc.last_line=yylineno; yylloc.first_column=yylloc.last_column=yyTempColumn+yyleng-1; yyTempColumn+=yyleng; #if(VERBOSE_LEVEL<=DEB_L) ";%15s %15s detected(%d) line %d char %d\n", typeToString(tokenType), yytext, tokenType, (yylloc).first_line, (yylloc).first_column ); #endif return tokenType; } void incrementNumChar() { //// if(lexLinesCount<1) //// { //// addLineRead(); //// } //// #if(VERBOSE_LEVEL<=DEB_L) //// if(YY_START==INITIAL) //// { //// ECHO; //// } //// #endif // lexCharsCountBeforeToken+=yyleng; //// lexCharsCountBeforeToken=lexCharsTempPsn-yyleng; //// lexCharsTempPsn+=yyleng; lexTotalCharsCount+=yyleng; //printf("%s => line %d, bt=%d,tmp=%d, t=%d\n", yytext, lexLinesCount, lexCharsCountBeforeToken,lexCharsTempPsn,lexTotalCharsCount); } void addLineRead() { // #if(VERBOSE_LEVEL<=DEB_L) // printf("\n;\t"); // #endif // ++lexLinesCount; // lexLinesCountBeforeToken = yylineno; // lexCharsCountBeforeToken = 0; yyTempColumn=1; // if(psnBeforeToken!=NULL) // { ++lexLinesCount; // lexLinesCountBeforeToken = yylineno; // lexCharsCountBeforeToken = 0; // //lexCharsTempPsn = 0; // //psnBeforeToken->line = ; // psnAfterToken->line++; //// printf( //// ";\t%s detected line %d(%d, %d) char %d\n", //// yytext, //// lexLinesCount, //// yylineno, //// psnAfterToken->line, //// lexTotalCharsCount-yyleng//lexCharsCountBeforeToken //// ); // } } //int main() //{ // #if(VERBOSE_LEVEL<=DEB_L) // print("\n;\t"); // #endif // yylex(); // return 0; //}
Autres extraits de codes en Lex
- lsd10.l Fichier Lex du langage LSD010 Compilateur LSD010
- Tous les extraits
Nederlandse vertaling
U hebt gevraagd om deze site in het Nederlands te bezoeken. Voor nu wordt alleen de interface vertaald, maar nog niet alle inhoud.Als je me wilt helpen met vertalingen, is je bijdrage welkom. Het enige dat u hoeft te doen, is u op de site registreren en mij een bericht sturen waarin u wordt gevraagd om u toe te voegen aan de groep vertalers, zodat u de gewenste pagina's kunt vertalen. Een link onderaan elke vertaalde pagina geeft aan dat u de vertaler bent en heeft een link naar uw profiel.
Bij voorbaat dank.
Document heeft de 05/10/2009 gemaakt, de laatste keer de 28/10/2018 gewijzigd
Bron van het afgedrukte document:https://www.gaudry.be/nl/sniplet-rf-lsd010/project/source/lsd10.l.html
De infobrol is een persoonlijke site waarvan de inhoud uitsluitend mijn verantwoordelijkheid is. De tekst is beschikbaar onder CreativeCommons-licentie (BY-NC-SA). Meer info op de gebruiksvoorwaarden en de auteur.