Related articles |
---|
help: using yacc, and my grammar needs to count items. madings@baladi.nmrfam.wisc.edu (1999-04-18) |
Re: help: using yacc, and my grammar needs to count items. gneuner@dyn.com (1999-04-19) |
Re: help: using yacc, and my grammar needs to count items. debray@CS.Arizona.EDU (1999-04-26) |
From: | debray@CS.Arizona.EDU (Saumya K. Debray) |
Newsgroups: | comp.compilers |
Date: | 26 Apr 1999 02:04:25 -0400 |
Organization: | University of Arizona CS Department, Tucson AZ |
References: | 99-04-057 |
Keywords: | parse, comment |
Steve Mading <madings@baladi.nmrfam.wisc.edu> wrote:
>I am running into something I can't express in BNF, and I can't figure
>out how to make lex/yacc parse this correctly. Can anyone offer
>advice on this? My problem is that my grammer needs to count items
>and group them into 'rows' in a table even though there are no
>delimiters.
>
>Here's an example using this language's syntax:
>
> loop_
> _name1
> _name2
> _name3
>
> value1-1
> value2-1
> value3-1
> value1-2
> value2-2
> value3-2
> value1-3
> value2-3
> value3-3
> stop_
>
>The idea is that this represents a table of data like so:
>
> name1 name2 name3
> -----------------------------------
> value1-1 value2-1 value2-1
> value1-2 value2-2 value2-2
> value1-3 value2-3 value2-3
Building a parser using yacc seems overkill; a much simpler solution
would be to use an awk-ish script. The script attached below produces
the following output, which seems close to what you want modulo minor
pretty-printing:
_name1 _name2 _name3
value1-1 value2-1 value3-1
value1-2 value2-2 value3-2
value1-3 value2-3 value3-3
The AWK script I used to produce this is as follows. The primary
assumption is that the names are separated from the values by (exactly)
one empty line.
# AWK script for grouping-into-rows problem
#
BEGIN { names_seen = 0; ctr = 0; }
NF == 0 { \
names_seen = 1;
i = 0;
while (i <= ctr) {
printf(" %s", Names[i++]);
}
printf("\n");
i = ctr;
}
NF > 0 && $1 !~ /loop_/ && $1 !~ /stop_/ { \
if (!names_seen) {
Names[ctr++] = $1;
}
else {
printf(" %s", $0);
i--;
if (i == 0) {
printf("\n");
i = ctr;
}
}
}
END { if (i != ctr) printf("\n"); }
--
Saumya Debray
Dept. of Computer Science, University of Arizona, Tucson
debray@cs.arizona.edu
http://www.cs.arizona.edu/people/debray
[I believe that he was saying that he needs to process the input data as
though it were in a table. I agree that if all you need to do is make it
look like a table and print it out, a quick awk or perl hack is the way
to go. -John]
Return to the
comp.compilers page.
Search the
comp.compilers archives again.