How can I parse REXX code in Java?
Asked Answered
P

3

5

I'd like to parse REXX source so that I can analyse the structure of the program from Java.

I need to do things like normalise equivalent logic structures in the source that are syntactically different, find duplicate variable declarations, etc. and I already have a Java background.

Any easier ways to do this than writing a load of code?

Patmos answered 28/3, 2009 at 22:37 Comment(0)
E
5

If you have BNF Rexx grammar, then javacc can help you build an AST (Abstract Syntax Tree) representation of that Rexx code.

More accurately, javacc will build the Java classes which will :

  • parse Rexx code and
  • actually builds the AST.

There would still be "load of code", but you would not to be the one doing the writing of the classes for that Rexx code parser. Only its generation.

Est answered 28/3, 2009 at 22:45 Comment(0)
K
8

REXX is not an easy language to parse with common tools, especially those that expect a BNF grammar. Unlike most languages designed by people exposed to C, REXX doesn't have any reserved words, making the task somewhat complicated. Every term that looks like a reserved word is actually only resolved in its specific context (e.g., "PULL" is only reserved as the first word of a PULL instruction or the second word of a PARSE PULL instruction - you can also have a variable called PULL ("PULL = 1 + 2")). Plus there are some very surprising effects of comments. But the ANSI REXX standard has the full syntax and all the rules.

Kamseen answered 1/1, 2010 at 4:42 Comment(0)
E
5

If you have BNF Rexx grammar, then javacc can help you build an AST (Abstract Syntax Tree) representation of that Rexx code.

More accurately, javacc will build the Java classes which will :

  • parse Rexx code and
  • actually builds the AST.

There would still be "load of code", but you would not to be the one doing the writing of the classes for that Rexx code parser. Only its generation.

Est answered 28/3, 2009 at 22:45 Comment(0)
S
2

Have a look at ANTLR, it really does a nice work of building an AST, transforming it etc... It has a nice editor (ANTLRWorks), is built on Java, and can debug your parser / tree walkers while they run in your application. Really worth investigating for any kind of parsing job.

Speculator answered 28/3, 2009 at 23:51 Comment(1)
And since 2017, ANTLR has a sample grammar for Rexx in the grammars-v4/rexx section.Kamseen

© 2022 - 2024 — McMap. All rights reserved.