title: Cleanest data structure to use when interpreting data from neatly-structured user commands (in C++)
body: <p>I would like to write a simple in-house program that parses user commands written in a language of our team's own invention (but based closely on another program we are already familiar with). The command parser that I am working on now will simply be the UI through which the user can run the other algorithms I have already written. (Those other algorithms, by the way, are used to generate the input files for a molecular dynamic simulation package called <a href="http://lammps.sandia.gov/" rel="nofollow">LAMMPS</a>.) The only thing I really have left to do is just write this UI, but as it turns out, writing your own scripting language is almost an intractable challenge for a non software engineer to tackle on his own.</p>
  
  <p>According to the answers I received, what I am try to make would be considered a <a href="http://en.wikipedia.org/wiki/Domain-specific_language" rel="nofollow">Domain Specific Language</a>, and it is not advisable to try to make one's own DSL due to the enormous amount of work required to make it useful and bug-free.</p>
  
  <p>The best option then would actually be to use an existing scripting language like Lua or Python, and embed it in the program. </p>
  
  <p>To do this, I will most likely use <a href="http://www.lua.org/" rel="nofollow">Lua</a> because it seems most fitting for our needs. So at this point, the rest of this question is no longer relevant since the answer would be: "Don't do it yourself." But I'm still going to keep part of it here for other users to be able read and learn from the wonderful answers below.</p>
  
  <p>Thanks again to everyone who replied!</p>
  
  <p><br/>  </p>
  
  <p><strong><em>Old Question:</em></strong></p>
  
  <blockquote>
    <p>I would like to write a program that parses a user text input and then
    runs a function corresponding to that input. To do this I would need
    to parse the string for relevant keywords. I believe there will be
    less than 15 keywords when I'm done, so ideally I'd like this code
    to be simple and short.</p>
    
    <p>The problem is that I am currently using if-statements to parse the
    strings. This is an extremely inconvenient way to parse commands
    because even for a short 3 word commands the code explodes into nested-ifs
    3 layers deep. So longer 8+ word sentences will become nested-ifs more than
    8 layers deep.</p>
    
    <p>This kind of programing approach quickly becomes unmanageable, especially
    when I need to make any significant changes to a command.</p>
    
    <p><br/></p>
    
    <p>My question is whether or not there exists a data structure in C++ that
    can help me better manage my giant nested-ifs, or if anyone could suggest
    a better way to parse a string for lots of different data types (i.e.
    substings, ints, and floats) and output an error message when the expected
    type is not found?</p>
    
    <p><br/></p>
    
    <p>Here is an example of a short user session to show the kinds of commands
    I would like to interpret:</p>
  
  <pre><code>    load "Basis.Silicon" as material 1
      add material 1 to layer 1
      rotate layer 1 about x-axis by 45 degrees
      translate layer 1 in x-axis by 10 nm
      generate crystal
  </code></pre>
    
    <p>These commands are based on an already-existing program that our team
    uses, but unfortunately the source code for this program has never been
    publicly released so I am left guessing as to how it was actually
    implemented.</p>
    
    <p>One final note, unlike natural language processors, I know exactly what
    the format of each line will be. So my issue isn't so much <em>how</em> to interpret
    the text, but rather how to code the logic in a concise and manageable way.</p>
    
    <p>Thanks everyone!</p>
  </blockquote>