[Parrot-users] A few questions about grammar generated by create_language.pl

Patrick R. Michaud pmichaud at pobox.com
Sun Feb 28 17:30:33 UTC 2010


On Sun, Feb 28, 2010 at 10:15:16AM +0100, Hugo Arts wrote:
> 
> 1) The <EXPR> subrule is used a few times, but I'm not clear on
> exactly what it means. I assume it somehow incorporates the "token
> term" as well as the circumfix/infix/etc. operator tokens, but I'm not
> sure exactly how this works.

The <EXPR> subrule is the entry point to the operator-precedence 
parser.  It then goes through and processes according to
term/infix/prefix/postfix/etc.

> 2) token quote:sym<"> { <?["]> <quote_EXPR ':qq'> }
> If I'm correct, the first part of that rule is simply a zero-width
> assertion that the first character is a double quote. The quote_EXPR,
> I assume, is a rule that actually parses a quoted string. Why is the
> assertion included? Doesn't quote_EXPR assert the expression is within
> quotes anyway?

<quote_EXPR> accepts _any_ bracketing or non-alphanumeric character as 
delimiters, not just double quotes.  The <?["]>  makes sure that we 
in fact have a double-quote at the beginning but doesn't consume it, 
because <quote_EXPR> needs to be able to read the first character 
in order to know what the appropriate closing delimiter will be.

> 3) the <integer> subrule seems to parse decimal, binary, and hex
> numbers. What about octal? I've tried prefixing with 0, o, and 0o
> prefixes, but those all throw syntax errors.

The <integer> subrule is inherited from HLL::Grammar (in nqp-rx),
and 0o should be working.  If not, that's likely a bug.

> 4) What exactly is the definition of <ident>? It is mentioned many
> times in the perl 6 synopse, but I seem to have missed its definition.
> My experiments lead me to believe it's something like: token ident { [
> <alpha> | '_' ] \w* } (that would be consistent with what an
> identifier is in most languages)

You're correct.

> 5) token circumfix:<( )> { '(' <.ws> <EXPR> ')' }
> is this the same as { '(' ~ ')' [ <.ws> <EXPR> ] } ? If not, what is
> the difference? And another question, why is there no <.ws> after
> <EXPR>? Does the <EXPR> rule take care of that?

The main difference is that the ~ form would give a "closing paren
not found" exception upon reaching the end of the expression,
while the other form simply fails the rule and leaves it up to
a higher-level rule to issue an appropriate message.  I suppose
it could be argued either way (but I tend to follow Perl 6's
STD.pm in such situations).

Currently I think the <EXPR> takes care of trailing <.ws>, yes.

> 6) Grammar.O(':prec<u>, :assoc<left>', '%multiplicative')
> what does the prec<u> mean? I assume it's something to do with
> precedence, but what the 'u' argument means is not clear to me.

Precedence is handled by simple string comparisons, so :prec<u>
simply means that any operator with a precedence gt 'u' will
be considered "tighter", while any operator with a precedence
lt 'u' will be considered "looser".  You can use any strings you
want here, keeping in mind that it's a string comparison and
not a numeric comparison.

The specific letters used in the sample grammar were again chosen
to match the precedence levels used by the Perl 6 standard grammar.

Hope this helps!

Pm


More information about the Parrot-users mailing list