Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding unbalanced braces and parentheses

Tags:

c

emacs

gcc 4.6.0 GNU Emacs 23.2.1

I have some c code and at some point I must have made a typing mistake. And now I am left with unbalanced curly braches or a parentheses.

I have about 2000 lines of code and I am just wondering is there any technique for finding them?

Emacs has some good features, so I am just wondering is there any way it can scan the code and tell me where they are. Currently I have a compile error related to this.

Many thanks for any suggestions,

like image 451
ant2009 Avatar asked Jun 12 '11 16:06

ant2009


3 Answers

In emacs you can use M-x check-parens . See http://www.gnu.org/software/libtool/manual/emacs/Parentheses.html

like image 182
phoxis Avatar answered Oct 09 '22 00:10

phoxis


I would suggest adding "#ifdef 0 .... #endif" around blocks of code until the source compiles. Keep adjusting the size of the ifdef'd out code until you narrow down a chunk that compiles when ifdef'd out and doesn't otherwise. This chunk will contain the unbalanced brace.

Personally I'd start by ifdefing out almost the whole file and moving my "#ifdef 0" ahead each function until I started seeing compilation errors.

If you have crazy long functions then take the same sort of approach but within the function that's giving you trouble (once you've determined which function it is).

like image 24
Chris Mennie Avatar answered Oct 09 '22 00:10

Chris Mennie


One good point to start is selecting all (M-x select-all) and then indenting the code (M-x indent-region). As emacs does a very good job indenting, you'll notice where the indentation failed.

Also, using show-paren-mode (M-x show-paren-mode) emacs will show you the matching paren of each ending paren (and vice-versa), so you can go through your code.

like image 26
Diego Sevilla Avatar answered Oct 08 '22 22:10

Diego Sevilla