> I'm going to go with specifying t_STRING after t_NAME, so that the
> word "string" is taken as a NAME. Try putting t_STRING before
> t_NAME...
>
[snip]
> # Tokens
>
> t_NAME = r'[a-zA-Z_][a-zA-Z0-9_]*'
> t_STRING = r'"[a-zA-Z0-9]*"'
> t_STRDEF = 'string'
I'm going to agree with this. The t_STRDEF token is being matched by
the t_NAME rule above it which is almost certainly causing this
problem. The original poster should look at http://www.dabeaz.com/ply/ply.html#ply_nn6
and note the part about "reserved words."
This problem comes up so often, I'm almost wondering if I should add
an extra validation check to PLY. It occurs to me that I could
modify lex to look for regexes that are made up of nothing but simple
characters (e.g., 'string' above) and then test them for matches
against the other regular expressions (e.g., t_NAME). If a match is
found, some kind of stern warning message could be issued about it.
As the documentation states, it's really a bad idea to make separate
token rules for reserved words like 'string', 'if', 'else', etc.
Cheers,
Dave
That's because you changed all NAME tokens to have the value of NAME.
You really want to change the type. Change "t.value" to "t.type" so
the assignment becomes
t.type = reserved.get(t.value, 'NAME')
Andrew
da...@dalkescientific.com
It is for counting curly brace levels.
> to implement an 'if', do you have to use 'ast' like in GardenSnake
Please do not assume we know anything but PLY.
I have no idea what you mean with the above question. This is a PLY mailing
list, not a GardenSnake mailing list. Also, I fail to see any relation between
the lexer functions and your question.
(The lexer functions of your question are nearly the first thing in a parser,
and an ast is about the last thing in the parser. In between are at least 2 or
3 other steps that you don't mention at all. Therefore, I cannot make a
relation between the code and your question.)
In general, if you want useful answers, plz give a concrete coherent concise
problem description, instead of a random piece of code and a (in my view)
unrelated random question without any context. Also show what you have done so
far with respect to that problem.
In addition, a title "Why doesn't this work" is too generic. Please use a more
specific title, and change the title of the post when you change the subject.
Many readers will decide whether or not read a post based on the title alone.
From your previous posts I believe that you have little Python experience.
That is not a problem in itself, but asking basic Python questions here is not
the most effective way of learning the language. Instead, I'd like to
recommend you read a few tutorials, and/or subscribe the python-tutor mailing
list.
To a lesser extent, that also holds for asking basic parsing questions here.
Your question above seems to indicate (to me) a lack of understanding basic
parsing terminology. Please make sure you understand what those terms mean and
how they are related when using them, otherwise they will only confuse the
discussion.
Sincerely,
Albert
Ok, sorry for the confusion.
It just shows I also don't read everything of the list, and/or know everything
about PLY.
Albert
GardenSnake was something I wrote years ago. It uses the ast because
GardenSnake ends up generating Python byte code and I could leverage
existing Python data structures and byte code generation to help with
that.
Implementing an 'if' can be done in many ways, so there's no way to
answer than without a lot more context.
Based on your postings, you still have a lot to go before you
understand PLY and how it works, You posted a snippet of code (with
t_ccode_lbrace and t_ccode_rbrace) and asked what it does. It comes
from the PLY documentation, and with a description in the paragraph
before the code.
But to understand that description you have to know why lexers (being
regular grammars) can't on their own handle balanced braces. And to
get that requires some theory background that can be hard for people
to pick up on their own.
I cannot help you there. I learned this through university course
work and a lot of work on my own. There are books on the topic, but
then you'll have to learn how to convert from the syntax used in the
book to using PLY, which isn't always direct. Nor do I have advice on
which books to even look at to get a better understanding.
Best regards,
Andrew
da...@dalkescientific.com
I would agree. I originally developed PLY so I could use it in a
university course I was teaching on compilers--that was a graduate-
level computer science course. I don't think you need to suffer
through the infamous Dragon book to be able to use PLY. However,
you'll definitely want to get some supporting texts. The O'Reilly
"Lex & Yacc" book isn't too bad as a reference. Almost everything in
that book applies to PLY and can be used as a source of ideas. Other
than that, I don't really have many
recommendations for a modern compilers text.
Cheers,
Dave