Solved

error: not all code paths return a value

Posted on 2012-03-17
6
385 Views
Last Modified: 2012-03-17
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

0
Comment
Question by:crazy4s
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 500 total points
ID: 37733249
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;
            }

//your code finishes here BUT no object of Type is returned - hence the error
        }
0
 

Author Comment

by:crazy4s
ID: 37733266
so what it'll be return if is the default error? since i have already have the return t for the above 4 cases?
0
 

Author Comment

by:crazy4s
ID: 37733276
instead of Console.Write should i return the error string?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 500 total points
ID: 37733285
private Type type()

That tells the compiler it WILL return something of Type

You could try this

private Type type()
        {
            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;
            }

return t;      //which is null,  or return new Type("");
        }
0
 

Author Comment

by:crazy4s
ID: 37733292
so the return t outside of the switch case will return a null if is an error, right?
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 37733343
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
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question