help-bison
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to implement Variable Substitution / Interpolation


From: Hans Aberg
Subject: Re: How to implement Variable Substitution / Interpolation
Date: Thu, 17 Sep 2009 23:09:15 +0200

On 17 Sep 2009, at 19:52, Kyle Brandt wrote:

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.

You might inquiry in the Usenet newsgroup comp.compilers, which can give better advice on general parsing problems. There are probably examples out there.

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.string to what is
returned using a look up function. My problem is that this doesn't help me
when text appears as one token:

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

I think the normal thing, though, would be to be letting the lexer just parse the tokens, and hand that over to the parser, which in turn builds the actions. So a grammar perhaps like:
%token TILDE
%token SLASH

%%
command_line:
  command args
  ;

args:
    /* empty */
  | args arg
  ;

arg:
    TILDE other_stuff
    SLASH other_stuff
  | other_stuff
  ;

other_stuff: ... ;

Also, the man page of say bash, section invocation, describes in detail how commands are interpreted, which can be useful if you do not have an example grammar.

  Hans






reply via email to

[Prev in Thread] Current Thread [Next in Thread]