阅读背景:

YACC:使用符号****,但未定义为令牌且没有规则

来源:互联网 
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "parser.h"
extern int yylval;
%}
%start E
%token number
%%
S   :E  {printf("%s",
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "parser.h"
extern int yylval;
%}
%start E
%token number
%%
S   :E  {printf("%s",$1);}
    ;
E   :E PLUS T   {$$ = $1 + $3;}
    |T  {$$ = $1;}
    ;
T   :T STAR F   {$$ = $1 * $3;}
    |F  {$$ = $1;}
    ;
F   :LP E RP    {$$ = $2;}
    |number {$$ = $1;}
    ;
%%

yylex(){
    char ch;
    ch = getchar();

    switch(ch){
        case '+':
            return PLUS;
            break;
        case '*':
            return STAR;
            break;
        case '(':
            return LP;
            break;
        case ')':
            return RP;
            break;
        default:
            if(ch >= '0' && ch <= '9'){
                ch -= '0';
                yylval = ch;;
                return (number);
            }
            else
                return -1;
    }
}

void main(){
    yyparse();
}

Afternoon, I was doing my assignment about how to use yacc and the thing is that I don't know how to properly include a header file on a .y file. From what I've seen to various pages which describes how to write a .y file, there is nothing I've done wrong about writing this .y file. So please, help me!

下午,我正在做关于如何使用yacc的任务,事情是我不知道如何在.y文件中正确包含头文件。从我所看到的各种描述如何编写.y文件的页面来看,编写这个.y文件时我没有做错任何事。所以,请帮助我!

These are the error message I got

这些是我收到的错误消息

embedded@embedded-P15xEMx:~/Project/Compiler$ yacc parser.y
parser.y:19.10-11: error: symbol LP is used, but is not defined as a token and has no rules
 F  :LP E RP    {$$ = $2;}
          ^^
parser.y:13.12-15: error: symbol PLUS is used, but is not defined as a token and has no rules
 E  :E PLUS T   {$$ = $1 + $3;}
            ^^^^
parser.y:19.15-16: error: symbol RP is used, but is not defined as a token and has no rules
 F  :LP E RP    {$$ = $2;}
               ^^
parser.y:16.12-15: error: symbol STAR is used, but is not defined as a token and has no rules
 T  :T STAR F   {$$ = $1 * $3;}
            ^^^^

And this is the header file I included

这是我包含的头文件

#ifndef _PARSER_H_
#define _PARSER_H_

    #include <stdio.h>

    #define PLUS 1
    #define STAR 2
    #define LP 3
    #define RP 4
    #define number 5

    #endif

1 个解决方案

#1


2  

The token symbols can't be declared by yourself, you have to use Yacc to do it, like

令牌符号不能由您自己声明,您必须使用Yacc来执行此操作,例如

%token PLUS

etc.

等等


);} ; E :E PLUS T {$$ =
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "parser.h"
extern int yylval;
%}
%start E
%token number
%%
S   :E  {printf("%s",$1);}
    ;
E   :E PLUS T   {$$ = $1 + $3;}
    |T  {$$ = $1;}
    ;
T   :T STAR F   {$$ = $1 * $3;}
    |F  {$$ = $1;}
    ;
F   :LP E RP    {$$ = $2;}
    |number {$$ = $1;}
    ;
%%

yylex(){
    char ch;
    ch = getchar();

    switch(ch){
        case '+':
            return PLUS;
            break;
        case '*':
            return STAR;
            break;
        case '(':
            return LP;
            break;
        case ')':
            return RP;
            break;
        default:
            if(ch >= '0' && ch <= '9'){
                ch -= '0';
                yylval = ch;;
                return (number);
            }
            else
                return -1;
    }
}

void main(){
    yyparse();
}

Afternoon, I was doing my assignment about how to use yacc and the thing is that I don't know how to properly include a header file on a .y file. From what I've seen to various pages which describes how to write a .y file, there is nothing I've done wrong about writing this .y file. So please, help me!

下午,我正在做关于如何使用yacc的任务,事情是我不知道如何在.y文件中正确包含头文件。从我所看到的各种描述如何编写.y文件的页面来看,编写这个.y文件时我没有做错任何事。所以,请帮助我!

