OiO.lk Blog C# Priority Issues – Use LEX to write regular expressions to match strings of specific patterns
C#

Priority Issues – Use LEX to write regular expressions to match strings of specific patterns


I try to write regular definitions to display the line of string for the following
using LEX.

a.Match any string starting with d, and ending with t
b. Matches the string def
c.Match one or more occurrences ab concatenated
d. Match any string of one or more characters that do not include upper
case A-Z

I don’t know why the program got an unexpected result. And the program couldn’t stop.

Below is my C code:

%{
    #include <stdio.h>
%}

%%

def {
    printf("Matched 'def' string: %s\n", yytext);
}

d.*t {
    printf("Matched 'd...t' string: %s\n", yytext);
}

(ab)+ {
    printf("Matched 'ab' concatenated: %s\n", yytext);
}

[^A-Z]+ {
    printf("Matched string (no uppercase A-Z): %s\n", yytext);
}

%%

int main(int argc, char* argv[]){
    printf("\nEnter the string:\n");
    yylex();  // Start lexical analysis
    return 0;
}

int yywrap(){
    return 1;
}

The below picture is my output. I don’t know why the program got an unexpected result. And the program couldn’t stop.

PS C:\CODE\CST302-Compiler-Principles\Lab3\Q2> flex Q2.l
PS C:\CODE\CST302-Compiler-Principles\Lab3\Q2> g++ lex.yy.c
PS C:\CODE\CST302-Compiler-Principles\Lab3\Q2> ./a

Enter the string:
dooot def abc abab ABCd
Matched string (no uppercase A-Z): dooot def abc abab
ABC

abc



You need to sign in to view this answers

Exit mobile version