Return-Path: simon.hay@lincoln.oxford.ac.uk Received: from 129.67.1.161 (RELAY0.MAIL.OX.AC.UK) by null (org.ibex.mail.protocol.SMTP) with ESMTP for ; Wed, 19 Jul 2006 04:37:43 -0700 Received: from smtp0.mail.ox.ac.uk ([129.67.1.205]) by relay0.mail.ox.ac.uk with esmtp (Exim 4.62) (envelope-from ) id 1G3AN4-0000as-0t; Wed, 19 Jul 2006 12:37:42 +0100 Received: from musketeer.comlab.ox.ac.uk ([163.1.27.163] helo=[10.1.1.17]) by smtp0.mail.ox.ac.uk with esmtp (Exim 4.62) (envelope-from ) id 1G3AN3-0000I4-2Q; Wed, 19 Jul 2006 12:37:42 +0100 In-Reply-To: References: <4DCD5332-BBCF-43F7-AA83-7D0F9FF325D7@lincoln.ox.ac.uk> <8B3BA505-38A3-49A1-A281-02B8C850F374@lincoln.ox.ac.uk> Mime-Version: 1.0 (Apple Message framework v750) Content-Type: multipart/alternative; boundary=Apple-Mail-3-1055679781 Message-Id: Cc: sbp-interest@research.cs.berkeley.edu From: Simon Hay Subject: "[sbp-interest] " Re: SBP Date: Wed, 19 Jul 2006 12:37:35 +0100 To: Adam Megacz X-Mailer: Apple Mail (2.750) Envelope-To: sbp-interest@research.cs.berkeley.edu List-Id: The Scannerless Boolean Parser --Apple-Mail-3-1055679781 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed Adam, Thanks again for your help. The annotations stuff is much clearer now :-) I've pulled the latest changes and adjusted for the new names, so I now have something like: Tree res = new CharParser(MetaGrammar.newInstance()).parse (new FileInputStream(s[0])).expand1(); Union mygrammar = Grammar.create(res, "Expr", new Grammar.Bindings()); CharParser parser = new CharParser(mygrammar); Forest r2 = parser.parse(new FileInputStream(s[1])); Tree t = r2.expand1(); System.out.println(t); but now it breaks (on the parser.parse() bit), either saying unexpected character '2' encountered at 1:0 for e.g. Expr = [0-9]++ | Plus:: Expra "+" Expr Expra = ("a" | "b") trying a+2 or unexpected end of file for e.g. demo.g with (11+2*3)-44 - generally I get one of those two errors whatever I try. I'm clearly doing something very simple wrong! Sorry to be wasting so much of your time like this. Simon On 19 Jul 2006, at 08:17, Adam Megacz wrote: > > Hi, Simon! BTW, I've set up a mailing list for SBP, and I'm cc'ing > this message to it: > > http://research.cs.berkeley.edu/project/sbp/list/ > > This reply might be a bit brief; I'll write more in the morning. > >> Tree res = new CharParser(MetaGrammar.make()).parse(new >> FileInputStream(s[0])).expand1(); // meta.g >> Union meta = MetaGrammar.make(res, "s"); >> SequenceInputStream sis = new SequenceInputStream(new FileInputStream >> (s[0]), new FileInputStream(s[1])); // grammar >> res = new CharParser(meta).parse(sis).expand1(); >> Union mygrammar = MetaGrammar.make(res, "ts"); //, new >> TestCaseMaker()); >> CharParser parser = new CharParser(mygrammar); >> Forest r2 = parser.parse(new FileInputStream(s[2])); // input >> Tree t = r2.expand1(); >> System.out.println(t); >> >> Firstly, out of curiosity, what's the >> advantage of parsing meta.g and using that instead of just using the >> built-in meta grammar used to parse meta.g in the first place? > > No advantage whatsoever. RegressionTest only does that as an extra > "sanity check" to make sure I haven't broken anything. > >> Also, previously I could call MetaGrammar.make(res, "ts") or similar >> without any mention of GrammarBindingResolvers; now calling that >> assumes I want an AnnotationGrammarBindingResolvers, which promptly >> gets unhappy when you try to use it (e.g. with the grammar >> >> ts = Expr >> Expr = [0-9]++ >> | Plus:: (left::Expra) "+" (right::Expr) >> Expra = Foo:: ("a" | "b") >> >> >> copied-and-pasted from regression.tc and input a+2 you get >> >> Exception in thread "main" java.lang.RuntimeException: could not find >> a Java method/class/ctor matching tag "Foo", nonterminal "Expra" with >> 1 arguments >> at >> edu.berkeley.sbp.meta.AnnotationGrammarBindingResolver.resolveTag >> (AnnotationGrammarBindingResolver.java:60) > > The short version: try removing "left::", "right::", and "Foo::" from > your grammar; they're not really needed. > > Ah yes. The idea with AnnotationGrammarBindingResolver (yes, I know, > that name is way too long) is this: > > Any sequence of grammar elements in which *two or more elements* are > *not* dropped must "resolve". That is, you have to explain to SBP > how to make a tree out of the two nodes. For example, > > MultiplyExpr = Expr "+" Expr > > SBP needs to know how to turn those two "sub-exprs" into a tree. So > it "resolves" the sequence by looking at two things: the name of the > nonterminal ("MultiplyExpr") and the tag (in this case there is no > tag). You should avoid using tags unless you're forced to; for > example: > > SomeExpr = OtherExpr (Foo:: SecondExpr ThirdExpr) > > The "Foo" is a tag (designated by the double-colon after it); it > tells SBP how to resolve the subsequence with two elements > (SecondExpr and ThirdExpr). The "outer" sequence (OtherExpr (...)) > is resolved using the name of the nonterminal (SomeExpr). > > Okay, now back to the annotations. When you call MetaGrammar.make(), > you should always pass in a GrammarBindingResolver as the third > argument unless what you're parsing a grammar file. (I just pushed a > new set of changes with some much more sensible names for some of this > stuff). > > There are basically two choices if you're trying to keep it simple: > > 1. Plain old "new GrammarBindingResolver()" -- this just creates a > tree using the "resolver" (tag or nonterminal name) as the head > of the tree and the results of the subexpressions as the children > of that tree. > > 2. AnnotationGrammarBindingResolver() -- this takes a class as an > argument and inspects that class's *static* inner classes and > *static* methods to try to match things its resolving with > members that have the @bind or @bind.as attributes. > > Eventually if you're trying to build grammars at runtime you'll almost > certainly want to use the first one, since you can't change > annotations at runtime. This approach is more tedious but more > flexible. > > Please keep pestering me to write that tutorial. I really need to do > it right away. I've finally got TibDoc working, so I don't have any > excuses left... > > - a > --Apple-Mail-3-1055679781 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=ISO-8859-1 Adam,

