Python grammar: EBNF to Flex & Bison

Programming, for all ages and all languages.
Post Reply
User avatar
narke
Member
Member
Posts: 119
Joined: Wed Dec 26, 2007 3:37 am
Location: France

Python grammar: EBNF to Flex & Bison

Post by narke »

Hello,

I'm trying to write a python interpreter with Flex & Bison.
There is the python grammar available here: http://docs.python.org/dev/py3k/reference/grammar.html.

I take as an example the following specification in EBNF notation from the python grammar:

Code: Select all

file_input: (NEWLINE | stmt)* ENDMARKER
How can I translate this in Flex & Bison?

In a bison file I wrote:

Code: Select all

file_input:
    NEWLINE
  | stmt
  | ENDMARKER
  ;
But this is not pretty exact, as the ENDMARKER is mandatory at the end and here I can't force it.

Do you know how to translate EBNF notation to Flex & Bison?

Thanks in advance!
OS for PowerPC Macs: https://github.com/narke/Einherjar
Operating system: colorForth computing environment for x86.: https://github.com/narke/Roentgenium
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Python grammar: EBNF to Flex & Bison

Post by Solar »

I have absolutely zero experience with writing for Flex & Bison, but I've read one or two standards documents, so I dare a guess. Would this work?

Code: Select all

# file_input is file_input_statements plus ENDMARKER
file_input:
    file_input_statements ENDMARKER

# file_input_statements (plural) is one or more file_input_statement (singular)
file_input_statements:
    file_input_statement | file_input_statements

# file_input_statement is a statement or a newline
file_input_statement:
    statement | NEWLINE
Syntax is probably off, and naming isn't really intuitive, but I hope I got the idea across. (Credits to ISO 9899:1999...)
Every good solution is obvious once you've found it.
User avatar
stephenj
Member
Member
Posts: 140
Joined: Wed Jul 23, 2008 1:37 am
Location: Canada

Re: Python grammar: EBNF to Flex & Bison

Post by stephenj »

This isn't the best way to do this, but I'm in a hurry to go to work.

Code: Select all

file_input
   : NEWLINE file_input
   | stmt file_input
   | ENDMARKER
   ;
I know books on compilers suck, but you may want to pick one up.
User avatar
narke
Member
Member
Posts: 119
Joined: Wed Dec 26, 2007 3:37 am
Location: France

Re: Python grammar: EBNF to Flex & Bison

Post by narke »

As I understand I must find solutions case by case.
This would be a little more difficult than I imaganed but I will try.
OS for PowerPC Macs: https://github.com/narke/Einherjar
Operating system: colorForth computing environment for x86.: https://github.com/narke/Roentgenium
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Re: Python grammar: EBNF to Flex & Bison

Post by JamesM »

stephenj's solution brings everything into one nonterminal, but the most "honest" translation would probably be

Code: Select all

file_input: file_input_optional ENDMARKER
          ;

file_input_optional: /* Intentionally left blank, for lambda-transition */
                   | NEWLINE file_input_optional
                   | stmt file_input_optional
                   ;
Introducing another nonterminal for the optional Kleene-starred part.
Post Reply