Question about grammar organization

9 views
Skip to first unread message

Bjørn Arild Mæland

unread,
Jun 4, 2009, 9:53:43 AM6/4/09
to Treetop Development
Hello,

I'm trying to build a parser for a language that closely resembles the
language for writing grammars in Xerox Linguistic Environment (XLE).
This language is divided into several sub-languages, two of which I am
interested in. My original plan was to have two separate grammars for
each sublanguage, but there's a lot of common elements between the
two. Is there a way to "include" another grammar or a set of grammar
rules in a treetop file? I tried to create a third "Shared" grammar
and then do "RulesParser.class_eval { include Shared }", but that did
not work out well. To include some context, here are two examples of
what the sublanguages look like:

RULES (language 1):
S --> NP: (^ SUBJ)=!
(! CASE)=NOM;
VP: ^=!.

LEXICON (language 2):
walk V * (^ PRED)='WALK<(^ SUBJ)>'.

It might be that my approach is flawed and that I instead of dividing
them into two grammars instead should create a large one for both.
However, the two syntactic styles can not be used interchangeably so
I'm not sure how to accomplish that. Any advice would be greatly
appreaciated.

- Bjørn

Markus

unread,
Jun 4, 2009, 10:29:31 AM6/4/09
to treet...@googlegroups.com


Bjørn --

I've had good luck with something along the following lines:

def extend_grammar(base_grammar,name,new_rules,new_top='super')
Treetop.load_from_string(%Q{
grammar #{name}
include #{base_grammar.name.sub(/Parser$/,'')}
rule top
#{new_top}
end
#{new_rules}
end
})
end

My version has more goo, but this is the core. With this you you can do
fun tricks like generating grammars at run-time, building trees of
grammars, etc. For your needs it sounds like you could just do it
literally in the grammar files (and skip the string manipulation) like
so:

grammar This_grammar
include Shared_grammar
rule top
# top rule for this grammar
end
#
# rules for this grammar
#
end

...with the comments replaced with the indicated content.

-- Markus
> --
Markus <mar...@reality.com>

Clifford Heath

unread,
Jun 4, 2009, 5:40:52 PM6/4/09
to treet...@googlegroups.com
Why load_from_string? If you can't include the first grammar into the
second,
just make a third file to include them both and load (or require, see
Polyglot)
that one.

My CQL grammar is in six files, and a seventh with the include
statements.


Clifford Heath.

Markus

unread,
Jun 4, 2009, 6:03:30 PM6/4/09
to treet...@googlegroups.com
Clifford --

Because the supplementary grammar rules were coming from run-time
sources (e.g. being entered directly by the user or being constructed by
user initiated processes).

-- Markus
> --
Markus <mar...@reality.com>

Clifford Heath

unread,
Jun 4, 2009, 6:05:18 PM6/4/09
to treet...@googlegroups.com
On 05/06/2009, at 8:03 AM, Markus wrote:
> Because the supplementary grammar rules were coming from run-time
> sources (e.g. being entered directly by the user or being
> constructed by
> user initiated processes).

Ahh, sorry, I didn't read carefully enough. All the same, that wasn't
what Arild was doing.

Clifford.
Reply all
Reply to author
Forward
0 new messages