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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T15:19:59+00:00 2026-05-12T15:19:59+00:00

Wikipedia’s Interpolation Definition I am just learning flex / bison and I am writing

  • 0

Wikipedia’s Interpolation Definition
I am just learning flex / bison and I am writing my own shell with it. I am trying to figure out a good way to do variable interpolation. My initial approach to this was to have flex scan for something like ~ for my home directory, or $myVar , and then set what the yyval.stringto what is returned using a look up function. My problem is, that this doesn’t help me when text appears one token:

kbsh:/home/kbrandt% echo ~
/home/kbrandt
kbsh:/home/kbrandt% echo ~/foo
/home/kbrandt /foo
kbsh:/home/kbrandt%

The lex definition I have for variables:

\$[a-zA-Z/0-9_]+    {
    yylval.string=return_value(&variables, (yytext + sizeof(char)));;
    return(WORD);
}

Then in my Grammar, I have things like:

chdir_command:
    CD WORD { change_dir($2); }
    ;

Anyone know of a good way to handle this sort of thing? Am I going about this all wrong?

  • 1 1 Answer
  • 1 View
  • 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-12T15:19:59+00:00Added an answer on May 12, 2026 at 3:19 pm

    The way ‘traditional’ shells deal with things like variable substitution is difficult to handle with lex/yacc. What they do is more like macro expansion, where AFTER expanding a variable, they then re-tokenize the input, without expanding further variables. So for example, an input like “xx${$foo}” where ‘foo’ is defined as ‘bar’ and ‘bar’ is defined as ‘$y’ will expand to ‘xx$y’ which will be treated as a single word (and $y will NOT be expanded).

    You CAN deal with this in flex, but you need a lot of supporting code. You need to use flex’s yy_buffer_state stuff to sometimes redirect the output into a buffer that you’ll then rescan from, and use start states carefully to control when variables can and can’t be expanded.

    Its probably easier to use a very simple lexer that returns tokens like ALPHA (one or more alphabetic chars), NUMERIC (one or more digits), or WHITESPACE (one or more space or tab), and have the parser assemble them appropriately, and you end up with rules like:

    simple_command: wordlist NEWLINE ;
    
    wordlist: word | wordlist WHITESPACE word ;
    
    word: word_frag
        | word word_frag { $$ = concat_string($1, $2); }
    ;
    
    word_frag: single_quote_string
             | double_quote_string
             | variable
             | ALPHA
             | NUMERIC
            ...more options...
    ;
    
    variable: '$' name { $$ = lookup($2); }
            | '$' '{' word '}' { $$ = lookup($3); }
            | '$' '{' word ':' ....
    

    as you can see, this get complex quite fast.

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

Sidebar

Related Questions

The following definition from wikipedia explains what is the Data Access Layer in a
Wikipedia has a good summary about the various design patterns. Which ones have you
Wikipedia's definition for Iterator pattern design: the Iterator pattern is a design pattern in
Wikipedia does has the definition of ID3 algorithm but it is very complex. Is
Wikipedia has a definition for a control flow graph . I've also heard terminology
Wikipedia says SQL Server Express Edition is limited to one processor, 1 GB memory
Wikipedia says Ruby is a functional language, but I'm not convinced. Why or why
Wikipedia has this to say: Total functional programming (also known as strong functional programming,
Wikipedia provides a number of citations used in the sciences , however does one
Wikipedia allows you to reproduce its content. If you want to use Wikipedia materials

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.