0

I need a lexical parser that will parse C source codes and I have to do it using Java language. I researched for it and saw ANTLR and JAVACC. Which of these parsers are better to use and why? Or do you have any other parser to recommend?

Answers will be much appreciated. Thanks.

3
  • 1
    Nobody "just needs" a C source code lexer. And without the understanding of why you need it, it is hard to make any remarks about h ow ANTLR or JavaCC (or something else including rolling your own ad hoc lexer) might be a better way to go.
    – Ira Baxter
    Commented Mar 13, 2013 at 2:01
  • I would need to count the functions, variables, and etc. of my C source code and display the result in an excel file. I would need to code it using Java. Have I searched the wrong parsers?
    – MAC
    Commented Mar 13, 2013 at 2:57
  • "I need a C lexer and I would need to code it using Java". This doesn't sound like real-world constraint.
    – Ira Baxter
    Commented Mar 13, 2013 at 3:03

2 Answers 2

1

A lexer only breaks the input stream into tokens. They don't count anything so are incomplete for your purpose of computing simple metrics.

If you want to differentiate "functions" from "variables" you'll need some kind of parser to check sequences of tokens to determine what they represent (e.g., "a variable declaration, a use, or a function declaration"). You can build an ad hoc parser that may satisfy your counting needs at the price of making occasional mistakes, or you can get a real parser and do it right. (Parsing a C variable declaration is a lot harder than it looks at first glance; its pretty arcane syntax).

If this is homework, or a real problem and you don't care if the answer is wrong, any lexer generator and ad hoc parsing code will likely do well enough.

If you want to do this accurately, you'll need a preprocessor and a parser, and you'd better look for those (implicitly including a lexer).

1

A compiler typically follows these steps:


  1. Lexical Analysis (Scanner)

  2. Syntax Analysis (Parser)

  3. Semantic Analysis (Parser)

  4. Intermediate Code Generation

  5. Code Optimization

  6. Code Generation


What you are trying to do is to scan the given source code and count the variables and functions in the code. That involves the first 2 steps. A Lexical analyzer breaks the input from a character stream into tokens (stage 1) then the tokens are sent to the parser (stage 2) to identify and validate the tokens. There is no constraints on the programming languages you can write a compiler for because you can make a full compiler for any language by any language. You are the one that designs the compiler and you have full control on what it does (you can make a compiler for Hebrew -if you have the patience-)

So, for your case, you can either write the lexer and the parser manually from scratch (which is the better-for-you approach) or you could use a lexical analyzer generator like JLex which can generate a lexical analyzer out of very simple instructions and regex patterns for your tokens.

A quick start guide for JLex can be found here

Not the answer you're looking for? Browse other questions tagged or ask your own question.