Unknown94
Unknown94

Reputation: 45

Print part of matched string in LEX

I want to print the arguments of a function written in C, in lex. Now I want the arguments to be printed in the format :

Arguments:

x1 of type int

x2 of type float

Things tried till now: 1) I am able to print the arguments but with the starting and ending brackets included in the otuput. Regular expression used for this matching is:

datatypes (("int")|("float")|("void")|("double")|("char")|("short")|("long"))
blanks [ ]
letter [a-z]
digits [0-9]
id ({letter}|_)({letter}|{digits}|_)*
arguments "("{datatypes}{blanks}+{id}{blanks}*","{blanks}*{datatypes}{blanks}+{id}{blanks}*")";

This is in the declaration part. In the rule section:

%%
{arguments}                 {ECHO; fprintf(outputfile1,""Arguments:"%s\n",yytext);}
%%

Output obtained: Arguments: (int x1,float x2);

This is part of a code and not the full code. Any help is appreciated

Upvotes: 1

Views: 848

Answers (1)

Chris Dodd
Chris Dodd

Reputation: 126203

You want to actually parse the input text, breaking into related parts. This is a job for a parser (such as bison) and not for a regular expression recognizer (like flex), though you need a regex recognizer to recognize the tokens.

If you really need to do this purely in (f)lex, you can build a (limited) parser using start states:

%s ARGLIST1, ARGLIST2, ARGLIST3
%{
static char type[16];
%}
%%
<INITIAL>"("             { fprintf(output, "Arguments:\n");
                           BEGIN(ARGLIST1); }
<ARGLIST1>{datatypes}    { strcpy(type, yytext); BEGIN(ARGLIST2); }
<ARGLIST2>{id}           { fprintf(output, "%s of type %s\n", yytext, type);
                           BEGIN(ARGLIST3); }
<ARGLIST3>","            { BEGIN(ARGLIST1); }
<ARGLIST3>")"            { BEGIN(INITIAL); }
{blanks}                 ;

doing this for any kind of non-trivial parser is error-prone and tedious. It is much better to use a tool like bison or yacc.

Upvotes: 3

Related Questions