0

I have an application; to interact with it I have to develop an interactive shell. The shell will take input from user and dispatch it to the server, which will process and return a response.

In the past I have done such things, but they typically had the following format:

Shell>> COMMAND arg1 arg2 arg3 ..

and my implementation was in a manner similiar to:

/* Mock implementation */
while(true) {
   in = readline()
   tokens = in.split(' ') //split based on space
   switch(token[0])  {
      case "Command_a": processA(token[1], token[2])
      case "Command_b": processB(token[1], token[2], token[3])
   }
}

This time I have to work with more extensive grammar for user input. If I use my current approach, it will make things very difficult with lots of if , if-elseif-else, if-else, switch statements for both parsing and generating response.

How can I approach this problem in a manner that will make the interpreter modular and maintainable? What are some ways in which popular CLI-Interface are implemented? I will really appreciate some code examples.

PS: Language choices limited C++/Python

WeaklyTyped
  • 1,331
  • 4
  • 16
  • 31
  • 1
    What is the "extensive grammar of user input"? It can be from "free-form English" (that takes serious AI support to parse) to exactly what you have had before (that takes the switch clause to parse). – Öö Tiib Sep 05 '12 at 20:35
  • What would some expected user input look like? – x-x Sep 06 '12 at 05:28

4 Answers4

2

You can develop your own parser and interpreter using bison and flex, but probably wiser is to embed an existing interpreter like Python, Lua or Squirrel.

piokuc
  • 25,594
  • 11
  • 72
  • 102
  • But users will not be programmers – WeaklyTyped Sep 05 '12 at 17:20
  • But it is fairly easy syntax and not elaborate as a programming language. – WeaklyTyped Sep 05 '12 at 17:25
  • 1
    @WeaklyTyped Have you actually looked at the syntax to scheme (the default language behind Guile, the GNU extension language)? It's very, very simple. – Charles Duffy Sep 05 '12 at 17:33
  • I agree Scheme is great as an extension language. There are other good implementations suitable for embedding in C or C++ programs, for example http://code.google.com/p/chibi-scheme/ – piokuc Sep 05 '12 at 17:36
  • But Scheme's legendary parenthesis as well as the mere look and feel will create serious UX issues among users. – WeaklyTyped Sep 05 '12 at 17:39
1

Take a look at boost::spirit, or just embed Lua as others suggested.

mitchnull
  • 6,161
  • 2
  • 31
  • 23
1

The cmd module in Python, perhaps with the addition of Pyparsing should fit your needs perfectly.

Chinmay Kanchi
  • 62,729
  • 22
  • 87
  • 114
0

Go for the cmd module :

The Cmd class provides a simple framework for writing line-oriented command interpreters. These are often useful for test harnesses, administrative tools, and prototypes that will later be wrapped in a more sophisticated interface.

Wai Yip Tung
  • 18,106
  • 10
  • 43
  • 47