lex grammar

Discussion in 'C Programming' started by Vuun Harjnes, Oct 19, 2003.

  1. Vuun Harjnes

    Vuun Harjnes Guest

    Hi,

    I hope this is acceptable for the C newsgroup. I wasn't sure where else to
    post this.

    I'm trying to write a lex file to correctly identify the tokens in my
    config file except I've run into a little puzzle. Perhaps this is just a
    limitation of lex but I'd like to hear other opinnions.

    Heres my config file format

    [header]

    label = value.

    Pretty similar to windows .ini files - except the header string can contain
    most printable characters (ie !@#$% or even ] if its escaped) Also the
    value can contain a sumilar subset of characters.

    The question I have is how can I tokenize the file if I've got strings that
    are arbitrary length and can contain almost any character?

    Ie the following wont work.

    %{
    #include <stdio.h>
    %}
    W [[:alnum:]_]
    B [[:blank:]]
    %%
    {W}+ printf("WORD ");
    \[ printf("OBRACE ");
    \} printf("EBRACE ");
    {B} /* Ignore whitespace */
    \n printf("\n");
    ..+ printf("ARBITRARY-STRING ");
    %%

    The following is the closest I've been able to get to what I want.

    %{
    #include <stdio.h>
    %}
    W [[:alnum:]_]
    B [[:blank:]]
    V .
    %%
    \[(\\.|[^\\\]\n])*\] printf("HEADER ");
    {W}+ printf("WORD ");
    {B}*={B}*{V}+ printf("VALUE ");
    {B} /* Ignore whitespace */
    \n printf("\n");
    .. printf("UNKNOWN ");
    %%

    While this works its not ideal as I've still got to parse VALUE and HEADER
    to extract what I'm after. Is there a better way to do this?

    As a side note, whats the preferred way to parse config files? I've only
    recently started to go down the lex/yacc path but I'm noticing other tools
    out there (like antlr) are they any good/better suited for what I want to
    do?

    Thanks for any response

    Vuun
    Vuun Harjnes, Oct 19, 2003
    #1
    1. Advertising

  2. Vuun Harjnes

    T.M. Sommers Guest

    Vuun Harjnes wrote:
    > Hi,
    >
    > I hope this is acceptable for the C newsgroup. I wasn't sure where else to
    > post this.


    It isn't. Try comp.compilers, or comp.unix.programmer, or
    comp.programming.
    T.M. Sommers, Oct 19, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Moonlit

    Best lex/yacc for C++?

    Moonlit, Oct 8, 2003, in forum: C++
    Replies:
    18
    Views:
    1,541
    Moonlit
    Oct 14, 2003
  2. Arthur T. Murray

    Re: Parsing English with lex and yacc

    Arthur T. Murray, Jan 23, 2004, in forum: C++
    Replies:
    5
    Views:
    1,319
    Amnon Meyers
    Jan 26, 2004
  3. Alvaro Puente

    YACC-LEX parsing overflow

    Alvaro Puente, Jul 10, 2003, in forum: C Programming
    Replies:
    1
    Views:
    385
    Chris Dollin
    Jul 10, 2003
  4. cylin
    Replies:
    1
    Views:
    424
    Ben Pfaff
    Jan 7, 2004
  5. Stephane CHAZELAS

    Re: Stripping multiline C comments without using Lex

    Stephane CHAZELAS, Feb 4, 2004, in forum: C Programming
    Replies:
    3
    Views:
    914
    Jens Schweikhardt
    Feb 5, 2004
Loading...

Share This Page