gpt4 book ai didi

c - Yacc 文件中字符串库的使用

转载 作者:太空宇宙 更新时间:2023-11-04 08:47:06 24 4
gpt4 key购买 nike

我正在尝试使用 lax 和 yacc 编写一个编译器。我首先通过添加任何关联的操作来定义标记和语法树,但是当我编译它时,我遇到了一些错误。

词法分析器:

%{
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "y.tab.h"
%}

%%

" " {};
"is" {};
[A-Z]+ { return LAYER;}
[0-9]+\.*[0*9]* {return NUMBER;}
"minimum width" {return MIN_WIDTH;}
"maximum width" {return MAX_WIDTH;}
"minimum length" {return MIN_LENGTH;}
"maximum length" {return MAX_LENGTH;}
"minimum space to" {return MIN_SPACE_TO;}
"minimum space" {return MIN_SPACE;}
"with minimum width" {return MIN_W_FILTER;}
"with minimum span" {return MIN_SPAN_FILTER;}

%%

解析器:

%{
#include <stdio.h>
#include <string.h>

extern FILE * file_in;
%}

%start rule

%token <strval> LAYER NUMBER MIN_WIDTH MAX_WIDTH MIN_LENGTH MAX_LENGTH MIN_SPACE MIN_SPACE_TO MIN_W_FILTER MIN_SPAN_FILTER
%%

rule:
LAYER operation NUMBER {}
|
derived_layer operation NUMBER {}
;


derived_layer:
'(' LAYER filter NUMBER ')' {}
|
'(' LAYER operation LAYER ')' {}
;

operation:
MIN_WIDTH {}
|
MAX_WIDTH {}
|
MIN_LENGTH {}
|
MAX_LENGTH {}
|
MIN_SPACE_TO {}
;

filter:
MIN_W_FILTER {}
|
MIN_SPAN_FILTER {}
;

%%

int parsefile (const char * FileName)
{

FILE * fileIn;
string strFileName = string(FileName);
fileIn = fopen(strFileName.c_str(),"r");
if(!fileIn)
{
Std::cout << “error while opening file\n”;
}
else
{
file_in = fileIn;
flag = cdfparse();

if(fileIn)
{
fclose(fileIn);
}
}
}

我收到以下错误:

drc.y: In function ‘parsefile’:
drc.y:53: error: ‘string’ undeclared (first use in this function)
drc.y:53: error: (Each undeclared identifier is reported only once
drc.y:53: error: for each function it appears in.)
drc.y:53: error: expected ‘;’ before ‘strFileName’
drc.y:54: error: ‘strFileName’ undeclared (first use in this function)
drc.y:57: error: expected expression before ‘:’ token
drc.y:57: error: stray ‘\342’ in program
drc.y:57: error: stray ‘\200’ in program
drc.y:57: error: stray ‘\234’ in program
drc.y:57: error: stray ‘\’ in program
drc.y:57: error: stray ‘\342’ in program
drc.y:57: error: stray ‘\200’ in program
drc.y:57: error: stray ‘\235’ in program
drc.y:62: error: ‘flag’ undeclared (first use in this function

我认为该错误意味着未定义字符串库,但我已经在 header 中包含了 string.h。这还不够吗?

最佳答案

您需要包括:

#include <string>
#include <iostream>

parsefile 应该返回一些东西或者它应该是 void

这个声明:

string strFileName = string(FileName);

应该是:

std::string strFileName = FileName;

这个声明:

      Std::cout << “error while opening file\n”;

应该是(注意 std 中的 S 和引号):

      std::cout << "error while opening file\n";

没有flag = cdfparse();的定义或声明,检查一下。

关于c - Yacc 文件中字符串库的使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21348155/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com