我正在嘗試使用lax and yacc
編寫一個編譯器。我首先定義了令牌和語法樹並添加了任何關聯的動作,但是當我編譯它時,會出現一些錯誤。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
。這還不夠嗎?
string.h中沒有'string()'函數。你想做什麼?你用C++混淆了C嗎? –
你發現了這個問題,我在這裏混淆了c&C++!當使用C++編譯器時,它工作正常。 – yassin
很高興我能幫忙:) –