Hao
Hao

Reputation: 428

Need help on ply LALR parser

I'm trying to build a script tool with ply. However I'm blocked by some parsing errors. By moving those p_xxx functions to different places, I got different syntext errors. Could any one give kind help?

e.g. If I move p_funcall after other p_xxx functions, then I got:

Syntax error at '(', lineno 4, pos 8, data ' fun(arg1,arg2,arg3) '

Below is the source code:

    #!/usr/bin/python
# -*- coding: UTF-8 -*-

__version__ = '3.16'

import sys
sys.path.insert(0, "..")

import ply.lex as lex
import ply.yacc as yacc
import os

import dumper

class TdsParser(object):
  '''
  Base class for a lexer/parser that has the rules defined as methods
  '''
  def __init__(self, **kw):
    self.debug = kw.get('debug', 0)
    self.names = {}
    try:
      modname = os.path.split(os.path.splitext(__file__)[0])[
        1] + "_" + self.__class__.__name__
    except:
      modname = "parser" + "_" + self.__class__.__name__
    self.debugfile = modname + ".dbg"
    self.tabmodule = modname + "_" + "parsetab"
    print(self.debugfile, self.tabmodule)

    # Build the lexer and parser
    self.lexer = lex.lex(module=self, debug=self.debug)
    self.yaccer = yacc.yacc(module=self,
          debug=self.debug,
          debugfile=self.debugfile,
          tabmodule=self.tabmodule)

  tokens = (   
    'COMMA', 'COLON', 
    'LPAREN', 'RPAREN', 'LBRACE', 'RBRACE',
    'ATOM', 'NUMBER',
  )

  # Tokens

  # Tokens

  t_COMMA = r','
  t_COLON = r':'

  def t_LPAREN(self, t):
    r'\('
    return t

  def t_RPAREN(self, t):
    r'\)'
    return t

  def t_ATOM(self, t):
    r'[a-zA-Z_][a-zA-Z0-9_]*'
    return t

  def t_NUMBER(self, t):
    r'\d+|0[Bb][01]+|0[oO][0-7]|0[xX][a-f0-9]'
    return t

  def t_newline(self, t):
    r'\n+'
    t.lexer.lineno += len(t.value)

  t_ignore  = ' \t'

  def t_error(self, t):
    print("Illegal character '%s', lineno %d, pos %d, data '%s'" 
           % (t.value, t.lexer.lineno, t.lexer.lexpos, t.lexer.lexdata))
    t.lexer.skip(1)

  def t_COMMENT(self, t):
    r'\#.*'
    pass

  #yacc parser


  def p_funcall(self, p):
    '''
    funcall : ATOM LPAREN arglist RPAREN
         | ATOM LPAREN RPAREN
    '''
    print("\n###", sys._getframe().f_code.co_name, "###\n")
    pass

  def p_varlist(self, p) :
    '''          
    varlist : varlist expression
         | expression
    '''
    print("\n###", sys._getframe().f_code.co_name, "###\n")
    pass    

  def p_arglist(self, p) :
    '''          
    arglist : arglist COMMA expression
         | expression
    '''    
    print("\n###", sys._getframe().f_code.co_name, "###\n")
    pass


  def p_expression(self, p) :
    '''
    expression : ATOM
         | NUMBER
    '''
    print("\n###", sys._getframe().f_code.co_name, "###\n")
    dumper.dump(p[1])
    pass

  def p_error(self, p):
    if p:
      #dumper.dump(p)
      print("Syntax error at '%s', lineno %d, pos %d, data '%s'" 
           % (p.value, p.lexer.lineno, p.lexer.lexpos, p.lexer.lexdata))
    else:
      print("Syntax error at EOF")

  def yacc_input(self, data):
    self.yaccer.parse(data)

if __name__ == "__main__":      
  data = 'fun(arg1,arg2,arg3)'  
  tdsparser = TdsParser()  
  tdsparser.yacc_input(data)

Thanks.

Upvotes: 1

Views: 229

Answers (1)

rici
rici

Reputation: 241671

The first production in the first parser function in your file defines the target of the parse: on each invocation, the parser will attempt to parse a single instance of the start non-terminal.

As written in the question, the parser will recognise (a single) funcall. But if you move p_funcall elsewhere, you will end up generating a parser which is trying to match a different start symbol. If you don't move anything else, that will be varlist, and your input doesn't look at all like a varlist.

You can also explicitly declare the start symbol. See the Ply manual for details.

Upvotes: 1

Related Questions