-2

I'm trying to write a python parser, and in my opiniion it could parse an "if statement" but it doesn't. It shows me a "syntax error" message.

Can someone tell me what I'm doing wrong?

Thanks in advance.

The code is here: https://github.com/narke/py2neko


I modified the input string like this:

s = '''if 5:
    print 10
else:
    print 20 \n'''
check_syntax(s)

and the output is:

Syntax error at '5'
atom: 10
factor None
None
cmp: None None
atom: 20
factor None
None
cmp: None None 
simple_stmt: None
Tim Post
  • 33,371
  • 15
  • 110
  • 174
narke
  • 103
  • 1
  • 1
  • 4
  • 2
    You could provide the "syntax error" message. That might help us. – S.Lott Feb 08 '11 at 16:55
  • On a note unrelated to your actual question, you may want to look into using python's ast module. That way you can use python's parser and can focus on the 2neko part of the problem. – Winston Ewert Feb 08 '11 at 17:11

1 Answers1

1

From your code:

s = "if 5:\n"
check_syntax(s)

if 5:\n is not valid syntax because it is not a complete if statement. You need to provide a suite (code to execute) if the expression is True. For example:

>>> if 5:
... 
  File "<stdin>", line 2

    ^
IndentationError: expected an indented block

>>> compile('if 5:\n', '<string>', 'exec')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1
    if 5:
        ^
SyntaxError: unexpected EOF while parsing

>>> compile('if 5:\n  print 5', '<string>', 'exec')
<code object <module> at 0xb7f60530, file "<string>", line 2>
Andrew Clark
  • 202,379
  • 35
  • 273
  • 306