2014-11-26 15 views
0

Google fu가 약해서 도움이 필요합니다.YACC : 다른 토큰이 하나 이상있는 토큰 (반복)

Lex/Yacc로 시작 했으므로 지금까지는 컨셉 증명에 이상이 있습니다. 반복 및 선택적 토큰이 필요합니다.

언급 한 바와 같이
instance 0 { 
    application 0 { 
     id 0 { 
      server 10.143.23.240 
      server backup 10.132.34.240 
     } 
     id 24 { 
      server 10.143.23.240 
     } 
    } 
} 

는 렉스가 괜찮 :

나는 다음과 같은 내용이 텍스트 파일이

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

%} 

%% 

\{                return OBRACE; 
\}                return CBRACE; 

instance              return INSTANCE; 
id[0-9]+              return ID; 
     application            return APPLICATION; 
       id            return ID; 
         server         return SERVER; 
         backup         return backup; 
         [0-9]+\.[0-9]+\.[0-9]+\.[0-9]+   { yylval.string = strdup(yytext); 
                     return SERVER_IPADDRESS; } 
[0-9]+               { yylval.string = strdup(yytext); 
                     return NUMBER; } 
[ \t\n]+              /* ignore whitespace */; 
.                printf("%s\n", "Incorrect syntax"); 
%% 

다음과 제공하는 단순화 된대로 문법/Yacc에이다 문맥을 나타내지 않으며 반복 할 수 없습니다.

commands: /* empty */ 
     | commands command 
     ; 

command: 
     server 
     ; 

server: 
     SERVER SERVER_IPADDRESS 
     { 
       char server_ip_from_yyval[257] = { '\0' }; 

       if(NULL == $2){ 
         printf("%s\n", "SERVER_IPADDRESS is NULL"); 
       } 
       else{ 
         strncpy(server_ip_from_yyval, $2, 256); 
         set_server_ip(server_ip_from_yyval); 
       } 
     } 
     ; 
%% 
$ 
$ 

그래서 내 차는 llenges are :

) 1.) "id"를 텍스트 파일이나 ID의 Lex 파일에 허용하는 옵션은 한 번 이상 표시 될 수 있습니까? 2.) 텍스트 파일의 "서버 백업"을 선택 가능하게하려면?

나는 (몇 아래)하지만 정답을 찾기 위해 고군분투 일부 참조 살펴 보았다 :

This switch statement is working with one Case how to make it multiple case Bison yacc - How to do a if condition Bison: how to fix reduce/reduce conflict

어떤 도움을 크게 감상 할 수있다. :)

감사합니다. 당신의 중첩 규칙이 무엇인지에 따라이 같은

답변

0

뭔가 :

commands 
    : instances 
    ; 

instances 
    : instance /* or empty if there can be zero instances */ 
    | instances instance 
    ; 

instance 
    : INSTANCE ids '{' applications '}' 
    ; 

applications 
    : application /* or empty if there can be zero applications */ 
    | applications application 
    ; 

application 
    : APPLICATION ids '{' servers '}' 
    ; 

servers 
    : server /* or empty if there cabn be zero servers */ 
    | servers server 
    ; 

server 
    : SERVER backup ids SERVER_IPADDRESS 
    { 
     // etc. as you have now 
    } 
    ; 

ids 
    : ID /* You probably don't want to allow zero IDs */ 
    | ids ID 
    ; 

backup 
    : /* empty */ 
    | BACKUP 
    ; 

당신에게 일반적인 아이디어를 줄 것이다. 다만이 같은 최종 규칙이 있습니다 : 당신은 '{'와 '}'의 렉스 규칙이 필요하지 않습니다

모든 특수 문자를 포함
. return yytext[0]; 

. 이 기술을 사용하면 잘못된 문자가 구문 분석기로 처리되는 파서로 반환됩니다.