These are the error message I got

这些是我收到的错误消息

embedded@embedded-P15xEMx:~/Project/Compiler$ yacc parser.y
parser.y:19.10-11: error: symbol LP is used, but is not defined as a token and has no rules
 F  :LP E RP    {$$ = $2;}
          ^^
parser.y:13.12-15: error: symbol PLUS is used, but is not defined as a token and has no rules
 E  :E PLUS T   {$$ = $1 + $3;}
            ^^^^
parser.y:19.15-16: error: symbol RP is used, but is not defined as a token and has no rules
 F  :LP E RP    {$$ = $2;}
               ^^
parser.y:16.12-15: error: symbol STAR is used, but is not defined as a token and has no rules
 T  :T STAR F   {$$ = $1 * $3;}
            ^^^^

And this is the header file I included

这是我包含的头文件

#ifndef _PARSER_H_
#define _PARSER_H_

    #include <stdio.h>

    #define PLUS 1
    #define STAR 2
    #define LP 3
    #define RP 4
    #define number 5

    #endif

1 个解决方案

#1


2  

The token symbols can't be declared by yourself, you have to use Yacc to do it, like

令牌符号不能由您自己声明,您必须使用Yacc来执行此操作,例如

%token PLUS

etc.

等等


+ ;} |T {$$ =
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "parser.h"
extern int yylval;
%}
%start E
%token number
%%
S   :E  {printf("%s",$1);}
    ;
E   :E PLUS T   {$$ = $1 + $3;}
    |T  {$$ = $1;}
    ;
T   :T STAR F   {$$ = $1 * $3;}
    |F  {$$ = $1;}
    ;
F   :LP E RP    {$$ = $2;}
    |number {$$ = $1;}
    ;
%%

yylex(){
    char ch;
    ch = getchar();

    switch(ch){
        case '+':
            return PLUS;
            break;
        case '*':
            return STAR;
            break;
        case '(':
            return LP;
            break;
        case ')':
            return RP;
            break;
        default:
            if(ch >= '0' && ch <= '9'){
                ch -= '0';
                yylval = ch;;
                return (number);
            }
            else
                return -1;
    }
}

void main(){
    yyparse();
}

Afternoon, I was doing my assignment about how to use yacc and the thing is that I don't know how to properly include a header file on a .y file. From what I've seen to various pages which describes how to write a .y file, there is nothing I've done wrong about writing this .y file. So please, help me!

下午,我正在做关于如何使用yacc的任务,事情是我不知道如何在.y文件中正确包含头文件。从我所看到的各种描述如何编写.y文件的页面来看,编写这个.y文件时我没有做错任何事。所以,请帮助我!

These are the error message I got

这些是我收到的错误消息

embedded@embedded-P15xEMx:~/Project/Compiler$ yacc parser.y
parser.y:19.10-11: error: symbol LP is used, but is not defined as a token and has no rules
 F  :LP E RP    {$$ = $2;}
          ^^
parser.y:13.12-15: error: symbol PLUS is used, but is not defined as a token and has no rules
 E  :E PLUS T   {$$ = $1 + $3;}
            ^^^^
parser.y:19.15-16: error: symbol RP is used, but is not defined as a token and has no rules
 F  :LP E RP    {$$ = $2;}
               ^^
parser.y:16.12-15: error: symbol STAR is used, but is not defined as a token and has no rules
 T  :T STAR F   {$$ = $1 * $3;}
            ^^^^

And this is the header file I included

这是我包含的头文件

#ifndef _PARSER_H_
#define _PARSER_H_

    #include <stdio.h>

    #define PLUS 1
    #define STAR 2
    #define LP 3
    #define RP 4
    #define number 5

    #endif

1 个解决方案

#1


2  

The token symbols can't be declared by yourself, you have to use Yacc to do it, like

令牌符号不能由您自己声明,您必须使用Yacc来执行此操作,例如

%token PLUS

etc.

等等


;} ; T :T STAR F {$$ =
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "parser.h"
extern int yylval;
%}
%start E
%token number
%%
S   :E  {printf("%s",$1);}
    ;
