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
English translation
You have asked to visit this site in English. For now, only the interface is translated, but not all the content yet.If you want to help me in translations, your contribution is welcome. All you need to do is register on the site, and send me a message asking me to add you to the group of translators, which will give you the opportunity to translate the pages you want. A link at the bottom of each translated page indicates that you are the translator, and has a link to your profile.
Thank you in advance.
Document created the 05/10/2009, last modified the 28/10/2018
Source of the printed document:https://www.gaudry.be/en/sniplet-rf-lsd010/project/source/lsd10.l.html
The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.