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 7082855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:06:23+00:00 2026-05-28T07:06:23+00:00

I am trying to learn Lex. I have a simple program where i want

  • 0

I am trying to learn Lex. I have a simple program where i want to read in a file and recognize tokens.

Right now i am getting some errors. I think i am having problems because there is more than one line in the file to recognize the tokens?

Here is the file

fd 3x00
bk
setc 100
int xy3 fd 10 rt 90

here is the output i am trying to achieve:

Keyword: fd
Illegal: 3x00
Keyword: bk
Keyword: setc
Number: 100
Keyword: int

here is my program:

%{

/* Comment  */

 #include <stdio.h>
 #include <stdlib.h>

%}
%%
fd                     {printf("Keyword: fd\n");}
[0-9][a-z][0-9]        {printf("Illegal: 3x00\n");}
bk                     {printf("Keyword: bk\n");}
setc[0-9]              {printf("Keyword: setc\n Number: %s\n", yytext);}
int                    {printf("Keyword: int\n");}
xy3                    {printf("ID: xy3\n");}
fd[0-9]                {printf("Keyword: fd\n Number %s\n", yytext);}
rt[0-9]                {printf("Keyword: rt \n Number %s\n", yytext);}
%%

main( argc, argv)
int argc;
char** argv;
{
    if(argc > 1)
    {

        FILE *file;
        file = fopen(argv[1], "r");
        if(!file)
        {
           fprintf(stderr, "Could not open %s \n", argv[1]);
           exit(1);
        }

        yyin = file;

    }

    yylex();

}

here are the errors i am getting when i try to compile it:

 In function 'yylex':
miniStarLogo.l:11: error: expected expression before '[' token
miniStarLogo.l:11: error: 'a' undeclared (first use in this function)
miniStarLogo.l:11: error: (Each undeclared identifier is reported only once
miniStarLogo.l:11: error: for each function it appears in.)
miniStarLogo.l:11: error: expected ';' before '{' token
miniStarLogo.l:13: error: expected expression before '[' token
miniStarLogo.l:13: error: expected ';' before '{' token

is the error in my printf statements?
Thank you

  • 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-05-28T07:06:24+00:00Added an answer on May 28, 2026 at 7:06 am

    When I compiled a copy of your code on MacOS X (10.7.2) with flex (2.5.35) and gcc (4.6.1), the only complaints I got from the C compiler were about the non-prototype definition of main(), and that was because I always compile with that warning enabled and mention of yyunput() defined but not used (which is not your fault).

    Since you’re learning C, you should only be using the notation:

    int main(int argc, char **argv)
    {
        ...
    }
    

    or an equivalent.

    I also converted the miniStarLogo.l file to DOS format (CRLF line endings), and both flex and gcc seemed to be OK with the results – somewhat to my surprise. It might not be the case on your machine.

    When I ran the code on your test data, I got:

    Keyword: fd
     Illegal: 3x00
    0
    Keyword: bk
    
    setc 100
    Keyword: int
     ID: xy3
     Keyword: fd
     10 rt 90
    

    So, you are not far off where you need to be by my reckoning.


    Confusion reigneth over my commands.

    I used (hmmm, it was GCC 4.2.1 rather than 4.6.1), but:

    $ flex miniStarLogo.l
    $ gcc -Wall -Wextra -O3 -g -o lex.yy lex.yy.c -lfl
    miniStarLogo.l:22: warning: return type defaults to ‘int’
    miniStarLogo.l: In function ‘main’:
    miniStarLogo.l:42: warning: control reaches end of non-void function
    miniStarLogo.l: At top level:
    lex.yy.c:1114: warning: ‘yyunput’ defined but not used
    $ ./lex.yy <<EOF
    > fd 3x00
    > bk
    > setc 100
    > int xy3 fd 10 rt 90
    > EOF
    Keyword: fd
     Illegal: 3x00
    0
    Keyword: bk
    
    setc 100
    Keyword: int
     ID: xy3
     Keyword: fd
     10 rt 90
    $
    

    (OK – I cheated marginally: the first time around, I ran rmk lex.yy LDLIBS=-lfl, where rmk is a variant of make and the compilation rules in the directory use the command line shown. But I redid the compilations to get the error messages right, exactly as above.)

    You might need to look at expanding your patterns to accept ‘one or more’ digits with [0-9]+ in place of just [0-9]. You might need to look at a rule dealing with unmatched characters. And personally, I go to great pains to avoid blanks immediately before newlines, so you would need to tighten up your print formatting to meet my criteria. However, that’s not germane to getting the program running.

    Also, if you need to convert your file from DOS to Unix line endings, the easiest is the dos2unix command, if you have it on your machine. Otherwise, use:

    $ tr -d '\015' < miniStarLogo.l > x
    $ od -c x
    0000000   %   {  \r  \n  \r  \n   /   *       C   o   m   m   e   n   t
    ...
    0001560  \n   }  \r  \n
    0001564
    $ mv x miniStarLogo.l
    $
    

    I carefully added the carriage returns using vim and :set fileformat=dos; it would also be possible to undo it with vim and :set fileformat=unix. This is Unix so TMTOWTDI (There’s More Than One Way To Do It — the Perl motto), and I’m not even trying to use Perl.

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

Sidebar

Related Questions

Trying to learn Django, I closed the shell and am getting this problem now
Trying to learn a bit of CSS and I want a horizontal navbar and
Trying to learn about php's arrays today. I have a set of arrays like
In trying to learn how to create objects in ActionScript, I have had no
I was trying learn lex and yacc using the oreilly book. I tried following
Trying to learn some F# and I've run into a couple of hangups. Here's
trying to learn some html/css and I'm having a problem with fixed position divs.
I have been trying to learn how to write faster more efficient jQuery and
I'm trying to learn some basics of Ruby on Rails and encountered a problem
I'm trying to learn RoR and I keep getting pesky page doesn't exist pages.

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.