2014-01-25 36 views
0

我正在嘗試使用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。這還不夠嗎?

+1

string.h中沒有'string()'函數。你想做什麼?你用C++混淆了C嗎? –

+0

你發現了這個問題,我在這裏混淆了c&C++!當使用C++編譯器時,它工作正常。 – yassin

+0

很高興我能幫忙:) –

回答

1

您需要包括:

#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();沒有定義或聲明,請檢查它。

1

string.h包含char數組的字符串函數。字符串類型包含在string文件中。使用#include <string>

相關問題