سؤال

I am dealing with the course project. Basically, this is a yacc Context Free Grammar project and I wrote some code but I stuck and got syntax error. Let me show you details:

.l file

%{
#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 file (that I wrote)

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

Input:

# 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

Expected Output:

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

I think this project should be very easy but I couldn't move from COURSE-DEF. Can anyone help me?

هل كانت مفيدة؟

المحلول

Several rules/productions are missed in your specification. Try this one:

%{
#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 مع الإسناد
لا تنتمي إلى StackOverflow
scroll top