help-bison
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Help constructing tiny expression grammar


From: Randy W. Sims
Subject: Help constructing tiny expression grammar
Date: Tue, 19 Oct 2004 00:27:17 -0400
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8a4) Gecko/20040925

I'm trying to figure out how to write a grammar for a tiny expression language that checks for complicated module dependencies for Perl. These expressions have some unusual characteristics, and I'm new to parser generators. Here is an example:

    # This is a comment
    def {mysql} = DBD::mysql in [0.2-1.2 !1] && DateTime::Format::mysql;
    def {pg}    = DBD::pg && DateTime::Format::pg;
    def {db}   := [ :mysql :pg ]; # eol comment

    ( DateTime > 1 && {db} && {OSNAME} in ['MSWin32' 'MacOSX' 'VMS'] )
      ||
    ( {OSNAME} eq 'Linux' && HAS_HEADER('std.h') )

The def statements define macros that are to be substituted into the main expression. There are two type of definition: 1) just an assignment of an arbitrary expression to a macro, and 2) a special assignment that works like creating an ORed list of macros ([ :macro1 :macro2 ] is functionally equivalant to {macro1} || {macro2}), but has special semantics in that it defines commandline options (--use-macro1 --use-macro2).

Expressions can include perl package names:

File::Spec # True if module installed
File::Spec > 0.8
File::Spec in [0.8-1.0 !0.86] # set notation for version list

Set notation contains VERSIONs, negated VERSIONs, RANGEs, and negated RANGEs

# macro expansion, evaluates to boolean
{macro}

# builtin macros
{OSNAME} == 'Linux'
{OSNAME} in ['Linux' 'MacOSX'] # set notation compare to multiple strings

# invoke builtin function with arguments, returns boolean
FUNC(args)

I'm unsure how to define some of these elements. Below is what I have so far. (Please stop laughing. I've been reading the manual and googling. I've also just ordered some books, but would like to get some work done while waiting.)

Thanks for any pointers,
Randy.

macro:    '{' identifier '}'

macdef:   DEF macro '=' expr ';'

/* symbol is ':identifier' */
symlst:   symbol
        | symlst symbol

/* OPTASSN is ':=' */
optdef:   DEF macro OPTASSN '[' symlst ']' ';'

strarg:   string
        | strarg ',' string

func:     identifier '(' strarg ')'

/* ranges can be open */
range:    version '-'
        | '-' version
        | version '-' version

verexp:   version
        | '!' version
        | range

verlst:   verexp
        | verlst verexp

strlst:   string
        | strlst string

term:     package
        | package RELOP version
        | package IN '[' verlst ']'
        | macro
        | macro IN '[' strlst ']'
        | func

expr:     term
        | term LOGOP term
        | '(' expr ')'





reply via email to

[Prev in Thread] Current Thread [Next in Thread]