lexer.h

#ifndef LEXER_H
#define LEXER_H
 
#include <string>
#include <fstream>
using namespace std;
 
enum TokenKind { ident, number, text, separator, eos };
 
class Lexer
{
private:
    ifstream f;
    char ch;
    void nextChar ();
 
public:
    TokenKind kind;
    string token;
    void nextToken ();
    void error (string msg);
 
public:
    Lexer (string fileName);
    ~ Lexer ();
};
 
#endif

lexer.cpp

#include "lexer.h"
 
#include <iostream>
using namespace std;
 
const char zero = 0;
 
Lexer::Lexer (string fileName)
{
    f.open (fileName);
    if (!f.good ())
        error ("Cannot open file: " + fileName);
 
    ch = zero;
    nextChar ();
    nextToken ();
}
 
Lexer::~Lexer ()
{
    if (f.good ())
        f.close ();
}
 
void Lexer::error (string msg)
{
    cerr << "Error: " << msg << ", token=" << token << endl;
    exit (1);
}
 
void Lexer::nextChar ()
{
    f >> ch;
    if (!f.good ())
        ch = zero;
}
 
void Lexer::nextToken ()
{
    token = "";
    while (ch != zero && ch <= ' ')
        nextChar ();
 
    if (ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z')
    {
        kind = ident;
        while (ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z')
        {
            token = token + ch;
            nextChar ();
        }
    }
    else if (ch >= '0' && ch <= '9')
    {
        kind = number;
        while (ch >= '0' && ch <= '9')
        {
            token = token + ch;
            nextChar ();
        }
    }
    else if (ch != zero)
    {
        kind = separator;
        token = string (1, ch);
        nextChar ();
    }
    else
    {
        kind = eos;
    }
}

Hlavní program

#include "lexer.h"
 
#include <iostream>
using namespace std;
 
int main ()
{
    Lexer inp ("abc.txt");
    while (inp.kind != eos)
    {
        cout << inp.token << endl;
        inp.nextToken ();
    }
    cout << "O.K.";
}

Soubor abc.txt se vstupními daty

{
   [ 1, 2, 3, 4 ],
   [ 10, 20, 30, 40 ],
   [ 100, 200, 300, 400 ]
}
 
zpro/lexer.txt · Last modified: 2020/12/02 17:21 by 88.103.111.44
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki