For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > October 2006 > ignoring {} stanzas in lex









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author ignoring {} stanzas in lex
Frank Cusack

2006-10-30, 7:15 pm

Hi

I have a yacc/bison parser which reads a config like

sectionname {
...
}

But I want to be able to ignore additional sections in the config file like

foo {
...
}

I don't deal with this in the parser, I just have a lexer which looks like

%x FOO

%%
foo BEGIN FOO;

.... sectionname keywords here ...

<FOO>\} BEGIN INITIAL;
<FOO>\n /* ignore eol */;
<FOO>. /* ignore all */;

%%

Now for the simple case shown above, this works great. However if the
foo section itself contains subsections:

foo {
#bar {
#}
baz {
}
}

then the first occurence of '}' goes back to the INITIAL state in the
lexer. Even if I ignore comments before looking for '}' then the baz
subsection trips me up. Is there a relatively simple way for me to
fix the lexer to ignore subsections (and we can assume no
sub-subsections) or is it better to put it into the grammar? I'm
having trouble figuring out how to do the former, I think I can
handle the latter but not sure if that's the best way.

-frank
Frank Cusack

2006-10-30, 7:15 pm

On Tue, 24 Oct 2006 12:03:43 -0700 Frank Cusack <fcusack@fcusack.com> wrote:
> Hi
>
> I have a yacc/bison parser which reads a config like
>
> sectionname {
> ...
> }
>
> But I want to be able to ignore additional sections in the config file like
>
> foo {
> ...
> }
>
> I don't deal with this in the parser, I just have a lexer which looks like
>
> %x FOO
>
> %%
> foo BEGIN FOO;
>
> ... sectionname keywords here ...
>
> <FOO>\} BEGIN INITIAL;
> <FOO>\n /* ignore eol */;
> <FOO>. /* ignore all */;
>
> %%
>
> Now for the simple case shown above, this works great. However if the
> foo section itself contains subsections:
>
> foo {
> #bar {
> #}
> baz {
> }
> }
>
> then the first occurence of '}' goes back to the INITIAL state in the
> lexer. Even if I ignore comments before looking for '}' then the baz
> subsection trips me up. Is there a relatively simple way for me to
> fix the lexer to ignore subsections (and we can assume no
> sub-subsections) or is it better to put it into the grammar? I'm
> having trouble figuring out how to do the former, I think I can
> handle the latter but not sure if that's the best way.


Sorry, I should have added, I figured out how to do it by adding
another start state, but that seems ugly.

-frank
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com