Thread: Grammer rules

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    50

    Grammer rules

    in a programing language how do you represent grammer rules?

    For example

    say we have a hypothetical programming language called ABED

    ABED can have only one type of loop. And it's a while loop. The while loop condition should be a int and if the value of the int is greater than zero it means true otherwise it means false;


    How do i represent the above grammer in C.

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    What?

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    50
    now the above rules can be defined using backus naur form. So how do i store these rules in a program so that these rules can be used later to check for syntax validity?

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You might want to investigate something like Lex.

    Using BNF may (or may not) be of much use in actual parsing.

    Quote Originally Posted by iamnew View Post
    So how do i store these rules in a program so that these rules can be used later to check for syntax validity?
    This is really not very different from someone saying, "How do I store the rules of chess in a program so that I can check if a move in a game is valid?" Certainly, there are many chess games and syntax parsers around, so there should be no shortage of existing source code for you to examine, but if you are looking for some function API like:

    Code:
    int DefineMyChessGame (char *rules);  // "rules" is a string definition
    int DefineMyBNF_lexer (char *rules);  // "rules" is a string definition
    Well, you may (or may not) find some library around to suit your needs, there is certainly no standard procedure here. Like I said, google lex and yacc.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by iamnew
    now the above rules can be defined using backus naur form. So how do i store these rules in a program so that these rules can be used later to check for syntax validity?
    Along the lines of what MK27 suggested, you can write those rules in C++ masquerading as a kind of extended BNF using Boost.Spirit.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Apr 2010
    Posts
    50
    Can't you use a parse tree to validate syntax?

  7. #7
    Registered User
    Join Date
    Jul 2007
    Posts
    131
    When you parse, you check source codes syntatical validaty. Parsers output (or are able to output) parse trees. So yes.

  8. #8
    Registered User
    Join Date
    Apr 2010
    Posts
    50
    say if you have a code like this

    Code:
    int a=4;
    if(a){
    }
    else{
    }
    How many parse tree's should i create? Is it Two?

  9. #9
    Registered User
    Join Date
    Jul 2007
    Posts
    131
    No. One translation unit produces one parse tree.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-03-2008, 03:10 AM
  2. Replies: 6
    Last Post: 11-01-2008, 12:01 PM
  3. Pls let me know the rules in C
    By ice_breaekr in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 06-28-2005, 06:27 AM
  4. Addition to the forum rules
    By webmaster in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-08-2004, 05:31 PM
  5. Red-Black Tree rules
    By ghettoman in forum C++ Programming
    Replies: 1
    Last Post: 11-06-2001, 12:36 PM