tbogatchev
tbogatchev

Reputation: 1641

Implement the Unix 'dc' utility in C

I am just hoping on getting some tips on how to get started on my assignment. It reads as follows:

"Implement a program that behaves similarly to the 'dc' utility, which evaluates expressions in a postfix notation, and also supports additional computations using variables and macro strings. Feel free to experiment with the existing utility and consulting its manual pages. The assignment will support only a subset of the complete utility: -- all numeric values will be integers, only using radix 10

-- numbers may be assumed to be within the range of 32-bit signed numbers

-- all register names will be alphanumeric (a subset of those dc allows)

-- no command line options or arguments will be required

-- only the following commands are are required (listed in the same order as the manual) p n f + - * / % ^ c d r s l S L x > !> < !< = != q #

Some key differences between the assignment and the existing utility: -- all input will be through standard input only (but not necessarily keyboard)

-- register stacks are initially not empty, but filled with infinite zeros

-- the q command will exit the program, regardless of macro call nesting level

-- additional spaces may appear between input tokens for legibility (the space is not a command or a value or a register name)"

I honestly don't know where to start... Any help is greatly appreciated, thanks guys.

Upvotes: 0

Views: 1603

Answers (2)

Dale Hagglund
Dale Hagglund

Reputation: 16440

@Serge has given a good outline of how to start, but you seem a bit at sea about just what dc does, so here are a few pointers for that.

  • The assignment says that you should experiment with the real dc program. Do this, if you haven't already done so, and you should learn quite a bit. (If you haven't done this, why not?)
  • Check out the Basic Operations section of the Wikipedia page on dc.
  • Read the dc man page. You'll find it confusing the first time, but read it again and try out what you see there with the actual program.
  • If you're confused by the whole idea of postfix notation see the Wikipedia page on Reverse Polish Notation, which is another name for the same thing.

This should let you understand the following very simple examples, as well as predict what they do:

  • 3 p
  • 3 4 * p
  • 3 4 5 + * p
  • 3 4 + 5 * p

Your assignment goes quite a way beyond, but if you get this much implemented you'll be more than half-way there.

Upvotes: 0

Serge
Serge

Reputation: 6095

As it is an assignment and you do not know where to start here are some hints:

  • Start with reading the stdin and split it into tokens
  • implement the stack to store operands and results
  • implement a few operations like +,-,*,/
  • make it all working
  • then implement the missing features one by one

Upvotes: 1

Related Questions