C++ 基本词法分析器,读取文本文件作为输入,并写入所有;代币;逐行复制到输出文件

C++ 基本词法分析器,读取文本文件作为输入,并写入所有;代币;逐行复制到输出文件,c++,parsing,C++,Parsing,好的,我就快到了,我已经制作了一个输入文件,它接受任何写入它的内容,然后标记它并显示在屏幕上。当显示此输出标记0033F8C8时,生成一个输出文件。我认为它是数组地址。现在,我需要做的就是将屏幕上显示的输出存储到输出文本文件中,然后在屏幕上打印输出文件,查看您的输出文件,您的输入被标记化 代码编辑3 writing a very basic lexical analyzer in standard C++ (std usage is allowed) that reads a

好的,我就快到了,我已经制作了一个输入文件,它接受任何写入它的内容,然后标记它并显示在屏幕上。当显示此输出标记0033F8C8时,生成一个输出文件。我认为它是数组地址。现在,我需要做的就是将屏幕上显示的输出存储到输出文本文件中,然后在屏幕上打印输出文件,查看您的输出文件,您的输入被标记化

代码编辑3

     writing a very basic lexical analyzer in standard
    C++ (std usage is allowed) that reads a text file
    as input and writes all the "tokens" to an output
    file line by line
    ● All items that are separated by white space are
    tokens
    ● White space includes
    ● Space(s)
    ● Newlines
    ● Tabs

    Input file having this

        if (x > 50)
        x=0

    Output file should be like this

        if

        (

        x

        >

        50

        )

        x

        =

        0



Edit 3
\define\u CRT\u SECURE\u NO\u警告
#包括
#包括
使用std::ifstream;
#包括
使用名称空间std;
每行最大字符数=512;
const int MAX_TOKENS_PER_LINE=20;
常量字符*常量分隔符=”;
int main()
{
//创建文件读取对象
河流充填;
infile.open(“input.txt”);//打开文件
如果(!infle.good())
返回1;//如果找不到文件,则退出
出流孔的直径;
outfile.open(“output.txt”);
如果(!outfile)
{

因为你已经把你的问题标记为C++,所以我建议使用C++语言的特性,因为它们比C.</P>更安全(更少的缺陷注入)。 例如,将
char*
替换为
std::string
std::string
比C样式的字符串函数(
str*()
)具有更多的词法分析或搜索功能

对于你的项目,我不建议使用正则表达式。让它们正常工作可能比实现一个简单的lexer花费你更长的时间

编辑1
要获得更具体的帮助,请使用您正在解析的语言的语法规则编辑您的问题

一种简单的技术是从符号中分离“单词”或标识符

也请阅读

你的问题太复杂(从全局来看),无法在StackOverflow中回答

编辑2-示例

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <fstream>
using std::ifstream;

#include <cstring>
using namespace std;

const int MAX_CHARS_PER_LINE = 512;
const int MAX_TOKENS_PER_LINE = 20;
const char* const DELIMITER = " ";

int main()
{
    // create a file-reading object
    ifstream infile;
    infile.open("input.txt"); // open a file
    if (!infile.good())
        return 1; // exit if file not found

    ofstream outfile;
    outfile.open("output.txt");
    if (!outfile)
    {
        cout << "error opening fiLe";
    }

    // read each line of the file
    while (!infile.eof())
    {
        // read an entire line into memory
        char buf[MAX_CHARS_PER_LINE];
        infile.getline(buf, MAX_CHARS_PER_LINE);

        // parse the line into blank-delimited tokens
        int n = 0; // a for-loop index

        // array to store memory addresses of the tokens in buf
        const char* token[MAX_TOKENS_PER_LINE] = {}; // initialize to 0

        // parse the line
        token[0] = strtok(buf, DELIMITER); // first token
        if (token[0]) // zero if line is blank
        {
            for (n = 1; n < MAX_TOKENS_PER_LINE; n++)
            {
                token[n] = strtok(0, DELIMITER); // subsequent tokens
                if (!token[n]) break; // no more tokens
            }
        }

        // process (print) the tokens
        for (int i = 0; i < n; i++) // n = #of tokens
            cout << "Token[" << i << "] = " << token[i] << endl;
        cout << endl;



        outfile<< "Output tokens" <<token;
        infile.close();
        outfile.close();


    }


    system("pause");
    return 0;
}
#包括
#包括
#包括
#包括
内部主(空)
{
std::ifstream输入(“input.txt”);
如果(!输入)
{

std::cerr添加一些代码,这样我们就可以帮助您解决问题。使用
strtok
regular expression
@KirChou:
strtok
是邪恶的,它修改传递给它的C样式字符串。非常感谢@thomasatthews;我在输入文件中编写的任何内容都可以通过转换为令牌来简化,除了告诉它是一个运算符、标识符或其他任何内容。我想链接一个将打开的文件,将空白制表符和空格转换为标记,然后将新输出保存在其他文本文件中。如果您认为我的答案有帮助,请单击复选标记。
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>

int main(void)
{
  std::ifstream input("input.txt");
  if (!input)
  {
    std::cerr << "Error opening \"input.txt\".\r\n";
    return 1;
  }

  // Read the file.
  std::string input_text;
  while (std::getline(input, input_text))
  {
    static const char white_space[] = " \t";
    static const char identifier_letters[] = "abcdefghijklmnopqrstuvwxyz";
    std::string::size_type position = 0;

    if (input_text.length() == 0)
    {
      continue; // Empty line
    }

    // Skip white space.
    position = input_text.find_first_not_of(white_space, 0);

    if ((position == std::string::npos)            
    {
      continue; // Blank line.
    }

    // Check if the first (non-whitespace character) is a
    //    valid identifier letter
    if (std::isalpha(input_text[position]))
    {
       // First character is a letter.
       // Find the end of the identifier.
       std::string::size_type identifier_end_posn = 0;
       identifier_end_posn = input_text.find_first_not_of(identifier_letters, position);
       if (identifier_end_posn != std::string::npos)
       {
         const int identifier_length = identifier_end_posn - position + 1;
         std::string identifier = input_text.substr(position, identifier_length);
         cout << identifier << "\n";
         continue;
       }
    }
  }
  return 0;
}