E   :E PLUS T   {$$ = $1 + $3;}
    |T  {$$ = $1;}
    ;
T   :T STAR F   {$$ = $1 * $3;}
    |F  {$$ = $1;}
    ;
F   :LP E RP    {$$ = $2;}
    |number {$$ = $1;}
    ;
%%

yylex(){
    char ch;
    ch = getchar();

    switch(ch){
        case '+':
            return PLUS;
            break;
        case '*':
            return STAR;
            break;
        case '(':
            return LP;
            break;
        case ')':
            return RP;
            break;
        default:
            if(ch >= '0' && ch <= '9'){
                ch -= '0';
                yylval = ch;;
                return (number);
            }
            else
                return -1;
    }
}

void main(){
    yyparse();
}

Afternoon, I was doing my assignment about how to use yacc and the thing is that I don't know how to properly include a header file on a .y file. From what I've seen to various pages which describes how to write a .y file, there is nothing I've done wrong about writing this .y file. So please, help me!

下午,我正在做关于如何使用yacc的任务,事情是我不知道如何在.y文件中正确包含头文件。从我所看到的各种描述如何编写.y文件的页面来看,编写这个.y文件时我没有做错任何事。所以,请帮助我!

These are the error message I got

这些是我收到的错误消息

embedded@embedded-P15xEMx:~/Project/Compiler$ yacc parser.y
parser.y:19.10-11: error: symbol LP is used, but is not defined as a token and has no rules
 F  :LP E RP    {$$ = $2;}
          ^^
parser.y:13.12-15: error: symbol PLUS is used, but is not defined as a token and has no rules
 E  :E PLUS T   {$$ = $1 + $3;}
            ^^^^
parser.y:19.15-16: error: symbol RP is used, but is not defined as a token and has no rules
 F  :LP E RP    {$$ = $2;}
               ^^
parser.y:16.12-15: error: symbol STAR is used, but is not defined as a token and has no rules
 T  :T STAR F   {$$ = $1 * $3;}
            ^^^^

And this is the header file I included

这是我包含的头文件

#ifndef _PARSER_H_
#define _PARSER_H_

    #include <stdio.h>

    #define PLUS 1
    #define STAR 2
    #define LP 3
    #define RP 4
    #define number 5

    #endif

1 个解决方案

#1


2  

The token symbols can't be declared by yourself, you have to use Yacc to do it, like

令牌符号不能由您自己声明,您必须使用Yacc来执行此操作,例如

%token PLUS

etc.

等等


* ;} |F {$$ =
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "parser.h"
extern int yylval;
%}
%start E
%token number
%%
S   :E  {printf("%s",$1);}
    ;
E   :E PLUS T   {$$ = $1 + $3;}
    |T  {$$ = $1;}
    ;
T   :T STAR F   {$$ = $1 * $3;}
    |F  {$$ = $1;}
    ;
F   :LP E RP    {$$ = $2;}
    |number {$$ = $1;}
    ;
%%

yylex(){
    char ch;
    ch = getchar();

    switch(ch){
        case '+':
            return PLUS;
            break;
        case '*':
            return STAR;
            break;
        case '(':
            return LP;
            break;
        case ')':
            return RP;
            break;
        default:
            if(ch >= '0' && ch <= '9'){
                ch -= '0';
                yylval = ch;;
                return (number);
            }
            else
                return -1;
    }
}

void main(){
    yyparse();
}

Afternoon, I was doing my assignment about how to use yacc and the thing is that I don't know how to properly include a header file on a .y file. From what I've seen to various pages which describes how to write a .y file, there is nothing I've done wrong about writing this .y file. So please, help me!

下午,我正在做关于如何使用yacc的任务,事情是我不知道如何在.y文件中正确包含头文件。从我所看到的各种描述如何编写.y文件的页面来看,编写这个.y文件时我没有做错任何事。所以,请帮助我!

These are the error message I got

这些是我收到的错误消息

embedded@embedded-P15xEMx:~/Project/Compiler$ yacc parser.y
parser.y:19.10-11: error: symbol LP is used, but is not defined as a token and has no rules
 F  :LP E RP    {$$ = $2;}
          ^^
