简体   繁体   中英

YACC Grammar Optional prefix on a variable

I would like to put an optional letter before my variables like 2p where p is an ID and 2 is a NUM. My ID matches only letters, and I would like to keep it that way (ie I don't want to change the regex for ID). I tried something like this, but it didn't work:

var
    : NUM ID
    ;

This doesn't work because NUM only matches numbers. I tried changing the regex for NUM to match the empty string ( I think this is how you do it):

num     ({digits}{fraction}{exponent}|"")

but id didn't work. I also tried introducing another rule like this:

var
  : number ID;

number:
  : NUM
  | ''
  ;

I am guessing there is some symbol in yacc to take care of the empty string that I can put in the grammar, but i am not sure. Any help would be apprecaited.

I was over thinking it, this would do it:

var
: ID
| NUM ID
;

try this

var
    : NUM ID   {...}
    | ID       {...}
;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM