17

我正在研究一个解析文本文件的项目,该文件假设是一个简单的编码程序。问题是当我尝试编译程序时出现此错误:

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 的文件,但我知道它工作得很好,因为我已经在所有可能的输入中对其进行了测试并且没有遇到任何问题。

如果有人可以提供帮助,我将不胜感激。

4

5 回答 5

12
AST_NODE* Statement(AST_NODE* node)

缺少分号(主要线索是错误消息“In function 'Statement': ...”),第 24 行也是如此,

   return node

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

于 2012-10-08T10:40:38.047 回答
1
  1. 您似乎包含了一个来自花药的 C 文件。#include通常应仅与头文件一起使用。

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

于 2012-10-08T10:36:15.633 回答
1

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

威斯康

于 2019-05-07T14:57:33.887 回答
0

在解析器函数的末尾附近您错过了“}”

于 2012-10-08T10:38:48.420 回答
0

你的函数声明出错了,看下面这句话!你需要一个分号!</p>


AST_NODE* Statement(AST_NODE* node)
于 2018-07-23T11:34:26.780 回答