簡體   English   中英

Yacc上下文無關語法

[英]Yacc Context Free Grammar

我正在處理課程項目。 基本上,這是一個yacc Context Free Grammar項目,我寫了一些代碼,但是遇到了語法錯誤。 讓我向您顯示詳細信息:

.l文件

%{
#include "y.tab.h"
%}

%%

e[0-9]{6}-?[0-9] { 
    return ST; 
}

[-+]?[0-9]+(.[0-9]+)? { return NUMBER; }

[a-z]+ { return ID; }

CNG[0-9][0-9][0-9] { return COURSE; }

ASN[0-9]+ { return ASN; }

\"[^"]*\" { return STR; }

MT[0-9]* { return MT; }

#.* {} /* printf("COMMENT "); */ 

[-+{},:()] { return yytext[0]; } 

[ \t]   { }

\n  { }

.   { printf ("found other data \"%s\"\n", yytext);
    return 1;
}

%%

.y文件(我寫的)

%{
#include <stdio.h>
#include <string.h>
int yydebug = 0;
%}
%token NUMBER ST STR COURSE ASN MT ID
%%
program: program stmt | stmt;
stmt: func | coursedef | student_info;
coursedef: COURSE ':' '{'st_list '}' { printf("COURSE-DEF\n"); };
st_list: st_list ',' ST | ST;
student_info: ST ':' '{' course_list '}' {printf("STUDENT-INFO\n");};
course_def: COURSE ':' '{' ASN ':' NUMBER ',' MT ':' NUMBER '}';
func: ID '(' param_list ')' { printf("FUNC-CALL\n"); };
param: COURSE | NUMBER | func | STR | ASN | MT | course_add ;
course_add: COURSE "+" COURSE { printf("COURSE-ADD\n"); };

%%

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

int yywrap() { return 1; } 

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

輸入:

# define students taking the course
CNG230 : {e8390231, e8390232, e839023-9}

# define grades of students
e8390231 : { CNG230 : { ASN1 :  90.0, MT1 : +100 } } # who writes +100?
e8390232 : { CNG230 : { ASN1 :  30.0, MT1 :   90 } }
e839023-9 : { 
    CNG230 : { ASN1 : 52.6, MT1 : 45.0 },
    CNG492 : { ASN1 : 10.0, MT1 : 20.0 } 
}

# report the average of all grades for CNG230
report(average(CNG230))

# report the curve we get by taking the passing grade down by 30 points
report(curve(CNG230, -30))

# report the average asn 1 grades for CNG230
title("CNG230 ASN1 Average")
report(average(CNG230, ASN1))

# report the average grades over two courses
title("CNG230/492 Global average")
report(average(CNG230 + CNG492))

# report the average grades of students taking
# CNG230 but not taking CNG492
report(average(CNG230 - CNG492))

# report the best student in CNG230
report(best(CNG230, 1)) 

# report the average of grades of the best
# student taking CNG230
report(average(best(CNG230, 1))) # can replace 1 with 3 to get top 3

預期產量:

COURSE-DEF
STUDENT-INFO
STUDENT-INFO
STUDENT-INFO
FUNC-CALL
FUNC-CALL
FUNC-CALL
FUNC-CALL
FUNC-CALL
FUNC-CALL
FUNC-CALL
FUNC-CALL
COURSE-ADD
FUNC-CALL
FUNC-CALL
COURSE-SUB
FUNC-CALL
FUNC-CALL
FUNC-CALL
FUNC-CALL
FUNC-CALL
FUNC-CALL
FUNC-CALL

我認為這個項目應該很容易,但是我不能離開COURSE-DEF。 誰能幫我?

您的規范中遺漏了一些規則/產品。 試試這個:

%{
#include <stdio.h>
#include <string.h>
int yydebug = 0;
%}
%token NUMBER ST STR COURSE ASN MT ID
%%
program         : program stmt
                | stmt
                ;

stmt            : func
                | coursedef
                | student_info
                ;

coursedef       : COURSE ':' '{' st_list '}' { printf("COURSE-DEF\n"); }
                ;

st_list         : st_list ',' ST
                | ST
                ;

student_info    : ST ':' '{' course_list '}' { printf("STUDENT-INFO\n"); }
                ;

course_def      : COURSE ':' '{' ASN ':' NUMBER ',' MT ':' NUMBER '}'
                ;

course_list     : course_list ',' course_def
                | course_def
                ;

func            : ID '(' param_list ')' { printf("FUNC-CALL\n"); }
                ;

param_list      : param_list ',' param
                | param
                ;

param           : COURSE
                | ASN
                | STR
                | MT
                | NUMBER
                | func
                | course_add
                | course_sub
                ;

course_add      : COURSE '+' COURSE { printf("COURSE-ADD\n"); }
                ;

course_sub      : COURSE '-' COURSE { printf("COURSE-SUB\n"); }
                ;

%%

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

int yywrap() { return 1; } 

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

暫無
暫無

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

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