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

  • Home
  • SEARCH
  • 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 9214231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T01:55:51+00:00 2026-06-18T01:55:51+00:00

I’m implementing a lex version of a simple scanner I’ve coded in standard C

  • 0

I’m implementing a lex version of a simple scanner I’ve coded in standard C. What I’m having trouble with is that I/O isn’t behaving the way I expect in the lex file. fscanf will not store strings and will store integers as 0 in the variables I indicate. fgetc returns characters that are not present in my test file. Is there anything (or something lacking) in my code below that jumps out as to why this is happening? Is how I’m using lex totally wrong?

scanner-lf.l:

%{
#include <stdio.h>
#include <stdlib.h>
extern int lineno;
extern int number;
extern char string[];
extern FILE *yyin;
#define INTEGER 1
#define FLOAT 2
#define READ 3
#define WRITE 4
#define ID 5
#define LPAREN 6
#define RPAREN 7
#define PLUS 8
#define MINUS 9
#define MULT 10
#define ASSIGN 11
#define DIV 12
#define COMMENT 13
#define ERROR 14
%}

%%
[ \t] {
    printf("whitespace\n");
}
[0-9]* {
    fscanf(yyin, "%d", &number);
    printf("%d\n", number);
    int c = fgetc(yyin);
    printf("%c", c);
    /*return INTEGER;*/
}
[a-zA-Z][a-zA-Z0-9]* {
    fscanf(yyin, "%s", string);
    printf("%s\n", string);
    /*return ID;*/
}

scanner-lex.c:

#include <stdio.h>
#include <stdlib.h>
/* A couple of globals */
int lineno = 0;
int number;
char string[100];
FILE *yyin;
char charSet[] = { '(', ')', '+', '-', '*' };
#define INTEGER 1
#define FLOAT 2
#define READ 3
#define WRITE 4
#define ID 5
#define LPAREN 6
#define RPAREN 7
#define PLUS 8
#define MINUS 9
#define MULT 10
#define ASSIGN 11
#define DIV 12
#define COMMENT 13
#define ERROR 14

int yywrap(){
}

int main(int argc, char **argv){
    int rc;
    if (argc > 1){
        yyin = fopen(argv[1], "r");
        while ((rc=yylex())){
            switch (rc){
                case READ:
                printf("found a read\n");
                break;
                case WRITE:
                printf("found a write\n");
                break;
                case ID:
                printf("found an id\n");
                break;
                case LPAREN:
                printf("found an (\n");
                break;
                case RPAREN:
                printf("found an )\n");
                break;
                case PLUS:
                printf("found an +\n");
                break;
                case MINUS:
                printf("found an -\n");
                break;
                case MULT:
                printf("found an *\n");
                break;
                case ASSIGN:
                printf("found an assign\n");
                break;
                case DIV:
                printf("found a /\n");
                break;
                case INTEGER:
                printf("found an integer\n");
                break;
                case FLOAT:
                printf("found a float\n");
                break;
                case COMMENT:
                printf("found a comment\n");
                break;
                case ERROR:
                printf("Error\n");
                break;
            }
        }
    }
    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-18T01:55:52+00:00Added an answer on June 18, 2026 at 1:55 am

    This is wrong.

    [0-9]* {
        fscanf(yyin, "%d", &number);
        printf("%d\n", number);
        int c = fgetc(yyin);
        printf("%c", c);
        /*return INTEGER;*/
    }
    

    Lex reads from a file into its internal buffer, and then runs the specified action when the given pattern is maximally matched.

    Let’s look at an example from the Flex manual:

    {DIGIT}+    {
                printf( "An integer: %s (%d)\n", yytext,
                        atoi( yytext ) );
                }
    

    As you can see,

    1. The example pattern uses + instead of *. You don’t want to try to match the empty string, so you should use + too.

    2. The contents of the matched pattern are stored in yytext. You should not try to read the pattern from the file using fscanf(), because the matched text has already been read by lex. So, simply use number = strtol(yytext, NULL, 10); in your action. (DO NOT use atoi(yytext) or sscanf(yytext, "%i", &number) — these will give bad output if your number has a leading zero.)

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
We're building an app, our first using Rails 3, and we're having to build

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.