Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2010-10

TPLY

21 messages · 6 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Brian Winfrey

I recently saw a semi working example of a Java to Delphi converter that was done with javacc. Unfortunately it would only convert simple java (v 1.4 maybe).

Can anyone recommend a good place to start reading or offer any advise to accomplish this with TPLY.

I am looking into porting some Java libraries to FPC 2.5.x, and am unfamiliar with the TPLY tools.

Thanks,
Brian

Brian Winfrey
to leledumbo
I prefer doing this manually, syntax conversion is easy, but library design is totally different.

A conversion of the Java code will provide a good starting point I think. Additionally, learning TPLY or lex/yacc could prove very useful I think.

Do you have grammer files for syntax conversion? I have not found an up to date grammer file for FPC's current objpas dialect.

Thierry Coq
to Brian Winfrey: I recently saw a semi working example of a Java to Delphi converter that was done with…

Hello Brian,
there is a port to Lazarus here:

http://www.tcoq.org/

with some examples. I can help you if needed. The documentation is quite complete.

Translating the whole Java language would be very difficult, but translating only the structure could be done. You would need to write a grammar capable of bypassing all internal code.

I hope this helps.
Best regards,
Thierry

Show quoted text 14 lines
Can anyone recommend a good place to start reading or offer any advise to accomplish this with TPLY. I am looking into porting some Java libraries to FPC 2.5.x, and am unfamiliar with the TPLY tools. Thanks, Brian _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Brian Winfrey
to Thierry Coq
Show quoted text 7 lines
> > I am looking into porting some Java libraries to FPC 2.5.x, and am > unfamiliar with the TPLY tools. > Translating the whole Java language would be very difficult, but translating only the structure could be done. You would need to write a grammar capable of bypassing all internal code.

The libraries are some of the Google Data libraries. The libs are available in other languages, so I don't necessarily have to start with java, although I have some familiarity with it.

Brian Winfrey
to Brian Winfrey
Brian Winfrey wrote 10 lines
> there is a port to Lazarus here: > > http://www.tcoq.org/ > > with some examples. I can help you if needed. The documentation is quite > complete. Thanks it looks interesting, I like the component approach, I will take a close look.

I was not able to set paths and compile. There were file naming problems with case and duplication. I have a diff file if you like.

leledumbo
to Brian Winfrey

Learn compilation technique, a recursive descent parser should be easy to understand and code instead of learning automatic lexer and parser generator. Plus, structurally, Java is a very simple language, so AST transformation should be easy. With FPC 2.5.1, almost all Java constructs can be directly translated to FPC dialect, minus anonymous inner class only AFAIK.

View this message in context: http://free-pascal-general.1045716.n5.nabble.com/TPLY-tp3235828p3239905.html Sent from the Free Pascal - General mailing list .
Thierry Coq
to Brian Winfrey: I was not able to set paths and compile. There were file naming problems with case and…

Hello Brian,

Yes, please send the diff file, so I can platform (Windows, Linux, FPC) are you using?

Another approach would be to look at (and reverse engineer) the byte code. If it's not obfuscated, it might even be easier to parse and port than the native Java.

Brian Winfrey wrote 21 lines
On Wed, Oct 27, 2010 at 9:21 AM, Brian Winfrey wrote: >> there is a port to Lazarus here: >> >> http://www.tcoq.org/ >> >> with some examples. I can help you if needed. The documentation is quite >> complete. >> > Thanks it looks interesting, I like the component approach, I will > take a close look. > > I was not able to set paths and compile. There were file naming problems with case and duplication. I have a diff file if you like. _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Brian Winfrey
to Thierry Coq
Yes, please send the diff file, so I can platform (Windows, Linux, FPC) are you using?

Linux, Kubuntu 10.04 .
Linux is case sensitive, so the case in the uses must match the case of the file or unit declaration I think. ie.
* filename = test.pas
* unit Test;
is accessed as test or Test in the uses clause.

I'm not sure , but I think if the file name were Test.pas then it must be Test in the uses.

The changes I made in this diff were simply to get it to compile.

Subsequently I renamed all files and folders to lowercase, converted spaces to underscores and added a prefix to eliminate duplicate file names where needed.

Show quoted text 5 lines
Another approach would be to look at (and reverse engineer) the byte code. If it's not obfuscated, it might even be easier to parse and port than the native Java.

How would you suggest I proceed in that.

Brian Winfrey
to leledumbo: Learn compilation technique, a recursive descent parser should be easy to understand and…

Thank you, What techiniques have you used in the past that you could share to get me started?

leledumbo wrote 14 lines
Learn compilation technique, a recursive descent parser should be easy to understand and code instead of learning automatic lexer and parser generator. Plus, structurally, Java is a very simple language, so AST transformation should be easy. With FPC 2.5.1, almost all Java constructs can be directly translated to FPC dialect, minus anonymous inner class only AFAIK. -- View this message in context: http://free-pascal-general.1045716.n5.nabble.com/TPLY-tp3235828p3239905.html Sent from the Free Pascal - General mailing list . _______________________________________________ fpc-pascal maillist  -   http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Paul Nicholls
to Brian Winfrey

"Brian Winfrey" wrote in
message
Thank you, What techiniques have you used in the past that you could share to get me started?

leledumbo wrote 8 lines
Learn compilation technique, a recursive descent parser should be easy to understand and code instead of learning automatic lexer and parser generator. Plus, structurally, Java is a very simple language, so AST transformation should be easy. With FPC 2.5.1, almost all Java constructs can be directly translated to FPC dialect, minus anonymous inner class only AFAIK.

I would start with this site to learn how to create recursive descent parsers using Pascal :)

http://compilers.iecc.com/crenshaw/

