Ankur Gautam
Ankur Gautam

Reputation: 1422

getting shift/reduce errors in my grammar

This is the part of grammar for my programming language. I am getting shift/reduce conflicts while compiling.

Rule 1:    encryption_spec: key_spec 

Rule 2:   key_spec:
    key_spec key_spec_content
    | key_spec_content
    ;

Rule 3:    key_spec_content: TOK_PROTECT key_keyowner 
    |TOK_PROTECT key_keyname
    |TOK_PROTECT key_keymethod
    |TOK_PROTECT key_pub_key
    |TOK_COMMA key_keyowner 
    |TOK_COMMA key_keyname
    |TOK_COMMA key_keymethod
    |TOK_COMMA key_pub_key
    |encoding
    ;

I am getting shift/reduce conflict in Rule 1. Can you please suggest me something to modify ?

Upvotes: 1

Views: 108

Answers (2)

Léo Ercolanelli
Léo Ercolanelli

Reputation: 1070

Your key_spec rule is malformed. Indeed you have an infinite recursion.
You should change it for something like

key_spec: /* Empty. */
    | key_spec key_spec_content
;

EDIT: Remove the %empty bison extension.

Upvotes: 3

Paul Ogilvie
Paul Ogilvie

Reputation: 25286

The rule by @Leo is not correct because it allows zero occurrences of key_spec_content where the original requires at least one. The following should do this:

key_spec:
  key_spec key_spec_content
  | key_spec_content
;

However, this rule has an unnecessary recursion. The following should be better:

key_spec:
  key_spec_content key_spec
  | key_spec_content
;

This allows to reduce after seeing each key_spec_content.

Upvotes: 1

Related Questions