Related articles |
---|
Exchange parameter, How? qiwen@hamilton.mechanik.th-darmstadt.de (1995-06-24) |
Re: Exchange parameter, How? boggs@osage.csc.ti.com (1995-06-26) |
Newsgroups: | comp.compilers |
From: | boggs@osage.csc.ti.com (Lowell Boggs) |
Keywords: | parse, translator |
Organization: | Texas Instruments |
References: | 95-06-044 |
Date: | Mon, 26 Jun 1995 16:16:44 GMT |
>Hi!
>
>We have a special language.
>This is the function declaration:
>
>function_declaration: function_name
> '('
> parameters
> ')'
>
>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
>qiwen@rbg.informatik.th-darmstadt.de
>[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.
>-John]
>--
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
sed.
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,
Lowell
--
Return to the
comp.compilers page.
Search the
comp.compilers archives again.