i am trying to build a parser for mini java where i am getting shift/reduce conflicts in the expression grammar part.I can't resolve this conflict

StackOverflow https://stackoverflow.com/questions/18639145

Question

this is part of y.ouput file

state 65

15 Expression: Expression . "&&" Expression

16 | Expression . "<" Expression

17 | Expression . "+" Expression

18 | Expression . "-" Expression

19 | Expression . "*" Expression

20 | Expression . "[" Expression "]"

21 | Expression . "." "length"

22 | Expression . "." Identifier "(" Expression "," Expression ")"

25 | "!" Expression .

"["  shift, and go to state 67
"<"  shift, and go to state 69
"+"  shift, and go to state 70
"-"  shift, and go to state 71
"*"  shift, and go to state 72
"."  shift, and go to state 73

"["       [reduce using rule 25 (Expression)]
"<"       [reduce using rule 25 (Expression)]
"+"       [reduce using rule 25 (Expression)]
"-"       [reduce using rule 25 (Expression)]
"*"       [reduce using rule 25 (Expression)]
"."       [reduce using rule 25 (Expression)]
$default  reduce using rule 25 (Expression)

this is how the precedence of operators is set

%left "&&"

%left '<'

%left '-' '+' 

%left '*'

%right '!'

%left '.'

%left '(' ')'

%left '[' ']'
Was it helpful?

Solution

In bison, there is a difference between "x" and 'x'; they are not the same token. So, assuming you are using bison, your precedence declarations don't refer to the terminals in the productions.

Bison also allows %token definitions of the following form:

%token name quoted-string ...

For example (a short excerpt from bison's own grammar file):

%token
  PERCENT_CODE            "%code"
  PERCENT_DEBUG           "%debug"
  PERCENT_DEFAULT_PREC    "%default-prec"
  PERCENT_DEFINE          "%define"
  PERCENT_DEFINES         "%defines"
  PERCENT_ERROR_VERBOSE   "%error-verbose"

Once the symbols have been aliased, they can be used interchangeably in the grammar, making it possible to use the double-quoted string in productions; some people find such grammars easier to read. However, there is no mechanism to ensure that the lexer produces the correct token number for a double-quoted string since it only has access to the token names.

The "original" yacc, at least in the current "byacc" version maintained by Thomas Dickey, allows both single- and double-quoted token names, but does not distinguish between them; both "+" and '+' are mapped to token number 43 ('+'). It also does not provide any easy way to alias token names, so the double-quoted multi-character strings are not particularly easy to use in a reliable way.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top