Thread: Dynamic variable assignment

  1. #1
    Make Fortran great again Epy's Avatar
    Join Date
    Sep 2009
    California, USA

    Dynamic variable assignment

    The other thread got me thinking about this. Before I go and look at GNU Octave's source code, say I wanted to make a similar program that interprets strings from a prompt. I know how to parse a string like "a=5" and recognize that "a" is being assigned the value of "5", but how would I go about actually storing that? Can you do some sort of cast to a char array so that you can assign it as an actual variable? Otherwise, all I could think of is making a struct with a char array for the name and a double for the value.

    Tldr, how would one get from "a=5" to int a = 5; ?

  2. #2
    Registered User
    Join Date
    Oct 2008
    If I got it then use fscanf() to read stdin and store it in an integer variable.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    segmentation fault
    Quote Originally Posted by Epy View Post
    Tldr, how would one get from "a=5" to int a = 5; ?
    You can't -- at least, not directly in a C program.

    Is Octave interpreted or compiled? Since it has a CLI interface, I would guess the former.

    all I could think of is making a struct with a char array for the name and a double for the value.
    Well, from what I have read about compilers/interpreters, that is literally how a "symbol table" -- which could be a binary tree or a linked list or something similar -- is constructed.

    Maybe you will find this interesting, I wrote it in the summer, it is short n' sweet and includes eg. a prog for parsing math expressions like "2+2*(14/6)-12" by using compiler type techniques:


    So, in fact, if you wanted a program that accepted variable assignments as inputs, you could proceed from there. The variable would simply be a "leaf node" like a number/value, since that is what it stands in for.
    Last edited by MK27; 10-15-2009 at 01:31 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Oct 2006
    this week i have been working on a similar sort of thing (not C++, but thats irrelevant). among other work, the two key things were: 1) creating classes to represent the equations, 2) using (hash)maps to map a variable to its value.

    1) the equation "a = 5" has two parts, left hand side (LHS) and right hand side (RHS). the LHS is always just a string (or a char) to identify the value of its RHS. the RHS is a sequence of values (identifiers or numbers) and binary operations (+, -, /, *). the RHS in this example is a "primitive" one and is just a number "5". another equation could be "b = 1 + 2 * 3". this time the RHS is a "complex" sequence of operations, which also has a "left" and "right" and an "operator". the "left" is the primitive value "1", the operation is "+", the right is the complex expression "2*3". now, compex right is broken down into an "expression" with left "2", op "*", right "3". note that this is right-associative, and you may not be doing it like this, and rather doing it using BEDMAS or introducing brackets to enforce ordering. (assuming either left or right associativity makes things easier).

    2) use maps to associate a key to an number value (integer, real, whatever). in the first case above, we would insert into the map the key "a" and its numerical value of 5. similarly, we would insert the key "b" with value of its expression, 7 (right associative, it would have been 9 if left associative).

    of course some expressions can use variables in it. such as "c = 1 + a + b". however, in math, just as in programming, you have to define something before you use it. that means that we already know the values of a and b (they are in the map, values 5 and 6 respectively). so when we see these variables in the expression, we look them up in the map and replace them with their value, so "c = 1 + a + b" becomes "c = 1 + 5 + 6" and we save key c with value 12 into the map.

    i never even mentioned the process of converting a string "5" (from "a=5") into a number, but that is the least of your worries! . you are basically writing a parser to take input ("a=5"), do lexical analysis (split it into 3 parts, lhs, rhs, symbol "="), and do semantic analysis to take these 3 "symbols" and make sense of it (i.e. make the c++ objects from them; "equation", "expression", etc). this is what i did also... its definitely challenging, but quite interesting and rewarding! good luck.

  5. #5
    Make Fortran great again Epy's Avatar
    Join Date
    Sep 2009
    California, USA
    I've been so spoiled by LISP. haha

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Menu
    By Krush in forum C Programming
    Replies: 17
    Last Post: 09-01-2009, 02:34 AM
  2. Dynamic Variable Naming
    By scott_ill in forum C# Programming
    Replies: 5
    Last Post: 07-29-2009, 01:46 PM
  3. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 03:33 PM
  4. float/double variable storage and precision
    By cjschw in forum C++ Programming
    Replies: 4
    Last Post: 07-28-2003, 06:23 PM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM