0
votes

I have the grammatical:

%token T_SHARE
%token T_COMMENT T_PUBLIC T_WRITEABLE T_PATH T_GUESTOK T_VALID_USERS
       T_WRITE_LIST T_CREATE_MODE T_DIRECTORY_MODE
%union
{
    int number;
    char *string;
}

%token <string> T_STRING
%token <number> T_NUMBER T_STATE

%%

parameters:
   |parameters parameter
       ;
parameter:
   section_share
       |comment
....
section_share:
       '[' T_SHARE ']' {section_print(T_SHARE);}
   ;
comment:
   T_COMMENT '=' T_STRING {print(2);parameter_print(T_COMMENT);}
       ;

the function print is: void print(int arg) { printf("%d\n", arg); }

but it prints the argument `2' of print to other values that like "8508438", without rule. why?

1
Your problem is not clear enough for anybody to provide you with a relevant answer. You need to provide more details. Also, have a look at Bison's debug mode.akim

1 Answers

0
votes

It's very hard to understand what you are trying to ask, but I think you are confusing tokens' numeric codes with their semantic values. In particular, there is nothing special about the print(2) call in the action associated with your 'comment' rule. It is copied literally to the generated parser, so, given the definition of the print() function, a literal '2' should be printed each time that rule fires. I think that's what you say you observe.

If instead you want to print the semantic value associated with a symbol in the rule, then the syntax has the form $n, where the number after the dollar sign is the number of the wanted symbol in the rule, counting from 1. Thus, in the 'comment' rule, the semantic value associated with the T_STRING symbol can be referenced as $3. For example:

comment:
    T_COMMENT '=' T_STRING { printf("The string is %s\n", $3); }
;

Semantic values of primitive tokens must be set by your lexical analyzer to be available; semantic values of non-terminals must be set by actions in your grammar. Note also that mid-rule actions get included in the count.

Although token symbols such as your T_COMMENT can be used directly in actions, it is not typically useful to do so. These symbols will be resolved by the C preprocessor to numbers characteristic of the specific symbol. The resulting token codes have nothing to do with the specific values parsed.