一个礼拜之后我终于从成都回来了, 从今天开始更新会恢复...

一点小的改进

lex()的时候距离我上一次写已经一个礼拜了, 所以我回顾了一下之前的代码, 发现还是有瑕疵. 比如考虑到一个较短的程序, 短到小于BUFFERSIZE(256), 这时其实我的程序是有错的, 因为此时buffer中的内容有一部分是未定义的... 所以为了防止这种情况我又添加了一个变量, 叫做num, 它代表的是目前buffer中实际有效的字符数.

只有几个地方进行了修改, 很简单 :

Lexer(std::ifstream& ifs):ifs(ifs), EndOfFile(false), idx(0), row(1), column(0){
    updateBuffer();
    lex();
}
    void updateBuffer(){

        // first read...
        if(num == 0 && !EndOfFile){
            ifs.read(buffer, BUFFERSIZE);
            num = ifs.gcount();
            if(ifs.eof()){
                EndOfFile = true;
            }
            return;
        }
        if(idx <= LIMITSIZE || EndOfFile){
            return;
        }

        idx -= LIMITSIZE;
        strncpy(buffer, buffer + LIMITSIZE, COPYLENGTH);
        ifs.read(buffer + COPYLENGTH, LIMITSIZE);
        num = COPYLENGTH + ifs.gcount();
        if(ifs.eof()){
            EndOfFile = true;
        }
    }
void Lexer::eatSpace(){
    char ch = 0;
   //change here!!! 
    while(idx != num && (ch = buffer[idx++])){
        updateBuffer();
        switch (ch){
            case '\n':{
                ++row;
                column = 0;
                break;
            }
            case ' ':{

            }
            case '\t':{
                ++column;
                break;
            }
            default:{
                --idx;
                return;
            }
        }
    }
}
char getNextChar(){
    updateBuffer();
    ++column;
  //change here!!! 
    if(idx == num){
        // error.
    }
    return buffer[idx++];
}

所以改动的地方都已经使用备注标出, 主要思路在于 :

  1. 增加了updateBuffer初始化buffer功能, 所以关于缓冲中有效字符数量(num), 是否达到文件末尾(EndOfFile)等变量的设置都会在这个函数, 也只会这个函数中进行.
  2. 源代码的结束只出现在eatSpace()中和每一次循环的开头, 所以在getNextChar()中出现只有一种情况, 就是源代码有错.

lex()的设计

最后的任务就是设计这个关键的函数lex(), 我个人想到的最清晰易懂的方式就是通过预读然后调用相应的识别函数的方式来进行词法解析.

我们从简单的几个入手 :

void Lexer::lex() {
    eatSpace();
    char ch;
    while(idx != num) {
        ch = getNextChar();
        switch (ch) {
            case '+':
            case '-':
            case '*':
            case '/': {
                if (getNextChar() == '=') {
                    list.pushBack(Token(Token::OPERATOR, ch + "=", row, column));
                } else {
                    backtrace();
                    list.pushBack(Token(Token::OPERATOR, ch + "", row, column));
                }
                break;
            }

            case '=':{
                if(getNextChar() == ch){
                    list.pushBack(Token(Token::OPERATOR, ch + ch + "", row, column));
                }else{
                    backtrace();
                    list.pushBack(Token(Token::OPERATOR, ch + "", row, column));
                }
                break;
            }

            case '&':
            case '|':
            case '!':
                list.pushBack(Token(Token::OPERATOR, ch + "", row, column));
                break;

            case ';':
                list.pushBack(Token(Token::SEMI, ch + "", row, column));
                break;

            case '(':
            case ')':
            case '{':
            case '}':
            case '[':
            case ']':
                list.pushBack(Token(Token::BRACKET, ch + "", row, column));
                break;

        }
        eatSpace();
    }
}

然后是字符串解析函数 :

std::string Lexer::stringParse() {
    char ch;
    std::string temp;
    bool escape = false;
    while(escape || (ch = getNextChar()) != '"'){
        if(escape){
            switch (ch){
                case 'n':
                    temp += '\n';
                    break;
                case 't':
                    temp += '\t';
                    break;
                case '"':
                    temp += '\"';
                    break;
                case '\\':
                    temp += '\\';
                    break;
                default:
                    //error
                    ;
            }
            escape = false;
            continue;
        }

        switch (ch){
            case '\\':
                escape = true;
                continue;
            default:
                temp += ch;
        }
    }
    return temp;
}

此时的lexer() ...

