kev670
kev670

Reputation: 880

Parsing hundreds of pl/sql files in Java

I need to take hunreds of pl/sql files which contain stored procedures, views and functions and split them all into individual files.

example:

create or replace function practice_function(hello in table.hello%type) return     varchar2 as

hello2 varchar2(6);


begin
if hello is not null then
    hello2 := "hi";
    end if;


return hello2;

end practice_function
/

This is just an example of a stored function. The file could contain 10 or 20 of these with comments and block comments etc. Could somebody give me advice on a high or low level of how I should go about it and how I should go about writing a parser.

At the moment I'm using Java to read in a file line by line and parse each line as they come in. This for instance has caused problems when I need to ignore code which has a block comment.

Any advice would be great

Upvotes: 0

Views: 1057

Answers (1)

Andrey Chaschev
Andrey Chaschev

Reputation: 16526

Writing a PL/SQL or almost any other parser is a challenging task. Basically you need to:

  • Create a lexer. This will break your text into tokens like identifiers, numbers, string literals, operators, etc.
  • Create a parser. This will build a tree of your tokens, AST (abstract syntax tree).
  • Create walker(s). These are tree walkers which will do some operations over your AST. They may even execute your PL/SQL script or i.e. remove all comments.

And even a task to remove comments from your code may become quite challenging. Walking this tree and removing comment nodes should be no problem. Difficult might be the part when you want to print your new tree because the natural walking order might not be the same as the correct textual order.

You may try using an existing ANTLR parser. This will complete the first two steps.

An article on how to start with ANTLR: antlr: is there a simple example?

Upvotes: 1

Related Questions