[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

error: not all code paths return a value

Posted on 2012-03-17
6
Medium Priority
?
414 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 45

Accepted Solution

by:
AndyAinscow earned 2000 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 45

Assisted Solution

by:AndyAinscow
AndyAinscow earned 2000 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 45

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

Technology Partners: 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

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

650 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