RSLogix structure text compiler

restron

Member
Join Date
Feb 2014
Location
Somewhere in europe
Posts
3
Hi there,

I am currently working on a C# software tool that needs to compile RSLogix 5000 structure text (ST) language (IEC61131-3 + additional functionality added by Rockwell).

The compiler should convert ST code (plain text, see sample below) out of the L5X project file (xml format) to C# objects.
I am aware that I probably need to create a database with all RSLogix instructions (like the Motion instructions and Controller instructions).

Here's a ST code sample out of L5X project file:
Code:
<STContent>
	<Line Number="0">
		<![CDATA[[COLOR="Blue"]ParamIndex:=0;[/COLOR]]]>
	</Line>
	<Line Number="1">
		<![CDATA[[COLOR="blue"]FOR ParamIndex:=0 TO (ParamArraySize-1) by 1 do[/COLOR]]]>
	</Line>
	<Line Number="2">
		<![CDATA[[COLOR="blue"]	Calced.Parameter[ParamIndex] := 9999; [/COLOR]]]>
	</Line>
	<Line Number="3">
		<![CDATA[[COLOR="blue"]	Min.Parameter[ParamIndex]:=-9999;    	[/COLOR]]]>
	</Line>
	<Line Number="4">
		<![CDATA[[COLOR="blue"]	Max.Parameter[ParamIndex]:=9999;    	[/COLOR]]]>
	</Line>
	<Line Number="5">
		<![CDATA[[COLOR="blue"]END_FOR;[/COLOR]]]>
	</Line>
	<Line Number="6">
		<![CDATA[[COLOR="blue"]	COP(ParamIndex, ParamIndexCopy,1);[/COLOR]]]>
	</Line>
</STContent>

Before I'll start implementing the compiler I've wanted to ask how you would proceed?
I've heared about the Backus-Naur form and the lexical analysis but I'm not sure if this is the right way to go. I already have advanced knowledge in C# programming, so deserializing from xml to objects is quite easy and not the question :) It's more about the method how to "understand" the code easily / correctly.

Do you guys have any advices / recommondations?

Big thanks in advance!
 
What you're looking for are lexer and parser generators. Lex, Flex, Bison, and YACC are popular. I don't think they have C# versions, but you can port the generated lexer and parser codes to C#. You'll need to have a language definition in order for things to work too, you can probably modify a langauge definition for Pascal to get one for ST.
 
I would think you need a method to first identify what the contents of a line of ST actually is. Your post is a bit confusing because your "code" is not ST, it's really XML with text that represents ST...but whatever.

So you need to identify what is a "tag", what is an "instruction" what is an "operand"...etc.

The snippet of XML you posted doesn't tell you that. You'll have to derive it from some type of dictionary to eliminate instructions/operators. Then cross-check the "unknown" against the tag definitions in the XML. Expanding on this, you probably have to identify the datatype of the tag. As well as if it's a UDT or and AOI. For UDTs you'd probably need to construct classes based on the UDT definition and of course generate another dictionary of the add-on instructions to reference against.
 
restron:

