0
votes

I have a config file as shown below.(tmp.conf)

[ main ]
e_type=0x1B

My yacc file (test.y) as follows

%%
config_file_sections
  : main 
  ;

main
  : main_attribute_list 
  {
    e_parse_debug_log_message(E_DEBUG_AT,"Found main_section\n");
    e_parse_found_main_section_complete();
  }
  ;
main_attribute_list
  : T_E_TYPE number
  {
    e_parse_debug_log_message(E_DEBUG_AT,"Found main section token T_E_TYPE:\n");
  }
  ;
number
    : NUMBER { $$ = $1; }
    ;
%%

If I pass the file to program it is hanging without any output.My question is the grammar given above to parse the config file shown above.

1

1 Answers

2
votes

This won't do at all. It doesn't even parse the first character correctly, but after that it only allows one section and one name-value pair. You need something more like this:

%%
config_file
    : /* empty */
    | config_file config_file_section
    ;

config_file_section
    : '[' identifier ']' attribute_list
    ; 

attribute_list
    : /* empty */
    | attribute-list attribute-pair
    ;

attribute-pair
    : identifier '=' number
    ;

identifier
    : IDENTIFIER
    ;

number
    : NUMBER
    ;
%%

Semantic actions are left as an exercise for the reader.