A simple single line, triple-quoted comment is giving syntax error. Why?

Gregory Ewing greg.ewing at canterbury.ac.nz
Thu Mar 26 18:43:29 EDT 2015


Ian Kelly wrote:
> What I mean is that if you construct a parse tree of "foo" "bar" using
> that grammar, it looks like this:
> 
>      expr
>        |
>     STRING+
>      /   \
> STRING  STRING

Not quite -- STRING+ is not a symbol in the grammar, it's
a shorthand for a combination of symbols. The parse tree
is actually just

       expr
       /   \
  STRING  STRING

> Not like this:
> 
>     expr
>      |
>    STRING+
>     /  \
>  expr  expr
>   |      |
> STRING  STRING

That would be

 >     expr
 >     /  \
 >  expr  expr
 >   |      |
 > STRING  STRING

Thomas 'PointedEars' Lahn wrote:
 > Prove it.

To get a parse tree like the above, there would need to be
a production like this in the grammar:

    expr ::= expr+

There is no such production, so that parse tree is impossible.
QED.

What you seem to be doing in your mind is taking this
production:

    expr ::= STRING

and using it "backwards" to justify expanding any occurrence
of STRING into expr. But grammars don't work that way. You're
committing a logical fallacy: just because some exprs are
STRINGs doesn't mean that all STRINGs are exprs.

-- 
Greg



More information about the Python-list mailing list