[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Need Help in Shift Reduce Errors
From: |
Atul Kulkarni |
Subject: |
Need Help in Shift Reduce Errors |
Date: |
Fri, 22 Jul 2005 09:53:21 +0530 |
All,
I am facing a problem with the Grammar i am feeding to the Bison. It
throws 2 shift reduce conflicts. Please help with this I am not able
to resolve them.
I am attaching my Grammar and Bison input that I created.
The Input for Bison I created from the Grammar.
/*----------------------------------*/
%{
#include <stdio.h>
#include <string.h>
void yyerror(char const *);
%}
%union {
int Int;
char Char;
char* CharPtr;
double Float;
bool Bool;
}
%{
#include "lex.yy.c"
%}
%token <Int> DIGIT
%token <Char> LETTER
%token <Float> TOK_NUMBER
%token <Char> TOK_PLUSOPERATOR
%token <Char> TOK_MINUSOPERATOR
%token <Char> TOK_MULTIPLYOPERATOR
%token <Char> TOK_DEVIDEOPERATOR
%token <Char> TOK_OPENBRACE
%token <Char> TOK_CLOSEBRACE
%token <Char> TOK_COMPARE_LESS_THAN
%token <Char> TOK_COMPARE_GREATER_THAN
%token <Char> TOK_TERNARY_IF
%token <Char> TOK_TERNARY_ELSE
%token <Char> TOK_ASSIGN_EQUAL
%token <Char> TOK_COMPARE_EQUAL_EQUAL
%token <Char> TOK_COMPARE_NOT_EQUAL
%token <Char> TOK_COMPARE_LESSTHAN_EQUAL
%token <Char> TOK_COMPARE_GREATERTHAN_EQUAL
%token <Char> TOK_LOGICAL_AND
%token <Char> TOK_LOGICAL_OR
%token <CharPtr> TOK_VARIABLE
%type <Char> Sign
%type <Float> Number
%type <Float> Variable
%type <Float> Operand
%type <Char> Operator
%type <Float> Simple_Exp
%type <Float> Expression
%type <CharPtr> And_or
%type <CharPtr> Comparison
%type <Bool> Equality
%type <Bool> Logical_Exp
%type <Float> Ternary_Exp
%type <Float> Eval_Exp
%type <Float> Assignment
%%
Assignment: Variable TOK_ASSIGN_EQUAL TOK_OPENBRACE Eval_Exp
TOK_CLOSEBRACE {$1 = $4; printf("%f = %f", $1, $4);}
| Variable TOK_ASSIGN_EQUAL Eval_Exp
{$1 = $3; printf("%f =
%f", $1, $3);}
;
Eval_Exp: Ternary_Exp
{$$ =
$1}
| Expression
{$$ = $1; printf("EVAL EXP: %f = %f \n", $$, $1);}
;
Ternary_Exp: Logical_Exp TOK_TERNARY_IF Expression TOK_TERNARY_ELSE
Expression {$1 ? $$ = $3 : $$ = $5}
;
Logical_Exp: TOK_OPENBRACE Equality TOK_CLOSEBRACE And_or
Logical_Exp { if(strcmp($4, "&&")== 0) \
$$ = $2 && $5; \
else \
$$ = $2 || $5; \
}
| TOK_OPENBRACE Equality TOK_CLOSEBRACE
{$$ = $2}
| Equality And_or Logical_Exp
{
if(strcmp($2, "&&")== 0)\
$$ = $1 && $2; \
else \
$$ = $1 || $2; \
}
| Equality
{$$ = $1}
;
Equality: Operand Comparison Operand
{ if(strcmp($2,
"<") == 0)\
$$ = $1 < $3; \
else if(strcmp($2, ">") == 0) \
$$ = $1 > $3; \
else if(strcmp($2, "==") == 0) \
$$ = $1 == $3; \
else if(strcmp($2, "!=") == 0) \
$$ = $1 != $3; \
else if(strcmp($2, "<=") == 0) \
$$ = $1 <= $3; \
else if(strcmp($2, ">=") == 0) \
$$ = $1 >= $3; \
}
;
Comparison: TOK_COMPARE_LESS_THAN
{$$ = (char *)
malloc(sizeof(char) * 3); sprintf($$, "%c", $1);}
| TOK_COMPARE_GREATER_THAN
{$$ = (char *)
malloc(sizeof(char) * 3); sprintf($$, "%c", $1);}
| TOK_COMPARE_EQUAL_EQUAL
{$$ = (char *)
malloc(sizeof(char) * 3); sprintf($$, "%s", $1);}
| TOK_COMPARE_NOT_EQUAL
{$$ = (char *)
malloc(sizeof(char) * 3); sprintf($$, "%s", $1);}
| TOK_COMPARE_LESSTHAN_EQUAL
{$$ = (char *)
malloc(sizeof(char) * 3); sprintf($$, "%s", $1);}
| TOK_COMPARE_GREATERTHAN_EQUAL
{$$ = (char *)
malloc(sizeof(char) * 3); sprintf($$, "%s", $1);}
;
And_or: TOK_LOGICAL_AND
{$$ = (char *)
malloc(sizeof(char) * 3); sprintf($$, "%s", $1);}
| TOK_LOGICAL_OR
{$$ =
(char *) malloc(sizeof(char) *
3); sprintf($$, "%s", $1);}
;
Expression: TOK_OPENBRACE Simple_Exp TOK_CLOSEBRACE Operator
Expression { if($4 == '+') \
$$ = $2 + $5; \
else if($4 == '-') \
$$ = $2 - $5; \
else if($4 == '*') \
$$ = $2 * $5; \
else if($4 == '/') \
$$ = $2 / $5; \
}
| Simple_Exp
{$$ = $1; printf("EXP: %f = %f\n", $$, $1);}
| Simple_Exp Operator Expression
{ if($2 == '+') \
$$ = $1 + $3; \
else if($2 == '-') \
$$ = $1 - $3; \
else if($2 == '*') \
$$ = $1 * $3; \
else if($2 == '/') \
$$ = $1 / $3;
}
| TOK_OPENBRACE Simple_Exp TOK_CLOSEBRACE
{$$ = $2}
;
Simple_Exp: TOK_OPENBRACE Operand Operator Operand
TOK_CLOSEBRACE { if($3 == '+') \
$$ = $2 + $4; \
else if($3 == '-') \
$$ = $2 - $4; \
else if($3 == '*') \
$$ = $2 * $4; \
else if($3 == '/') \
$$ = $2 / $4;
}
| Operand Operator Operand
{ if($2
== '+') \
$$ = $1 + $3; \
else if($2 == '-') \
$$ = $1 - $3; \
else if($2 == '*') \
$$ = $1 * $3; \
else if($2 == '/') \
$$ = $1 / $3;
printf("Simple Exp: %f = %f %c %f\n", $$, $1,(char)$2,$3);
}
;
Operator: TOK_PLUSOPERATOR
{$$ = '+';}
| TOK_MINUSOPERATOR
{$$ =
'-';}
| TOK_MULTIPLYOPERATOR
{$$ = '*';}
| TOK_DEVIDEOPERATOR
{$$ =
'/';}
;
Operand: Variable
{$$ =
$1; printf("Variable: %f\n", $1);}
| Number
{$$ = $1; printf("Number: %f\n", $1);}
;
Variable: TOK_VARIABLE
{$$ = 10000;}
/*TODO: Please note
this value is to Replaced */
;
Number: Sign DIGIT
{
if($1 == '-')
$$ = -$2;
else
$$ = $2;
}
| Sign TOK_NUMBER
{
if($1 == '-')
$$ = -$2;
else
$$ = $2;
}
| DIGIT
{
$$ = $1;
}
| TOK_NUMBER
{
$$ = $1;
}
;
Sign: TOK_PLUSOPERATOR
{$1 = '+';}
| TOK_MINUSOPERATOR
{$1 =
'+';}
;
%%
void yyerror(char const *err_str)
{
printf(" %s", err_str);
}
Grammar.gif
Description: GIF image
- Need Help in Shift Reduce Errors,
Atul Kulkarni <=