Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3661950
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T01:21:43+00:00 2026-05-19T01:21:43+00:00

I am embedding python in C/C++ program. What I am trying to do is

  • 0

I am embedding python in C/C++ program.

What I am trying to do is to parse the python script file from the C/C++ program, break the file to “blocks” so that each “block” is an a valid command in python code. Each block I need to put into std::string. For example:

#PythonScript.py

import math

print "Hello Python"
i = 0;
while (i < 10):
    print "i = " , i;
    i = i + 1;

print "GoodBye Python"

In this script are 5 different “blocks”:

  • the first one is "import math;"
  • the second is "print "Hello Python;"
  • the third is "i = 0;"
  • and the fourth is

    while (i < 10):\n\tprint "i = " , i;\n\ti = i + 1;
    

My knowledge in python is very basic and I am not familiar with the python code syntax. What is the best way to do this, is there any Python C/C++ API function that supports this?


why i need it -> for GUI purpose. My program , which is writen in C, uses python to make some calculations. I run from C code , using python C API , python script and what i need is a way to capture python’s output in my program. I catch it and evrything is ok, the problem is when the script involves user input. What happens is that i capture python’s output after the script is finished , therefore, when there is an input command in the script i get a black screen …. i need to get all the printings before the input command.

The first solution i tried is to parss the script to valid commands and run each comand, one after the other , seperatly …. for this i need to pars the script and deside what is a command and what is not …
The question is : what is the best way to do this and if there is somthing that allready does ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T01:21:44+00:00Added an answer on May 19, 2026 at 1:21 am

    If you want to do syntax analysis you should look into Pythons grammar (and maybe use Bison as a parser generator)

    Python grammar specs:

    • http://docs.python.org/reference/grammar.html
    • http://inst.eecs.berkeley.edu/~cs164/sp10/python-grammar.html
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm embedding Python interpreter to a C program. However, it might happen that while
I am embedding IronPython in a C# program. I understand that I need to
I am embedding Python into my C++ program, and have used PyRun_SimpleString quite effectively
I'm working on embedding python into my C++ program using swig. At the moment
I have this code in a C application that's embedding Python (2.7.1): { PyObject
I know that embedding CSS styles directly into the HTML tags they affect defeats
I'm embedding Python into a C++ application. I plan to use PyEval_EvalCode to execute
I'm interested in experimenting with embedding Python in my application, to let the user
I am currently writing an app in python that needs to generate large amount
I'm writing a Chrome extension, and want to write one JS file, that provides

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.