Hi,

I'm quite new to Parrot, and I'm trying to implement a (very small)
subset of the OCaml language.

The language has two different kinds of expressions (usual ones, and
type expressions):
- "*" in the statement "3*4" is a multiplication,
- while "type pair = int * int" creates a new type named "pair" which
corresponds to pairs of integers.

The natural way to implement this would be to use two optables, which of
course doesn't work: by the way, one could update the answer to "How
many operator tables can I use in my language?" in
docs/pct/pct_optable_guide.pod, it clearly doesn't work (for the moment...).

To illustrate this, I've attached a (simplified) grammar example.

Yet it's clearly not far from working, if we look at the generated
gen_grammar.pir:
* the __onload sub creates only one PGE::OPTable object, for the first
optable, and adds all operators to it, but it's easy to create another
PGE::OPTable and store it in a different global variable,

* make sure, in the remainder of the file, that the statement:
$P0 = get_hll_global ["Testeur::Grammar"], "$optable"
refers to the good "$optable".

I've also attached the interesting parts of gen_grammar.pir, and it
seems to work fine.

So here are my two questions :

* without changing the syntax, we could allow many optables to be
defined, and decree that an operator belongs to the last optable before
its declaration. Does it seem acceptable?

* I haven't played yet with PGE's internals, but I can take some time to
try and write the patch for it, if someone can tell me where to look
first in the code.

Regards,

-- 
Florian,
http://openweb.eu.org/
http://www.linux-france.org/

# $Id$

=begin overview

This is the grammar for Testeur written as a sequence of Perl 6 rules.

=end overview

grammar Testeur::Grammar is PCT::Grammar;

rule TOP {
    ['type' <type>
    | <expr>]
    [$ || <.panic 'Rate'>]
    {*}
}

rule expr is optable { ... }

proto 'term:' is precedence('1') is parsed(&term) { ... }
proto 'infix:*' is looser('term:') { ... }

token keyword { 'type' }

rule term {
  <!keyword><lower><alpha>*
}

rule type is optable { ... }
proto 'term:' is precedence('1') is parsed(&typeterm) { ... }
proto 'infix:*' is looser('term:') { ... }

rule typeterm {
  <upper><alpha>*
}
      .sub '__onload' :load :init
          .local pmc optable
          ## namespace Testeur::Grammar
          push_eh onload_280
          .local pmc p6meta
          p6meta = get_hll_global 'P6metaclass'
          p6meta.'new_class'('Testeur::Grammar', 'parent'=>'PCT::Grammar')
          pop_eh
        onload_280:
          optable = new 'PGE::OPTable'
          set_hll_global ['Testeur::Grammar'], '$optableexpr', optable
          $P0 = get_hll_global ['Testeur::Grammar'], 'term'
          optable.newtok("term:", 'precedence'=>"1", 'parsed'=>$P0)
          optable.newtok("infix:*", 'looser'=>"term:")

          ########## Added lines: repeat the two first lines, with a different
          ########## name.
          optable = new 'PGE::OPTable'
          set_hll_global ['Testeur::Grammar'], '$optabletype', optable
          ########## End

          $P0 = get_hll_global ['Testeur::Grammar'], 'typeterm'
          optable.newtok("term:", 'precedence'=>"1", 'parsed'=>$P0)
          optable.newtok("infix:*", 'looser'=>"term:")
          .return ()
      .end

##### cut

## <Testeur::Grammar::expr>
      .namespace [ "Testeur::Grammar" ]
      .sub "expr"
        .param pmc mob
        .param pmc adverbs :named :slurpy
        # Call the optable associated with "rule expr"
        $P0 = get_hll_global ["Testeur::Grammar"], "$optableexpr"
        .return $P0.'parse'(mob, 'rulename'=>"expr", adverbs :named :flat)
      .end

##### cut

## <Testeur::Grammar::type>
      .namespace [ "Testeur::Grammar" ]
      .sub "type"
        .param pmc mob
        .param pmc adverbs :named :slurpy
        # Call the optable associated with "rule type"
        $P0 = get_hll_global ["Testeur::Grammar"], "$optabletype"
        .return $P0.'parse'(mob, 'rulename'=>"type", adverbs :named :flat)
      .end

Attachment: signature.asc
Description: OpenPGP digital signature

_______________________________________________
http://lists.parrot.org/mailman/listinfo/parrot-dev

Reply via email to