Data File Parsing (Neil S Kenig)
Mon, 7 Jun 1993 20:27:52 GMT

          From comp.compilers

Related articles
Data File Parsing (1993-06-07)
| List of all articles for this month |

Newsgroups: comp.compilers,comp.sw.components,comp.sources.wanted
From: (Neil S Kenig)
Keywords: question, lex, comment
Organization: TeleSciences CO Systems, Inc.
Date: Mon, 7 Jun 1993 20:27:52 GMT

I need to read parameters from an Ascii text file into a C program in a
Unix environment. I am looking for a technique that's easy to get working
quickly. I'm not a real lex/yacc user. If that's the way to go, would
somebody please get me started on a grammar definition or modifications
that I should make to the file syntax in order to make the file easier to
parse? Any other suggestions about possible solutions are most welcome.

Ideally, I'd like to make calls like:

        float *optimization;
        num_values = get_values ("Performance", "optimization", &optimization);

        Which would return 1, allocate a float and set *optimization to 1 (TRUE).

        float *module_ids;
        num_values = get_values ("Modules", "module_id", &module_ids);

        Which would return 8 and allocate an array to hold the 8 module ids.

  The format of this parameter file is

  1) lines beginning with a pound sign (#) are considered comments
        and ignored. Leading blanks, if present, are ignored. Trailing
        comments may NOT be appended to section or parameter lines.

  2) blank lines are ignored

  3) Files are divided into sections. Sections contain logical aggregations
        of parameters. Section names are indicated by lines of the format

        - section

        where "section" is the name of the section. Blanks before or after
        the "-" are ignored. Any words beyond the first word of the section
        name are ignored. All parameters specified after a section heading are
        considered to be part of that section until another section name
        or EOF is encountered. At least one section name must always be
        present. Any parameters appearing before the first section name
        will be considered a syntax error.

  5) Within each section, parameters are specified in groups. Each group
        begins with a parameter definition line followed by parameter value
        lines. The parameter definition line is of the format:


        where each name is a valid C language variable name and type is one of:

        'n' or 'N' for numeric data
        's' or 'S' for string (character) data
        'b' or 'B' for Boolean data (which may only take on the values
                                              't', 'true', 'y', 'yes', '1', or 'on'
                                              'f', 'false', 'n', 'no', '0', or 'off'
                                        in any mixed case.

        Parameter value lines are specified by:


        Numbers may be positive or negative and may be floating point or integer.
        String data must be enclosed in double quotes (").

        If values are ommited, e.g. by specifying consecutive colons (:), then
        numeric values will default to 0, string values will default to the
        NULL string and boolean data will default to false.

        Multiple parameter value lines may follow a single parameter definition
        line thus designating an array of parameter values.

An example parameter file is:

# Parameters for system configuration

- Performance Parameters





- Modules


#Host LSM1

    10:"LSM1": 1:"LSM": "/usr/bin/lsm -path /usr/lsm"
    15:"LSM1": 2:"CLI": "/usr/bin/cli -path /usr/cli"
    20:"LSM1": 4:"Cerberus":"/usr/bin/cerberus -path /usr/cerberus"
    25:"LSM1": 8:"Dragon": "/usr/bin/dragon -path /usr/dragon"

# Host LSM2

    40:"LSM2": 1:"LSM": "/usr/bin/lsm -path /usr/lsm"
    45:"LSM2": 2:"CLI": "/usr/bin/cli -path /usr/cli"
    50:"LSM2": 4:"Cerberus":"/usr/bin/cerberus -path /usr/cerberus"
    55:"LSM2": 8:"Dragon": "/usr/bin/dragon -path /usr/dragon"

# Relationships between the various processors in the system

- Primary Systems

- Secondary Systems

- Master Systems
              Neil Kenig | TeleSciences CO Systems
                                                    | 351 New Albany Road | Moorestown, NJ 08057
                                                    | (609)866-1000 ext. 228
[This should be straightforward to get working using lex and some C code.

Post a followup to this message

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