2014-05-12 57 views
0

我正在處理課程項目。基本上,這是一個yacc上下文免費語法項目,我寫了一些代碼,但我堅持並得到語法錯誤。讓我告訴你細節:Yacc上下文免費語法

.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中移動。誰能幫我?

+0

你收到的語法錯誤是什麼? – ssube

+0

COURSE-DEF之後只是語法錯誤。沒有具體的解釋。 :( – Xethen

+1

那麼,在你的'.y'文件上運行野牛給出了明顯的錯誤「符號course_list被使用,但沒有被定義」和「符號param_list被使用,但沒有被定義」 –

回答

1

您的規格中有幾條規則/產品未被使用。試試這個:

%{ 
#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; } 
+0

工作完美,非常感謝! – Xethen