简体   繁体   English

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

[英]error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token

I am working on a project that parses a text file thats suppose to be a simple coded program. 我正在研究一个解析文本文件的项目,该文件假设是一个简单的编码程序。 The problem is that when I try to complile the program I get this error: 问题是,当我尝试编译程序时,我收到此错误:

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

Im not familiar with this error and not sure how to fix it. 我不熟悉这个错误,不知道如何解决它。

Here is my parser.c file which the error is happening in: 这是我的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;

}

Here is my driver.c file where I am also getting the error "old-style parameter declarations in prototyped function definition expected '{' at end of input". 这是我的driver.c文件,其中我还得到错误“原型函数定义中的旧式参数声明预期'{'在输入结束时”。 This also I am very unfamiliar with. 这对我也很陌生。

#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;
}

Here is my parser.h file where I declare my TOKEN and my AST_NODE to create my tree and form my tokens to fill the tree. 这是我的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

There is one more file called scanner.c but I know its working perfectly because I have tested it in all the possible inputs and got no problems. 还有一个名为scanner.c的文件,但我知道它的工作正常,因为我已经在所有可能的输入中测试了它并没有遇到任何问题。

If anyone could help I would appreciate it very much. 如果有人能提供帮助,我会非常感激。

AST_NODE* Statement(AST_NODE* node)

is missing a semicolon (a major clue was the error message "In function 'Statement': ...") and so is line 24, 缺少一个分号(一个主要线索是错误信息“在函数'语句':......”),第24行也是如此,

   return node

(Once you fix those, you will encounter other problems, some of which are mentioned by others here.) (一旦你解决了这些问题,你将遇到其他问题,其中一些问题在这里被其他人提及。)

  1. You seem to be including one C file from anther. 你似乎包括来自花药的一个C文件。 #include should normally be used with header files only. #include通常只应与头文件一起使用。

  2. Within the definition of struct ast_node you refer to struct AST_NODE , which doesn't exist. struct ast_node的定义中,您可以引用struct AST_NODE ,它不存在。 C is case-sensitive. C区分大小写。

I encountered the same problem in the code and What I did is I found out all the changes I have made from the last correct compilation. 我在代码中遇到了同样的问题,我所做的是我发现了上次正确编译所做的所有更改。 And I have observed one function declaration was without ";" 我观察到一个函数声明没有“;” and also it was passing a value and I have declared it to pass nothing "void". 而且它传递了一个值,我宣称它没有通过任何“无效”。 this method will surely solve the problem for many. 这种方法肯定会解决很多问题。

Viscon VISCON

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

函数声明中出现错误,请看下面的句子!你需要一个分号!


AST_NODE* Statement(AST_NODE* node)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:“ {”令牌|之前的预期&#39;=&#39;,&#39;,&#39;,&#39;;&#39;,&#39;asm&#39;或&#39;__attribute__&#39; - error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token| 错误:在&#39;:&#39;标记前应有&#39;=&#39;,&#39;,&#39;,&#39;;&#39;,&#39;asm&#39;或&#39;__attribute__&#39; - error: expected '=', ',', ';', 'asm' or '__attribute__' before ':' token :1:错误:在&#39;{&#39;标记之前应为&#39;=&#39;,&#39;,&#39;,&#39;;&#39;,&#39;asm&#39;或&#39;__attribute__&#39; - :1: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token 错误:在 '.' 之前应有 '='、','、';'、'asm' 或 '__attribute__' 令牌 - error: expected '=', ',', ';', 'asm' or '__attribute__' before '.' token 错误:在“*”标记之前应有“=”、“、”、“;”、“asm”或“__attribute__” - error: expected '=', ',', ';', 'asm' or '__attribute__' before '*' token 仍然是“错误:在&#39;*&#39;标记之前出现预期的&#39;=&#39;,&#39;,&#39;,&#39;;&#39;,&#39;asm&#39;或&#39;__attribute__&#39;” - Still the “error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token” 在&#39;&#39;之前预期&#39;=&#39;,&#39;,&#39;,&#39;;&#39;,&#39;asm&#39;或&#39;__attribute__&#39;。 代币 - expected '=', ',', ';', 'asm' or '__attribute__' before '.' token “在&#39;-&gt;&#39;标记之前的预期&#39;=&#39;,&#39;,&#39;,&#39;;&#39;,&#39;asm&#39;或&#39;__attribute__&#39; - "Expected '=', ',', ';', 'asm' or '__attribute__' before '->' token 在&#39;{&#39;令牌之前预期&#39;=&#39;,&#39;,&#39;,&#39;;&#39;,&#39;asm&#39;或&#39;__attribute__&#39; - expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token 错误:在&#39;int&#39;之前预期&#39;=&#39;,&#39;,&#39;,&#39;;&#39;,&#39;asm&#39;或&#39;__attribute__&#39; - error: expected '=', ',', ';', 'asm' or '__attribute__' before 'int'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM