lsd10.l

Description du code

lsd10.l est un fichier du projet Compilateur LSD010.
Ce fichier est situé dans /var/www/bin/sniplets/lsd010/.

Projet Compilateur LSD010 :

Compilateur LSD010 développé dans le cadre du cours de syntaxe et sémantiqueref 1

Code source ou contenu du fichier

  1. %{
  2. /*
  3.  * lsd10.l : lexical parsing file for Lex
  4.  * @see http://www.gaudry.be/langages-flex.html
  5.  * Part of the compiler Project for LSD10 language
  6.  */
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10.  
  11. #include "y.tab.h"
  12. /*
  13. #include "ast.h"
  14. */
  15.  
  16. #include "common.h"
  17. /**
  18.  * Count of lines read
  19.  */
  20. int lexLinesCount=0;
  21. /**
  22.  * Count of chars read
  23.  */
  24. int lexTotalCharsCount=0;
  25. extern AstNode *rootNode;
  26.  
  27. /*
  28.  * internal business
  29. *************************************************************
  30. */
  31. extern DebugInfo *psnBeforeToken;
  32. extern DebugInfo *psnAfterToken;
  33. int processToken(int tokenType);
  34. void incrementNumChar();
  35. void addLineRead();
  36.  
  37. /*
  38.  * Lex overriding
  39. *************************************************************
  40. */
  41. int yyTempColumn=1;
  42. //define YY_USER_ACTION yylloc.first_line=yylloc.last_line=yylineno;yylloc.first_column=yylloc.last_column=yyTempColumn+yyleng-1;yyTempColumn+=yyleng;
  43. %}
  44.  
  45. %option noyywrap
  46. %option yylineno
  47. %x COMMENT_LINE COMMENT_BLOC
  48.  
  49. /*nbr 0|[-][1-9][0-9]*|[1-9][0-9]* */
  50. nbr 0|[1-9][0-9]*
  51. id [a-zA-Z]|([a-zA-Z]([a-zA-Z]|[0-9])*)
  52. blank [ \t]+
  53.  
  54. %%
  55. %{
  56. //we specify that we begin with the rules of the state INITIAL
  57. BEGIN INITIAL;
  58. %}
  59. "boolean" {
  60. incrementNumChar();
  61. return processToken(LEXICAL_BOOLEAN_TYPE);}
  62. "integer" {
  63. incrementNumChar();
  64. return processToken(LEXICAL_INTEGER_TYPE);}
  65.  
  66. "=" {
  67. incrementNumChar();
  68. return processToken(LEXICAL_AFFECTATION); }
  69.  
  70. "&&" {
  71. incrementNumChar();
  72. return processToken(LEXICAL_AND); }
  73. "||" {
  74. incrementNumChar();
  75. return processToken(LEXICAL_OR); }
  76. "&" {
  77. incrementNumChar();
  78. return processToken(LEXICAL_ANDLAZY); }
  79. "|" {
  80. incrementNumChar();
  81. return processToken(LEXICAL_ORLAZY); }
  82. "!" {
  83. incrementNumChar();
  84. return processToken(LEXICAL_NOT); }
  85. "==" {
  86. incrementNumChar();
  87. return processToken(LEXICAL_EQUALS); }
  88. "<=" {
  89. incrementNumChar();
  90. return processToken(LEXICAL_LESS_EQUALS); }
  91. "<" {
  92. incrementNumChar();
  93. return processToken(LEXICAL_LESS); }
  94.  
  95. "+" {
  96. incrementNumChar();
  97. return processToken(LEXICAL_PLUS); }
  98. "-" {
  99. incrementNumChar();
  100. return processToken(LEXICAL_MINUS); }
  101. "*" {
  102. incrementNumChar();
  103. return processToken(LEXICAL_MULT); }
  104. "div" {
  105. incrementNumChar();
  106. return processToken(LEXICAL_DIV); }
  107. "mod" {
  108. incrementNumChar();
  109. return processToken(LEXICAL_MOD); }
  110.  
  111. "(" {
  112. incrementNumChar();
  113. return processToken(L_PARENTHESIS); }
  114. "{" {
  115. incrementNumChar();
  116. return processToken(LSQUI_BRACKET); }
  117. "." {
  118. incrementNumChar();
  119. return processToken(POINT); }
  120. ")" {
  121. incrementNumChar();
  122. return processToken(R_PARENTHESIS); }
  123. "}" {
  124. incrementNumChar();
  125. return processToken(RSQUI_BRACKET); }
  126. "[" {
  127. incrementNumChar();
  128. return processToken(LSQUA_BRACKET); }
  129. "]" {
  130. incrementNumChar();
  131. return processToken(RSQUA_BRACKET); }
  132. "," {
  133. incrementNumChar();
  134. return processToken(COMMA); }
  135. ":" {
  136. incrementNumChar();
  137. return processToken(COLON); }
  138. ";" {
  139. incrementNumChar();
  140. return processToken(SEMICOLON); }
  141.  
  142. "true" {
  143. incrementNumChar();
  144. return processToken(LEXICAL_TRUE_VAL); }
  145. "false" {
  146. return processToken(LEXICAL_FALSE_VAL); }
  147. "void" {
  148. incrementNumChar();
  149. return processToken(LEXICAL_VOID_TYPE); }
  150.  
  151. "var" {
  152. incrementNumChar();
  153. return processToken(LEXICAL_VAR); }
  154. "get" {
  155. return processToken(LEXICAL_GET_OPS); }
  156. "isempty" {
  157. incrementNumChar();
  158. return processToken(LEXICAL_ISEMPTY_OPS); }
  159. "write" {
  160. incrementNumChar();
  161. return processToken(LEXICAL_WRITE_OPS); }
  162. "read" {
  163. incrementNumChar();
  164. return processToken(LEXICAL_READ_OPS); }
  165. "put" {
  166. incrementNumChar();
  167. return processToken(LEXICAL_PUT_OPS); }
  168. "return" {
  169. incrementNumChar();
  170. return processToken(LEXICAL_RETURN_STMT); }
  171.  
  172. "for" {
  173. incrementNumChar();
  174. return processToken(LEXICAL_FOR_STMT); }
  175. "if" {
  176. incrementNumChar();
  177. return processToken(LEXICAL_IF_STMT); }
  178. "else" {
  179. incrementNumChar();
  180. return processToken(LEXICAL_ELSE_STMT); }
  181. "while" {
  182. incrementNumChar();
  183. return processToken(LEXICAL_WHILE_STMT); }
  184. "intstack" {
  185. incrementNumChar();
  186. return processToken(LEXICAL_INSTACK_TYPE); }
  187.  
  188. {blank} {
  189. incrementNumChar();
  190. }
  191. "\n" {
  192. addLineRead();
  193. }
  194.  
  195. {nbr} {
  196. yylval.text=yytext;
  197. yylval.nval=atoi(yytext);
  198. incrementNumChar();
  199. return processToken(NUMBER);
  200. }
  201. {id} {
  202. incrementNumChar();
  203.  
  204. yylval.text=(char*)calloc(strlen(yytext)+1,sizeof(char));
  205. strcpy(yylval.text,yytext);
  206.  
  207. return processToken(ID);
  208. }
  209.  
  210. "//" {
  211. BEGIN(COMMENT_LINE);
  212. incrementNumChar();
  213. }
  214. "/*" {
  215. BEGIN(COMMENT_BLOC);
  216. incrementNumChar();
  217. }
  218.  
  219. <COMMENT_BLOC>;"*/" {
  220. BEGIN(INITIAL);
  221. incrementNumChar();
  222. }
  223.  
  224. <COMMENT_BLOC>;"\n" {
  225. addLineRead();
  226. }
  227. <COMMENT_BLOC>;. {
  228. incrementNumChar();/*Ignore comment*/}
  229. <COMMENT_LINE>;"\n" {
  230. BEGIN(INITIAL);
  231. addLineRead();
  232. }
  233. <COMMENT_LINE>;. {
  234. incrementNumChar();/*Ignore comment*/}
  235.  
  236. . {
  237. char errorStr[1024];//todo: minimize length
  238. sprintf(errorStr, "Error : invalid '%s' On parsed file, Line %d col %d\n",yytext, yylloc.first_line, yylloc.first_column);
  239. yyerror(errorStr);
  240. }
  241.  
  242. %%
  243. int processToken(int tokenType)
  244. {
  245. yylloc.first_line=yylloc.last_line=yylineno;
  246. yylloc.first_column=yylloc.last_column=yyTempColumn+yyleng-1;
  247. yyTempColumn+=yyleng;
  248. #if(VERBOSE_LEVEL<=DEB_L)
  249. ";%15s %15s detected(%d) line %d char %d\n",
  250. typeToString(tokenType),
  251. yytext,
  252. tokenType,
  253. (yylloc).first_line,
  254. (yylloc).first_column
  255. );
  256. #endif
  257. return tokenType;
  258. }
  259. void incrementNumChar()
  260. {
  261. //// if(lexLinesCount<1)
  262. //// {
  263. //// addLineRead();
  264. //// }
  265. //// #if(VERBOSE_LEVEL<=DEB_L)
  266. //// if(YY_START==INITIAL)
  267. //// {
  268. //// ECHO;
  269. //// }
  270. //// #endif
  271. // lexCharsCountBeforeToken+=yyleng;
  272. //// lexCharsCountBeforeToken=lexCharsTempPsn-yyleng;
  273. //// lexCharsTempPsn+=yyleng;
  274. lexTotalCharsCount+=yyleng;
  275. //printf("%s => line %d, bt=%d,tmp=%d, t=%d\n", yytext, lexLinesCount, lexCharsCountBeforeToken,lexCharsTempPsn,lexTotalCharsCount);
  276. }
  277. void addLineRead()
  278. {
  279. // #if(VERBOSE_LEVEL<=DEB_L)
  280. // printf("\n;\t");
  281. // #endif
  282. // ++lexLinesCount;
  283. // lexLinesCountBeforeToken = yylineno;
  284. // lexCharsCountBeforeToken = 0;
  285. yyTempColumn=1;
  286. // if(psnBeforeToken!=NULL)
  287. // {
  288. ++lexLinesCount;
  289. // lexLinesCountBeforeToken = yylineno;
  290. // lexCharsCountBeforeToken = 0;
  291. // //lexCharsTempPsn = 0;
  292. // //psnBeforeToken->line = ;
  293. // psnAfterToken->line++;
  294. //// printf(
  295. //// ";\t%s detected line %d(%d, %d) char %d\n",
  296. //// yytext,
  297. //// lexLinesCount,
  298. //// yylineno,
  299. //// psnAfterToken->line,
  300. //// lexTotalCharsCount-yyleng//lexCharsCountBeforeToken
  301. //// );
  302. // }
  303. }
  304. //int main()
  305. //{
  306. // #if(VERBOSE_LEVEL<=DEB_L)
  307. // print("\n;\t");
  308. // #endif
  309. // yylex();
  310. // return 0;
  311. //}

