Link to home
Start Free TrialLog in
Avatar of crazy4s
crazy4s

asked on

Error in c# Visual Studio

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
    }
}

Open in new window

Avatar of Dirk Haest
Dirk Haest
Flag of Belgium image

Where exactly do you get the error ?
What is token ?
Do you try to assign a string to a char somewhere ?
debug and use stack trace to find the error location in your code
Avatar of crazy4s
crazy4s

ASKER

I have this token file....
the program actually executed with no errors... but when i debug the console disappeared within a sec and the msg box(the error) pop up!

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

namespace Clist_Parser
{
    class Token
    {
        public enum TokenType
        {

            Bool, Char, Else, False, Float,
            If, Int, Main, True, While,
            Eof, LeftBrace, RightBrace, LeftBracket, RightBracket,
            LeftParen, RightParen, Semicolon, Comma, Assign,
            Equals, Less, LessEqual, Greater, GreaterEqual,
            Not, NotEqual, Plus, Minus, Multiply,
            Divide, And, Or, Identifier, IntLiteral,
            FloatLiteral, CharLiteral
        }

        private static readonly int KEYWORDS = 10;

        private static readonly String[] reserved = new String[KEYWORDS];
        private static Token[] token = new Token[KEYWORDS];

        public static readonly Token eofTok = new Token(TokenType.Eof, "<<EOF>>");
        public static readonly Token boolTok = new Token(TokenType.Bool, "bool");
        public static readonly Token charTok = new Token(TokenType.Char, "char");
        public static readonly Token elseTok = new Token(TokenType.Else, "else");
        public static readonly Token falseTok = new Token(TokenType.False, "false");
        public static readonly Token floatTok = new Token(TokenType.Float, "float");
        public static readonly Token ifTok = new Token(TokenType.If, "if");
        public static readonly Token intTok = new Token(TokenType.Int, "int");
        public static readonly Token mainTok = new Token(TokenType.Main, "main");
        public static readonly Token trueTok = new Token(TokenType.True, "true");
        public static readonly Token whileTok = new Token(TokenType.While, "while");
        public static readonly Token leftBraceTok = new Token(TokenType.LeftBrace, "{");
        public static readonly Token rightBraceTok = new Token(TokenType.RightBrace, "}");
        public static readonly Token leftBracketTok = new Token(TokenType.LeftBracket, "[");
        public static readonly Token rightBracketTok = new Token(TokenType.RightBracket, "]");
        public static readonly Token leftParenTok = new Token(TokenType.LeftParen, "(");
        public static readonly Token rightParenTok = new Token(TokenType.RightParen, ")");
        public static readonly Token semicolonTok = new Token(TokenType.Semicolon, ";");
        public static readonly Token commaTok = new Token(TokenType.Comma, ",");
        public static readonly Token assignTok = new Token(TokenType.Assign, "=");
        public static readonly Token eqeqTok = new Token(TokenType.Equals, "==");
        public static readonly Token ltTok = new Token(TokenType.Less, "<");
        public static readonly Token lteqTok = new Token(TokenType.LessEqual, "<=");
        public static readonly Token gtTok = new Token(TokenType.Greater, ">");
        public static readonly Token gteqTok = new Token(TokenType.GreaterEqual, ">=");
        public static readonly Token notTok = new Token(TokenType.Not, "!");
        public static readonly Token noteqTok = new Token(TokenType.NotEqual, "!=");
        public static readonly Token plusTok = new Token(TokenType.Plus, "+");
        public static readonly Token minusTok = new Token(TokenType.Minus, "-");
        public static readonly Token multiplyTok = new Token(TokenType.Multiply, "*");
        public static readonly Token divideTok = new Token(TokenType.Divide, "/");
        public static readonly Token andTok = new Token(TokenType.And, "&&");
        public static readonly Token orTok = new Token(TokenType.Or, "||");

        private TokenType type;
        private String value = "";


        private Token(TokenType t, String v)
        {
            type = t;
            value = v;
            if (t.CompareTo(TokenType.Eof) < 0)
            {
                int ti = (int)t;
                reserved[ti] = v;
                token[ti] = this;
            }
        }

        public TokenType getType() { return type; }

