Hello Alessandro,

This rule:

 > def t_OTHER(t):
 >     r'[^ $]+'
 >     return t

is a negative rule. It matches way to much. You should probably specify 
it in a positive way. I recommend introducing a t_ID to scan identifiers.

Also, take a look at http://www.dabeaz.com/ply/ply.html (sections 4.3 
and 4.4). This explains how to do lexing of identifiers vs keywords.

Best,
Dennis


Alessandro wrote:
> Hi all,
>   I'm programming a simple templating system. The simplest expression
> I have to manage is:
> " bla bla bla .. $FUNC ( bla bla bla.. ) bla bla.."
> 
> Here my code:
> 
> tokens = (  'OTHER', 'FUNC'   )
> 
> def t_FUNC(t):
>     r'\$FUNC'
>     return t
> 
> def t_OTHER(t):
>     r'[^ $]+'
>     return t
> 
> t_ignore = " \t"
> 
> def t_error(t):
>     print("Illegal character '%s'" % t.value[0])
>     t.lexer.skip(1)
> 
> import ply.lex as lex
> lex.lex()
> 
> def p_expression(p):
>     '''expression : OTHER expression
>               | func expression
>               | empty'''
>     pass
> 
> def p_func(p):
>     "func : FUNC '(' expression ')'"
> 
> def p_empty(p):
>     'empty :'
>     pass
> 
> 
> def p_error(p):
>     if p:
>         print("Syntax error at '%s'" % p.value)
>     else:
>         print("Syntax error at EOF")
> 
> import ply.yacc as yacc
> yacc.yacc(debug=1)
> 
> yacc.parse(' bla bla $FUNC( bli bli ) ')
> 
> 
> But I get the "Syntax error at '(' " error. Probably the "(" is
> interpreted as "OTHER" and not as "func".
> How can I solve?
> I have the same problem with "FUNC"; it must start with "$"; if I
> remove it (or if I put it into "OTHER" definition) the "FUNC" word is
> interpreted as a "OTHER" token.
> 
> It's a precedence issue?
> 
> Thanks
> Alessandro
> 
> --
> 
> 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.
> 
> 

--

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.


Reply via email to