เกิดอะไรขึ้นกับไวยากรณ์ของฉัน

ฉันพยายามป้อนข้อมูลต่อไปนี้ลงในตัวแยกวิเคราะห์ yacc ของฉัน:

int main(void)
{
return;
}

ดูเหมือนถูกต้องสำหรับฉันตามสิ่งที่กำหนดไว้ในไฟล์ yacc แต่ฉันได้รับข้อความ "ข้อผิดพลาดทางไวยากรณ์" หลังจากการส่งคืน ทำไมเป็นอย่างนั้น?

ไฟล์ yacc:

/* C-Minus BNF Grammar */
%{
#include "parser.h"
#include <string.h>
%}
%union 
{
    int intval;
    struct symtab *symp;
}

%token ELSE
%token IF
%token INT
%token RETURN
%token VOID
%token WHILE

%token <symp> ID
%token <intval> NUM

%token LTE
%token GTE
%token EQUAL
%token NOTEQUAL

type <string> paramlist


%%

program : declaration_list ; 

declaration_list : declaration_list declaration | declaration ;

declaration : var_declaration 
            | fun_declaration 
            | '$' { printTable();};

var_declaration : type_specifier ID ';' {$2->value = 0; $2->arraysize = 0;};
                | type_specifier ID '[' NUM ']' ';' {$2->arraysize = $4;printf("Array size is %d", $2->arraysize);} ;

type_specifier : INT | VOID ;

fun_declaration : type_specifier ID '(' params ')' compound_stmt {printf("function declaration\n"); $2->args = 'a'; printf("Parameters: \n", $2->args); } ;


params : param_list | VOID ;

param_list : param_list ',' param
           | param ;

param : type_specifier ID | type_specifier ID '[' ']' ;

compound_stmt : '{' local_declarations statement_list '}' {printf("exiting scope\n"); } ;

local_declarations : local_declarations var_declaration
                   | /* empty */ ;

statement_list : statement_list statement
               | /* empty */ ;

statement : expression_stmt
          | compound_stmt
          | selection_stmt
          | iteration_stmt
          | return_stmt ;

expression_stmt : expression ';'
                | ';' ;

selection_stmt : IF '(' expression ')' statement
               | IF '(' expression ')' statement ELSE statement ;

iteration_stmt : WHILE '(' expression ')' statement ;

return_stmt : RETURN ';' | RETURN expression ';' ;

expression : var '=' expression | simple_expression ;

var : ID | ID '[' expression ']' ;

simple_expression : additive_expression relop additive_expression
                  | additive_expression ;

relop : LTE | '<' | '>' | GTE | EQUAL | NOTEQUAL ;

additive_expression : additive_expression addop term | term ;

addop : '+' | '-' ;

term : term mulop factor | factor ;

mulop : '*' | '/' ;

factor : '(' expression ')' | var | call | NUM ;

call : ID '(' args ')' ;

args : arg_list | /* empty */ ;

arg_list : arg_list ',' expression | expression ;

%%
/* look up a symbol table entry, add if not present */
struct symtab *symlook(char *s) {

    printf("Putting %s into the symbol table\n", s);
    //char *p;
    struct symtab *sp;
    for(sp = symtab; sp < &symtab[NSYMS]; sp++) {
        /* is it already here? */
        if(sp->name && !strcmp(sp->name, s))
        {
            yyerror("already in symbol table\n");
            exit(1);
            return sp;
        }
        if(!sp->name) { /* is it free */
            sp->name = strdup(s);
            return sp;
        }
        /* otherwise continue to next */
    }
    yyerror("Too many symbols");
    exit(1); /* cannot continue */
} /* symlook */
yyerror(char *s)
{
    printf( "yyerror: %s\n", s);
}

printTable()
{
    printf("Print out the symbol table:\n\n");

    struct symtab *sp;
    for(sp = symtab; sp < &symtab[NSYMS]; sp++) 
    {
        printf("name: %s\t"
                "args: %s\t"
                "value %d\t"
                "arraysize %d\n", 
                sp->name, 
                sp->args, 
                sp->value, 
                sp->arraysize);
    }


}

person neuromancer    schedule 17.11.2009    source แหล่งที่มา
comment
คุณมีไฟล์ [F]Lex ที่เกี่ยวข้องด้วยหรือไม่ นั่นทำให้สตริงส่งคืนโทเค็น RETURN หรือไม่ ฉันได้สัมผัส YACC มานานแล้ว แต่คิดว่ามันจำเป็น หากไม่มีข้อขัดแย้งในการลดกะ นั่นเป็นปัญหาเดียวที่เป็นไปได้ที่ฉันมองเห็น   -  person Morinar    schedule 18.11.2009
comment
ใช่ ไฟล์ Flex จะส่งคืนโทเค็น RETURN เมื่อพบการส่งคืนสตริง   -  person neuromancer    schedule 18.11.2009


คำตอบ (2)


ฉันไม่เห็นสิ่งผิดปกติกับไฟล์ไวยากรณ์ yacc ของคุณ ฉันเดาได้ดีที่สุดว่าเครื่องสแกนของคุณส่งคืนโทเค็น RETURN ไม่ถูกต้อง ถ้าฉันรวบรวมแหล่ง yacc ของคุณพร้อมกับ

parser.h

#define NSYMS   100
struct symtab {
    char    *name;
    int     value;
    int     arraysize;
    char    *args;
} symtab[NSYMS];

scanner.l

%{
#include "parser.tab.h"
extern struct symtab *symlook(char *);
extern void printTable();
%}
%option noyywrap

NUM [0-9][0-9]*
ID  [_a-zA-Z][_a-zA-Z0-9]*

%%

else   return ELSE;
if     return IF;
int    return INT;
return return RETURN;
void   return VOID;
while  return WHILE;
"=="   return EQUAL;
"!="   return NOTEQUAL;
">="   return LTE;
"<="   return GTE;
[ \t\n]  ;
{NUM}  { yylval.intval = atoi(yytext); return NUM; }
{ID}   { yylval.symp = symlook(yytext); return ID; }
.      return *yytext;

%%

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

จากนั้นจะยอมรับโปรแกรมตัวอย่างของคุณโดยไม่มีข้อผิดพลาด และพิมพ์ตารางสัญลักษณ์ที่มีเพียงสัญลักษณ์ main

person Chris Dodd    schedule 21.02.2010

yacc ของฉันออกคำเตือนเกี่ยวกับข้อขัดแย้งในการลดกะ ฉันสงสัยว่าปัญหาคือไม่สามารถแยกความแตกต่างระหว่างฟังก์ชันและคำจำกัดความของตัวแปรได้เร็วพอ

person Robert Obryk    schedule 17.11.2009
comment
ข้อขัดแย้งในการเปลี่ยน/ลดนั้นเกิดจากปัญหาอื่นที่ห้อยต่องแต่ง ดังนั้นจึงไม่มีอันตรายใด ๆ - การเปลี่ยนแปลงเริ่มต้นคือคำตอบที่ถูกต้อง - person Chris Dodd; 21.02.2010