This is an interesting little project! Back in my college days I took a "Compiler Design" (Master's level) course, and the semester-long project was to write a compiler. We were told to use Lex and Awk, so that's where I would start with this. You wouldn't be writing a compiler, though - just a "converter". You already have the C# compiler. Been a lot of years now since that course, but I remember setting up lots of "grammars" (?) to do the lexical analysis and tokenization (?) ... not sure on the terminology off-hand. It's a bit tedious ... I *do* remember that much.

But I have to ask ... *why* are you doing this? Is it just for fun? Or are you trying to move PLC programs into an off-the-shelf PC (or server)? I've often contemplated doing this where I am, as we are in dire need of replacing an old, proprietary DCS ... and instead of just surrendering to Rockwell, Emerson, or Siemens, it would be nice to custom-build the new system using off-the-shelf machines and industry-standard languages like C# or C++. We would need to maintain our I/O-gathering hardware (which is newer and non-proprietary), but all the background process logic could surely be customized. No more license fees. No more constant patching. No more forced upgrades. No more "support".

So keep us updated on your progress, please ...
 
At first I would remove the xml tags to get the raw ST source.
Then you need a language specification for ST. If you google for it, then you can find one of the latest drafts of the IEC standard, with includes a language specification at least for ST in a kind of BNF.
But most real world implementation differ a little bit from the standard. And if I remember correctly, the language description for ST in the IEC standard was also not 100% correct/complete, maybe they have fixed this in the release version (which you have to buy then).

As ST is a kind of LL(k) grammar, you also can write the lexer/scanner by hand, maybe even complete only with some regexes and additions to solve some conflicts.
For C#, there is a neat relative simple compiler generator called Coco, which is written completely in C#. I have used this for another language than ST and found it relative easy to use, because you can do all in a C# project:
http://www.ssw.uni-linz.ac.at/Coco/

I also have tried out lex, yacc and all those stuff. In my opinion these tools do their job, but are a little bit outdated.

There was an interesting "Writing a ST compiler" thread in this forum:

http://www.plctalk.net/qanda/showthread.php?t=68958
 
...Then you need a language specification for ST. If you google for it, then you can find one of the latest drafts of the IEC standard, with includes a language specification at least for ST in a kind of BNF.
But most real world implementation differ a little bit from the standard.

You can't really google that, doesn't apply to AB since they've been around longer than IEC-61131-3 they have a well defined instruction set already. OP would have to make his own AB instruction dictionary, well dictionary for all the different parts AB uses. Best just pull out the Logic instruction set manual and start building from scratch.
 
I have a ST compiler that use flex and Bison. It is written in C. All I have left to do is the code generator and implement the built in functions. A lot of the built in or mandatory functions I am going to generate in line. I can compile the functions now if I want to compile the functions from ST code but that isn't very efficient.

Warning. Don't think this is easy. College classes don't teach what you really need to know. There are tricks that must be learned. There are some options when implementing Bison like allowing multiple stacks. I think Bison allows 5. My Bison file has over 600 rules.

At this time my code generator just output a stack based assembly like text code. This makes it easy to see if the right code is being produced.
 
Coco/R seems to be perfect for this, thanks Thomas_v2. It combines lex and yacc and also comes with a Visual Studio 2015 extension (y)
I've also found an atg file for PASCAL which I maybe can use to start with (thanks to Epy for this hint).

So now I'll try to understand how Coco/R works and how I start with all of this :) Do you have some tips or recommondations for this, Thomas_v2?

But I have to ask ... *why* are you doing this?
Long story short: I'm writing a toolkit to e.g. verify controller software. Understanding and processing ladder code (RLL) is pretty easy because there are not many variations (just instruction calls and branches). Also for most tasks belonging the structure text (ST) code it was not necessary to write a compiler (or converter). But since I want to evalute if a tag has been written somewhere (just an example) I need to understand whats really happening in the ST code.
 
So now I'll try to understand how Coco/R works and how I start with all of this :) Do you have some tips or recommondations for this, Thomas_v2?

The IEC ST is not as easy as Pascal language. Some time ago when I was playing around with Coco/R, I've started to write an .atg for the IEC specification of ST, just based on the language description from the IEC norm. I still have one of those files only with the grammar rules. I remember it was not working perfect, e.g. detecting some literals cannot be done only with a grammar rule.

An interesting project for your purposes may be the OpenPlc project:

https://github.com/thiagoralves/OpenPLC_v2

The description says, that there is an IEC to C converter.
 
Some time ago when I was playing around with Coco/R, I've started to write an .atg for the IEC specification of ST, just based on the language description from the IEC norm. I still have one of those files only with the grammar rules.
Would be super cool if you would share the .atg file with us :)
 
Would be super cool if you would share the .atg file with us :)

See attached file. But there are only the rules from the spec in it, not really functional if I remember. Some tokens need to be productions and other things, especially with literals.
 

Similar Topics

Hi, I'm having some problems with an ons structure in studio logix 5000. I latch the process_all_off and sometimes I end up in this state...
Replies
8
Views
2,657
Hi All, I have a couple of questions and would appreciate any help from you guys. I've included my rslogix code and a youtube video of our PLC...
Replies
13
Views
9,427
Hi all, In my Rslogix 5000 project, I use FAL instruction(File Arithmetic and Logic),Dest and expression are INT data type, When use res...
Replies
12
Views
12,755
Hallo Everybody ! I was a little confused here. I try to shift a structure with FFL What I want is to shift a structure of values from Array[2]...
Replies
28
Views
27,681
Hi Everyone, I am not proficient in RSLogix 500 so I have a question regarding the evaluation of N7:0 data as an input. So as I understand in...
Replies
1
Views
88
Back
Top Bottom