Question: These are my C-MINUS BNF Yacc implementation: My current error is it is unable to read in read y[ 3+4 ]; and

These are my C-MINUS BNF Yacc implementation:

My current error is it is unable to read in " read y[ 3+4 ]; " and " {{while ( x < 3) read x; }} " from a testfile.c, I also have 56 shift/reduce conflicts and 2 reduce/reduce, this program should only have 1 shift/reduce conflict the rest i need help solving and i need to fix the rest of the code to be able to take the inputs i previously listed.

if an answer is provided I'll make sure to upvote your response.

******************************************************************************************************************************************************************

Assingment1.Y

%{

#include #include

int yylex(); extern int lineno; // from lex extern int mydebug; // from lex

void yyerror(s) // called by yyparse char *s; { printf ("YACC PARSE ERROR: %s on line number %d ", s,lineno); }

%} /* Define the union type used by the parser, which can hold either an int or a string */ %union { int value; char *string; } /* Define the start symbol of the grammar */ %start Program /* Define the token types, along with their attributes (value or string) */ %token T_NUM %token T_ID %left '+' '-' %left '*' '/' %token T_INT %token T_VOID %token T_WRITE %token T_RETURN %token T_READ %token T_ELSE %token T_STRING %left T_IF T_WHILE %nonassoc T_LT T_GT T_LE T_GE T_EQ T_NE

%% /* end specs, begin rules */ Program : Declaration_List | /*empty*/ ; Declaration_List: Declaration | Declaration '{' Declaration '}' | Declaration Declaration_List ; Declaration : Var_Declaration | Fun_Declaration ; Var_Declaration : Type_Specifier Var_List ';' ; Var_List: T_ID | T_ID '[' T_NUM ']' {printf("Var_LIST ID is %s ", $1);} | T_ID '[' T_NUM ']' ',' Var_List {printf("Var_LIST ID is %s ", $1);} | T_ID ',' Var_List {printf("Var_LIST ID is %s ", $1);} ; Type_Specifier: T_INT | T_VOID ; Fun_Declaration : Type_Specifier T_ID '(' Params ')' Compound_Stmt ; Params : Param_List | T_VOID ; Param_List : Param | Param_List ',' Param ;

Param : Type_Specifier T_ID '[' ']' %prec T_ID | Type_Specifier T_ID '[' T_NUM ']' %prec T_ID | Type_Specifier T_ID %prec T_ID ;

Compound_Stmt : '{'Local_Declarations Statement_List '}' ; Local_Declarations: /*empty*/ | Local_Declarations Var_Declaration ;

Statement_List : Statement Statement_List | /*empty*/ ; Statement: Expression_Stmt | Compound_Stmt | Selection_Stmt | Iteration_Stmt | Assignment_Stmt | Return_Stmt | Read_Stmt | Write_Stmt ; Expression_Stmt: Expression ';' | ';' ;

Selection_Stmt:T_IF '(' Expression ')' Statement ;

Iteration_Stmt: T_WHILE '(' Expression ')' Statement ;

Return_Stmt: T_RETURN Expression ';' | T_RETURN ';' | ;

Read_Stmt: T_READ Var '[' Expression ']' ';' | T_READ Var ';' ;

Write_Stmt:T_WRITE Expression ';' | T_WRITE T_STRING ';' ;

Assignment_Stmt: Var '=' Simple_Expression ;

Var:T_ID '[' Expression ']' |T_ID ;

Expression: Simple_Expression ;

Simple_Expression: Additive_Expression | Additive_Expression '[' Relop Additive_Expression ']' ; Relop: T_LE | T_LT | T_GT | T_GE | T_EQ | T_NE ;

Additive_Expression: Term | Additive_Expression Addop Term ; Addop: '+' | '-' ;

Term: Factor | Multop Factor ;

Multop: '*' | '/' ; Factor: '(' Expression ')' | T_NUM | Var | Call | '-' Factor ; Call: T_ID '(' Args ')' ; Args: Arg_List | /*empty*/ ; Arg_List: Expression | Arg_List ',' Expression ;

%%

/* end of rules, start of program */ int main() { yyparse(); }

/********************************************************************************************************************************************************/

Assingment1.L

%{ int mydebug=0; int lineno=1; #include "y.tab.h" %} %% int {return(T_INT);} void {return(T_VOID);} write {return(T_WRITE);} read {return(T_READ);} if {return(T_IF);} else {return(T_ELSE);} while {return(T_WHILE);} return {return(T_RETURN);} '<' {return(T_LT);} '>' {return(T_GT);} '<=' {return(T_LE);} '>=' {return(T_GE);} '==' {return(T_EQ);} '!=' {return(T_NE);} \/\/.* {/*do nothing just eat */ } \".*\" {yylval.string=strdup(yytext); return (T_STRING);} [a-zA-Z][a-zA-z0-9_]* {if (mydebug) fprintf(stderr,"ID found "); yylval.string=strdup(yytext); return(T_ID);} [0-9][0-9]* {if (mydebug) fprintf(stderr,"Digit found "); yylval.value=atoi((const char *)yytext); return(T_NUM);} [ \t] {if (mydebug) fprintf(stderr,"Whitespace found ");} [;()=\\\-+*/%&{},\[\]|] { if (mydebug) fprintf(stderr,"return a token%c ",*yytext); return (*yytext);} { if (mydebug) fprintf(stderr,"carriage return %c ",*yytext); lineno++; } %% int yywrap(void) { return 1;}

/************************************************************************************************************************************************/

Assignment1Test.c

int x; int x,x;

int main(int a, void b, int a[]) { int x , A[100]; read x; read y[ 3+4 ]; read x; write "helo"; write 3+4+f(3,2,A[20]) ; return; return 3+4+f(3,2,A[20]); 3+4+f(3,2,A[20]); ;;; {{while ( x < 3) read x; }}

read x; if ( x ) write 5 ; if( x < 3) return 5; else write "great";

} // of main

void f(void) {}

int x , A[100]; void A[100]; void A[100], x; void A[100], B[2];

*****************************************************************************************************

testfile.c

int x; int x,x;

int main(int a, void b, int a[]) { int x , A[100]; read x; read y[ 3 + 4 ]; read x; write "helo"; write 3+4+f(3,2,A[20]) ; return; return 3+4+f(3,2,A[20]); 3+4+f(3,2,A[20]); ;;; {{while ( x < 3) read x; }}

read x; if ( x ) write 5 ; if( x < 3) return 5; else write "great";

} // of main void f(void) {} int x , A[100]; void A[100]; void A[100], x; void A[100], B[2];

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!