Link to home
Start Free TrialLog in
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

ASKER CERTIFIED SOLUTION
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of crazy4s
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?
Avatar of crazy4s

ASKER

instead of Console.Write should i return the error string?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of crazy4s

ASKER

so the return t outside of the switch case will return a null if is an error, right?
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