YACC Parsing Question

"Pradeep" <kalyanpradeep@gmail.com>
8 Nov 2005 23:41:09 -0500

          From comp.compilers

Related articles
YACC Parsing Question kalyanpradeep@gmail.com (Pradeep) (2005-11-08)
Re: YACC Parsing Question jatin.bhateja@amdocs.com (Jatin Bhateja) (2005-11-12)
Re: YACC Parsing Question oliverhunt@gmail.com (oliverhunt@gmail.com) (2005-11-12)
| List of all articles for this month |

From: "Pradeep" <kalyanpradeep@gmail.com>
Newsgroups: comp.compilers
Date: 8 Nov 2005 23:41:09 -0500
Organization: http://groups.google.com
Keywords: parse

Hello


Please someone help me out understand how to solve the problem
described below. Basically I am trying to parse the test program below
using LR parser and create symbol table. I am able to enter an
IDENTIFIER and its TYPE into symbol table easily. But I am having
trouble when it comes to find out if an IDENTIFIER is a function name
or not. As shown in the example below, each statement parses several
levels up and starts over again.


data_or_func_defn
                : function_definition
{
                            $$.symbolrec->symboltype = FUNCTION;
                            printf(" reduced by function_definition\n");
                            printf(" FUNCTION IS SET TO TRUE\n");
                }
                | declaration


{
                            $$.symbolrec->symboltype = VAR;
                            printf(" reduced by declaration\n");
}
{
printf(" FUNCTION IS SET TO FALSE\n");
                }
              ;


When the parser encounters Function1, it is reduced down to identifier.
When I tried to enter this symbol in symbol table, all it knows is that
it's an identifier, and the following code does not work, since it does
not reduce to function_definition till it the parser reaches the end
braces of that function. But by then it will reduces all identifiers in
the function.


identifier
                              : IDENTIFIER


{
printf("ENTERING %s into symbol table\n",yylval.str_attr) ;
addsymbol($$.symbolrec);
                              }
;


I tried many hours to find a way to solve this problem. I appreciate
your input.


Thanks
Pradeep




------------------------------------------------
Test Program:
------------------------------------------------
char c;
int d;


void Function1 (char aname, int value)
{
              write (aname);
              write (value);
}


------------------------------------------------
Parsing Trace:
------------------------------------------------
reduced by CHAR_OP
reduced by IDENTIFIER ##### c #####
reduced by identifier
reduced by declarator
reduced by type_specifier init_declarator_list ';'
reduced by declaration
reduced by data_or_func_defn
reduced by INT_OP
reduced by IDENTIFIER ##### d #####
reduced by identifier
reduced by declarator
reduced by type_specifier init_declarator_list ';'
reduced by declaration
reduced by program data_or_func_defn
reduced by VOID_OP
reduced by IDENTIFIER ##### Function1 #####
reduced by identifier
reduced by CHAR_OP
reduced by IDENTIFIER ##### aname #####
reduced by identifier
reduced by type_specifier declarator
reduced by parameter_declaration
reduced by INT_OP
reduced by IDENTIFIER ##### value #####
reduced by identifier
reduced by type_specifier declarator
reduced by parameter_type_list ',' parameter_declaration
reduced by declarator '(' parameter_type_list ')'
reduced by IDENTIFIER ##### aname #####
reduced by identifier
reduced by identifier_expr
reduced by postfix_expr
reduced by unary_expr
reduced by multiplicative_expr
reduced by additive_expr
reduced by relational_expr
reduced by equality_expr
reduced by logical_and_expr
reduced by conditional_expr
reduced by '(' expr ')'
reduced by WRITE primary_expr ';'
reduced by write_statement
reduced by statement
reduced by IDENTIFIER ##### value #####
reduced by identifier
reduced by identifier_expr
reduced by postfix_expr
reduced by unary_expr
reduced by multiplicative_expr
reduced by additive_expr
reduced by relational_expr
reduced by equality_expr
reduced by logical_and_expr
reduced by conditional_expr
reduced by '(' expr ')'
reduced by WRITE primary_expr ';'
reduced by write_statement
reduced by statement_list statement
reduced by '{' statement_list '}'
reduced by compound_statement
reduced by type_specifier declarator function_body
reduced by function_definition
FUNCTION IS SET TO TRUE
reduced by program data_or_func_defn


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.