void Lexer::lex() {
    eatSpace();
    char ch;
    while(idx != num) {
        ch = getNextChar();
        switch (ch) {
            case '+':
            case '-':
            case '*':
            case '/': {
                if (getNextChar() == '=') {
                    list.pushBack(Token(Token::OPERATOR, ch + "=", row, column));
                } else {
                    backtrace();
                    list.pushBack(Token(Token::OPERATOR, ch + "", row, column));
                }
                break;
            }

            case '=':{
                if(getNextChar() == ch){
                    list.pushBack(Token(Token::OPERATOR, ch + ch + "", row, column));
                }else{
                    backtrace();
                    list.pushBack(Token(Token::OPERATOR, ch + "", row, column));
                }
                break;
            }

            case '&':
            case '|':
            case '!':
                list.pushBack(Token(Token::OPERATOR, ch + "", row, column));
                break;

            case ';':
                list.pushBack(Token(Token::SEMI, ch + "", row, column));
                break;

            case '(':
            case ')':
            case '{':
            case '}':
            case '[':
            case ']':
                list.pushBack(Token(Token::BRACKET, ch + "", row, column));
                break;

            case '0': {
                // int or float
                std::string temp("0");
                if (getNextChar() == '.') {
                    temp += '.';
                    while (isDigit(ch = getNextChar())) {
                        temp += ch;
                    }
                }
                backtrace();
                list.pushBack(Token(Token::INT, temp, row, column));
                break;
            }

            case '"':
                list.pushBack(Token(Token::STRING, stringParse(), row, column));

        }
        eatSpace();
    }
}

由于intfloat有两种情况, 所以这里分开设计, 先讨论了比较简单的0开头的情况.

然后把剩下的一些在switch的default中补齐.

void Lexer::lex() {
    eatSpace();
    char ch;
    while(idx != num) {
        ch = getNextChar();
        switch (ch) {
            case '+':
            case '-':
            case '*':
            case '/': {
                if (getNextChar() == '=') {
                    list.pushBack(Token(Token::OPERATOR, ch + std::string("="), row, column));
                } else {
                    backtrace();
                    list.pushBack(Token(Token::OPERATOR, ch + std::string(""), row, column));
                }
                break;
            }

            case '=':{
                if(getNextChar() == ch){
                    list.pushBack(Token(Token::OPERATOR, std::string("") + ch + ch , row, column));
                }else{
                    backtrace();
                    list.pushBack(Token(Token::OPERATOR, ch + std::string(""), row, column));
                }
                break;
            }

            case '&':
            case '|':
            case '!':
                list.pushBack(Token(Token::OPERATOR, ch + std::string(""), row, column));
                break;

            case ';':
                list.pushBack(Token(Token::SEMI, ch + std::string(""), row, column));
                break;

            case '(':
            case ')':
            case '{':
            case '}':
            case '[':
            case ']':
                list.pushBack(Token(Token::BRACKET, ch + std::string(""), row, column));
                break;

            case '0': {
                // int or float
                bool isFloat = false;
                std::string temp("0");
                if (getNextChar() == '.') {
                    isFloat = true;
                    temp += '.';
                    while (isDigit(ch = getNextChar())) {
                        temp += ch;
                    }
                }
                backtrace();
                isFloat ? list.pushBack(Token(Token::FLOAT, temp, row, column))
                        : list.pushBack(Token(Token::INT, temp, row, column));
                break;
            }

            case '"':
                list.pushBack(Token(Token::STRING, stringParse(), row, column));

            default:{
                if(isDigit(ch) && ch != 0){
                    bool isFloat = false;
                    std::string temp;
                    temp += ch;
                    while (isDigit(ch = getNextChar())) {
                        temp += ch;
                    }
                    if (getNextChar() == '.') {
                        isFloat = true;
                        temp += '.';
                        while (isDigit(ch = getNextChar())) {
                            temp += ch;
                        }
                    }
                    backtrace();
                    isFloat ? list.pushBack(Token(Token::FLOAT, temp, row, column))
                            : list.pushBack(Token(Token::INT, temp, row, column));
                }

                else if(isID(ch) && !isDigit(ch)){
                    std::string temp;
                    temp += ch;
                    while (isID(ch = getNextChar())) {
                        temp += ch;
                    }
                    backtrace();
                    list.pushBack(Token(Token::IDENTIFIER, temp, row, column));
                }

                else{
                    //error
                }
            }

        }
        eatSpace();
    }
}

然后在进行了简单的测试 :

main.cpp :

#include <iostream>
#include <fstream>
#include "Font/Lexer/Lexer.h"



int main() {
    std::ifstream ifstream("/Users/zhangzhimin/x.txt");
    Lexer lexer(ifstream);
    lexer.print();
    return 0;
}

x.txt

int main(){
    int x = 3;
    x += 3.5;
    a123b = 4.3333;
    "\n1\t2\\\"";
    > *= +++ < 
    return 0;
}

结果如下

int
main
(
)
{
int
x
=
3
x
+=
3
5
a123b
=
4
3333

1	2\"
;
*=
+
+
+
return
0
;
}

除了没有加入报错, 其他的都ok了, 我目前还不太了解C++中的异常机制, 毕竟才学了不到一个月, 其他的以后再说吧, 反正词法分析就告一段落了...

posted on 2016-08-21 23:10  内脏坏了  阅读(542)  评论(0编辑  收藏  举报