Structure et Fichiers du projet

Afficher/masquer...


Répertoires contenus dans /var/www/bin/sniplets/lsd010/project/source/ 
IcôneNomTailleModification
Pas de sous-répertoires.
IcôneNomTailleModification
| _ Répertoire parent0 octets1714127272 26/04/2024 12:27:52
Fichiers contenus dans /var/www/bin/sniplets/lsd010/project/source/ 
IcôneNomTailleModificationAction
IcôneNomTailleModificationAction
Afficher le fichier .o|.ographVizHelper.o4.72 Ko31/10/2018 18:32:37-refusé-
Afficher le fichier .c|.cast.c27.73 Ko31/10/2018 18:32:37-refusé-
Afficher le fichier .h|.hconst.h4.01 Ko31/10/2018 18:32:37-refusé-
Afficher le fichier .c|.cy.tab.c84.53 Ko31/10/2018 18:32:39-refusé-
Afficher le fichier .y|.ylsd10.y22.88 Ko31/10/2018 18:32:38-refusé-
Afficher le fichier .o|.olex.yy.o18.88 Ko31/10/2018 18:32:38-refusé-
Afficher le fichier .c|.cscopeStack.c3.69 Ko31/10/2018 18:32:38-refusé-
Afficher le fichier .h|.hgraphVizHelper.h573 octets31/10/2018 18:32:37-refusé-
Afficher le fichier .h|.hsymbolsTableDataRepresentation.h1.31 Ko31/10/2018 18:32:38-refusé-
Afficher le fichier .o|.osymbolsTable.o5.2 Ko31/10/2018 18:32:38-refusé-
Afficher le fichier .h|.hcommon.h1.08 Ko31/10/2018 18:32:37-refusé-
Afficher le fichier .c|.cscopeHelper.c4.09 Ko31/10/2018 18:32:38-refusé-
Afficher le fichier .o|.ohashCode.o1.45 Ko31/10/2018 18:32:37-refusé-
Afficher le fichier .h|.hconsole.h2.21 Ko31/10/2018 18:32:37-refusé-
Afficher le fichier .o|.oconsole.o12.23 Ko31/10/2018 18:32:37-refusé-
Afficher le fichier .|lsd10lsd1075.26 Ko31/10/2018 18:32:38-refusé-
Afficher le fichier .h|.hhashCode.h1020 octets31/10/2018 18:32:37-refusé-
Afficher le fichier .h|.hsymbolsTable.h2.65 Ko31/10/2018 18:32:38-refusé-
Afficher le fichier .h|.hpcode.h417 octets31/10/2018 18:32:38-refusé-
Afficher le fichier .o|.oast.o11.45 Ko31/10/2018 18:32:37-refusé-
Afficher le fichier .h|.hscopeStack.h2.2 Ko31/10/2018 18:32:38-refusé-
Afficher le fichier .c|.cgraphVizHelper.c6.11 Ko31/10/2018 18:32:37-refusé-
Afficher le fichier .l|.llsd10.l6.46 Ko31/10/2018 18:32:38-refusé-
Afficher le fichier .h|.hy.tab.h4.69 Ko31/10/2018 18:32:39-refusé-
Afficher le fichier .output|.outputy.output81.69 Ko31/10/2018 18:32:39-refusé-
Afficher le fichier .o|.oy.tab.o24.45 Ko31/10/2018 18:32:39-refusé-
Afficher le fichier .c|.clex.yy.c57.93 Ko31/10/2018 18:32:37-refusé-
Afficher le fichier .h|.hast.h2.39 Ko31/10/2018 18:32:37-refusé-
Afficher le fichier .o|.oscopeStack.o1.34 Ko31/10/2018 18:32:38-refusé-
Afficher le fichier .o|.oscopeHelper.o2.59 Ko31/10/2018 18:32:38-refusé-
Afficher le fichier .h|.hastDataRepresentation.h1.87 Ko31/10/2018 18:32:37-refusé-
Afficher le fichier .c|.csymbolsTable.c14.91 Ko31/10/2018 18:32:38-refusé-
Afficher le fichier .c|.cpcode.c23.45 Ko31/10/2018 18:32:38-refusé-
Afficher le fichier .c|.chashCode.c3.94 Ko31/10/2018 18:32:37-refusé-
Afficher le fichier .c|.cconsole.c18.01 Ko31/10/2018 18:32:37-refusé-
Afficher le fichier .h|.hscopeHelper.h719 octets31/10/2018 18:32:38-refusé-

