Hello clousot,

you indicate you know there is an overlap between 
t_END/t_HOST/T_HOSTNAMES and t_NAME, since t_NAME also matches the texts 
"END", "HOST" and "HOSTNAMES". Precedence rules apply to the parser, not 
the lexer, so they won't help you with that problem. I suggest you take 
a look at http://www.dabeaz.com/ply/ply.html, section 4.3, the t_ID 
function, which handles reserved words. This should solve your problem.

Dennis

clousot wrote:
> I get in trouble with the definitions of precedence.
>
> I am trying to parse this file content :
>
> HOSTNAMES
>       HOST  1 "Name_a" "ctrla2" END
>       HOST  4 "NameC" "ctrl4" "ctrl25" END
> END
>
> using this code that I wrote to experience PLY:
> #---------------------------------------------------------------------------------------------------
>
> # -*- coding: utf-8 -*-
> """Module basic_lexyacc : """
>
> # imports
> import sys
> import lex
> import yacc
>
> tokens =
> ( 'CONTROL_STR','END','HOST','HOSTNAMES','NAME','NUMBER','PLUS', )
>
> # Tokens
> t_CONTROL_STR = r'("[^"]*")|(\'[^\']*\')'
> t_END     = 'END'
> t_HOST    = 'HOST'
> t_HOSTNAMES = 'HOSTNAMES'
> #t_NAME    = r'[a-zA-Z_][a-zA-Z0-9_]*'
>
> def t_NUMBER(t):
>     r'\d+'
>     t.value = int(t.value)
>     return t
>
> t_ignore = " \t"
>
> def t_newline(t):
>     r'\n+'
>     t.lexer.lineno += t.value.count("\n")
>
> def t_error(t):
>     print("Illegal character '%s'" % t.value[0])
>     t.lexer.skip(1)
>
> # Build the lexer
> lex.lex()
>
> # Parsing rules
> precedence = (
>     ('left', 'NAME'),
>     ('left','END','HOST','HOSTNAMES','NUMBER'),
>     ('left', 'CONTROL_STR'),
>     )
>
> # Init global variables as support to the parsing function
> control_str_lst = []
>
> def p_expression_UNIX_FileGrammar(p):
>     '''expression : hostnames'''
>
> def p_hostnames(p):
>     '''hostnames : HOSTNAMES host_def_lst END
>                  | HOSTNAMES END'''
>     # an other way to declare 'host' optional in one shot
>
> def p_host_def_lst(p):
>     '''host_def_lst : host_def_lst host_def
>             | host_def'''
>
> def p_host_def(p):
>     '''host_def : HOST NUMBER CONTROL_STR control_str_lst END'''
>     print "HOST number %d :" % p[2]
>     print "\tName is %s" % p[3]
>     print "\tlist of controls is %",control_str_lst
>
> def p_control_str_lst(p):
>     '''control_str_lst : control_str_lst CONTROL_STR
>                        | CONTROL_STR'''
>     if len(p) == 2:
>         # Make sure the list is empty at the first pass when building
> it
>         del control_str_lst[0:len(control_str_lst)]
>     control_str_lst.append(p[len(p)-1])
>
> def p_expression_number(p):
>     "expression : NUMBER"
>     p[0] = p[1]
>
> def p_error(p):
>     if p:
>         print("Syntax error at '%s'" % p.value)
>     else:
>         print("Syntax error at EOF")
>
> yacc.yacc()
>
> filePath = ""
> f = open('C:\FileToParse.txt','r')
> text_to_parse = f.read()
> f.close()
> yacc.parse(text_to_parse)
>
> #------------------------------------------------------------------------------------------------------
>
> The problem is that code does not work anymore if I uncomment the
> line :
> #t_NAME    = r'[a-zA-Z_][a-zA-Z0-9_]*'
>
> I understand there is a conflict with the other token but I don't
> understand how to set my precedence then to fix this problem ?
> (to be honnest I didn't know lex and yacc but have to use them)
>
> Thank you...
> >
>   


--~--~---------~--~----~------------~-------~--~----~
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