        public String getValue() { return value; }

        public static Token keyword(String name)
        {
            char ch = name[0];
            if (ch >= 'A' && ch <= 'Z') return mkIdentTok(name);
            for (int i = 0; i < KEYWORDS; i++)
                if (name.CompareTo(reserved[i]) == 0) return token[i];
            return mkIdentTok(name);
        } // keyword

        public static Token mkIdentTok(String name)
        {
            return new Token(TokenType.Identifier, name);
        }

        public static Token mkIntLiteral(String name)
        {
            return new Token(TokenType.IntLiteral, name);
        }

        public static Token mkFloatLiteral(String name)
        {
            return new Token(TokenType.FloatLiteral, name);
        }

        public static Token mkCharLiteral(String name)
        {
            return new Token(TokenType.CharLiteral, name);
        }

        public String toString()
        {
            if (type.CompareTo(TokenType.Identifier) < 0) return value;
            return type + "\t" + value;
        } // toString
    }
}

Open in new window


this is the main program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Clist_Parser
{
    class Program
    {
        static void Main(string[] args)
        {
            Lexer lexer = new Lexer(args[0]);
            Token tok = lexer.next();
            while (tok != Token.eofTok)
            {
                Console.WriteLine("Line " + lexer.getCurrentLine() + "Token Type is " + tok.getType() + " string value is " + tok.getValue());
                tok = lexer.next();
            }
        }
    }
}

Open in new window

see this: http://msdn.microsoft.com/en-us/library/system.console.read.aspx
i guess that your error is around the nextchar = input.Read();
when it return -1 then you have problem during converting -1 to char.
Avatar of crazy4s

ASKER

but why is it returning -1?
it should be reading the first character in the file ryte? how can i solve this prob?
ASKER CERTIFIED SOLUTION
Avatar of rambovn
rambovn
Flag of Germany 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

ASKER

do you meant something like this:
private int result = 1;

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

Open in new window

yes, st. like this
Avatar of crazy4s

ASKER

hmm but i still get the same error msg box pop up?
do i still need to do anything with it?
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

sorry can you explain in more details... n do i need to check for those in the func next() too or?
everywhere, to make sure that you do not pass -1 to the function that convert number to char.
Try it, if not , just share your current code and a sample test file, i will try to debug this
Avatar of crazy4s

ASKER

looks abit messy but i tried checking it all... but still got the same error msg...
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Clist_Parser
{
    class Lexer
    {
        private char ch  = ' ';
        private char ch1;
        private StreamReader input;
        private int lineno = 1;
        private int col = 1;
        private int result = 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)
        {
            while (result != -1)
            {
                ch = Convert.ToChar(nextChar());
                result++;
            }
            if (ch != c)
                error("Illegal character, expecting " + c);
            while (result != -1)
            {
                ch = Convert.ToChar(nextChar());
                result++;
            }
        }

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

        private String concat(String set)
        {
            String r = "";
            do
            {
                r += ch;
                while (result != -1)
                {
                    ch = Convert.ToChar(nextChar());
                    result++;
                }
            } 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
         * Also, you need to 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);
                while (result != -1)
                {
                    ch = Convert.ToChar(nextChar());
                    result++;
                }
            }
            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:
                        while (result != -1)
                        {
                            ch = Convert.ToChar(nextChar());
                            result++;
                        }
                        break;
                    case '/':
                        while (result != -1)
                        {
                            ch = Convert.ToChar(nextChar());
                            result++;
                        }
                        if (ch != '/')
                            return Token.divideTok;
                        do 
                        {
                            while (result != -1)
                            {
                                ch = Convert.ToChar(nextChar());
                                result++;
                            }
                        } while (ch != eolnCh);
                        while (result != -1)
                        {
                            ch = Convert.ToChar(nextChar());
                            result++;
                        }
                        break;
                    case '\'':
                        while (result != -1)
                        {
                            ch1 = Convert.ToChar(nextChar());
                            result++;
                        }
                        nextChar();
                        while (result != -1)
                        {
                            ch = Convert.ToChar(nextChar());
                            result++;
                        }
                        return Token.mkCharLiteral("" + ch1);
                    case '+':
                        while (result != -1)
                        {
                            ch = Convert.ToChar(nextChar());
                            result++;
                        }
                        return Token.plusTok;
                    case '-':
                        while (result != -1)
                        {
                            ch = Convert.ToChar(nextChar());
                            result++;
                        }
                        return Token.minusTok;
                    case '*':
                        while (result != -1)
                        {
                            ch = Convert.ToChar(nextChar());
                            result++;
                        }
                        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 ',':
                        while (result != -1)
                        {
                            ch = Convert.ToChar(nextChar());
                            result++;
                        }
                        return Token.commaTok;
                    case ';':
                        while (result != -1)
                        {
                            ch = Convert.ToChar(nextChar());
                            result++;
                        }
                        return Token.semicolonTok;
                    case '(':
                        while (result != -1)
                        {
                            ch = Convert.ToChar(nextChar());
                            result++;
                        }
                        return Token.leftParenTok;
                    case ')':
                        while (result != -1)
                        {
                            ch = Convert.ToChar(nextChar());
                            result++;
                        }
                        return Token.rightParenTok;
                    case '[':
                        while (result != -1)
                        {
                            ch = Convert.ToChar(nextChar());
                            result++;
                        }
                        return Token.leftBracketTok;
                    case ']':
                        while (result != -1)
                        {
                            ch = Convert.ToChar(nextChar());
                            result++;
                        }
                        return Token.rightBracketTok;
                    case '{':
                        while (result != -1)
                        {
                            ch = Convert.ToChar(nextChar());
                            result++;
                        }
                        return Token.leftBraceTok;
                    case '}':
                        while (result != -1)
                        {
                            ch = Convert.ToChar(nextChar());
                            result++;
                        }
                        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
    }
}