Thanks again for your = help.=A0 The annotations stuff is much clearer now :-)=A0 I've pulled = the latest changes and adjusted for the new names, so I now have = something like:

Tree<String> res =3D new = CharParser(MetaGrammar.newInstance()).parse(new = FileInputStream(s[0])).expand1();
Union mygrammar =3D = Grammar.create(res, "Expr", new = Grammar.Bindings());
CharParser parser = =3D new = CharParser(mygrammar);
Forest<String> r2 =3D parser.parse(new = FileInputStream(s[1]));
Tree t =3D = r2.expand1();
System.out.println(t);

but now it breaks (on the = parser.parse() bit), either saying
=A0unexpected character=A0 = '2' encountered at 1:0
for e.g.
=A0=A0 =A0Expr=A0 =A0 = =3D [0-9]++
=A0 =A0 =A0 =A0 =A0 =A0 | Plus:: Expra "+" = Expr
=A0 =A0 Expra=A0=A0 =3D ("a" | "b")
trying = a+2

or=A0
=A0 = unexpected end of file
for e.g. demo.g = with=A0(11+2*3)-44

- generally I get one of = those two errors whatever I try.=A0=A0I'm clearly doing something very = simple wrong!

Sorry to be wasting so much = of your time like this.

Simon

On 19 Jul 2006, at 08:17, Adam Megacz wrote:


Hi, = Simon!=A0 BTW, I've set up = a mailing list for SBP, and I'm cc'ing
this = message to it:


This reply = might be a bit brief; I'll write more in the morning.

