Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using C++ code implement a recursive descent parser for a logic calculator based

ID: 3757912 • Letter: U

Question

Using C++ code implement a recursive descent parser for a logic calculator based on the Grammar below

Grammar

Note: This grammar uses the '|' symbol as a terminal. In place of that symbol, we will use '' to separate replacement options for each grammar rule.

Expression Term Expression | Term

Term Factor Term & Factor

Factor Primary Factor ^ Primary

Primary T F ( Expression ) ! Expression

Code Requirements

Functions for the grammar rules will return type bool.

You must use a recursive descent parser.

Use either a capital (Q) or lowercase (q) to indicate you want to exit the program.

Sample Output

Explanation / Answer

INPUT = EXPRESSION

EXPRESSION = TERM { "+" | "-" TERM }

TERM = (FUNCTION | FACTOR) { "*" | "/" | "%" | "^" FUNCTION | FACTOR } { "!" }

FUNCTION = ( "COS"| "SIN" | "TAN" | "EXP" |"LN" | "LOG" "(" FACTOR ")" )

FACTOR = NUMBER|VARIABLE| ( "(" ["-"] EXPRESSION ")" )

VARIABLE = ALPHABHET,{ALPHABHET}

NUMBER= DIGIT,{["."] DIGIT}

DIGIT = 0|1|..|9

ALPHABHET = a|b|..|z|A|B|..|Z

public interface IExpression

{

    string Expression { get; set; }

    double Result { get; set; }

    bool CanEvaluate { get; set; }

    string StrResult { get; set; }

}

public enum SymbolType {expression, operation, function, variable, digit,brackets,endTag}

public interface ISymbol:IExpression

{

    SymbolType SymbolType { get; set; }

    double FloatingValue { get; set; }

    string StringValue { get; set; }

}

public static class ExtensionLib

{

    public static Symbol TypedClone(this IExpression v)

    {

        return ((Symbol) v).TypedClone();

    }

}

private int _currentPositionCnt;

private bool _processVariableExpression;

private Symbol _lastReadSymbol;

private IExpression _expression;

private IExpression _originalExpression;

private readonly List<string> _listOfFunctions = new List<string>();

private readonly List<string> _listOfBinaryOperators = new List<string>();

private readonly List<string> _listOfUnaryOperators = new List<string>();

public readonly Dictionary<string, double> VariableMapper { get; private set; }

public Evaluator()

{

    _listOfFunctions.AddRange(new [] { "COS", "SIN", "TAN", "EXP", "LN", "LOG" });

    _listOfBinaryOperators.AddRange(new [] { "*", "/", "^", "%", "+", "-" });

    _listOfUnaryOperators.Add("!");

    VariableMapper = new Dictionary<string, double> {{"PI", Math.PI}};

}

public IExpression Resolve(IExpression exp, bool resolveExp=false)

{

    _currentPositionCnt = 0;

    _originalExpression = exp;

    _expression = exp.TypedClone();

    _expression.Expression = _expression.Expression.Replace(" ", "");

    _expression.Expression += "#";

    var result = Expression();

    if (!NextIs("#"))

       throw new Exception

               ("An error occurred while procession the expression [ "

                + _originalExpression.Expression + " ] At position "

                + _currentPositionCnt.ToString());

    if (resolveExp && !result.CanEvaluate && VariableMapper.Count > 0)

    {

        _processVariableExpression = true;

        _lastSymbolType = SymbolType.expression;

        _currentPositionCnt = 0;

      _lastReadSymbol = null;

        _expression.Expression = result.StrResult.Replace(" ", "");

        result = Expression();

    }

    return result;

}

private ISymbol Expression()

{

    var x = Term();

    if (x == null) return null;

    for (; ; )

    {

        if(NextIs("+"))

        {

            var y = Term();

            if (y.CanEvaluate && x.CanEvaluate)

                x.Result = x.FloatingValue = x.FloatingValue + y.FloatingValue;

            else

            {

                x.StringValue =

                    "("+(x.CanEvaluate ? x.FloatingValue.ToString()

                                       : x.StringValue) +

                      " + " + (y.CanEvaluate ? y.FloatingValue.ToString()

                                             : y.StringValue) + ")";

                x.CanEvaluate = false;

            }

        }

        else if (NextIs("-"))

        {

            var y = Term();

            if (y.CanEvaluate && x.CanEvaluate)

                x.Result = x.FloatingValue = x.FloatingValue - y.FloatingValue;

            else

            {

                x.StringValue = "("+(x.CanEvaluate ? x.FloatingValue.ToString()

                                                   : x.StringValue) +

                                " - "+ (y.CanEvaluate ? y.FloatingValue.ToString()

                                                      : y.StringValue) + ")";

                x.CanEvaluate = false;

            }

        }

        else

        {

            break;

        }

    }

    x.Result = x.FloatingValue;

    x.StrResult = x.StringValue;

    return x;

}

private Symbol Term()

{

    var x = Factor();

    if (x == null) return null;

    for (; ; )

    {

        if (NextIs("*"))

        {

            var y = Factor();

            if (y.CanEvaluate && x.CanEvaluate)

                x.Result = x.FloatingValue = x.FloatingValue * y.FloatingValue;

            else

            {

                x.StringValue = "("+(x.CanEvaluate? x.FloatingValue.ToString()

                                                   : x.StringValue) +

                        " * " + (y.CanEvaluate ? y.FloatingValue.ToString()

                                       : y.StringValue) + ")";

                x.CanEvaluate = false;

              

            }

        }

        else if (NextIs("/"))

        {

            var y = Factor();

            if (y.CanEvaluate && x.CanEvaluate)

                x.Result = x.FloatingValue = x.FloatingValue / y.FloatingValue;

            else

            {

                x.StringValue = "("+(x.CanEvaluate ? x.FloatingValue.ToString()

                                                   : x.StringValue) +

                        " / " + (y.CanEvaluate ? y.FloatingValue.ToString()

                                       : y.StringValue) + ")";

                x.CanEvaluate = false;

            }

        }

        else if (NextIs("%"))

        {

            var y = Factor();

            if (y.CanEvaluate && x.CanEvaluate)

            {

                x.Result = x.FloatingValue = x.FloatingValue % y.FloatingValue;

            }

            else

            {

                x.StringValue = "("+(x.CanEvaluate ? x.FloatingValue.ToString()

                                                   : x.StringValue) +

                        " % " + (y.CanEvaluate ? y.FloatingValue.ToString()

                                       : y.StringValue) + ")";

                x.CanEvaluate = false;

            }

        }

        else if (NextIs("^"))

        {

            var y = Factor();

            if (y.CanEvaluate && x.CanEvaluate)

            {

                x.Result = x.FloatingValue = Math.Pow(x.FloatingValue , y.FloatingValue);

            }

          else

            {

                x.StringValue = "( (" + (x.CanEvaluate ? x.FloatingValue.ToString()

                                                       : x.StringValue) +

                        ")^ " + (y.CanEvaluate ? y.FloatingValue.ToString()

                                       : y.StringValue) + ")";

                x.CanEvaluate = false;

            }

        }

        else if (NextIs("!"))

        {

            if (x.CanEvaluate)

            {

                x.Result = x.FloatingValue = Factorial(x.FloatingValue);

            }

            else

            {

                   x.CanEvaluate = false;

                   x.StringValue = "(" + (x.CanEvaluate ? x.FloatingValue.ToString()

                                                     : x.StringValue) +")! ";

            }

        }

        else if (_listOfFunctions.Contains(x.StringValue))

        {

            x = FunctionEvaluator(x.StringValue);

        }

        else

        {

            break;

        }

    }

    return x;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote