Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

parsing function name and arguments

32 views
Skip to first unread message

Lee

unread,
May 9, 2013, 7:05:30 AM5/9/13
to
Hi all,

I am strugling with this simple thing, any help will be greatly appreciated.
I would like to extract the name and the argument from the following line:
FUNCTION Entry (args)
I have the flex file defined as:
%{
#include <fct.tab.h>
%}
T_FUNCTION FUNCTION
SYMBOL_ID [a-zA-Z]+[a-zA-Z0-9_]*
OPEN_PARA [\(]
CLOSE_PARA [\)]
WHITE_SPACE [ \t\f\r\v]+
NEW_LINE [\n]
%%
{T_FUNCTION} { printf("1.yylval = %s\n", yytext);
return (FUNCTION);
}
{SYMBOL_ID} { printf("2.yylval = %s\n", yytext);
yylval.all_str = yytext;
return (SYMBOLID);
}
{OPEN_PARA} { return ('('); }
{CLOSE_PARA} { return (')'); }
{WHITE_SPACE} { }
<<EOF>> { yyterminate(); }
. { printf("3.error_msg = yytext : %s\n", yytext);
return(0);
}
%%
int main()
{
printf("Starting...\n\n");
return yyparse();
}

and the grammar as:

%{
void yyerror(char *s);
%}
%union {
int intnum;
char *all_str;
}

%token FUNCTION
%token <all_str> SYMBOLID
%type <program> program
%start program
%%
program
: FUNCTION SYMBOLID '(' SYMBOLID ')'
{ printf("FUNCTION SYMBOL = %s\n", $2);
printf("func name and args: '%s' '%s'\n", $2, $4);
}
;
%%
void yyerror(char *s)
{
printf("Errors\n");
exit(1);
}

The lexer is extracting corectly the tokens but not the grammar. I'am using flex and bison from GNUWin distribution (tested on ubuntu same result).

Thanks,
Lee

Hans Aberg

unread,
May 10, 2013, 4:24:30 PM5/10/13
to
On 2013/05/09 13:05, Lee wrote:
> I have the flex file defined as:
...
> SYMBOL_ID [a-zA-Z]+[a-zA-Z0-9_]*
...
> %%
...

> {SYMBOL_ID} { printf("2.yylval = %s\n", yytext);
> yylval.all_str = yytext;
> return (SYMBOLID);
> }
...
> %%

You have forgotten to copy the string pointed at by yytext...

> and the grammar as:
...
> %%
> program
> : FUNCTION SYMBOLID '(' SYMBOLID ')'
> { printf("FUNCTION SYMBOL = %s\n", $2);
> printf("func name and args: '%s' '%s'\n", $2, $4);
> }

...so here, the action will only be executed after all grammar
components have been pointed at, and then yytext of the first SYMBOLID
points at something else. If you are using C, then you must also clean
up allocated strings no longer in use. In C++, this can be done using
the std::string class.
0 new messages