need help with revised lexing algorithim

closed account (Dy7SLyTq)
so i rewrote my lexical analyzer so that its based off stroustrops calculator rather than using regex's. the only problem is trying to handle multi character operators like >>=, +=, and --. I can fix it, but my solution will just repeat a lot of code and i would rather not do that. thats why im trying to let switch cases fall through

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
void Lexer::Lex()
{
    istringstream Stream(this->Source);
    char Current = 0,
         Temp    = 0;

    while(Stream)
    {
        do
        {
            if(!Stream.get(Current)) return;
        } while(isspace(Current));

        switch(Current)
        {
            //this starts testing for operators
            case '<': case '>': case '*':
                Stream.get(Temp);

                if((Current + Temp + Stream.peek()) == Current + Temp + '=')
                {
                    SymbolTable.PushBack("OPERTATOR", Current + Current + "=", -1, -1);
                    Stream.putback(Temp);
                    break;
                }

                Stream.putback(Temp);

	        case '/': case '+': case '-':
                if(Current == Stream.peek())
                {
                    SymbolTable.PushBack("OPERTATOR", this->ChToS(Current) + this->ChToS(Current), -1, -1);
                    break;
                }

	        case '=': case '!': case '%': case '^': case '&': case '|':
                if(Stream.peek() == '=')
                {
                    SymbolTable.PushBack("OPERATOR", Current + "=", -1, -1);
                    break;
                }

            case ';': case '(': case ')': case ',': case '[': case ']': case '{': case '}':
                SymbolTable.PushBack("OPERATOR", this->ChToS(Current), -1, -1);
                break;
                //this ends it.

            case '0': case '1': case '2': case '3': case '4':
	        case '5': case '6': case '7': case '8': case '9':
            case '.':
            {
                string NumberValue;

                NumberValue = Current;
        		while(Stream.get(Current) && isdigit(Current)) NumberValue.push_back(Current);
  
              Stream.putback(Current);
                SymbolTable.PushBack("NUMBER", NumberValue, -1, -1);
                break;
            }

            case '\'': case '\"':
            {
                char Match = Current;
                string QuoteValue;
                QuoteValue = Current;

                while(Stream.get(Current) && Current != Match) QuoteValue.push_back(Current);

                QuoteValue.push_back(Current);
                SymbolTable.PushBack("STRING", QuoteValue, -1, -1);
            }

	        default:
            {

                string StringValue;

                if(isalpha(Current))
	            {
		            StringValue = Current;

        		    while(Stream.get(Current) && isalnum(Current)) StringValue.push_back(Current);

            		Stream.putback(Current);

                    if(this->IsKeyword(StringValue)) SymbolTable.PushBack("KEYWORD",    StringValue, -1, -1);
                    else                             SymbolTable.PushBack("IDENTIFIER", StringValue, -1, -1);
	            }
            }
        }
    }
}


so in case you cant tell what im doing... I first test Current if its going to be a 3 character (which comprises of >>=, <<=, **=). if it is it breaks. otherwise it should fall through and test for dual character operators that are themselves + themselves (ie ++, --, //, **). then it tests for Current + = (ie +=, -=, ==, >=). and finally failing all of those it just logs it into the symbol table as an operator containing itself
Topic archived. No new replies allowed.