Lexical Analysis/getToken() Question: Simplified

This question resides within a lexical analysis program:
I've been working on a member function Token getToken(): each call to this function is supposed to return the next token available from the source stream.

Does my attempt at the function on the right track? (am unable to build/run/debug)

Here is my crack at it below, :

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
 Token Lexer::getToken() {
      static char ch = '';
      lexeme.clear();
    
      while(isspace(ch)) 
      {
         ch = getChar();
      }
      //ch is a nonspace, starts n token or a comment
      if (isalpha(ch) || ch=='_')
      {
        //id a keyword
        lexeme += ch;
        while(isalpha(ch) || ch=='_')
        {
            ch = getChar();
            if(isalpha(ch) || ch=='_')
            {
                lexeme += ch;
            }
            else
            {
                
            }
        }
        return;
       }
    
       if(isdigit(ch))
       {
          //while is digit
        
          return Token::NUM;
      }
        switch(ch)
        {
         case',': //String
         {    
            return Token::COMMA;
            ch = getChar();
         }
        case ';':
        {
            return Token::SEMICOLON;
            ch = getChar();
        }
        case ':':
        {
            return Token::COLON;
            ch =ljl;
        }
        case '>': // GT, GE, EXTRACT
        {
            lexeme += ch;
            ch = getChar();
            if(ch != '>' && ch != '=')
            {
                //ch must always be first char before return
                Token t = ch; //lexeme t = ch;
                ch = getChar();
                if(lexeme.back() = '>')
                {
                    return Token:: EXTRACT;
                }
                else
                { //greater than equal
                    return Token:: GE; 
                }
            }  
        }
        case '/': //could be comment // or /*
        {
            lexeme += ch;
            ch = getChar();
            if(ch != '/' && ch != '*')
            {
                
            }
        }
        case EOF:
        {
               
          }
          default:
          {
            
          }
      }
 }

Does this seem right?
Line 26 needs to return the token type

Line 56 looks wrong to me. If the next char IS > or = then you need to do something special. And you need to add an else case to this if to return Token::GT.
Topic archived. No new replies allowed.