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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:47:08+00:00 2026-05-27T06:47:08+00:00

I am using a char* as YYSTYPE in a compiler built with flex and

  • 0

I am using a char* as YYSTYPE in a compiler built with flex and bison. The line

#define YYSTYPE char*

is at the top of my grammar file. A few of the tokens in my lexer need to pass the entire string that they match to my grammar, and the others just need to pass their token, so this works well for me. I do this sort of thing in my lexer:

[(foo|bar)]    {yylval = *strdup(yytext); return FOOBAR;}

In my grammar, I use them with productions like this one:

fb:
    FOOBAR
    {
        sprintf($$, "%s", &$1);
    }
    ;

This sets the value of $$ to the first character in the original matched token. I (probably) understand why, since a dereferenced char* is a char, but the steps I took to fix it caused problems. For instance, removing the & from the sprintf() line causes a segfault. Removing the * from the assignment causes a “makes integer from pointer without a cast”. What do I do? I think the problem lies in the assignment to yylval.

  • 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-27T06:47:09+00:00Added an answer on May 27, 2026 at 6:47 am

    There are several problems with what you are doing. First and foremost, since YYSTYPE is a char pointer, there is actually no space allocated for a string. So when you do sprintf($$, "%s", &$1), you try to print a string into a pointer that is not initialized ($$ is a pointer, but not initialized to anything, so it can point to anywhere in memory.)

    Another problem might be your use of &$1 in the sprintf. It takes the address of the pointer, not the actual string the pointer is pointing to.

    A third problem is you are using strdup in the lexer, which allocates memory. But you never free it somewhere, creating a memory leak.

    The fourth and final problem is why you only get a single character, and you are actually lucky you get that, and that because while strdup(yytext) is returning a copy of a string, the star in front of it returns the dereferenced pointer which is a char. So you set the pointer to a single character.

    Edit: I hope it all makes sense, it’s late and I might have a glass of wine or two…

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

Sidebar

Related Questions

I'm using a struct like this: define struct _Fragment{ int a; char *seq; }Fragment;
I am reading the contents of the file using fread into an char array.
I'm writing to a text file using the following declaration: void create_out_file(char file_name[],long double
This code fails when I try to debug it using VC2010: char frd[32]=word-list.txt; FILE
strtok wont work correctly when using char *str as the first parameter (not the
I have a char array buffer that I am using to store characters that
How do I map a CHAR(1) to a boolean using Hibernate for Java?
I need to convert a char array into int and float using C The
how to convert char to LPCTSTR in vc++ I am using MVC. QByteArray qBary;
I have a function using strtok like this void f1(char *name) { ... char

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.