Related articles |
---|
parsing function name and arguments lchaplin13@gmail.com (Lee) (2013-05-09) |
Re: parsing function name and arguments haberg-news@telia.com (Hans Aberg) (2013-05-10) |
From: | Lee <lchaplin13@gmail.com> |
Newsgroups: | comp.compilers |
Date: | Thu, 9 May 2013 04:05:30 -0700 (PDT) |
Organization: | Compilers Central |
Keywords: | question, parse |
Posted-Date: | 10 May 2013 10:11:54 EDT |
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
Return to the
comp.compilers page.
Search the
comp.compilers archives again.