Fahad/MCG-CS/Scanner.cs
author Christian Urban <urbanc@in.tum.de>
Fri, 10 May 2019 11:56:37 +0100
changeset 318 43e070803c1c
parent 45 7545b1bc1514
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
45
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
     1
using System;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
     2
using System.Collections.Generic;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
     3
using System.Text;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
     4
//Added
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
     5
using System.IO;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
     6
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
     7
public sealed class Lexer
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
     8
{
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
     9
    private readonly IList<object> listLexemes;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    10
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    11
    public Lexer(TextReader inputFile)
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    12
    {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    13
        listLexemes = new List<object>();
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    14
        ReadProgramFile(inputFile);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    15
    }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    16
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    17
    public IList<object> GetLexemes
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    18
    {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    19
        get 
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    20
        { 
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    21
            return listLexemes; 
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    22
        }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    23
    }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    24
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    25
    public enum Tokens
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    26
    {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    27
        Add,
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    28
        Subtract,
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    29
        Multiply,
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    30
        Divide,
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    31
        Terminator,
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    32
        EqualTo,
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    33
        LessThan,
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    34
        GreaterThan,
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    35
        Comma,
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    36
        OpenParen,
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    37
        CloseParen,
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    38
        OpenBrac,
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    39
        CloseBrac
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    40
    }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    41
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    42
    private void ReadProgramFile(System.IO.TextReader programFile)
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    43
    {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    44
        while (programFile.Peek() != -1)
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    45
        {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    46
            char symbol = (char)programFile.Peek();
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    47
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    48
            if (char.IsWhiteSpace(symbol))
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    49
            {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    50
                // if the symbol is whitespace then move to next symbol
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    51
                programFile.Read();
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    52
            }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    53
            else if (char.IsLetter(symbol) || symbol == '_')
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    54
            {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    55
                //identify the tokens
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    56
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    57
                StringBuilder token = new StringBuilder();
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    58
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    59
                while (char.IsLetter(symbol) || symbol == '_')
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    60
                {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    61
                    token.Append(symbol);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    62
                    programFile.Read();
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    63
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    64
                    if (programFile.Peek() == -1)
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    65
                    {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    66
                        break;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    67
                    }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    68
                    else
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    69
                    {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    70
                        symbol = (char)programFile.Peek();
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    71
                    }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    72
                }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    73
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    74
                listLexemes.Add(token.ToString());
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    75
            }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    76
            else if (symbol == '"')
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    77
            {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    78
                // string literal
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    79
                StringBuilder stringLiteral = new StringBuilder();
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    80
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    81
                programFile.Read(); // skip the '"'
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    82
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    83
                if (programFile.Peek() == -1)
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    84
                {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    85
                    throw new Exception("String literal is not terminated");
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    86
                }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    87
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    88
                while ((symbol = (char)programFile.Peek()) != '"')
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    89
                {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    90
                    stringLiteral.Append(symbol);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    91
                    programFile.Read();
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    92
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    93
                    if (programFile.Peek() == -1)
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    94
                    {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    95
                        throw new Exception("String literal is not terminated");
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    96
                    }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    97
                }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    98
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
    99
                // skip the terminating "
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   100
                programFile.Read();
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   101
                listLexemes.Add(stringLiteral);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   102
            }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   103
            else if (char.IsDigit(symbol))
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   104
            {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   105
                // numeric literal
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   106
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   107
                StringBuilder numericLiteral = new StringBuilder();
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   108
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   109
                while (char.IsDigit(symbol))
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   110
                {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   111
                    numericLiteral.Append(symbol);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   112
                    programFile.Read();
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   113
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   114
                    if (programFile.Peek() == -1)
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   115
                    {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   116
                        break;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   117
                    }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   118
                    else
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   119
                    {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   120
                        symbol = (char)programFile.Peek();
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   121
                    }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   122
                }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   123
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   124
                listLexemes.Add(int.Parse(numericLiteral.ToString()));
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   125
            }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   126
            else switch (symbol)
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   127
            {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   128
                case '(':
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   129
                    programFile.Read();
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   130
                    listLexemes.Add(Tokens.OpenParen);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   131
                    break;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   132
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   133
                case ')':
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   134
                    programFile.Read();
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   135
                    listLexemes.Add(Tokens.CloseParen);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   136
                    break;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   137
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   138
                case '=':
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   139
                    programFile.Read();
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   140
                    listLexemes.Add(Tokens.EqualTo);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   141
                    break;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   142
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   143
                case ';':
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   144
                    programFile.Read();
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   145
                    listLexemes.Add(Tokens.Terminator);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   146
                    break;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   147
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   148
                case '<':
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   149
                    programFile.Read();
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   150
                    listLexemes.Add(Tokens.LessThan);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   151
                    break;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   152
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   153
                case '>':
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   154
                    programFile.Read();
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   155
                    listLexemes.Add(Tokens.GreaterThan);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   156
                    break;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   157
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   158
                case ',':
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   159
                    programFile.Read();
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   160
                    listLexemes.Add(Tokens.Comma);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   161
                    break;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   162
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   163
                case '/':
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   164
                    programFile.Read();
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   165
                    listLexemes.Add(Tokens.Divide);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   166
                    break;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   167
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   168
                case '*':
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   169
                    programFile.Read();
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   170
                    listLexemes.Add(Tokens.Multiply);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   171
                    break;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   172
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   173
                case '-':
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   174
                    programFile.Read();
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   175
                    listLexemes.Add(Tokens.Subtract);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   176
                    break;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   177
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   178
                case '+':
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   179
                    programFile.Read();
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   180
                    listLexemes.Add(Tokens.Add);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   181
                    break;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   182
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   183
                case '{':
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   184
                    programFile.Read();
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   185
                    listLexemes.Add(Tokens.OpenBrac);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   186
                    break;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   187
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   188
                case '}':
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   189
                    programFile.Read();
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   190
                    listLexemes.Add(Tokens.CloseBrac);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   191
                    break;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   192
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   193
                default:
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   194
                    ExceptionHandler("unidentified symbol '" + symbol + "'");
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   195
                    break;
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   196
            }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   197
        }    
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   198
    }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   199
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   200
    public void ExceptionHandler(string strException)
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   201
    {
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   202
        throw new Exception(strException);
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   203
    }
7545b1bc1514 msc project
Fahad Ausaf <fahad.ausaf@kcl.ac.uk>
parents:
diff changeset
   204
}