野牛问题 - 开始符号不会得出任何句子

时间:2014-09-14 23:44:25

标签: c++ compiler-construction bison flex-lexer

我一直在"致命错误:开始符号N_START没有得出任何句子。我完全迷失了这段代码可能出错的地方。

我查看过以前的版本,但我找不到任何与众不同的内容。

这是代码(长D:)

%error-verbose
%{

#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string>
#include <cstring>

using namespace std;

void printTokenInfo(char* tokenType, char* lexeme);
void printRule(const char *lhs, const char *rhs);
int yyerror(const char *s);

int numLines = 0; 

extern "C" {
 int yyparse(void);
 int yylex(void);
 int yywrap() { return 1; }
}

%}

/* Token declarations */
%token T_ASSIGN T_MULT T_PLUS T_MINUS T_DIV
%token T_AND T_OR T_NOT T_LT T_GT
%token T_LE T_GE T_EQ T_NE T_VAR
%token T_ARRAY T_OF T_BOOL T_CHAR T_INT
%token T_PROG T_PROC T_BEGIN T_END T_WHILE
%token T_DO T_IF T_READ T_UNKNOWN
%token T_WRITE T_TRUE T_FALSE T_LBRACK T_RBRACK
%token T_SCOLON T_COLON T_LPAREN T_RPAREN T_COMMA
%token T_DOT T_DOTDOT T_INTCONST T_CHARCONST T_IDENT

%nonassoc T_THEN
%nonassoc T_ELSE

/* Starting point */
%start N_START

%%
N_START : N_PROG
    {
        printRule("START", "PROG");
        return 0;
    }
    ;
N_PROG  : N_PROGLBL T_IDENT T_SCOLON N_BLOCK T_DOT
    {
        printRule("PROG", "PROG_LBL ident ; BLOCK.");
    }
    ;
N_PROGLBL : T_PROG
    {
        printRule("PROG_LBL", "T_PROGTOKEN");
    }
    ;
N_BLOCK : N_VAR_DEC N_PROC_DEC N_STMT
    {
        printRule("BLOCK", "VAR_DEC PROC_DEC STMT");
    }
    ;
N_VAR_DEC_PART  :   //Epsilon
    {
        printRule("VAR_DEC_PART", "epsilon");
    }
            |   T_VAR N_VAR_DEC T_SCOLON N_VAR_DEC_LST
    {
        printRule("VAR_DEC_PART", "var VAR_DEC : VAR_DEC_LST");
    }
    ;
N_VAR_DEC_LST   :   //Epsilon
    {
        printRule("VAR_DEC_LST", "epsilon");
    }
                |   N_VAR_DEC T_SCOLON N_VAR_DEC_LST
    {
        printRule("VAR_DEC_LST", "VAR_DEC : VAR_DEC_LST");
    }
    ;
N_VAR_DEC   :   T_IDENT N_VAR_DEC_LST T_COLON N_TYPE
    {
        printRule("VAR_DEC", "ident VAR_DEC_LST : TYPE");
    }
    ;
N_IDENT     :   T_IDENT
    {
        printRule("IDENT", "ident");
    }
    ;
N_IDENT_LST :   //Epsilon
    {
        printRule("IDENT_LST", "epsilon");
    }
            |   T_COMMA N_IDENT N_IDENT_LST
    {
        printRule("IDENT_LST", ", IDENT IDENT_LST");
    }
    ;
N_TYPE  :   N_SIMPLE
    {
        printRule("TYPE", "SIMPLE");
    }
        |   N_ARRAY
    {
        printRule("TYPE", "ARRAY");
    }
    ;
N_ARRAY :   T_ARRAY T_LBRACK N_IDX_RANGE T_RBRACK T_OF N_SIMPLE
    {
        printRule("ARRAY", "ARRAY ( IDX_RANGE ) of SIMPLE");
    }
    ;
N_IDX   :   N_INTCONST
    {
        printRule("IDX", "IN_CONST");
    }
    ;
N_IDX_RANGE :   N_IDX T_DOTDOT N_IDX
    {
        printRule("IDX_RANGE", "IDX .. IDX");
    }
    ;
N_SIMPLE    :   T_INTCONST
    {
        printRule("SIMPLE", "intconst");
    }
            |   T_CHARCONST
    {
        printRule("SIMPLE", "charconst");
    }
            |   T_BOOL
    {
        printRule("SIMPLE", "bool");
    }
    ;
N_PROC_DEC_PART :   //Epsilon
    {
        printRule("PROC_DEC_PART", "epsilon");
    }
                |   N_PROC_DEC T_SCOLON N_PROC_DEC_PART
    {
        printRule("PROC_DEC_PART", "PROC_DEC : PROC_DEC_PART");
    }
    ;
