Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7180597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:22:55+00:00 2026-05-28T17:22:55+00:00

All, I’m writing a very simplistic parser with python PLY . It mostly does

  • 0

All,

I’m writing a very simplistic parser with python PLY. It mostly does the job, but for many of the lines of input, I get a Syntax error from yacc. Here is the lexer and parser code, slightly modified for easier testing:

tokens = ('VAR', 'NUMBER', 'CLOSE', 'JUNK')

# Tokens

t_VAR     = r'%[mM]\['
t_CLOSE   = r'\]'
t_JUNK    = r'.'

# Ignored characters
t_ignore = " \t\r"

def t_NUMBER(t):
    r'\d+'
    try:
        t.value = int(t.value)
    except ValueError:
        print("Integer value too large %d", t.value)
        t.value = 0
    return 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
import ply.lex as lex
lex.lex()

# Parsing rules

def p_statement(p):
    '''statement : field'''
    try:
        print p[1]
    except IndexError:
        pass

def p_trash(p):
    '''statement : JUNK'''
    pass

def p_field(p):
    '''field : VAR NUMBER CLOSE'''
    #print p[1], p[2], p[3]
    p[0] = p[2]

def p_error(p):
    print("Syntax error at '%s'" % repr(p)) #p.value)

import ply.yacc as yacc
yacc.yacc()

For a sample: yacc.parse('.set %m[702] $substr($currentlength,2,$currentpg)') which gives as output:

Syntax error at 'LexToken(JUNK,'s',1,1)'
Syntax error at 'LexToken(JUNK,'$',1,13)'

It should output 702 only.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T17:22:56+00:00Added an answer on May 28, 2026 at 5:22 pm

    Your top level rule requires a single statement. p_trash matches the first ‘.’ and returns a statement and there is no top level rule to allow it to continue. You could do something like:

    def p_junk(p):
        '''statement | JUNK statement'''
    

    You could also do something like this (and create a list of statements):

    def p_statements(p):
        '''statements | statement statements
                      | empty'''
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

All, I am very new to stored procedures in general but I am struggling
All I know about the constraint is it's name ( SYS_C003415 ), but I
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
all, I am trying to multiply a matrix to a vector in OpenGL, but
All, I'm working on a proof of concept replacement for the windows taskbar. (Mostly
All the articles I've found via google are either obsolete or contradict one another.
All the recent VisualSVN Server posts made me want to check it out. I
All front-end developers know the pain of coding for Firefox, then viewing our then
All I want is to be able to change the color of a bullet
All throughout an application wherever error messages (or other user messages) are used I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.