Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.compilers > #891 > unrolled thread
| Started by | Lee <lchaplin13@gmail.com> |
|---|---|
| First post | 2013-05-09 04:05 -0700 |
| Last post | 2013-05-10 22:24 +0200 |
| Articles | 2 — 2 participants |
Back to article view | Back to comp.compilers
parsing function name and arguments Lee <lchaplin13@gmail.com> - 2013-05-09 04:05 -0700
Re: parsing function name and arguments Hans Aberg <haberg-news@telia.com> - 2013-05-10 22:24 +0200
| From | Lee <lchaplin13@gmail.com> |
|---|---|
| Date | 2013-05-09 04:05 -0700 |
| Subject | parsing function name and arguments |
| Message-ID | <13-05-005@comp.compilers> |
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
[toc] | [next] | [standalone]
| From | Hans Aberg <haberg-news@telia.com> |
|---|---|
| Date | 2013-05-10 22:24 +0200 |
| Message-ID | <13-05-007@comp.compilers> |
| In reply to | #891 |
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.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.compilers
csiph-web