N_PROC_DEC  :   N_PROC_HDR N_BLOCK
    {
        printRule("PROC_DEC", "PROC_HDR BLOCK");
    }
    ;
N_PROC_HDR  :   T_PROC T_IDENT T_SCOLON
    {
        printRule("PROC_HDR", "proc ident :");
    }
    ;
N_STMT_PART :   N_COMPOUND
    {
        printRule("STMT_PART", "COMPOUND");
    }
    ;
N_COMPOUND  :   T_BEGIN N_STMT N_STMT_LST T_END
    {
        printRule("COMPOUND", "begin STMT STMT_LST end");
    }
    ;
N_STMT_LST  :   //Epsilon
    {
        printRule("STMT_LST", "epsilon");
    }
            |   T_SCOLON N_STMT N_STMT_LST
    {
        printRule("STMT_LST", ": STMT STMT_LST");
    }
    ;
N_STMT  :   N_ASSIGN
    {
        printRule("STMT", "ASSIGN");
    }
        |   N_PROC_STMT
    {
        printRule("STMT", "PROC_STMT");
    }
        |   N_READ
    {
        printRule("STMT", "READ");
    }
        |   N_WRITE
    {
        printRule("STMT", "WRITE");
    }
        |   N_CONDITION
    {
        printRule("STMT", "CONDITION");
    }
        |   N_WHILE
    {
        printRule("STMT", "WHILE");
    }
        |   N_COMPOUND
    {
        printRule("STMT", "COMPOUND");
    }
    ;
N_ASSIGN    :   N_VAR T_ASSIGN N_EXPR
    {
        printRule("ASSIGN", "VAR := EXPR");
    }
    ;
N_PROC_STMT :   N_PROC_IDENT
    {
        printRule("PROC_STMT", "PROC_IDENT");
    }
    ;
N_PROC_IDENT    :   T_IDENT
    {
        printRule("PROC_IDENT", "ident");
    }
    ;
N_READ  :   T_READ T_LPAREN N_INPUT_VAR N_INPUT_LST T_RPAREN
    {
        printRule("READ", "read ( INPUT INPUT_LST )");
    }
    ;
N_INPUT_LST :   //Epsilon
    {
        printRule("INPUT_LST", "epsilon");
    }
            |   T_COMMA N_INPUT_VAR N_INPUT_LST
    {
        printRule("INPUT_LST", ", INPUT_VAR INPUT_LST");
    }
    ;
N_INPUT_VAR :   N_VAR
    {
        printRule("INPUT_VAR", "VAR");
    }
    ;
N_WRITE :   T_WRITE T_LPAREN N_OUTPUT N_OUTPUT_LST T_RPAREN
    {
        printRule("WRITE", "write ( OUTPUT OUTPUT_LST )");
    }
    ;
N_OUTPUT_LST    :   //Epsilon
    {
        printRule("OUTPUT_LST", "epsilon");
    }
                |   T_COMMA N_OUTPUT N_OUTPUT_LST
    {
        printRule("OUTPUT_LST", ", OUTPUT OUTPUT_LST");
    }
    ;
N_OUTPUT    :   N_EXPR
    {
        printRule("OUTPUT", "EXPR");
    }
    ;
N_CONDITION :   T_IF N_EXPR T_THEN N_STMT
    {
        printRule("CONDITION", "if EXPR then STMT");
    }
            |   T_IF N_EXPR T_THEN N_STMT T_ELSE N_STMT
    {
        printRule("CONDITION", "if EXPR then STMT else STMT");
    }
    ;
N_WHILE :   T_WHILE N_EXPR T_DO N_STMT
    {
        printRule("WHILE", "while EXPR do STMT");
    }
    ;
N_EXPR  :   N_SIMPLE_EXPR
    {
        printRule("EXPR", "SIMPLE_EXPR");
    }
        |   N_SIMPLE_EXPR N_REL_OP N_SIMPLE_EXPR
    {
        printRule("EXPR", "SIMPLE_EXPR REL_OP SIMPLE_EXPR");
    }
    ;
N_SIMPLE_EXPR   :   N_TERM N_ADD_OP_LST
    {
        printRule("SIMPLE_EXPR", "TERM ADD_OP_LST");
    }
    ;
N_ADD_OP_LST    :   //Epsilon
    {
        printRule("ADD_OP_LST", "epsilon");
    }
                |   N_ADD_OP N_TERM N_ADD_OP_LST
    {
        printRule("ADD_OP_LST", "ADD_OP TERM ADD_OP_LST");
    }
    ;
