Question

I have this piece of EBNF grammar that I'd like to convert to Bison syntax.

E ::= {S [`;´]} [L [`;´]]

I don't have much experience with grammars but I think I managed to figure out that there are some optional parts and some repetition.

My attempt:

E:      S semiColOp E lOp semiColOp
;
semiColOp:  /* No Action*/ 
        | semiCol//';' 
;

lOp:        /* No Action*/ 
        | laststat   
;

I can't test this atm so it'd be nice to get some input from you guys.

Was it helpful?

Solution

This is how I'd do it:

program   : statements
          | statements last opt_semi
          ;

statements: /* empty */
          | statements statement opt_semi
          ;

opt_semi  : /* empty */
          | ';'
          ;

where program corresponds to E; statement to S and last to L.

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