Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9225755
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:39:37+00:00 2026-06-18T04:39:37+00:00

I am trying to write lex code which will take an input and then

  • 0

I am trying to write lex code which will take an input and then find and print the first permutation of that input that it finds in a large dictionary text file. This is what I have so far:

%{
#include <stdio.h>
%}
%option noyywrap
%% 
INPUT GOES HERE { //Not sure what expression to put here 
    printf("Longest is: %s", yytext);
    return;
}

.|\n    {      }

%%

int main(void)
{
        yylex();
        return 0;
}

I have a feeling I’ll have to use states, but I’m not too familiar with how those work. Can someone point me in the right direction?

EDIT: Here is the code for the accepted answer in case anyone wants it:

%{
#include <stdio.h>
#include <string.h>
%}
%option noyywrap
%% 
^[ablm]{4}$ { 
    char originalWord [5];
    strcpy(originalWord, yytext);
    char input[5] = {"ablm"};
    char tmp;
    int i, j;
    for(i=0; i<4; i++)
    {
        for (j=i+1; j<4; j++)
        {
            if (yytext[i] > yytext[j])
            {
                tmp=yytext[i];
                yytext[i]=yytext[j];    
                yytext[j]=tmp;
            }
        }
    }
    if(strcmp(input,yytext)==0){
        printf("First permutation is: %s", originalWord);
        return;     
    }
    else
        ;

}

.|\n    {      }

%%

int main(void)
{
        yylex();
        return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T04:39:38+00:00Added an answer on June 18, 2026 at 4:39 am

    Regular expressions do not tend to natively support string matching for strings of the form “some permutation of the following symbols.” You can write regular expressions that match permutations of some string, but to do so you would (more or less) have to write out all permutations of those characters, then OR them all together.

    An easier way to do this would be to have a regular expression that matches all strings that are of the appropriate length and which are made of symbols taken from the string in question. You could then associate an action with this regular expression which would take in candidate strings, then use normal C code to determine whether or not the string was a permutation of the original set of characters. This should be extremely fast, since the number of false positives will likely be very low in a real dictionary and the amount of time spent processing a candidate match is not very great.

    Hope this helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write generic code that can lex any stream of characters (
I am trying to write a lex pattern that will allow me to recognize
Trying to write a code that searches hash values for specific string's (input by
I'm currently trying write code that will maintain the sorting preference while changing page
I m trying write code that after reset set up rrpmax as 3000. It
Trying to write app for service technicians that will display open service calls within
Trying to write a couple of functions that will encrypt or decrypt a file
Trying to write a code at the moment that basically tests to see if
I'm trying write a query to find records which don't have a matching record
Trying to write a function that will write my structure array to a binary

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.