How to get a control flow graph of a program?











up vote
0
down vote

favorite












I want to get a control flow graph of a code/program (be it any programming language and given its grammar).
I have tried using lark library in python to parse a basic C sample program [I provided the grammar for basic c syntax to lark]. As a result, it gave me an object of parse tree or similar sort of stuff, now I am wondering where to proceed.



Having said that, any kind of new approach is highly appreciated. The prime goal is to get the control flow graph of a code/program, given the grammar of the language in which it is written.










share|improve this question


























    up vote
    0
    down vote

    favorite












    I want to get a control flow graph of a code/program (be it any programming language and given its grammar).
    I have tried using lark library in python to parse a basic C sample program [I provided the grammar for basic c syntax to lark]. As a result, it gave me an object of parse tree or similar sort of stuff, now I am wondering where to proceed.



    Having said that, any kind of new approach is highly appreciated. The prime goal is to get the control flow graph of a code/program, given the grammar of the language in which it is written.










    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I want to get a control flow graph of a code/program (be it any programming language and given its grammar).
      I have tried using lark library in python to parse a basic C sample program [I provided the grammar for basic c syntax to lark]. As a result, it gave me an object of parse tree or similar sort of stuff, now I am wondering where to proceed.



      Having said that, any kind of new approach is highly appreciated. The prime goal is to get the control flow graph of a code/program, given the grammar of the language in which it is written.










      share|improve this question













      I want to get a control flow graph of a code/program (be it any programming language and given its grammar).
      I have tried using lark library in python to parse a basic C sample program [I provided the grammar for basic c syntax to lark]. As a result, it gave me an object of parse tree or similar sort of stuff, now I am wondering where to proceed.



      Having said that, any kind of new approach is highly appreciated. The prime goal is to get the control flow graph of a code/program, given the grammar of the language in which it is written.







      python-3.x grammar control-flow-graph lark-parser






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Aug 22 at 14:11









      Ben Claude

      1




      1
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote














          As a result, it gave me an object of parse tree or similar sort of stuff, now I am wondering where to proceed.




          A common approach is to




          1. walk the tree to generate an intermediate representation in which all looping and conditional constructs are replaced with jumps,

          2. divide the instructions of the IR into basic blocks by starting a new block before each jump label and after each jump and then

          3. construct a control flow graph where the basic blocks are the nodes, each block that doesn't end with a jump has an edge to the block that comes after it, and each block that does end with a jump has an edge to the possible jump targets (where the jump targets for a traditional conditional jump instruction would include the following block).



          The prime goal is to get the control flow graph of a code/program, given the grammar of the language in which it is written.




          You can't get the CFG of a program if all you know about the language is its grammar. You'll need some understanding of the semantics of the language to be able to construct a CFG.






          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51968789%2fhow-to-get-a-control-flow-graph-of-a-program%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote














            As a result, it gave me an object of parse tree or similar sort of stuff, now I am wondering where to proceed.




            A common approach is to




            1. walk the tree to generate an intermediate representation in which all looping and conditional constructs are replaced with jumps,

            2. divide the instructions of the IR into basic blocks by starting a new block before each jump label and after each jump and then

            3. construct a control flow graph where the basic blocks are the nodes, each block that doesn't end with a jump has an edge to the block that comes after it, and each block that does end with a jump has an edge to the possible jump targets (where the jump targets for a traditional conditional jump instruction would include the following block).



            The prime goal is to get the control flow graph of a code/program, given the grammar of the language in which it is written.




            You can't get the CFG of a program if all you know about the language is its grammar. You'll need some understanding of the semantics of the language to be able to construct a CFG.






            share|improve this answer

























              up vote
              1
              down vote














              As a result, it gave me an object of parse tree or similar sort of stuff, now I am wondering where to proceed.




              A common approach is to




              1. walk the tree to generate an intermediate representation in which all looping and conditional constructs are replaced with jumps,

              2. divide the instructions of the IR into basic blocks by starting a new block before each jump label and after each jump and then

              3. construct a control flow graph where the basic blocks are the nodes, each block that doesn't end with a jump has an edge to the block that comes after it, and each block that does end with a jump has an edge to the possible jump targets (where the jump targets for a traditional conditional jump instruction would include the following block).



              The prime goal is to get the control flow graph of a code/program, given the grammar of the language in which it is written.




              You can't get the CFG of a program if all you know about the language is its grammar. You'll need some understanding of the semantics of the language to be able to construct a CFG.






              share|improve this answer























                up vote
                1
                down vote










                up vote
                1
                down vote










                As a result, it gave me an object of parse tree or similar sort of stuff, now I am wondering where to proceed.




                A common approach is to




                1. walk the tree to generate an intermediate representation in which all looping and conditional constructs are replaced with jumps,

                2. divide the instructions of the IR into basic blocks by starting a new block before each jump label and after each jump and then

                3. construct a control flow graph where the basic blocks are the nodes, each block that doesn't end with a jump has an edge to the block that comes after it, and each block that does end with a jump has an edge to the possible jump targets (where the jump targets for a traditional conditional jump instruction would include the following block).



                The prime goal is to get the control flow graph of a code/program, given the grammar of the language in which it is written.




                You can't get the CFG of a program if all you know about the language is its grammar. You'll need some understanding of the semantics of the language to be able to construct a CFG.






                share|improve this answer













                As a result, it gave me an object of parse tree or similar sort of stuff, now I am wondering where to proceed.




                A common approach is to




                1. walk the tree to generate an intermediate representation in which all looping and conditional constructs are replaced with jumps,

                2. divide the instructions of the IR into basic blocks by starting a new block before each jump label and after each jump and then

                3. construct a control flow graph where the basic blocks are the nodes, each block that doesn't end with a jump has an edge to the block that comes after it, and each block that does end with a jump has an edge to the possible jump targets (where the jump targets for a traditional conditional jump instruction would include the following block).



                The prime goal is to get the control flow graph of a code/program, given the grammar of the language in which it is written.




                You can't get the CFG of a program if all you know about the language is its grammar. You'll need some understanding of the semantics of the language to be able to construct a CFG.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Aug 22 at 16:05









                sepp2k

                291k38593606




                291k38593606






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51968789%2fhow-to-get-a-control-flow-graph-of-a-program%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Berounka

                    Sphinx de Gizeh

                    Different font size/position of beamer's navigation symbols template's content depending on regular/plain...