Text to declaration: Write a program to convert input in a specific format into C language declarations

Publisher:BlissfulJoyLatest update time:2016-03-08 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
1. Program Function
Write a program to convert input in a specific format into C language statements.
 
2. Program source code
#include
#include
#include
 
#define MAXTOKEN 100
 
enum {NAME, PARENS, BRACKETS};
 
int tokentype;
char token[MAXTOKEN];
char name[MAXTOKEN];
char datatype[MAXTOKEN];
char out[1000];
int p_getch(void);
void p_ungetch(int);
 
int main(void)
{
    int type, lasttype;
    char temp[MAXTOKEN];
    
    printf("Please input(ctrl+z to quit)\n");
    while (gettoken() != EOF)
    {
        
        strcpy(out, token);
        lasttype = 0;
        while ((type = gettoken()) != '\n')
       {
            if (type == NAME)
           {
                sprintf(temp, "%s %s", token, out);
                strcpy(out, temp);    
            }    
            else
           {
                if (lasttype == '*')
               {
                    sprintf(temp, "(%s)", out);
                   strcpy(out, temp);    
               }    
                if (type == PARENS || type == BRACKETS)
                    strcat(out, token);
                else if (type == '*')
               {
                    sprintf(temp, "*%s", out);
                   strcpy(out, temp);    
                }
               else
                    printf("Error: Invalid input at %s!\n", token);
           }
            lasttype = type;
       }
        printf("%s\n",out);
   }    
 
    return 0;
}
 
int gettoken(void)
{
    int c;
    char *p = token;
    
    while ((c = p_getch()) == ' ' || c == '\t') 
        ;
        
    if (c == '(')
   {
        if ((c = p_getch()) == ')')
       {
            strcpy(token, "()");
           return tokentype = PARENT;    
       }    
       else
       {
            p_ungetch(c);
            return tokentype = '(';    
       }
   }
    else if (c == '[')
   {
        for (*p++ = c; (*p++ = p_getch()) != ']';)  
           ;
        *p = '\0';
        return tokentype = BRACKETS;  
   }
    else if (isalpha(c))
   {
        for (*p++ = c; isalnum(c = p_getch());)
            *p++ = c;
        *p = '\0'; 
       p_ungetch(c);
        return tokentype = NAME;   
   }
   else
        return tokentype = c;
}
 
#define BUFSIZE 100
 
char buf[BUFSIZE];
int bufp = 0;
 
int p_getch(void)
{
    return (bufp > 0) ? buf[--bufp]: getchar();    
}
 
void p_ungetch(int c)
{
    if (bufp < BUFSIZE)
       buf[bufp++] = c;
   else
        printf("Error: (ungetch) Too many chars in buffer1\n");    
}
 
3. Summary of the program
for example: 
Input: x * char
Output: char *x
 
Input: daytab * [13] int
Output: int (*daytab)[13]
Reference address:Text to declaration: Write a program to convert input in a specific format into C language declarations

Previous article:Declaration to text: Write a program to convert C language declarations into text descriptions
Next article:Write a program to sort the input string (you can specify the characters to compare)

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号