English 中文(简体)
2. 胎面错误:yn子错,意料外id,预期美元
原标题:Parser error at 2: syntax error, unexpected id, expecting $end
  • 时间:2024-01-10 05:55:19
  •  标签:
  • bison
  • yacc

我有一张图表: 它用图表表示简单的措辞。

%{
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include "../C_routines/SyntaxTree.h"
#define YYERROR_VERBOSE 1
extern int yylineno;

void yyerror(const char *s);
int i=0;
%}

%union {
    char *lexeme;
    char *value;
    double dvalue;
    struct SyntaxTree *Sy;
    int ivalue;
    char *op;
}

%type <Sy> E S
%token <lexeme> id ID 
%token <value> LITERAL
%token <dvalue> FLOAT
%token <ivalue> INT
%token <op> relop arith assign

%left  +   - 
%left UMINUS

%%

S : id assign E { $$ = newOpNode($2, newIDNode($1), $3); printSyntaxTree($$); }
  ;

E : E arith E { $$ = newOpNode($2, $1, $3); }
  | "(" E ")" { $$ = $2; }
  | "-" E %prec UMINUS { $$ = newOpNode(UMINUS, 0, $2); }
  | id { $$ = newIDNode($1); }
  | INT { $$ = newIntNode($1); }
  | FLOAT { $$ = newDoubleNode($1); }
  ;

%%

void yyerror(const char *s) {
    fprintf(stderr, "Parser error at %d: %s
", yylineno, s);
}

int main() {
    yyparse();
    return 0;
}

My syntax tree:

typedef struct SyntaxTree {
    int nodetype;
    union {
        char *id;
        int intval;
        double doubleval;
        char op;
    } value;
    struct SyntaxTree *l;
    struct SyntaxTree *r;
} SyntaxTree;

#define ID_NODE  I 
#define INT_NODE  D 
#define DOUBLE_NODE  F 
#define OP_NODE  + 
#define UMINUS_NODE  M 

SyntaxTree * newOpNode(char *op, SyntaxTree *l, SyntaxTree *r){
    SyntaxTree *node = malloc(sizeof(SyntaxTree));
    if(!node) {
        fprintf(stderr, "Out of space
");
        exit(1);
    }
    node->nodetype = op;
    node->value.op = op;
    node->l = l;
    node->r = r;
    printf("added new op node");
    return node;
}

SyntaxTree * newDoubleNode(double value){
    SyntaxTree *node = malloc(sizeof(SyntaxTree));
    if(!node) {
        fprintf(stderr, "Out of space
");
        exit(1);
    }
    node->nodetype = DOUBLE_NODE;
    node->value.doubleval = value;
    node->l = node->r = NULL;
    printf("added new double node");
    return node;
}

SyntaxTree * newIntNode(int value){
    SyntaxTree *node = malloc(sizeof(SyntaxTree));
    if(!node) {
        fprintf(stderr, "Out of space
");
        exit(1);
    }
    node->nodetype = INT_NODE;
    node->value.intval = value;
    node->l = node->r = NULL;
    printf("added new int node");
    return node;
}

SyntaxTree * newIDNode(char* id){
    SyntaxTree *node = malloc(sizeof(SyntaxTree));
    if(!node) {
        fprintf(stderr, "Out of space
");
        exit(1);
    }
    node->nodetype = ID_NODE;
    node->value.id = strdup(id);
    node->l = node->r = NULL;
    printf("added new id node");
    return node;
}

void printSyntaxTree(SyntaxTree *head) {
    if (head != NULL) {
        printSyntaxTree(head->l);

        switch (head->nodetype) {
            case ID_NODE:
                printf("Identifier : %s ", head->value.id);
                break;
            case INT_NODE:
                printf("Integer : %d ", head->value.intval);
                break;
            case DOUBLE_NODE:
                printf("Double : %f ", head->value.doubleval);
                break;
            case OP_NODE:
                printf("Arithmetic Operator ");
                break;
            case  = :
                printf("Assignment Operator ");
                break;
            default:
                printf("Unknown node type: %c ", head->nodetype);
                break;
        }

        printSyntaxTree(head->r);
    }
}

当我管理我的教官时,

a=a+b

Identifier : a Assignment Operator

Identifier : a added new id nodeArithmetic Operator

Identifier : b added new id node
a=0

Identifier : a added new op nodeadded new id nodeadded new op nodeParser error at 2: syntax error, unexpected id, expecting $end

它为第2行中我所给的任何事项提供<代码>查询<>。

I use bison for yacc/code> and flex for lex with syntaxtree structure.

什么是? 我怎么说?

我对SO和其他地点进行了回答,但仍未使用。 我甚至搜索了

问题回答

你在图表中有许多问题。 您将+/-定义为没有关联的症状,但并未在图表中使用。 看来,你消费了那些没有关联的ari。

You need to add +/- to your grammar separately from arith. You also need to make uminus right associative.





相关问题
How to turn this into a parser

If I just add on to the following yacc file, will it turn into a parser? /* C-Minus BNF Grammar */ %token ELSE %token IF %token INT %token RETURN %token VOID %token WHILE %token ID %token NUM %...

Bison/Yacc, make literal token return its own value?

Below is my rule, when i replace $2 with = my code works. I know by default all literal tokens uses their ascii value (hence why multi character token require a definition) The below doesnt work. ...

What s wrong with this yacc file?

When I run yacc -d parser.y on the following file I get the following errors: parser.y:23.3-24.4: warning: unused value: $4 15 rules never reduced parser.y: warning: 7 useless nonterminals and 15 ...

How to find shift/reduce conflict in this yacc file?

When I try to use yacc on the following file I get the error conflicts: 1 shift/reduce How can I find and fix the conflict? /* C-Minus BNF Grammar */ %token ELSE %token IF %token INT %token RETURN %...

Deriving a state machine from a BNF grammar

I am trying to put together a proof of concept of an XSS-safe string interpolation scheme. Given a string with substitutions, "Hello <b>$planetoid</b>!" I want break it into literal ...