2014-05-12 62 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