饭来 2019-04-29 17:49 采纳率: 100%
浏览 391
已采纳

如何在这个代码片中编写tokens?

// a skeleton implementation of a tokeniser

#include "tokeniser.h"
#include <iostream>
#include <ctype.h>

// to shorten the code
using namespace std ;

////////////////////////////////////////////////////////////////////////

namespace Assignment_Tokeniser
{

    // is the token of the given kind or does it belong to the given grouping?
    bool token_is_in(Token token,TokenKind kind_or_grouping)
    {
        TokenKind kind = token_kind(token) ;

        // check identity first
        if ( kind == kind_or_grouping ) return true ;

        switch(kind_or_grouping)
        {
        default:
            return false ;
        }
    }

    // the current input character, initiliased to ' ' which we ignore
    // it is an int so that the EOF marker is not confused with a legal character
    static int ch = ' ' ;

    // the current line number and column, initialised to line 1 column 0
    static int line_num = 1 ;
    static int column_num = 0 ;

    // the line number and column for the first character in the current token
    static int start_line = 0 ;
    static int start_column = 0 ;

    // generate a context string for the given token
    // it shows the line before the token,
    // the line containing the token, and
    // a line with a ^ marking the token's position
    // tab stops are every 8 characters
    // in the context string, tabs are replaced by spaces (1 to 8)
    // so that the next character starts on an 8 character boundary
    string token_context(Token token)
    {
        return "" ;
    }

    // read next character if not at the end of input
    // and update the line and column numbers
    static void nextch()
    {
        extern int read_char() ;

        if ( ch == EOF ) return ;

        if ( ch == '\n' )           // if last ch was newline ...
        {
            line_num++ ;            // increment line number
            column_num = 0 ;        // reset column number
        }

        ch = getchar() ;            // read the next character from stdin
        column_num++ ;              // increment the column number
    }

    ////////////////////////////////////////////////////////////////////////

    // called when we find end of input or we have a bad token
    Token parse_eoi()
    {
        // simulate end of input in case this is handling a bad token rather than a real end of input
        ch = EOF ;

        // return an eoi token
        return new_token(tk_eoi,"",start_line,start_column) ;
    }

    // return the next token object by reading more of the input
    Token next_token()
    {
        // you must read input using the nextch() function
        // the last character read is in the static variable ch
        // always read one character past the end of the token being returned

        // this loop reads one character at a time until it reaches end of input
        while ( ch != EOF )
        {
            start_line = line_num ;                 // remember current position in case we find a token
            start_column = column_num ;

            switch(ch)                              // ch is always the next char to read
            {
            case ' ':                               // ignore space, tab, CR and LF
            case '\t':
            case '\r':
            case '\n':
                nextch() ;                          // read one more character and try again
                break ;
                                                    // add additional case labels here for characters that can start tokens
                                                    // call a parse_* function to complete and return each kind of token
            default:
                return parse_eoi() ;                // the next character cannot start a token, return an EOI token
            }
        }

        start_line = line_num ;                     // remember current position so EOI token is correct
        start_column = column_num ;

        return parse_eoi() ;                         // return an EOI token
    }
}

在这个代码片中,添加 indentifier,integer的token
要求:
1.所有输入都必须用nextch()函数。
2.如果达到输入结束,则return tk_eoi()

3.如果发现一个字符不能作为token的一部分,或者不是space" "、tab“\t",carriage return“\r”或newline“\n”,则return token tk_eoi()

4.所有token必须是输入中的连续字符

5.搜索下一个token的开始时,所有space、tab,carriage return和newline都将被忽略。

  • 写回答

1条回答 默认 最新

  • threenewbee 2019-04-29 21:47
    关注

    你这个应该是词法解析程序。
    不知道按照什么语言的语法来。假设按照C语言来说
    indentifier的规则是:
    下划线、字母开头,后面跟字母、数字、下划线
    integer的规则是
    任意数字开头,如果是0开头,下一个字符可以是x或者X,如果是0x或者0X开头,后面可以是0-9a-fA-F,否则是0-9

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败