0

Don't ask why, but I want to create a Python program that generates a random Python program that is valid for execution. I found a similar thread Searching for a random python program generator , but it's old and all the links except the demonstration http://www.4geeks.de/cgi-bin/webgen.py are dead. And maybe something has changed in these years and it's easier to do such thing?

One of idea is to use quantum collapse algorithm/technique. This would require to make a kind of database, where I describe what symbols can came after previous one. For example after a can come (,, any letter, etc, but never, for example, ). Still I think this will create very gibberish source and I could easily set wrong conditions.

trshmanx
  • 600
  • 7
  • 16
  • 1
    I think you're looking for: [Software Reccomendations](https://softwarerecs.stackexchange.com/) this site is for coding relate issues. – Jab Oct 13 '21 at 16:52
  • 1
    If you formalize the representation of a Python program as a Context Free Grammar operating on tokens, you could easily generate "valid" Python programs as far as syntax goes. This gets into how the language is parsed and interpreted anyways. I would recommend first creating a Context-Free Grammar representation of Python syntax, then you will know what tokens to "randomly" generate at each stage of the code generation. – h0r53 Oct 13 '21 at 16:53
  • It's a dangerous thing to do. Such a program could wipe your hard drive or send a death threat to the president. – Jussi Nurminen Oct 13 '21 at 16:55
  • @Jab I am not looking for a software. I clearly sate "..want to create a Python program...". – trshmanx Oct 13 '21 at 16:59
  • @JussiNurminen that why it will have limited access. – trshmanx Oct 13 '21 at 17:00
  • @h0r53 "I would recommend first creating a Context-Free Grammar representation of Python syntax" << I don't understand this part. Create where and what? – trshmanx Oct 13 '21 at 17:02
  • @trshmanx but will it? It's entirely possible that a diabolically clever code will be created, which uses a loophole to break out of the Python interpreter, hijacks the Bluetooth connection to infiltrate your cellphone and becomes the Skynet. Or engages you in chat and uses sweet words to manipulate you into giving it access. How can you know? – Jussi Nurminen Oct 13 '21 at 17:16
  • @JussiNurminen :facepalm: – trshmanx Oct 13 '21 at 17:24
  • 1
    @trshmanx A Context-Free Grammar defines the syntax of a program. If each "word" in your code is considered a token, then only certain token sequences are permissible by the language. For example, consider `if` as a conditional token, which requires a `boolean expression`, a `boolean expression` can be `true`, `false`, or some comparator such as `expression < expression`. It's a very formal way to consider programs, but it's how Programs are validated and compiled in the first place. Research Context Free Grammars in Programming Languages. – h0r53 Oct 13 '21 at 17:43

1 Answers1

1

There are two main ways to achieve this:

  • Use third parties to generate runnable junk code, for instance using yarpgen to generate a valid C program, and then using things to translate that to python like https://github.com/DAN-329/C_to_Python_translator

  • Write a python code that writes randomly generated functions with random parameters calling eachother, if you just need a bunch of functions calling eachother doing random math operations that should not be that hard to implement.

Gabriel Slomka
  • 1,487
  • 1
  • 11
  • 24