parser.y:13.12-15: error: symbol PLUS is used, but is not defined as a token and has no rules
 E  :E PLUS T   {$$ = $1 + $3;}
            ^^^^
parser.y:19.15-16: error: symbol RP is used, but is not defined as a token and has no rules
 F  :LP E RP    {$$ = $2;}
               ^^
parser.y:16.12-15: error: symbol STAR is used, but is not defined as a token and has no rules
 T  :T STAR F   {$$ = $1 * $3;}
            ^^^^

And this is the header file I included

这是我包含的头文件

#ifndef _PARSER_H_
#define _PARSER_H_

    #include <stdio.h>

    #define PLUS 1
    #define STAR 2
    #define LP 3
    #define RP 4
    #define number 5

    #endif

1 个解决方案

#1


2  

The token symbols can't be declared by yourself, you have to use Yacc to do it, like

令牌符号不能由您自己声明,您必须使用Yacc来执行此操作,例如

%token PLUS

etc.

等等


;} ; F :LP E RP {$$ = ;} |number {$$ =
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "parser.h"
extern int yylval;
%}
%start E
%token number
%%
S   :E  {printf("%s",$1);}
    ;
E   :E PLUS T   {$$ = $1 + $3;}
    |T  {$$ = $1;}
    ;
T   :T STAR F   {$$ = $1 * $3;}
    |F  {$$ = $1;}
    ;
F   :LP E RP    {$$ = $2;}
    |number {$$ = $1;}
    ;
%%

yylex(){
    char ch;
    ch = getchar();

    switch(ch){
        case '+':
            return PLUS;
            break;
        case '*':
            return STAR;
            break;
        case '(':
            return LP;
            break;
        case ')':
            return RP;
            break;
        default:
            if(ch >= '0' && ch <= '9'){
                ch -= '0';
                yylval = ch;;
                return (number);
            }
            else
                return -1;
    }
}

void main(){
    yyparse();
}

Afternoon, I was doing my assignment about how to use yacc and the thing is that I don't know how to properly include a header file on a .y file. From what I've seen to various pages which describes how to write a .y file, there is nothing I've done wrong about writing this .y file. So please, help me!

下午,我正在做关于如何使用yacc的任务,事情是我不知道如何在.y文件中正确包含头文件。从我所看到的各种描述如何编写.y文件的页面来看,编写这个.y文件时我没有做错任何事。所以,请帮助我!

These are the error message I got

这些是我收到的错误消息

embedded@embedded-P15xEMx:~/Project/Compiler$ yacc parser.y
parser.y:19.10-11: error: symbol LP is used, but is not defined as a token and has no rules
 F  :LP E RP    {$$ = $2;}
          ^^
parser.y:13.12-15: error: symbol PLUS is used, but is not defined as a token and has no rules
 E  :E PLUS T   {$$ = $1 + $3;}
            ^^^^
parser.y:19.15-16: error: symbol RP is used, but is not defined as a token and has no rules
 F  :LP E RP    {$$ = $2;}
               ^^
parser.y:16.12-15: error: symbol STAR is used, but is not defined as a token and has no rules
 T  :T STAR F   {$$ = $1 * $3;}
            ^^^^

And this is the header file I included

这是我包含的头文件

#ifndef _PARSER_H_
#define _PARSER_H_

    #include <stdio.h>

    #define PLUS 1
    #define STAR 2
    #define LP 3
    #define RP 4
    #define number 5

    #endif

1 个解决方案

#1


2  

The token symbols can't be declared by yourself, you have to use Yacc to do it, like

令牌符号不能由您自己声明,您必须使用Yacc来执行此操作,例如

%token PLUS

etc.

等等


;} ; %% yylex(){ char ch; ch = getchar(); switch(ch){ case '+': return PLUS; break; case '*': return STAR; break; case '(': return LP; break; case ')': return RP; break; default: if(ch >= '0' && ch <= '9'){ ch -= '0'; yylval = ch;; return (number); } else return -1; } } void main(){ yyparse(); } %{ #include <stdio.h> #include <stdlib.h> #incl



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: