Boost logo

Boost :

From: John Max Skaller (skaller_at_[hidden])
Date: 2001-05-28 15:42:48


David Abrahams wrote:
 
> > The main problem I see (due to ignorance) is how
> > to specify the semantic rules. For a grammar, there
> > the notation
> >
> > $n
> >
> > refers to the attributes of the n'th symbol. What do you do with
> >
> > Symbol*
> > or
> > Symbol?
>
> People often handle this problem by allowing different semantic actions to
> be embedded at various points in the RHS.

        Right: I've seen examples of that in the Dragon Book.

> In my opinion, that approach tends
> to complicate the RHS to an extent that undermines any simplicity gained in
> the expression of grammar using EBNF. It also hugely complicates the
> description of the meta-language. You're much better off sticking to strict
> BNF rules, since they tend to enforce a reasonable amount of factorization.

        I'm using a yacc-like tool (ocamlyacc) and a literate
programming tool (interscript). What I have done is use
Python script to define an iterative meta-construct that
hides the dummy non-terminal:

----------------------------------------------------------------
@# define meta operators:
@# aster: t*
@# plus: t+
@# quest: t?
@# commalistof: (t (,t)*)?
@def plus(s):
  for line in [
    s + "_plus:",
    " | " + s + " " + s+ "_plus { $1 :: $2 }",
    " | " + s + " {[$1]}",
  ]: tangle (line)
@def aster(s):
  for line in [
    s + "_aster:",
    " | " + s + " " + s+ "_aster { $1 :: $2 }",
    " | { [] }"
  ]: tangle (line)
@def opt(s):
  for line in [
    s+"_opt:",
    " | " + s + " { Some $1 }",
    " | { None }"
  ]: tangle (line)
@def commalistof(s): # possibly empty comma separated list
  for line in [
    s+"_comma_list:",
    " | " + s + " COMMA " + s+"_comma_list { $1 :: $3 }",
    " | " + s + " { [$1] }",
    " | { [] }",
  ]: tangle (line)
-------------------------------------------------------

and then I use it like:

-------------------------------------------------------
@head(2,'Compilation Unit')
@select(h)
compilation_unit:
  | statement_aster ENDMARKER { $1 }

@aster("statement")
@doc()
A compilation unit is just a sequence of statements.
-------------------------------------------------------

I did this, because I kept getting the recursive forms
wrong, and the extra productions obscured the grammar.

[To understand the executable code, in SML/Ocaml:

        hd :: tl 'append hd element to list tl'
        [] 'empty list'
        Some x 'extant element of option type'
        None 'Non-extant element of option type'
]

-- 
John (Max) Skaller, mailto:skaller_at_[hidden]
10/1 Toxteth Rd Glebe NSW 2037 Australia voice: 61-2-9660-0850
checkout Vyper http://Vyper.sourceforge.net
download Interscript http://Interscript.sourceforge.net

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk