troubleshooting Question

Error in c# Visual Studio

Avatar of crazy4s
crazy4s asked on
C#
18 Comments2 Solutions448 ViewsLast Modified:
Hi all,
I'm a newbie in using c#. I got 2 errors when i debug the below program, can anyone help me to figure out where's wrong?

Thanks in advance.

Error:
An unhandled exception of type 'System.OverflowException' occurred in mscorlib.dll

Additional information: Value was either too large or too small for a character.

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Clist_Parser
{
    class Lexer
    {
        private char ch  = ' ';
        private StreamReader input;
        private int lineno = 1;
        private int col = 1;
        private const string letters = "abcdefghijklmnopqrstuvwxyz"
            + "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        private const string digits = "0123456789";
        private const char eolnCh = '\n';

        //        private const char eofCh = '\0';

        private int nextChar()
        {
            int nextchar;
            nextchar = input.Read();
            return nextchar;
        }

        public int get_lineno()
        {
            return lineno;
        }

        private bool isLetter(char c)
        {
            return (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z');
        }

        private bool isDigit(char c)
        {
            return (c >= '0' && c <= '9');
        }

        private void check(char c)
        {
            ch = Convert.ToChar(nextChar());
            if (ch != c)
                error("Illegal character, expecting " + c);
            ch = Convert.ToChar(nextChar());
        }

        private Token chkOpt(char c, Token one, Token two)
        {
            ch = Convert.ToChar(nextChar());
            if (ch != '=')
                return one;
            else
                return two;
        }

        private String concat(String set)
        {
            String r = "";
            do
            {
                r += ch;
                ch = Convert.ToChar(nextChar());
            } while (set.IndexOf(ch) >= 0);
            return r;
        }

        public int getCurrentLine()
        {
            return lineno;
        }

        public void error(String msg)
        {
            Console.WriteLine(lineno);
            Console.WriteLine(" Error: column " + col + " " + msg);
            return;
        }

        /*
         * param: filename is the file you need to open
         * set up the member ch which is the next char in 
         * the file to be analyzed
         */

        public Lexer(String fileName)
        {
            try
            {
                input = new StreamReader(fileName);
                ch = Convert.ToChar(nextChar());
            }
            catch (FileNotFoundException e)
            {
                // Let the user know what went wrong.
                Console.WriteLine(e.Message);
            }
        }

        /*
         * This method tokenize the input stream from the input; 
         * return each token to the caller.
         * 
         */

        public Token next()
        {
            do 
            {
                if (isLetter(ch))
                {
                    String spelling = concat(letters + digits);
                    return Token.keyword(spelling);
                }
                else if (isDigit(ch))
                {
                    String number = concat(digits);
                    if (ch != '.')
                        return Token.mkIntLiteral(number);
                    number += concat(digits);
                    return Token.mkIntLiteral(number);
                }
                else switch (ch)
                {
                    case ' ': case '\t': case '\r': case eolnCh:
                        ch = Convert.ToChar(nextChar());
                        break;
                    case '/':
                        ch = Convert.ToChar(nextChar());
                        if (ch != '/')
                            return Token.divideTok;
                        do 
                        {
                            ch = Convert.ToChar(nextChar());
                        } while (ch != eolnCh);
                        ch = Convert.ToChar(nextChar());
                        break;
                    case '\'':
                        char ch1 = Convert.ToChar(nextChar());
                        nextChar();
                        ch = Convert.ToChar(nextChar());
                        return Token.mkCharLiteral("" + ch1);
                    case '+':
                        ch = Convert.ToChar(nextChar());
                        return Token.plusTok;
                    case '-':
                        ch = Convert.ToChar(nextChar());
                        return Token.minusTok;
                    case '*':
                        ch = Convert.ToChar(nextChar());
                        return Token.multiplyTok;
                    case '!':
                        chkOpt('!', Token.notTok, Token.noteqTok);
                        break;
                    case '>':
                        chkOpt('>', Token.gtTok, Token.gteqTok);
                        break;
                    case '<':
                        chkOpt('<', Token.ltTok, Token.lteqTok);
                        break;
                    case ',':
                        ch = Convert.ToChar(nextChar());
                        return Token.commaTok;
                    case ';':
                        ch = Convert.ToChar(nextChar());
                        return Token.semicolonTok;
                    case '(':
                        ch = Convert.ToChar(nextChar());
                        return Token.leftParenTok;
                    case ')':
                        ch = Convert.ToChar(nextChar());
                        return Token.rightParenTok;
                    case '[':
                        ch = Convert.ToChar(nextChar());
                        return Token.leftBracketTok;
                    case ']':
                        ch = Convert.ToChar(nextChar());
                        return Token.rightBracketTok;
                    case '{':
                        ch = Convert.ToChar(nextChar());
                        return Token.leftBraceTok;
                    case '}':
                        ch = Convert.ToChar(nextChar());
                        return Token.rightBraceTok;
 /*                   case "while":
                        ch = Convert.ToChar(nextChar());
                        return Token.whileTok;
                    case "true":
                        ch = Convert.ToChar(nextChar());
                        return Token.trueTok;
                    case "main":
                        ch = Convert.ToChar(nextChar());
                        return Token.mainTok;
                    case "int":
                        ch = Convert.ToChar(nextChar());
                        return Token.intTok;
                    case "if":
                        ch = Convert.ToChar(nextChar());
                        return Token.ifTok;
                    case "float":
                        ch = Convert.ToChar(nextChar());
                        return Token.floatTok;
                    case "false":
                        ch = Convert.ToChar(nextChar());
                        return Token.falseTok;
                    case "else":
                        ch = Convert.ToChar(nextChar());
                        return Token.elseTok;
                    case "char":
                        ch = Convert.ToChar(nextChar());
                        return Token.charTok;
                    case "bool":
                        ch = Convert.ToChar(nextChar());
                        return Token.boolTok;
                    case "eof":
                        ch = Convert.ToChar(nextChar());
                        return Token.eofTok; */
                    case '&':
                        check('&');
                        return Token.andTok;
                    case '|':
                        check('|'); 
                        return Token.orTok;
                    case '=':
                        chkOpt('=', Token.assignTok, Token.eqeqTok);
                        break;
                } // end switch
            } while (true);
        } // next
    }
}
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 2 Answers and 18 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 18 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros