Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the %% for in Happy?

I'm building a parser with Happy and noticed this is the online documentation:

Like yacc, we include %% here, for no real reason.

%%

There must be a reason though, even if it's trivial. Does anyone know what it is?

like image 247
Nick Brunt Avatar asked Dec 08 '11 13:12

Nick Brunt


1 Answers

It separates the sections in a Yacc source file. e.g. See http://dinosaur.compilertools.net/yacc/

Names refer to either tokens or nonterminal symbols. Yacc requires token names 
to be declared as such. In addition, for reasons discussed in Section 3, it is 
often desirable to include the lexical analyzer as part of the specification 
file; it may be useful to include other programs as well. Thus, every 
specification file consists of three sections: the declarations, (grammar) 
rules, and programs. The sections are separated by double percent ``%%'' marks. 
(The percent ``%'' is generally used in Yacc specifications as an escape 
character.)

In other words, a full specification file looks like

        declarations
        %%
        rules
        %%
        programs
The declaration section may be empty. Moreover, if the programs section is 
omitted, the second %% mark may be omitted also; thus, the smallest legal Yacc 
specification is

        %%
        rules
like image 118
Duncan Avatar answered Sep 30 '22 21:09

Duncan