N_TERM  :   N_FACTOR N_MULT_OP_LST
    {
        printRule("TERM", "FACTOR MULT_OP_LST");
    }
    ;
N_MULT_OP_LST   :   //Epsilon
    {
        printRule("MULT_OP_LST", "epsilon");
    }
                |   N_MULT_OP N_FACTOR N_MULT_OP_LST
    {
        printRule("MULT_OP_LST", "MULT_OP FACTOR MULT_OP_LST");
    }
    ;
N_FACTOR    :   N_SIGN N_VAR
    {
        printRule("FACTOR", "SIGN VAR");
    }
            |   N_CONST
    {
        printRule("FACTOR", "CONST");
    }
            |   T_LPAREN N_EXPR T_RPAREN
    {
        printRule("FACTOR", "( EXPR )");
    }
            |   T_NOT N_FACTOR
    {
        printRule("FACTOR", "not FACTOR");
    }
    ;
N_SIGN  :   //Epsilon
    {
        printRule("SIGN", "epsilon");
    }
        |   T_PLUS
    {
        printRule("SIGN", "+");
    }
        |   T_MINUS
    {
        printRule("SIGN", "-");
    }
    ;
N_ADD_OP    :   T_PLUS
    {
        printRule("ADD_OP", "+");
    }
            |   T_MINUS
    {
        printRule("ADD_OP", "-");
    }
            |   T_OR
    {
        printRule("ADD_OP", "or");
    }
    ;
N_MULT_OP   :   T_MULT
    {
        printRule("MULT_OP", "*");
    }
            |   T_DIV
    {
        printRule("MULT_OP", "/");
    }
            |   T_AND
    {
        printRule("MULT_OP", "and");
    }
    ;
N_REL_OP    :   T_LT
    {
        printRule("REL_OP", "<");
    }
            |   T_GT
    {
        printRule("REL_OP", ">");
    }
            |   T_LE
    {
        printRule("REL_OP", "<=");
    }
            |   T_GE
    {
        printRule("REL_OP", ">=");
    }
            |   T_EQ
    {
        printRule("REL_OP", "=");
    }
    ;
N_VAR   :   N_ENTIRE_VAR
    {
        printRule("VAR", "ENTIRE_VAR");
    }
        |   N_IDX_VAR
    {
        printRule("VAR", "IDX_VAR");
    }
    ;
N_IDX_VAR   :   N_ARRAY_VAR T_LBRACK N_EXPR T_RBRACK
    {
        printRule("IDX_VAR", "ARRAY_VAR ( EXPR )");
    }
    ;
N_ARRAY_VAR :   N_ENTIRE_VAR
    {
        printRule("ARRAY_VAR", "ENTIRE_VAR");
    }
    ;
N_ENTIRE_VAR    :   N_VAR_IDENT
    {
        printRule("ENTIRE_VAR", "VAR_IDENT");
    }
    ;
N_VAR_IDENT :   T_IDENT
    {
        printRule("VAR_IDENT", "IDENT");
    }
    ;
N_CONST :   N_INTCONST
    {
        printRule("CONST", "INTCONST");
    }
        |   T_CHARCONST
    {
        printRule("CONST", "CHARCONST");
    }
        |   N_BOOLCONST
    {
        printRule("CONST", "BOOLCONST");
    }
    ;
N_INTCONST  :   N_SIGN T_INTCONST
    {
        printRule("INTCONST", "SIGN intconst");
    }
    ;
N_BOOLCONST :   T_TRUE
    {
        printRule("BOOLCONST", "true");
    }
            |   T_FALSE
    {
        printRule("BOOLCONST", "false");
    }
    ;
%%

#include "lex.yy.c"
extern FILE *yyin;

void printRule(const char *lhs, const char *rhs) {
    printf("%s -> %s\n", lhs, rhs);
    return;
}

int yyerror(const char *s) {
    printf("%s\n", s);
    return(1);
}

void printTokenInfo(const char* tokenType, const char* lexeme) {
    printf("TOKEN: %s LEXEME: %s\n", tokenType, lexeme);
}

int main() {
    do {
        yyparse();
    } while (!feof(yyin));

    printf("%d lines processed\n", numLines);

    return 0;
}

有什么想法吗?

1 个答案:

答案 0 :(得分:8)

根据您的语法,每个N_BLOCK必须包含N_PROC_DEC,每个N_PROC_DEC必须包含N_BLOCK。这种无限递归使得N_BLOCKN_PROC_DEC无法成功匹配任何内容,因为N_START需要N_BLOCKN_START也不可能成功。