Using this information as a starting point, I have created pascal-like script languages (compiled to byte code), and pascal-like HL language compiled to virtual machine ML instructions :)

cheers,
Paul

Thierry Coq
to Brian Winfrey: Linux, Kubuntu 10.04 . Linux is case sensitive, so the case in the uses must match the…

Brian,
thanks for the diff, I'll take it into account and publish an update.

On 28/10/2010 18:50, Brian Winfrey wrote:
...

Show quoted text 7 lines
The changes I made in this diff were simply to get it to compile. Subsequently I renamed all files and folders to lowercase, converted spaces to underscores and added a prefix to eliminate duplicate file names where needed.

...

Show quoted text 8 lines
> Another approach would be to look at (and reverse engineer) the byte code. > If it's not obfuscated, it might even be easier to parse and port than the > native Java. > > How would you suggest I proceed in that.

Using the class file format (for example here:
http://en.wikipedia.org/wiki/Class_%28file_format%29), it would be easy to parse through the tables, identify the class name, its super class name, the interfaces it's implementing, and even the public and private methods. The java compiler has done all the work into translating the java code into nice tables. ;-)
I remember I did the exercise once. I've destroyed the code, but I could do it again. I remember one of the more difficult parts was understanding how java coded strings ;-) This was before anonymous classes and methods, so this could be another difficulty. I don't know how these are converted to byte code.
Once you have the structure, you can generate the pascal code. I would recommend against generating one class per unit as Java does: since pascal dependency rules are strict (no cycles), this would most certainly result in uncompilable code. What I do when I translate Java is to pre-declare all classes and interfaces at the top of the unit(s) and then generate the full class interface further down. Translating the statements inside the bytecode I haven't tried, since my efforts were mostly for analyzing java code and measuring it. I hope this helps,
Thierry

David Riley
to Paul Nicholls: "Brian Winfrey" wrote in message Thank you, What techiniques have you used in the past…
Paul Nicholls wrote 33 lines
"Brian Winfrey" wrote in message Thank you, What techiniques have you used in the past that you could share to get me started? On Wed, Oct 27, 2010 at 8:04 PM, leledumbo wrote: > > Learn compilation technique, a recursive descent parser should be easy to > understand and code instead of learning automatic lexer and parser > generator. Plus, structurally, Java is a very simple language, so AST > transformation should be easy. With FPC 2.5.1, almost all Java constructs > can be directly translated to FPC dialect, minus anonymous inner class > only > AFAIK. I would start with this site to learn how to create recursive descent parsers using Pascal :) http://compilers.iecc.com/crenshaw/ Using this information as a starting point, I have created pascal-like script languages (compiled to byte code), and pascal-like HL language compiled to virtual machine ML instructions :) cheers, Paul _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal

A book I'd recommend reading in addition to the Dr. Jack Crenshaw articles mentioned above would be "Compiler Construction" by Niklaus Wirth. It's goes in to a lot more detail than "Let's Build A Compiler" but still start with the basics. I found it here: www-old.oberon.ethz.ch/WirthPubl/CBEAll.pdf . Mr. (Dr. yet?) Florian Klaempfl should write a book, he seems to have done pretty good with the whole Pascal compiler thing. With the entire FPC crew you'd each only have to write a few chapters...

Brian Winfrey
to David Riley

Well, seems like you all have given some material to get me started. As nobody suggested sticking with TPLY or plex/bison I will back-burner that and proceed with crenshaw/oberon.

Thanks to everyone.

Brian Winfrey
to Thierry Coq: thanks for the diff, I'll take it into account and publish an update. ... ... Using the…

Thanks, I'll look into this as well.

Thierry Coq wrote 53 lines
Brian, thanks for the diff, I'll take it into account and publish an update. On 28/10/2010 18:50, Brian Winfrey wrote: ... > > The changes I made in this diff were simply to get it to compile. > > Subsequently I renamed all files and folders to lowercase, converted > spaces to underscores and added a prefix to eliminate duplicate file > names where needed. > > ... >> >> Another approach would be to look at (and reverse engineer) the byte >> code. >> If it's not obfuscated, it might even be easier to parse and port than >> the >> native Java. >> >> > > How would you suggest I proceed in that. > Using the class file format (for example here: http://en.wikipedia.org/wiki/Class_%28file_format%29), it would be easy to parse through the tables, identify the class name, its super class name, the interfaces it's implementing, and even the public and private methods. The java compiler has done all the work into translating the java code into nice tables. ;-) I remember I did the exercise once. I've destroyed the code, but I could do it again. I remember one of the more difficult parts was understanding how java coded strings ;-) This was before anonymous classes and methods, so this could be another difficulty. I don't know how these are converted to byte code. Once you have the structure, you can generate the pascal code. I would recommend against generating one class per unit as Java does: since pascal dependency rules are strict (no cycles), this would most certainly result in uncompilable code. What I do when I translate Java is to pre-declare all classes and interfaces at the top of the unit(s) and then generate the full class interface further down. Translating the statements inside the bytecode I haven't tried, since my efforts were mostly for analyzing java code and measuring it. I hope this helps, Thierry _______________________________________________ fpc-pascal maillist  -   http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Paul Nicholls
to leledumbo: Definitely, but it doesn't build AST, which is required if you need to write a parser for…

"leledumbo" wrote
in message

Show quoted text 8 lines
> I would start with this site to learn how to create recursive descent > parsers using Pascal :) Definitely, but it doesn't build AST, which is required if you need to write a parser for a language that can't be parsed in single pass like Java.

True, but you just have to use the code as a starting point and generate an AST :)

"Shouldn't be too hard..." (tm) LOL

cheers,
Paul