Re: help with EBNF grammar

"Raymond Limpus" <limpus@immortalnet.com.au>
31 Mar 2001 02:45:46 -0500

          From comp.compilers

Related articles
help with EBNF grammar paul.meaney@qa.comstar.co.uk (Paul Meaney) (2001-03-28)
Re: help with EBNF grammar rael@zopyra.com (William S. Lear) (2001-03-31)
Re: help with EBNF grammar rboland@unb.ca (Ralph Boland) (2001-03-31)
Re: help with EBNF grammar mahesha@india.hp.com (Mahesha N) (2001-03-31)
Re: help with EBNF grammar limpus@immortalnet.com.au (Raymond Limpus) (2001-03-31)
Re: help with EBNF grammar mike@dimmick.demon.co.uk (Mike Dimmick) (2001-03-31)
Re: help with EBNF grammar uabbwat@uab.ericsson.se (Barry Watson) (2001-04-04)
Re: help with EBNF grammar rael@zopyra.com (William S. Lear) (2001-04-04)
| List of all articles for this month |
From: "Raymond Limpus" <limpus@immortalnet.com.au>
Newsgroups: comp.compilers,comp.compilers.tools.javacc
Date: 31 Mar 2001 02:45:46 -0500
Organization: @Home Network
References: 01-03-154
Keywords: parse
Posted-Date: 31 Mar 2001 02:45:46 EST

Ok I was bored so here is the EBNF and associated C implementation of it
(semi-tested):


EBNF:




Exp -> And-Exp {'|' And-Exp}
And-Exp -> Simp-Exp {'&' Simp-Exp}
Simp-Exp -> '(' Exp ')'
Simp-Exp -> 1
Simp-Exp -> 0
Simp-Exp -> '!' Exp




C Code:


#include <stdio.h>


/* Prototypes */
void Next();
void Error();


int Exp();
int OrExp();
int AndExp();
int SimpExp();




/* Globals */
int c;




/* Main Program */
int main(void)
{
        int r;
        fflush(stdin);
        printf("Enter a boolean expression (0, 1, &, |, !) press ENTER when
done:\n");
        Next();
        r = Exp();
        printf("Result = %d\n", r);
        return 0;
}


/* Gets next input character from stream (strips spaces) */
void Next()
{
        while ((c = fgetc(stdin)) == ' ') {
        }
}


/* Generates an error on parse and quits */
void Error()
{
        fprintf(stderr, "Parse Error!\n");
        exit(1);
}




/* Evaluates an expression */
int Exp()
{
        int r = OrExp();
        /* did we consume all input? */
        if (c != '\n')
                Error();
}




/* Evaluates an | expression */
int OrExp()
{
        int r = AndExp();
        while (c == '|') {
                Next();
                r |= AndExp();
        }
        return r;
}




/* Evaluates an & expression */
int AndExp()
{
        int r = SimpExp();
        while (c == '&') {
                Next();
                r &= SimpExp();
        }
}




/* Evaluates a simple expression - 1, 0, !exp or (exp) */
int SimpExp()
{
        if (c == '(') {
                int r;
                Next();
                r = OrExp();
                if (c != ')')
                        Error();
                Next();
                return r;
        } else if (c == '0' || c == '1') {
                int r = c - '0';
                Next();
                return r;
        } else if (c == '!') {
                Next();
                return !OrExp();
        } else
                Error();
}




This will be easily turned into any language.. have fun : )


I just realised I didn't include anything such as > or < test etc. etc. but
these are easily implemented.. this just evaluates boolean expressions.




Ray.




"Paul Meaney" <paul.meaney@qa.comstar.co.uk> wrote in message
> Hello.
>
> A quick introduction - my name is Paul Meaney, I am a former PhD
> geneticist now turned full time Java programmer. I am working for a
> company, they've given me a task and I am totally stumped. I have to
> write a parser that will evaluate input expressions to boolean true/
> false. I have never done anything like this before.



Post a followup to this message

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