Avatar of crazy4s
crazy4s
 asked on

error: not all code paths return a value

Hi all,
I'm getting this error for the below program, can anyone figure where is the issue?
 error CS0161: 'AbstractSyntax.Parser.type()': not all code paths return a value

in my parser.cs:
 private Type type() <-- error
        {
            Type t = null;
            switch (current_token.getType())
            {
                case Token.TokenType.Int:
                case Token.TokenType.Char:
                case Token.TokenType.Float:
                case Token.TokenType.Bool:
                    t = new Type(current_token.toString());
                    current_token = mylexer.next();
                    return t;
                default:
                    Console.WriteLine("Syntax error: line " + mylexer.getCurrentLine() + " expecting: int, char, bool, or float" + "; saw: " + current_token.getType() + "value is " + current_token.toString());
                    current_token = mylexer.next();
                    error_flag = true;
                    break;
            }
        }

Open in new window


in my abstractsyntax.cs:
public class Type
    {
        readonly static String INTEGER = "int";
        readonly static String BOOLEAN = "bool";
        readonly static String FLOAT = "float";
        readonly static String CHAR = "char";
        String id;

        public Type(String t)
        {
            id = t;
        }

        public bool isInt()
        {
            return id.Equals(INTEGER);
        }

        public bool isBool()
        {
            return id.Equals(BOOLEAN);
        }

        public bool isFloat()
        {
            return id.Equals(FLOAT);
        }

        public bool isChar()
        {
            return id.Equals(CHAR);
        }

        public String toString()
        {
            return toStringIndented("");
        }

        public String toStringIndented(String indent)
        {
            return indent + "Type(" + id + ")";
        }
    }

Open in new window

C#

Avatar of undefined
Last Comment
AndyAinscow

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
AndyAinscow

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
crazy4s

ASKER
so what it'll be return if is the default error? since i have already have the return t for the above 4 cases?
crazy4s

ASKER
instead of Console.Write should i return the error string?
SOLUTION
AndyAinscow

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
crazy4s

ASKER
so the return t outside of the switch case will return a null if is an error, right?
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
AndyAinscow

You can return it from within the switch statement if you wished.  

Currently it either returns from within the switch exlpicitly (line 12:  return t;) or it exits the switch (default) because there is no explicit return statement