Avertissement

Ce code présente une manière possible d'implémenter un compilateur, et certains choix peuvent être discutés.
Cependant, il peut donner des pistes pour démarrer, ou approcher certains concepts, et je tenterais par la suite de mettre à jour le code.

Utilisation de l'explorateur de code

  • Navigation :
    • Un clic sur une icône de répertoire ouvre ce répertoire pour en afficher les fichiers.
    • Lorsque le répertoire en cours ne contient pas de sous-répertoires il est possible de remonter vers le répertoire parent.
    • La structure de répertoires en treetable (tableau en forme d'arborescence) n'est plus possibledans cette version.
    • Un clic sur une icône de fichier ouvre ce fichier pour en afficher le code avec la coloration syntaxique adaptée en fonction du langage principal utilisé dans le fichier.
  • Affichage :
    • Il est possible de trier les répertoires ou les fichiers selon certains critères (nom, taille, date).
  • Actions :
    • Les actions possible sur les fichiers dépendent de vos droits d'utilisateur sur le site. Veuillez activer le mode utilisateur pour activer les actions.

Document créé le 07/03/2010, dernière modification le 28/10/2018
Source du document imprimé : https://www.gaudry.be/langages-lsd10-source-rf-project/source/lsd10.l.html

L'infobrol est un site personnel dont le contenu n'engage que moi. Le texte est mis à disposition sous licence CreativeCommons(BY-NC-SA). Plus d'info sur les conditions d'utilisation et sur l'auteur.

Notes

  1. a,b LSD010 : Langage Simple et Didactique Il existe une un certain nombre d'interprétations de l'acronyme LSD (Langage Symbolique Didactique, Langage Sans Difficulté, Langage Simple et Didactique). LSD010 est la version 2010 de la suite LSD80, LSD_02, LSD03, LSD04, LSD05, LSD06, LSD07, LSD08, et LSD09.

Table des matières Haut

Références

  1. livre Langue du document :fr IHDCB332 - Théorie des langages : Syntaxe et sémantique : PY Schobbens, Syntaxe et sémantique (January 2010)

Ces références et liens indiquent des documents consultés lors de la rédaction de cette page, ou qui peuvent apporter un complément d'information, mais les auteurs de ces sources ne peuvent être tenus responsables du contenu de cette page.
L'auteur de ce site est seul responsable de la manière dont sont présentés ici les différents concepts, et des libertés qui sont prises avec les ouvrages de référence. N'oubliez pas que vous devez croiser les informations de sources multiples afin de diminuer les risques d'erreurs.

Table des matières Haut