[c] matlex

Viewer

  1. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2. 1.Problem Statement : Lex programs to recognize Keywords.
  3. Description : token can look like anything that is useful for processing an input text stream or
  4. text file.
  5. Source Code:
  6. %{
  7. #include<stdio.h>
  8. %}
  9. %%
  10. int|float|char { printf(" data type: %s",yytext); }
  11. %%
  12. int main()
  13. {
  14. yylex();
  15. return(0);
  16. }
  17. int yywrap()
  18. {
  19. }
  20. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  21. 2.Problem Statement : Lex programs to recognize String ending with 00.
  22. Description : token can look like anything that is useful for processing an input text stream or
  23. text file.
  24. Source Code:
  25. %{
  26. #include<stdio.h>
  27. %}
  28. %%
  29. [a-z A-0-9]++00 { printf("string is acepted",yytext);}
  30. .* { printf("not acepted",yytext);}
  31. %%
  32. int main()
  33. {
  34. yylex();
  35. return(0);
  36. }
  37. int yywrap()
  38. {
  39. }
  40. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  41. 3.Problem Statement : Program to recognize the strings which are starting or ending with ‘k’.
  42. Description : token can look like anything that is useful for processing an input text stream or
  43. text file.
  44. Source Code:
  45. %{
  46. #include<stdio.h>
  47. %}
  48. begin-with-k k.*
  49. end-with-k .*k
  50. %%
  51. {begin-with-k} { printf(" %s is a word that begin with k",yytext);}
  52. {end-with-k} { printf(" %s is a word that end with k",yytext);}
  53. %%
  54. int main()
  55. {
  56. yylex();
  57. return(0);
  58. }
  59. int yywrap()
  60. {
  61. }
  62. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  63. 4.Problem Statement : program to assign line numbers for source code.
  64. Description : token can look like anything that is useful for processing an input text stream or
  65. text file.
  66. Source Code:
  67. %{
  68. #include<stdio.h>
  69. int lineno=0;
  70. %}
  71. line .*\n
  72. %%
  73. {line} { printf("%d .%s",lineno++,yytext); }
  74. %%
  75. int main(int argc,char **argv)
  76. {
  77. if(argc>1)
  78. {
  79. FILE *file;
  80. file=fopen(argv[1],"r");
  81. if(!file)
  82. {
  83. printf("could not open %s\n",argv[1]);
  84. exit(0);
  85. }
  86. yyin=file;
  87. }
  88. yylex();
  89. printf("\n\n");
  90. return 0;
  91. }
  92. int yywrap()
  93. {
  94. return 0;
  95. }
  96. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  97. 5.Problem Statement : Program to recognize the numbers which has 1 in its 5th position from
  98. right.
  99. Description : token can look like anything that is useful for processing an input text stream or
  100. text file.
  101. Source Code:
  102. %{
  103. #include<stdio.h>
  104. %}
  105. %%
  106. [0-9]*1[0-9]{4} { printf("acepted:");}
  107. [0-9]*1[0-9] { printf("NOT acepted:");}
  108. %%
  109. int main()
  110. {
  111. yylex();
  112. return(0);
  113. }
  114. int yywrap()
  115. {
  116. }
  117.  
  118. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  119. 6.Lex Specification to Replace Space with $
  120.  
  121. %{
  122. #include <stdio.h>
  123. %}
  124. %%
  125. [ ]     { printf("$"); }  // Replaces each space with a dollar sign
  126. %%
  127. int main()
  128. {
  129.     yylex();
  130.     return 0;
  131. }
  132.  
  133. int yywrap() {
  134.     return 1;
  135. }
  136.  
  137.  
  138.  
  139. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  140. 7.Lex Specification to Identify the Number Divisible by 9
  141.  
  142. %{
  143. #include <stdio.h>
  144. %}
  145. %%
  146. [0-9]+  { int num = atoi(yytext); if (num % 9 == 0) printf("%s is divisible by 9\n", yytext); }
  147. %%
  148. int main()
  149. {
  150.     yylex();
  151.     return 0;
  152. }
  153.  
  154. int yywrap() {
  155.     return 1;
  156. }
  157.  
  158.  
  159. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  160.  
  161. 8. Lex Specification to Identify Identifier and Functional Identifier
  162.  
  163. %{
  164. #include <stdio.h>
  165. %}
  166. %%
  167. [a-zA-Z_][a-zA-Z0-9_]*        { printf("Identifier: %s\n", yytext); }
  168. [a-zA-Z_][a-zA-Z0-9_]*\(      { printf("Functional Identifier: %s\n", yytext); }
  169. %%
  170. int main()
  171. {
  172.     yylex();
  173.     return 0;
  174. }
  175.  
  176. int yywrap() {
  177.     return 1;
  178. }
  179.  
  180.                         
  181.                         
  182. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  183.  
  184. 9.Lex Specification to Identify Comments
  185.  
  186. %{
  187. #include <stdio.h>
  188. %}
  189. %%
  190. "//"[^"\n"]*   { printf("Comment: %s\n", yytext); }  // Matches single line comments
  191. /\*[^*]*\*+([^/*][^*]*\*+)*/ { printf("Comment: %s\n", yytext); } // Matches multiline comments
  192. %%
  193. int main()
  194. {
  195.     yylex();
  196.     return 0;
  197. }
  198.  
  199. int yywrap() {
  200.     return 1;
  201. }
  202.  
  203. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  204.  
  205. 10.Lex Specification to Identify Real Numbers
  206.  
  207. %{
  208. #include <stdio.h>
  209. %}
  210. %%
  211. [0-9]+\.[0-9]+([eE][-+]?[0-9]+)?  { printf("Real Number: %s\n", yytext); }  // Matches floating point numbers with optional exponent
  212. %%
  213. int main()
  214. {
  215.     yylex();
  216.     return 0;
  217. }
  218.  
  219. int yywrap() {
  220.     return 1;
  221. }
  222.  
  223. ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Editor

You can edit this paste and save as new:


File Description
  • matlex
  • Paste Code
  • 26 Apr-2024
  • 5.24 Kb
You can Share it: