2012-04-16 63 views
1

在Flex中,您可以使用[a-zA-Z] [a-zA-Z0-9] *來定義一個單詞。我如何定義一個句子? 以下是我的Flex代碼:Flex-BISON:如何定義一個句子?

%{ 
#include <stdio.h> 
#include <string.h> 
#include "y.tab.h" 
%} 
%% 
[0-9]+     yylval=atoi(yytext);return NUMBER; 
[a-zA-Z][a-zA-Z]*  return WORD; 
[a-zA-Z][a-zA-Z0-9]* return TERM; 
%% 

而且下面是野牛:

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

void yyerror(const char *str) 
{ 
    fprintf(stderr,"error: %s\n",str); 
} 

int yywrap() 
{ 
    return 1; 
} 

main() 
{ 
    yyparse(); 
} 

%} 

%token NUMBER WORD TERM 

commands: /* empty */ 
    | commands command 
    ; 
command: 
    rule1 
    | 
    rule2 
; 

.....

感謝

回答

1

Flex或者是法詞法分析器解決它們的輸出是usualy一個象你之前定義的符號,它可以是一個世界,一個數字或一個術語。 另一方面,yacc或bison是parseres,我們在這裏定義我們的語法(規則集),它應該驗證您將要傳遞的輸入流,這裏是一個句子。

所以現在你的問題:驗證的句子是通過我們的語法中的定義規則來驗證句子,例如:「hello world」。此規則

sentence : WORD WORD '.' 
     ; 

或這句話驗證了「我有20歲」這樣的規則必須是這樣的

sentence : words NUMBER words  /*words non terminal (list of words)*/ 
      ; 
words  : words WORD 
      | WORD 
      ; 

所以它是由你來定義規則,瞭解輸入流,你會解析。

相關問題