我正在尝试使用lax and 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
我认为这个错误意味着没有定义字符串库,但是我已经在头中包含了
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();
的定义或声明,请检查它。