Geen cache-versie.

Caching uitgeschakeld. Standaardinstelling voor deze pagina:ingeschakeld (code LNG204)
Als het scherm te langzaam is, kunt u de gebruikersmodus uitschakelen om de cacheversie te bekijken.

lsd10.l

Description du code

Fichier Lex du langage LSD010 Compilateur LSD010

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. //}

Autres extraits de codes en Lex

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.