简体   繁体   中英

EBNF declaration syntax in c program

I'm a little new at programming (okay, very new), and I came across Extended Backus Naur Form, or EBNF, and decided to try to figure out how to use it. Unfortunately, even though their are tons of explanations online on how EBNF works, there is precious little on how to actually implement it. So I made a simple little program in C using it, just to see what happens. Here is what I wrote:

#include <stdio.h>
#include <stdlib.h>


mixture : [letter|digit] {letter | digit};
integer : [ "+"|"-"] digit {digit};
naturalNumber : digit {digit};
digit : "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9";
letter : "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" | "j" | "k" | "l" | "m" |    "n" | "o" | "p" | "q" | "r" | "s" | "t" | "u" | "v" | "w" | "x" | "y" | "z" | "A" | "B" | "C" | "D"| "E" | "F" | "G" | "H" | "I" | "J" | "K" | "L" | "M" | "N" | "O" | "P" | "Q" | "R" | "S" | "T" | "U" | "V" | "W" | "X" |"Y" | "Z";


int main()
{
    char c[7];
    c[0] = '1';
    c[1] = '2';
    c[2] = '3';
    c[3] = 'x';
    c[4] = 'y';
    c[5] = 'z';
    c[6] = '\0';

    if(c == mixture){
        printf("You have a mixture of numbers and letters");
    }
    else if(c == integer){
        printf("This is just a number");
    }
    else if(c == naturalNumber){
        printf("This is just a positive number");
    }
    else if(c == digit){
        printf("This is a plain digit");
    }
    else if(c == letter){
        printf("This is a plain letter");
    }

    return 0;
}

Right away I get a compiler error (I'm using Code Blocks) saying it expects a "=" before the ":", but I used a ":" because everything I read online seems to suggest a ":" is correct. Also, if I change it to "=" I get a compiler error that says "mixture" (and others) doesn't have a type or storage class. Well, "int" in front won't work, and "char" doesn't make sense either, since it's a mixture of both.

I'm also not sure if the declarations are supposed to go before main() or inside of it. I ended up putting it before because one thing I read online seemed to suggest this was right.

By the way, I'm finding absolutely no online examples in C to follow - does this mean I'm totally thinking about EBNF the wrong way and it can't be used like this at all?

Any help is greatly appreciated.

integer : [ "+"|"-"] digit {digit};
naturalNumber : digit {digit};

You can't just write EBNF within a C program like that. It is not part of C syntax.

Also: C is not a language meant for describing grammars, as such.

You should use something like ANTLR if you want to quickly create a program that can recognize a language described in EBNF. Also, This URL is an ANTLR tutorial which does not require much experience in the area, nd uses EBNF.

你写的绝对不是C.我认为你在混淆“使用EBNF语法”和“在C中实现EBNF解析器”。

You're getting the errors because what you are writing is not valid C.

You're confusing a notational tool to specify a grammar for a programming language with a programming language itself. The C language syntax is specified using a BNF grammar (see the online C99 language standard , appendix A); that doesn't mean a C compiler understands BNF or EBNF.

There are C-based tools that can take a BNF or EBNF specification and generate parsers that understand code written in that grammar.

Lex & Yacc are what you need. Check them out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM