Lexical Analyzer Help

How would I go about creating a simple lexical analyzer that takes input files and checks character by character, Identifying each token.
Also I want to use the enum() function. Does someone have an idea how to start the steps

Any tips, ideas, guides, insight, links would be greatly appreciated.
takes input files and checks character by character, Identifying each token.

Something like this:
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
#include <iostream>
#include <fstream>
#include <string>

int main()
{
    // "test.txt" presumably contains some characters in it.
    std::ifstream f("test.txt");
    
    char ch;
    while (f.get(ch))
    {
        //
        // Handle different characters from here:
        // http://www.asciitable.com/
        //
        if (ch == '\n') // Newline
        {
            std::cout << "Character: Newline\n";
        }
        else if (ch == '\t') // Tab
        {
            std::cout << "Character: Tab\n";
        }
        else
        {
            std::cout << "Character: " << ch << '\n';
        }
    }
}

Input:
This is a sentence.
This is a new sentence on the next line.


Output:
Character: T
Character: h
Character: i
Character: s
Character:
Character: i
Character: s
Character:
Character: a
Character:
Character: s
Character: e
Character: n
Character: t
Character: e
Character: n
Character: c
Character: e
Character: .
Character: Newline
Character: T
Character: h
Character: i
Character: s
Character:
Character: i
Character: s
Character:
Character: a
Character:
Character: n
Character: e
Character: w
Character:
Character: s
Character: e
Character: n
Character: t
Character: e
Character: n
Character: c
Character: e
Character:
Character: o
Character: n
Character:
Character: t
Character: h
Character: e
Character:
Character: n
Character: e
Character: x
Character: t
Character:
Character: l
Character: i
Character: n
Character: e
Character: .


Also I want to use the enum() function
No idea what that is.
If you want to learn about enums, see:
https://stackoverflow.com/questions/12183008/how-to-use-enums-in-c
http://www.stroustrup.com/C++11FAQ.html#enum
Last edited on
He probably wants to assemble the characters into tokens and identify the token category with enum labels.

 
enum { Keyword, Operator, .... };

He hasn't given enough info, though.
Topic archived. No new replies allowed.