Giter Site home page Giter Site logo

Comments (4)

w-vi avatar w-vi commented on June 18, 2024

pycparser is a parser and from parser you don't expect any understanding for semantics. I consider this a correct behaviour as I actually want to know if there are braces or not that is if it is a compound statement or not. You need to handle this on your side as this really depends on what you want to do. These ASTs are different because the code is different and AST represents code not meaning or anything else.

from pycparser.

eliben avatar eliben commented on June 18, 2024

As I noted in a different issue, pycparser makes no attempt to preserve syntactic information. It produces an AST, not a parse tree [http://eli.thegreenplace.net/2009/02/16/abstract-vs-concrete-syntax-trees/]

The example here is just one small one, there's an infinite amount of them - you can not faithfully reproduce the incoming source from pycparser; it's not, has never been, its design goal.

from pycparser.

akiradeveloper avatar akiradeveloper commented on June 18, 2024

Thank you for your persuasive comments. I will do on my side.

from pycparser.

akiradeveloper avatar akiradeveloper commented on June 18, 2024

I think this visitor braces all unbraced statements.

I referred to BNF grammer here and thought these are complete.
http://www.lysator.liu.se/c/ANSI-C-grammar-y.html#statement

class Brace(c_ast.NodeVisitor):

        def visit_If(self, n):
                if not isinstance(n.iftrue, c_ast.Compound):
                        comp = c_ast.Compound([n.iftrue])
                        n.iftrue = comp
                if not isinstance(n.iffalse, c_ast.Compound):
                        comp = c_ast.Compound([n.iffalse])
                        n.iffalse = comp
                c_ast.NodeVisitor.generic_visit(self, n)

        def visit_Case(self, n):
                if not isinstance(n.stmts, c_ast.Compound):
                        comp = c_ast.Compound(n.stmts)
                        n.stmts = [comp]
                c_ast.NodeVisitor.generic_visit(self, n)

        def visit_Default(self, n):
                if not isinstance(n.stmts, c_ast.Compound):
                        comp = c_ast.Compound(n.stmts)
                        n.stmts = [comp]
                c_ast.NodeVisitor.generic_visit(self, n)

        def visit_Switch(self, n):
                if not isinstance(n.stmt, c_ast.Compound):
                        comp = c_ast.Compound([n.stmt])
                        n.stmt = comp
                c_ast.NodeVisitor.generic_visit(self, n)

        def visit_For(self, n):
                if not isinstance(n.stmt, c_ast.Compound):
                        comp = c_ast.Compound([n.stmt])
                        n.stmt = comp
                c_ast.NodeVisitor.generic_visit(self, n)

        def visit_While(self, n):
                if not isinstance(n.stmt, c_ast.Compound):
                        comp = c_ast.Compound([n.stmt])
                        n.stmt = comp
                c_ast.NodeVisitor.generic_visit(self, n)

        def visit_DoWhile(self, n):
                if not isinstance(n.stmt, c_ast.Compound):
                        comp = c_ast.Compound([n.stmt])
                        n.stmt = comp
                c_ast.NodeVisitor.generic_visit(self, n)

from pycparser.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.