Search code examples
cparsingparser-generatorturbo-ctruthtable

how can I create a truth table in turbo c


I have a project where I will create a truth table, and the user will input propositions and its operator and my program will output the truth values providing with a truth table.

I have following questions:

  1. can I use parsing techniques using turbo c?
  2. how should I parse this expression in turbo c? Ex. (p ^ q) -> r
  3. Once I have the expression parsed, how should I go about generating the truth table? Each section of the expression needs to be divided up into its smallest components and re-built from the left side of the table to the right. How would I evaluate something like that?

Can anyone provide me with tips (or links) concerning the parsing of these arbitrary expressions and eventually evaluating the parsed expression?


Solution

  • Let me try to answer your questions.

    1. Yes. There is no reason why you can't.
    2. You need to write some sort of lexer to turn the expression into tokens. Then you can use the shunting yard algorithm to turn the expression into something you can easily evaluate.
    3. Use the result from (2) and evaluate it in a little stack machine. Set each free variable to all possible combinations to generate a truth table.

    Parsing arbitrary languages is not possible in general. A good introduction into compiler construction (which is the subfield you are interested in) is found in the Dragon Book (Compilers: Principles, Techniques, and Tools). It's a large field though, I recommed you to take a compiler construction class.

    Also, consider ditching Turbo C for something recent. Turbo C is ancient and full of weird quirks.