Re: Exchange parameter, How? (Lowell Boggs)
Mon, 26 Jun 1995 16:16:44 GMT

          From comp.compilers

Related articles
Exchange parameter, How? (1995-06-24)
Re: Exchange parameter, How? (1995-06-26)
| List of all articles for this month |

Newsgroups: comp.compilers
From: (Lowell Boggs)
Keywords: parse, translator
Organization: Texas Instruments
References: 95-06-044
Date: Mon, 26 Jun 1995 16:16:44 GMT

>We have a special language.
>This is the function declaration:
>function_declaration: function_name
> '('
> parameters
> ')'
> | parameters ',' one_parameter
> | one_parameter
> ;
>So this language is simular to C. A function is like:
> function(a,b,c,d);
>We want to write a program, which can exchange the parameters for us.
>that is: function(b,a,d,c);
>We are going to use LEX and YACC to do this. But we don't have the complete
>grammar from this language. Is there a simple way to do this using lex & yacc
>without to write the whole grammar?
>Thanks very much!
>Alexander Dong
>[You can probably do well enough by just fiddling things that look like
>function calls, looking lexically for a name followed by an open paren.

        You should be able to do this entirely with LEX.

        The default token processing in the lex tables should print the
        incoming characters to stdout. In this way, you copy the input file
        to stdout. If you don't define a main() of your own, the default
        lex main should copy stdin to stdout unless you superseed the
        default token processing.

        You can use the LEX state variables to implement a simple minded grammer
        that has two states:

1. parsing a function call
2. not parsing a function call

        When you're not parsing a function call, all characters and tokens are
        printed to stdout.

        When you are parsing a function call, you store tokens until you
        go back to the not parsing a function call state. At that time, you
        print the tokens to stdout in a different order than they appeared
        in the text.

        To change the LEX state, use the BEGIN(statename) directive.

        Most people think of LEX and YACC as an integrated whole, but in fact
        they are two separate, powerfull tools. LEX is like a super version of

        By the way, I'm not saying that any of this will be easy, mind you.
        It might make more sense to have someone do this by hand, if you only
        do it once and if the number of files is small.

Hope this helps,


Post a followup to this message

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