簡體   English   中英

我的 parser.y (FLEX/BISON) 有問題。 如何申報“$4”?

[英]Problem with my parser.y (FLEX/BISON). How to declare '$4'?

我正在學習 Flex/Bison,我們目前正在處理語義方面的問題,之前已經處理過詞匯和語法錯誤。 我在谷歌上進行了大量搜索,但未能找到解決我的錯誤的方法。 當我認為它是自動完成時,我無法理解為什么我需要聲明“$4”。

當我嘗試 makefile 時出現此錯誤:

柔性掃描儀.l

mv lex.yy.c 掃描儀.c

野牛-d -v parser.y

paser.y:114.71-72: 錯誤:'case' 的 $4 沒有聲明的類型

114 | case WHEN INT_LITERAL ARROW statement_ {case_statements.push_back($4);};

這是我試圖遵循的偽代碼:

陳述:

CASE expression IS cases OTHERS ARROW statement_ ENDCASE

    {If the attribute of cases, is a number then

    return it as the attribute otherwise return the

    attribute of the OTHERS clause};

案例:

cases case

    {if the attribute of cases is a number then return it as the
    attribute otherwise return the attribute of case} |

%empty

    {Set the attribute to the sentinel NAN} ;

案件:

WHEN INT_LITERAL ARROW statement_

    {$-2 contains the value of the expression after CASE.

    It must be compared with the attribute of INT_LITERAL.

    If they match the attribute of this production

    should become the attribute of statement_

    If they don't match, the attribute should be set to the

    sentinel value NAN} ;

解析器.y:

%{

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <math.h>

using namespace std;

#include "values.h"
#include "listing.h"
#include "symbols.h"
#include <stdlib.h>
#include <stdio.h>

int yylex();
void yyerror(const char* message);

Symbols<int> symbols;

//----------------------------------------------------------------------------------------------

vector<int> case_statements;  //<<<<<<<<<<<<Is this wrong?

//---------------------------------------------------------------------------------------------

int result;
double *params;

%}

%define parse.error verbose

%union
{
    CharPtr iden;
    Operators oper;
    int value;
}

%token <iden> IDENTIFIER
%token <value> INT_LITERAL REAL_LITERAL BOOL_LITERAL CASE TRUE FALSE

%token <oper> ADDOP MULOP RELOP OROP NOTOP REMOP EXPOP
%token ANDOP

%token BEGIN_ BOOLEAN END ENDREDUCE FUNCTION INTEGER IS REDUCE RETURNS
%token THEN WHEN ARROW
%token ELSE ENDCASE ENDIF IF OTHERS REAL

%type <value> body statement_ statement reductions expression relation term
    factor case cases exponent unary primary
%type <oper> operator

%%

function:   
    function_header optional_variable body {result = $3;} ;
    
function_header:    
    FUNCTION IDENTIFIER optional_parameter RETURNS type ';' |
    FUNCTION IDENTIFIER RETURNS type ';' |
    error ';' ;

optional_variable:
    optional_variable variable | 
    error ';' | 
    %empty ;

variable:   
    IDENTIFIER ':' type IS statement_ ;

parameters:
    parameter optional_parameter;

optional_parameter:
    optional_parameter ',' parameter |
    %empty ;

parameter:
    IDENTIFIER ':' type {symbols.insert($1, params[0]);} ;

type:
    INTEGER |
    REAL |
    BOOLEAN ;

body:
    BEGIN_ statement_ END ';' {$$ = $2;} ;
    
statement_:
    statement ';' |
    error ';' {$$ = 0;} ;
    
statement:
    expression |
    REDUCE operator reductions ENDREDUCE {$$ = $3;} |
    IF expression THEN statement_ ELSE statement_ ENDIF {
        if ($2 == true) {
            $$ = $4;
        }
        else {
            $$ = $6;
        }
    } ; |
    CASE expression IS cases OTHERS ARROW statement_ ENDCASE {$$ = $<value>4 == $1 ? $4 : $7;} ;

cases:
    cases case {$$ = $<value>1 == $1 ? $1 : $2;} |
    %empty {$$ = NAN;} ;

//-----------------------------------------------------------------------------------------------------------

case:
    case WHEN INT_LITERAL ARROW statement_ {case_statements.push_back($4);} ;  //<<<<<<<<<How do I declare $4?

//-------------------------------------------------------------------------------------------------------------

operator:
    ADDOP |
    RELOP |
    EXPOP |
    MULOP ;

reductions:
    reductions statement_ {$$ = evaluateReduction($<oper>0, $1, $2);} |
    {$$ = $<oper>0 == ADD ? 0 : 1;} %empty ;

expression:
    expression OROP relation {$$ = $1 || $3;} |
    relation ;

expression:
    expression ANDOP relation {$$ = $1 && $3;} |
    relation ;

relation:
    relation RELOP term {$$ = evaluateRelational($1, $2, $3);} |
    term ;

term:
    term ADDOP factor {$$ = evaluateArithmetic($1, $2, $3);} |
    factor ;
      
factor:
    factor MULOP primary {$$ = evaluateArithmetic($1, $2, $3);} |
    factor REMOP exponent {$$ = $1 % $3;} |
    exponent ;

exponent:
    unary |
    unary EXPOP exponent {$$ = pow($1, $3);} ;

unary:
    NOTOP primary {$$ = $2;} |
    primary;

primary:
    '(' expression ')' {$$ = $2;} |
    INT_LITERAL |
    REAL_LITERAL |
    BOOL_LITERAL |
    IDENTIFIER {if (!symbols.find($1, $$)) appendError(UNDECLARED, $1);} ;

%%

void yyerror(const char* message)
{
    appendError(SYNTAX, message);
}

int main(int argc, char *argv[])    
{
    params = new double[argc - 1]
    for (int i = 1; i < argc; i++)
    {
        params[i - 1] = atof(argv[i]);
    }
    firstLine();
    yyparse();
    if (lastLine() == 0)
        cout << "Result = " << result << endl;
    return 0;
} 

您需要為statement_分配一個值/類型:

statement_:
    statement ';' {$$ = $1;}|
    error ';' {$$ = MISMATCH;} ;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM