错误:在'{'令牌之前预期'=',',',';','asm'或'__attribute__'

时间:2012-10-08 10:31:53

标签: c

我正在开发一个解析文本文件的项目,该文件假设是一个简单的编码程序。问题是,当我尝试编译程序时,我得到了这个错误:

In file included from driver.c:10:
parser.c: In function ‘Statement’:
parser.c:24: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
parser.c:153: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
parser.c:159: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
parser.c:167: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
parser.c:176: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
parser.c:185: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
parser.c:194: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
parser.c:201: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
parser.c:209: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
driver.c:19: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
driver.c:50: error: old-style parameter declarations in prototyped function definition
driver.c:50: error: expected ‘{’ at end of input

我不熟悉这个错误,不知道如何修复它。

这是我的parser.c文件,其中发生了错误:

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

AST_NODE* Program(AST_NODE* node);
AST_NODE* Statement(AST_NODE* node)
AST_NODE* AssignStmt(AST_NODE* node);
AST_NODE* Print(AST_NODE *node);
AST_NODE* Repeat(AST_NODE* node);
AST_NODE* Exp(AST_NODE* node);
AST_NODE* Factor(AST_NODE* node);
AST_NODE* Term(AST_NODE* node);


AST_NODE* parser(TOKEN* token,AST_NODE* node, FILE* input_file)
{
    AST_NODE temp = malloc(sizeof(AST_NODE*));

    if(token->type == Id)
    {
        temp-> tok = token;
        node -> child1 = temp;
        return node
    }else
    if(token->type == keyword)
    {
        if(strcmp(node->attribute, "print") == 0)
        {
            temp -> type = print;
            node -> child1 = temp;
            return node;
        }
        else if(strcmp(node->attribute, "repeat") == 0)
        {
            temp -> type = repeat;
            node -> child1 = temp;
            return node;
        }
        return node->prev;
    }else
    if(token->type == num)
    {

        temp->type = factor;
        temp->tok = token;
        AST_NODE temp2 = Exp(Term(temp));
        node-> child3 = temp2

        return node;//back to ID->term->exp then to either print repeat or assignment
    }else
    if(token->type == addOp)
    {
        temp-> tok = token;
        node-> child2 = temp;
        return node;
    }else
    if(token->type == multOp)
    {
        temp-> tok = token;
        node-> child2 = temp;
        return node;
    }else
    if(token->type == assignment)
    {
        temp->type = assignStmt;
        temp->tok = token; 
        node->child2 = temp;
        return node;
    }else
    if(token->type == semicolon)
    {
        temp-> type = assignStmt;
        temp-> tok = token;
        if(node->type == keyword)
        {
            node->child3 = temp;
        }else
        {
            node->child4 = temp;
        }
        return node;
    }else
    if(token->type == lparen)
    {
        temp -> tok = token;
        if(node->type == repeat)
            node->child2 = temp;
        else
            node->child1 = temp;

        return node = node->prev;
    }else
    if(token->type == rparen)
    {
        temp -> tok = token;
        if(node->type == repeat)
            node->child4 = temp;
        else
            node->child3 = temp;

        return node;
    }else if(token->type == newLine)
    {
        while(node->type != program)
        {
            node = node->prev;
        }
        return node;
    }else{

        if(token->type == Id)
        {
            AST_NODE temp2 =  AssignStmt(Program(node));
            temp->type = Id;
            temp->tok = token
            temp->prev = temp2;
            temp2-> child1 = temp;
            return temp2;
        }else if(strcmp(token->attribute,"print"))
        {

            AST_NODE temp2 =  Print(Program(node));
            temp->type = print;
            temp->tok = token
            temp->prev = temp2;
            temp2-> child1 = temp;
            return temp2;
        }else if(strcmp(token->attribute,"repeat"))
        {

            AST_NODE temp2 =  Repeat(Program(node));
            temp->type = repeat;
            temp->tok = token
            temp->prev = temp2;
            temp2-> child1 = temp;
            return temp2;
        }
        printf("error");
        return NULL;
    }


}
AST_NODE* Program(AST_NODE* node)
{
    node->type = program;
    Statement(node);
    return node;
}
AST_NODE* Statement(AST_NODE* node)
{
    AST_NODE temp = malloc(sizeof(AST_NODE*));
    temp-> type = statement;
    temp-> prev = node;
    node->child1-> temp;
    return temp;
}
AST_NODE* AssignStmt(AST_NODE* node)
{
    AST_NODE temp = malloc(sizeof(AST_NODE*));
    temp->type = assignStmt;
    temp-> prev = node;
    node->child1-> temp;
    return temp;

}
AST_NODE* Print(AST_NODE* node)
{
    AST_NODE temp = malloc(sizeof(AST_NODE*));
    temp->type = print;
    temp-> prev = node;
    node->child1-> temp;
    return node;

}
AST_NODE* Repeat(AST_NODE* node)
{
    AST_NODE temp = malloc(sizeof(AST_NODE*));
    temp->type = repeat;
    temp-> prev = node;
    node->child1-> temp;
    return node;

}
AST_NODE* Exp(AST_NODE* node)
{
    AST_NODE temp = malloc(sizeof(AST_NODE*));
    temp->type = exp;
    temp->child1-> node;
    return temp;
}
AST_NODE* factor(AST_NODE* node)
{
    AST_NODE Temp = malloc(sizeof(AST_NODE*));
    temp->type = factor;
    node->child1-> temp;
    return temp;

}
AST_NODE* Term(AST_NODE* node)
{
    AST_NODE temp = malloc(sizeof(AST_NODE*));
    temp->type = term;
    temp->child1-> node;
    return temp;

}

这是我的driver.c文件,其中我也收到错误“原型函数定义中的旧式参数声明预期'{'在输入结束时”。这对我也很陌生。

#include <stdio.h>
#include "parser.c"
#include "parser.h"




AST_NODE* parser(TOKEN* token,AST_NODE node, FILE *input_file);

int main(void)
{
    TREE *current = 0;
    FILE *input_file = fopen("test.txt", "r");
    TOKEN *token = (TOKEN*) malloc(sizeof(TOKEN));
    TOKEN *tok = (TOKEN*) malloc(sizeof(TOKEN));
    AST_NODE* node = malloc(sizeof(AST_NODE));

    while(!feof(input_file))
    {
    token = scan(input_file);

        if(token->type != null)
        {
            parser(token,node,input_file);
            printf("%s", token->attribute);
            if(token->checkR == ';')
            {

                tok->type = semicolon;
                tok->attribute = ";";
                parser(tok,node,input_file);            
            }if(token->checkR == ')')
            {
                tok->type = rparen;
                tok->attribute = ")";
                parser(tok,node,input_file);
            }
        }
    }
    fclose(input_file);
    return 0;
}

这是我的parser.h文件,我在其中声明我的TOKEN和我的AST_NODE来创建我的树并形成我的标记来填充树。

#ifndef _parser_h
#define _parser_h


typedef enum token_type
{
    null,
    Id,
    keyword,
    num,
    addOp,
    multOp,
    assignment,
    semicolon,
    lparen,
    rparen,
    newLine
}TOKEN_TYPE;

typedef struct token{
    int type;
    char *attribute;
    char checkR;
}TOKEN;

typedef enum node_type
{
    program,
    statement,
    assignStmt,
    print,
    repeat,
    exp,
    factor,
    term
}NODE_TYPE;

typedef struct ast_node{
    NODE_TYPE type;
    TOKEN *tok;
    struct AST_NODE *prev;
    struct AST_NODE *child1;
    struct AST_NODE *child2;
    struct AST_NODE *child3;
    struct AST_NODE *child4;
    struct AST_NODE *child5;


}AST_NODE;

#endif

还有一个名为scanner.c的文件,但我知道它的工作正常,因为我已经在所有可能的输入中测试了它并且没有任何问题。

如果有人能提供帮助,我会非常感激。

5 个答案:

答案 0 :(得分:9)

AST_NODE* Statement(AST_NODE* node)

缺少分号(主要线索是错误消息“在函数'语句中:......”),第24行也是如此,

   return node

(一旦你解决了这些问题,你就会遇到其他问题,其中一些问题在这里被其他人提及。)

答案 1 :(得分:1)

  1. 您似乎要包含来自花药的一个C文件。 #include通常只应与头文件一起使用。

  2. struct ast_node的定义中,您引用的struct AST_NODE不存在。 C区分大小写。

答案 2 :(得分:0)

在解析器函数末尾附近你错过了'}'

答案 3 :(得分:0)

函数声明中发生错误,请看下面的句子!您需要分号!


AST_NODE* Statement(AST_NODE* node)

答案 4 :(得分:0)

我在代码中遇到了同样的问题,我所做的是我发现了对上一次正确编译所做的所有更改。而且我观察到一个函数声明没有“;”而且它正在传递一个值,我已经声明它不传递任何“无效”。 这种方法肯定会解决很多问题。

Viscon