Open in new window


the filename to read - test2.txt

// a first program with
// two comment lins


int main () {
char c ;
int i;
c = 'h';
}  // end of file

Open in new window

ok,

it is really like i think.
And here is a example how to fix
                int tmp= nextChar();
                while (tmp != -1)
                {

                    ch = Convert.ToChar(tmp);
                    tmp = nextChar();
                    result++;
                }
Avatar of crazy4s

ASKER

hmm is your result same as col since i've this at the beginning?
        private int col = 1;

but i got no output for this:(
        public Lexer(String fileName)
        {
            try
            {
                input = new StreamReader(fileName);
                int tmp = nextChar();
                while (tmp != -1)
                {
                    ch = Convert.ToChar(tmp);
                    tmp = nextChar();
                    col++;
                }
            }
            catch (FileNotFoundException e)
            {
                // Let the user know what went wrong.
                Console.WriteLine(e.Message);
            }
        }

        public Token next()
        {
            int tmp;
            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:
                        tmp = nextChar();
                        while (tmp != -1)
                        {
                            ch = Convert.ToChar(tmp);
                            tmp = nextChar();
                            col++;
                        }
                        break;
                    case '/':
                        ch = Convert.ToChar(nextChar());
                        if (ch != '/')
                            return Token.divideTok;
                        string line = input.ReadLine();
                        string comment = line.Substring(col);
                        Console.WriteLine("Line " + getCurrentLine() + "Token Type is COMMENT and value is /" + comment);
                        do
                        {
                            tmp = nextChar();
                            while (tmp != -1)
                            {
                                ch = Convert.ToChar(tmp);
                                tmp = nextChar();
                                col++;
                            }
                        } while (ch != eolnCh);
                        tmp = nextChar();
                        while (tmp != -1)
                        {
                            ch = Convert.ToChar(tmp);
                            tmp = nextChar();
                            col++;
                        }
                        break;

// other codes

Open in new window

you must use it appropriately:
 use while () , if (),   .... as necessary
Avatar of crazy4s

ASKER

i edited some of the codes... but i'm not sure how can i check for next char after the '=' by not changing the chkOpt method....
i only call nextChar() once since i want to check every single char once only...

But i got a really weird output for this test:
// a first program with
// two comment lins


int main () {
char c ;
int i;
c = 'h';
}  // end of file

User generated image
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);
                int tmp = nextChar();
                if (tmp != -1)
                {
                    ch = Convert.ToChar(tmp);
                }
            }
            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()
        {
            int tmp;
            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':
                        tmp = nextChar();
                        if (tmp != -1)
                        {
                            ch = Convert.ToChar(tmp);
                            tmp = nextChar();
                            col++;
                        }
                        break;
                    case eolnCh:
                        tmp = nextChar();
                        if (tmp != -1)
                        {
                            ch = Convert.ToChar(tmp);
                            tmp = nextChar();
                            col = 1;
                            lineno++;
                        }
                        break;
                    case '/':
                        tmp = nextChar();
                        if (tmp != -1)
                        {
                            ch = Convert.ToChar(tmp);
                            col++;
                        }
                        if (ch != '/')
                            return Token.divideTok;
                        string line = input.ReadLine();
                        string comment = line.Substring(0);
                        Console.WriteLine("Line " + getCurrentLine() + "Token Type is COMMENT and value is //" + comment);
                        tmp = nextChar();
                        if (tmp != -1)
                        {
                            ch = Convert.ToChar(tmp);
                            col = 1;
                            lineno++;
                        }
                        break;
                    case '\'':
                        char ch1 = ' ';
                        tmp = nextChar();
                        if (tmp != -1)
                        {
                            ch1 = Convert.ToChar(tmp);
                            tmp = nextChar();
                            col++;
                        }
                        nextChar();
                        tmp = nextChar();
                        if (tmp != -1)
                        {
                            ch1 = Convert.ToChar(tmp);
                            tmp = nextChar();
                            col++;
                        }
                        return Token.mkCharLiteral("" + ch1); 
                    case '+':
                        tmp = nextChar();
                        if (tmp != -1)
                        {
                            ch = Convert.ToChar(tmp);
                            col++;
                        }
                        return Token.plusTok;
                    case '-':
                        tmp = nextChar();
                        if (tmp != -1)
                        {
                            ch = Convert.ToChar(tmp);
                            col++;
                        }
                        return Token.minusTok;
                    case '*':
                        tmp = nextChar();
                        if (tmp != -1)
                        {
                            ch = Convert.ToChar(tmp);
                            col++;
                        }
                        return Token.multiplyTok;
                    case '!':
                        return chkOpt('!', Token.notTok, Token.noteqTok);
                    case '>':
                        return chkOpt('>', Token.gtTok, Token.gteqTok);
                    case '<':
                        return chkOpt('<', Token.ltTok, Token.lteqTok);
                    case ',':
                        tmp = nextChar();
                        if (tmp != -1)
                        {
                            ch = Convert.ToChar(tmp);
                            col++;
                        }
                        return Token.commaTok;
                    case ';':
                        tmp = nextChar();
                        if (tmp != -1)
                        {
                            ch = Convert.ToChar(tmp);
                            col++;
                        }
                        return Token.semicolonTok;
                    case '(':
                        tmp = nextChar();
                        if (tmp != -1)
                        {
                            ch = Convert.ToChar(tmp);
                            col++;
                        }
                        return Token.leftParenTok;
                    case ')':
                        tmp = nextChar();
                        if (tmp != -1)
                        {
                            ch = Convert.ToChar(tmp);
                            col++;
                        }
                        return Token.rightParenTok;
                    case '[':
                        tmp = nextChar();
                        if (tmp != -1)
                        {
                            ch = Convert.ToChar(tmp);
                            col++;
                        }
                        return Token.leftBracketTok;
                    case ']':
                        tmp = nextChar();
                        if (tmp != -1)
                        {
                            ch = Convert.ToChar(tmp);
                            col++;
                        }
                        return Token.rightBracketTok;
                    case '{':
                        tmp = nextChar();
                        if (tmp != -1)
                        {
                            ch = Convert.ToChar(tmp);
                            col++;
                        }
                        return Token.leftBraceTok;
                    case '}':
                        tmp = nextChar();
                        if (tmp != -1)
                        {
                            ch = Convert.ToChar(tmp);
                            col++;
                        }
                        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 '=':
                        return chkOpt('=', Token.assignTok, Token.eqeqTok);
                } // end switch
            } while (true);
        } // next
    }
}

Open in new window

Avatar of crazy4s

ASKER

since i solved the errors i should proceed to another thread to ask another question.
Thanks.