[c] matlex
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- 1.Problem Statement : Lex programs to recognize Keywords.
- Description : token can look like anything that is useful for processing an input text stream or
- text file.
- Source Code:
- %{
- #include<stdio.h>
- %}
- %%
- int|float|char { printf(" data type: %s",yytext); }
- %%
- int main()
- {
- yylex();
- return(0);
- }
- int yywrap()
- {
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- 2.Problem Statement : Lex programs to recognize String ending with 00.
- Description : token can look like anything that is useful for processing an input text stream or
- text file.
- Source Code:
- %{
- #include<stdio.h>
- %}
- %%
- [a-z A-Z 0-9]++00 { printf("string is acepted",yytext);}
- .* { printf("not acepted",yytext);}
- %%
- int main()
- {
- yylex();
- return(0);
- }
- int yywrap()
- {
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- 3.Problem Statement : Program to recognize the strings which are starting or ending with ‘k’.
- Description : token can look like anything that is useful for processing an input text stream or
- text file.
- Source Code:
- %{
- #include<stdio.h>
- %}
- begin-with-k k.*
- end-with-k .*k
- %%
- {begin-with-k} { printf(" %s is a word that begin with k",yytext);}
- {end-with-k} { printf(" %s is a word that end with k",yytext);}
- %%
- int main()
- {
- yylex();
- return(0);
- }
- int yywrap()
- {
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- 4.Problem Statement : program to assign line numbers for source code.
- Description : token can look like anything that is useful for processing an input text stream or
- text file.
- Source Code:
- %{
- #include<stdio.h>
- int lineno=0;
- %}
- line .*\n
- %%
- {line} { printf("%d .%s",lineno++,yytext); }
- %%
- int main(int argc,char **argv)
- {
- if(argc>1)
- {
- FILE *file;
- file=fopen(argv[1],"r");
- if(!file)
- {
- printf("could not open %s\n",argv[1]);
- exit(0);
- }
- yyin=file;
- }
- yylex();
- printf("\n\n");
- return 0;
- }
- int yywrap()
- {
- return 0;
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- 5.Problem Statement : Program to recognize the numbers which has 1 in its 5th position from
- right.
- Description : token can look like anything that is useful for processing an input text stream or
- text file.
- Source Code:
- %{
- #include<stdio.h>
- %}
- %%
- [0-9]*1[0-9]{4} { printf("acepted:");}
- [0-9]*1[0-9] { printf("NOT acepted:");}
- %%
- int main()
- {
- yylex();
- return(0);
- }
- int yywrap()
- {
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- 6.Lex Specification to Replace Space with $
- %{
- #include <stdio.h>
- %}
- %%
- [ ] { printf("$"); } // Replaces each space with a dollar sign
- %%
- int main()
- {
- yylex();
- return 0;
- }
- int yywrap() {
- return 1;
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- 7.Lex Specification to Identify the Number Divisible by 9
- %{
- #include <stdio.h>
- %}
- %%
- [0-9]+ { int num = atoi(yytext); if (num % 9 == 0) printf("%s is divisible by 9\n", yytext); }
- %%
- int main()
- {
- yylex();
- return 0;
- }
- int yywrap() {
- return 1;
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- 8. Lex Specification to Identify Identifier and Functional Identifier
- %{
- #include <stdio.h>
- %}
- %%
- [a-zA-Z_][a-zA-Z0-9_]* { printf("Identifier: %s\n", yytext); }
- [a-zA-Z_][a-zA-Z0-9_]*\( { printf("Functional Identifier: %s\n", yytext); }
- %%
- int main()
- {
- yylex();
- return 0;
- }
- int yywrap() {
- return 1;
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- 9.Lex Specification to Identify Comments
- %{
- #include <stdio.h>
- %}
- %%
- "//"[^"\n"]* { printf("Comment: %s\n", yytext); } // Matches single line comments
- /\*[^*]*\*+([^/*][^*]*\*+)*/ { printf("Comment: %s\n", yytext); } // Matches multiline comments
- %%
- int main()
- {
- yylex();
- return 0;
- }
- int yywrap() {
- return 1;
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- 10.Lex Specification to Identify Real Numbers
- %{
- #include <stdio.h>
- %}
- %%
- [0-9]+\.[0-9]+([eE][-+]?[0-9]+)? { printf("Real Number: %s\n", yytext); } // Matches floating point numbers with optional exponent
- %%
- int main()
- {
- yylex();
- return 0;
- }
- int yywrap() {
- return 1;
- }
- ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Editor
You can edit this paste and save as new: