0

I am working on assignment and need to develop a python to openmodelica translator. For which I am using flex and bison in initial stages. Initially I need to define a subset of python language on which I could perform a whole demo. I am new to Python language, Can anybody suggest how can I define a subset of python language? Thanks.

  • It depends entirely on what you want to demonstrate. Python is a fully featured language (depending on which version you use). So the subset of its syntax and semantics that you should implement is dependent entirely on what you want to demonstrate in your `initial stages` – inspectorG4dget Nov 07 '13 at 23:20
  • You might want to look at the [`pyparsing`](http://pyparsing.wikispaces.com) examples, where people have written parsers for various different subsets of Python. Not because pyparsing code will do you much good in bison, but because the subsets they chose will likely be decent examples of subsets of Python. – abarnert Nov 07 '13 at 23:32
  • Also, is there a good reason not to just write it in Python and rely on [`ast`](http://docs.python.org/3/library/ast.html) and related stdlib modules to do all the hard work for you? – abarnert Nov 07 '13 at 23:34
  • I am using version 3.3. Initially I need to take sample of programs which start similarly (for example all programs should start from "import"). So, I need to define subset on the basis of same start statement. – user2148189 Nov 07 '13 at 23:36

1 Answers1

1

Well as you are probably not interested in writing it in Python itself, I guess the language reference is the best starting point. It defines the whole grammar of the language. So this is likely a good starting point to find some features of the language you want to implement on your own; and then you need to write your own grammar and a parser for it in your language of choice.

Otherwise, you could use the built-in Python language services to actually parse real Python code and extract it into abstract syntax trees for example.

But if you are meant to only have a subset, I don’t think having the full language capabilities will do you any good. So you better start off with a real subset of the grammar. A good way to get to know which features you want to take over is probably by using the language yourself for a bit. Do some tutorials etc. and see how the basic syntax works.

poke
  • 369,085
  • 72
  • 557
  • 602