Continued attempts at Yacc-ification :)

Nolan Darilek nolan_d at bigfoot.com
Mon Jan 3 20:03:54 EST 2000


I'm continuing to try writing a Yacc grammar for Python.

Today I finished a large subset of the grammar. I am now trying to
pound out any bugs before I write a lexical analyzer. I am
particularly having problems with the following two rules which I
wrote for single_input and file_input:

Grammar rules:
single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE
file_input: (NEWLINE | stmt)* ENDMARKER
My whacked-up interpretation :)
single_input: '\n' | simple_stmt | compound_stmt '\n';

file_input: '\n'
| stmt
| file_input
| END;

The file_input rules don't reduce, though. My reasoning for each
branch of the rule is:

line 1: eliminate newlines in file.
line 2: Reduce statements and consider them as a file_input.
Line 3: Producing one file_input instead of lots of separate ones per
statement or newline.
Line 4: Matching END, which I've declared as a %token and my lexical
analyzer will return, and to signal the end of the file.

When I run byacc, these 4 rules don't reduce. The best explanation I
can come up with is that single_input somehow conflicts, since I can
comment it out and everything reduces. I want single_input though, since I'm also working on
an interpretter.

Can someone please show me how I've gone wrong here? Or, better yet,
if there are any pre-written Yacc grammars available for Python, which
I doubt but I thought I'd at least ask :), I'd love to see them to
 shamelessly ste...err, examine and learn from. :) Thanks.




More information about the Python-list mailing list