饭来 2020-05-07 09:36 采纳率: 100%
浏览 95
已采纳

语法编译器中的nextch()怎么写?



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

// to shorten the code
using namespace std ;



namespace Tokeniser
{
    // parse end of input - report we have reached end of input
    // simulate end of input to prevent further attempts to read input
    static Token parse_eoi()
    {
        // if called after an error we may not be at the end of input so simulate it
        ch = EOF ;

        // return a new Token object
        return new_token(tk_eoi,"") ;
    }

    // parse a single character that cannot start any other kind of token
    static Token parse_byte()
    {
        // create a string from the current value of ch
        string spelling(1,ch) ;

        // always read the next character - we have read past the end of the token
        nextch() ;

        // return a new Token object
        return new_token(tk_byte,spelling) ;
    }


    // ******   ADD NEW CODE HERE  ******
    // add your own parse_*() functions here ...


    // return the next Token object by reading more of the input
    // this function matches the next character, ch, against the start of one of the grammar rules
    // it then calls the appropriate parse_*() function to complete the Token object
    // you can use ranges such as case '0'...'9' to match characters from '0' to '9'
    // you cannot match multiple characters at once such as case '<=', this will not match any legal input character
    Token next_token()
    {
        switch(ch)
        {
                        // ******   ADD NEW CODE HERE  ******
                        // add case labels and calls for the each token kind here ...


                        // end of input
        case EOF:       return parse_eoi() ;

                        // a single character that cannot start any other kind of token
        default:        return parse_byte() ;
        }
    }
}
#include <string>

// Interface for the workshop tokeniser
namespace Tokeniser
{
    // shorthand name for the string type
    typedef std::string string ;

    // the ID used to reference a Token - the implementation is hidden
    typedef int Token ;

    // The kinds of token that are recognised?
    enum TokenKind
    {
        // *** BNF syntax for tokens
        // * literals inside ' '
        // * grouping ( )
        // * ASCII ranges -
        // * alternatives |
        // * 0 or 1 ?
        // * 0 or more *
        // * 1 or more +
        //

        //  Tokeniser Tokens
        // Grammar rule:    Definition
        // input:           token* eoi
        // token:           byte | stage1 | stage2 | stage3 | stage4 | stage5
        // byte:            any single character that cannot start another token
        // stage1:          ?? see below ??
        // stage2:          ?? see below ??
        // stage3:          ?? see below ??
        // stage4:          ?? see below ??
        // stage5:          ?? see below ??
        // eoi:             the end of the input or any unexpected character
        //
        // * all input after returning an eoi token is ignored
        // * when parsing a token, if the token cannot be completed return the eoi token
        // * the web submission system will reveal the grammar rule(s) for each stage
        //   once any test of the previous stage has been passed
        //   ie the stage1 rules will be revealed by the first successful web submission of the start up files
        //      the stage2 rules will be revealed once any of the stage1 tests is passed,
        //      the stage3 rules will be revealed once any of the stage3 tests is passed, and so on

        tk_eoi,             // end of input reached or an unexpected character was found
        tk_byte,            // any single byte character that cannot start another token

                            // details of the following token kinds will be revealed by the web submission system
                            // this will include the grammar rules and, if required, details of any spelling changes to make
                            // not all of these token kinds may be used
        tk_number,          // some form of number
        tk_identifier,      // some form of identifier
        tk_operator,        // some form of operator
        tk_comment,         // some form of comment
        tk_string,          // some form of string
        tk_arbitrary,       // some form of arbitrary token
        tk_weird,           // some form of arbitrary token

        tk_last             // ignore this
    } ;

    // ***** the following are implemented in library.cpp *****

    // create a new Token object and return its ID
    extern Token new_token(TokenKind kind,string spelling) ;

    // return the TokenKind for the given token
    extern TokenKind token_kind(Token token) ;

    // return the characters that make up the given token
    extern string token_spelling(Token token) ;

    // return a string representation of the given token
    extern string token_to_string(Token token) ;

    // the current input character, initiliased by first call of nextch()
    // it is type int rather than char so it does not get confused with character code 255
    extern int ch ;

    // read next character if not at the end of input
    extern void nextch() ;

    // ***** the practical involves implementing the following function by completing tokeniser.cpp *****

    // read the next token from standard input
    extern Token next_token() ;
}

#endif 

  • 写回答

1条回答 默认 最新

  • dabocaiqq 2020-09-25 16:59
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)