I have the following in my scanner class:
*def* t_newline(self, t):
r'\n+'
t.lexer.lineno += len(t.value)
*def* t_error(self, t):
*raise* ScanError(t.value[0], t.lineno, self.find_column(t.lexpos))
This keeps track of line numbers in the tokens themselves. If a scan
error occurs, a (custom) exception is raised, which contains the
character at which the scan error occured, the line number and the
position within that line.
In my parser class I have:
*def* p_error(self, p):
*if* p is None:
*raise* ParseError(None, None, None)
*else*:
*raise* ParseError(p.value, p.lineno, self.find_column(p.lexpos))
Here, if a parse error occurs, once again a (custom) exception is
raised, which contains the value of the token, the line number (of the
token) and the position within that line (of the token). This works. I
don't have the problem you have. Note that I use tracking=0 (the
default) for the parse(...) method.
Are you doing anything different? What's your code?
Also, are you sure you are not confused by the position within the line
(p.lexpos), which is relative to the input string and not to the
beginning of the line? This is the reason I use my find_column(...)
function to get the position within the line...
Dennis
Dave! wrote:
>Hello!
>I'm using PLY-2.3 to build a C language compiler, and I need to know
>if there's any way to restart the lineno counter between lexical and
>syntactic analysis.
>
> What I mean with that is that, first of all, I analyze the source
>code and show the tokens found by the lexer with lex.token() function.
>With that I obtain tokens that look like this: LexToken(RBRACE, '}',
>22, 485). The problem comes with the syntactic analysis, where I use
>yacc.parse() function. At this point, when it detects an error I made
>it print the token that caused it, but the line number isn't the real
>line number, it doesn't restart the lineno counter between lex.token()
>and yacc.parse().
>
> For example, in a source code with 20 lines, lex.token() says that
>line 1 is line 1, but yacc.parse() says that line 1 is line 21...
>
> What can I do??
>
>
> Thanks for your help!!
>>
>
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"ply-hack" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/ply-hack?hl=en
-~----------~----~----~----~------~----~------~--~---