Thread: declaration parser

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    31

    declaration parser

    this code compiles but doesn't work, if i try to open debugger, it still does not work.

    whats wrong?

    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    
    #define MAXTOKEN 100
    
    enum {NAME, PARENS, BRACKETS};
    
    int gettoken(void);
    void dcl(void);
    void dirdcl(void);
    
    int tokentype;
    char token[MAXTOKEN];
    char name[MAXTOKEN];
    char datatype[MAXTOKEN];
    
    char out[1000];
    
    
    int main(){
        while (gettoken() != EOF){
            strcpy(datatype, token);
            out[0] = '\0';
            dcl();
            if(tokentype != '\n')
                printf("Syntax error \n");
            printf("%s %s %s", name, out, datatype);
    
        }
    
        return 0;
    }
    
    int gettoken(void){
        int c, getch(void);
        void ungetch(int);
    
        char *p = token;
    
        while( (c = getch()) == ' ' || c == '\t' )
            ;
        if(c == '('){
            if ( (c = getch()) == ')' ){
                strcpy(token, "()");
                return tokentype = PARENS;
            }
            else {
                ungetch(c);
                return tokentype = '(';
            }
        }
        else if ( c == '['){
            for(*p++ = c ; (*p++ = getch() ) != ']' ; )
                ;
            *p = '\0';
            return tokentype = BRACKETS;
        }
        else if(isalpha(c)){
            for(*p++ = c; isalnum(c = getch()) ; )
                *p++ = c;
            *p = '\0';
            ungetch(c);
            return tokentype = NAME;
        }
        else
            return tokentype = c;
    }
    
    
    void dcl(void){
        int ns;
    
        for(ns = 0; gettoken() == '*'; )
            ns++;
        dirdcl();
    
        while(ns-- > 0)
            strcat(out, " pointer to");
    }
    
    void dirdcl(void){
        int type;
    
        if(tokentype == '(' ){
            dcl();
            if(tokentype != ')' )
                printf("error: missing ) \n");
        } else if (tokentype == NAME)
            strcpy(name, token);
        else
            printf("error: expected name or (dcl) \n");
        while ( (type = gettoken()) == PARENS || type == BRACKETS )
            if (type == PARENS)
                strcat(out, " function returning");
            else {
                strcat(out, " array");
                strcat(out, token);
                strcat(out, " of");
            }
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Not sure what you mean.
    After fixing your non-standard getch/ungetch, I get
    Code:
    $ gcc -g foo.c
    $ ./a.out 
    int a
    a  int
    int a[]
    a  array[] of int
    int (*fn)()
    fn  pointer to function returning int
    It seems to be the very basic 'decl' program found in K&R

    Next time, provide your actual input as well as the program, when saying "it doesn't work".
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2017
    Posts
    31
    Quote Originally Posted by Salem View Post
    Not sure what you mean.
    After fixing your non-standard getch/ungetch, I get
    Code:
    $ gcc -g foo.c
    $ ./a.out 
    int a
    a  int
    int a[]
    a  array[] of int
    int (*fn)()
    fn  pointer to function returning int
    It seems to be the very basic 'decl' program found in K&R

    Next time, provide your actual input as well as the program, when saying "it doesn't work".
    hello, salem. thanks for the answer, however. i'm not sure of what you mean by "non-standard". i typed out whats in the book K & R and the response when i compile
    is a blank command line window which doesn't show anything. What changes did you make to the code?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > i typed out whats in the book K & R and the response when i compile
    Then you also need the "getch() and ungetch() were discussed in chapter 4",

    > is a blank command line window which doesn't show anything
    Well it's expecting you to type something in.
    That much should have been obvious.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xml parser
    By holbond in forum C Programming
    Replies: 1
    Last Post: 10-10-2011, 06:01 AM
  2. XML Parser
    By Hexxx in forum C++ Programming
    Replies: 1
    Last Post: 03-30-2006, 07:52 PM
  3. Help! Bad parser :/.
    By Blackroot in forum C++ Programming
    Replies: 13
    Last Post: 03-07-2006, 11:08 AM
  4. Parser Help
    By Barnzey in forum C++ Programming
    Replies: 10
    Last Post: 10-26-2005, 12:10 PM
  5. which parser to use?
    By Ruchikar in forum Windows Programming
    Replies: 0
    Last Post: 07-25-2002, 02:02 AM

Tags for this Thread