繁体   English   中英

算术表达式或在yacc语法中未正确解析

[英]Arithmetic expression nor properly parsed in yacc grammar

我有以下yacc

%{
#include  <stdio.h>
extern FILE* yyin;
extern char* yytext;

%}

%token VAR ID_NAME TYPE_STRING TYPE_BOOL TYPE_NUMBER
%token CONST VALUE_STRING VALUE_BOOL VALUE_NUMBER

%left '*' '/'
%left '+' '-'

%%

program
    : declarations
    ;

declarations
    : declaration
    | declarations declaration
    ;

declaration
    : var_declaration
    | const_declaration
    ;

value
    : VALUE_BOOL
    | VALUE_STRING
    ;

arithmetic_expression
    : arithmetic_expression '+' arithmetic_expression
    | arithmetic_expression '-' arithmetic_expression
    | arithmetic_expression '*' arithmetic_expression
    | arithmetic_expression '/' arithmetic_expression
    | '(' arithmetic_expression ')'
    | VALUE_NUMBER
    ;

initializer
    : value
    | arithmetic_expression
    ;

initialization
    : ID_NAME '=' initializer
    ;

init_list
    : initialization
    | init_list ',' initialization
    ;

init_or_id
    : initialization
    | ID_NAME
    ;

init_or_id_list
    : init_or_id
    | init_or_id_list ',' init_or_id
    ;

var_declaration
    : VAR ':' type init_or_id_list ';' { printf("%s booyah\n", $1);  } 
    ;

const_declaration: CONST ':' type init_list ';' {printf("koskos\n");}
    ;

type: TYPE_NUMBER 
    | TYPE_STRING
    | TYPE_BOOL
    ;

%%
void yyerror (char const *s) {
    fprintf (stderr, "%s\n", s);
}

int main(int argc, char** argv[])
{

    yyparse();
    return 0;
}

功能之一应该是允许用户使用算术表达式(的值)初始化变量和常量。 像这样的var:number = (1+2+3); 但是,我不确定为什么,但是解析器仅识别使用运算符*/表达式。 给我一个语法错误,当我使用表达式其利用运营商的+-

这是关联的lex文件:

%{

#include <stdio.h>
#include "y.tab.h"

%}

id_name [a-zA-Z]([a-zA-Z0-9])*
value_string \"([a-zA-Z0-9*+z=])*\"
value_number [+-]?([0-9]*[.])?[0-9]+

%%

"var"                   { return VAR;  }
"const"                 { return CONST; }
"string"                { return TYPE_STRING; }
"number"                { return TYPE_NUMBER; }
"bool"                  { return TYPE_BOOL; }
"true"                  { return VALUE_BOOL; }
"false"                 { return VALUE_BOOL; }

{value_number}          { return VALUE_NUMBER; }
{value_string}          { return VALUE_STRING; }
{id_name}               { return ID_NAME; }


","                     { return ','; }
":"                     { return ':'; }
";"                     { return ';'; }
"="                     { return '='; }
"+"                     { return '+'; }
"-"                     { return '-'; }
"*"                     { return '*'; }
"/"                     { return '/'; }
"("                     { return '('; }
")"                     { return ')'; }


%%

我编译使用以下命令的文件:

yacc -vd grammar.y
flex -l lex.l
gcc -Wall -o lang lex.yy.c y.tab.c -ll -lfl

例如,以下表达式: var:number var1=12*12; 被成功识别。 但是这一个: var:number var1=12+12; 导致syntax error 我不确定我的意思是什么。

尝试使用flex --debug选项。 这将生成一个扫描仪,向您显示其运行情况的痕迹。 我想您会很快发现问题。

扰流板

+12被词法化为单个数字令牌,而不是运算符,后跟数字

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM