I need to write a parser for Java programming language. I’ve seen some implementations (JavaCC, SableCC) and i think i can handle it.
The thing is I need to rename the variables. Could I do this using the parser?
If yes, how?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
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.
You don’t need a parser if all you want to do is rename variables globally. All you need is an (island) lexer that can pick out identifiers, so that you can replace them. You can get such a lexer from any parser generator that has a Java grammar by simply ignore the parser part. That should save you a lot of trouble.
If you try to use a parser for this, that builds an AST, it will be easy to determine the identifiers (there will IDENTIFIER tree nodes) and probably even replacing them by smashing the tree nodes. Your problem will be regenerating legal source text. Most parser generators don’t provide text-to-tree-to-text capabilities.
If you want to rename variables according to scopes, you need a parser, and full name and type resolution. That’s way harder than a mere parser; the rules about name lookup in Java 7 are pretty complicated. Again, there are parser generators out there with Java grammars; but in this case, they won’t help you unless you want to implement that name and type resolution. You’ll find that a lot more work than you expect.