Tree<String> res =3D new = CharParser(MetaGrammar.make()).parse(new
Union meta =3D MetaGrammar.make(res, "s");
SequenceInputStream sis =3D new = SequenceInputStream(new FileInputStream
(s[0]), = new FileInputStream(s[1])); // grammar
res =3D = new CharParser(meta).parse(sis).expand1();
Union = mygrammar =3D MetaGrammar.make(res, "ts"); //, new = TestCaseMaker());
CharParser parser =3D new = CharParser(mygrammar);
Forest<String> r2 =3D = parser.parse(new FileInputStream(s[2])); // input
Tree t =3D r2.expand1();
System.out.println(t);

Firstly, out = of curiosity, what's the
advantage of = parsing meta.g and using that instead of just using the
built-in meta grammar used to parse meta.g in the = first place?

No advantage whatsoever.=A0 RegressionTest only does that = as an extra
"sanity check" to make sure I = haven't broken anything.

Also, = previously I could call MetaGrammar.make(res, "ts") or similar
without any mention of GrammarBindingResolvers; now = calling that
assumes I want an = AnnotationGrammarBindingResolvers, which promptly
gets unhappy when you try to use it (e.g. with the = grammar

=A0=A0 =A0 = ts =A0 =A0 =A0 =3D = Expr
=A0=A0 =A0 Expr=A0 =A0 =3D [0-9]++
=A0=A0 =A0 =A0 = =A0 =A0 =A0 | Plus:: (left::Expra) "+" (right::Expr)
=A0=A0 =A0 = Expra =A0 =3D Foo:: = ("a" | "b")



a Java method/class/ctor = matching tag "Foo", nonterminal "Expra" with
1 arguments
=A0=A0 =A0 =A0 =A0 at
(AnnotationGrammarBindingResolver.java:60)
=

The short version: try removing "left::", "right::", = and "Foo::" from
your grammar; they're not really = needed.

Ah yes.=A0 = The idea with AnnotationGrammarBindingResolver (yes, I = know,
that name is way too long) is = this:

=A0 Any = sequence of grammar elements in which *two or more elements* = are
=A0 *not* dropped must = "resolve".=A0 That is, you = have to explain to SBP
=A0 how to make a tree out of the = two nodes.=A0 For = example,

=A0=A0 =A0 = MultiplyExpr =3D Expr "+" Expr

=A0 SBP needs to know how to turn = those two "sub-exprs" into a tree.=A0= So
=A0 it "resolves" the sequence by = looking at two things: the name of the
=A0 nonterminal ("MultiplyExpr") = and the tag (in this case there is no
=A0 tag).=A0 You should avoid using tags = unless you're forced to; for
=A0 example:

=A0=A0 =A0 SomeExpr =3D OtherExpr = (Foo:: SecondExpr ThirdExpr)

=A0 The "Foo" is a tag = (designated by the double-colon after it); it
=A0 = tells SBP how to resolve the subsequence with two = elements
=A0 (SecondExpr and = ThirdExpr).=A0 The "outer" = sequence (OtherExpr (...))
=A0 is resolved using the name of = the nonterminal (SomeExpr).

Okay, now back to the = annotations.=A0 When you = call MetaGrammar.make(),
you should = always pass in a GrammarBindingResolver as the third
argument unless what you're parsing a grammar = file.=A0 (I just pushed = a
new set of changes with some much more sensible = names for some of this
stuff).

There = are basically two choices if you're trying to keep it simple:

=A0 1. Plain old "new = GrammarBindingResolver()" -- this just creates a
=A0=A0 =A0 = tree using the "resolver" (tag or nonterminal name) as the = head
=A0=A0 =A0 of the tree and the = results of the subexpressions as the children
=A0=A0 =A0 = of that tree.
=A0 2. = AnnotationGrammarBindingResolver() -- this takes a class as an
=A0=A0 =A0 = argument and inspects that class's *static* inner classes = and
=A0=A0 =A0 *static* methods to = try to match things its resolving with
=A0=A0 =A0 members that have the = @bind or @bind.as attributes.

Eventually if you're trying to = build grammars at runtime you'll almost
annotations at runtime.=A0 This approach is more tedious = but more
flexible.

Please = keep pestering me to write that tutorial.=A0 I really need to do
it right away.=A0= I've finally got TibDoc working, so I don't have any
excuses left...

=A0 - a

=

= --Apple-Mail-3-1055679781--