LangProc / langproc-2017-cw

1 stars 4 forks source link

YACC nothing #7

Open HarryAnkers opened 6 years ago

HarryAnkers commented 6 years ago

is there a way for you to have an option of matching a nothing case in yacc. for example: A : B C B C : SOMETHING |

Or would I have to do A : B C B | B B C : SOMETHING

fyquah commented 6 years ago

What you need might be empty rules

I found out about that by googling "Empty sequence" on google, which yields me this stackoverflow post.

m8pple commented 6 years ago

(I responded to this via email on the 13th, but it doesn't seem to have shown up here. Usually GH reflects them back up as issues if you respond.)


In bison there is a special token called %epsilon, which can be used to explicitly mark something as empty-by-intent:

https://www.gnu.org/software/bison/manual/html_node/Empty-Rules.html

C : SOMETHING | %empty

There is then some nice stuff which will warn you if you declare a production empty, but it isn't.

Doesn't work in classic yacc though, or older versions of bison. However, you can define a symbol called epsilon:

epsilon : / I am empty /

then use it in your grammar:

C : SOMETHING | epsilon