Question

i want to solve boolean functions like:

(2and2)or0

(true and false) and true etc.

I did this tutorial for some simple calculator: http://programming-pages.com/2013/12/14/antlr-4-with-c-and-visual-studio-2012/

When i try to solve functions like " true and false", i get a right result out of it. For my Project i need to solve functions with integer numbers like " 2 and 0 ". And as a result i want to have " True or False !".

So i tried to Convert the Integer Numbers of my Input to Boolean ( 0 = false, and everyting else is true) and then compare the expressions with && or || .

Can you tell me why ist not working with this:

public override bool VisitInt(Combined2Parser.IntContext context)
{
  return bool.Parse(context.INT().GetText());
}

In my opinion this shall convert all the Integers found in the Inputstring to Bool, but it is not working like this.

( For the whole Code please have a Look on the Tutorial , my code is almost the same )


Thanks for you help.

My next step is, that whenever the Parser finds some letter, it shall transform this into a float variable, and replace the P/T with the Input value.

But i Need some separation if my expr is a number or a letter.

Like:

IF( expr(0) != "Number" && expr(1) != "Number")

then ask what value i want to use

how can i code this if- function?

Was it helpful?

Solution

You can't use bool.Parse for converting numbers to boolean values. It doesn't work this way in C# (.Net framework), bool.Parse is for parsing string values for True and False.

You can use compare integer values to 0 and return accordingly like:

public override bool VisitInt(Combined2Parser.IntContext context)
{
  return context.Int() != 0; //true for anything other than 0, false for 0
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top