|
Let me first admit I'm not a big fan of Regex as they can be powerful, but also cryptic and slow. I do use them in some cases, e.g. I used a few for some of the web page parsing inside CP Vanity (see one of my articles).
I would not even consider using them while writing a compiler or cross-assembler (which I have done dozens of time, mostly in Fortran and later in C). BTW: the really fun part is writing a simulator to go with it.
I'd suggest you write methods (based on string manipulations) that check for and extract a label, a directive or instruction, a first operand, a second operand, etc.
FWIW: everything that can be done with Regex, can also be done without them. I see them as a language add-on, just like LINQ. You can use it if you want, you don't have to.
|
|
|
|
|
Thanks Luc, nice to have the view of someone who has actually done it.
Thanks
|
|
|
|
|
I know one z80 assembler that is written using only string manipulation (not even regex).. it's a nice assembler to use, but the source is a bit of a mess.
Anyway, as usual when parsing some non-trivial, I would suggest using an actual parser. Depending on whether/which parser generator you use, things like "include" may be a little hacky.
But of course string manipulation works fine too.
|
|
|
|
|
I have used Antlr[^] for a not dissimilar task in the past.
Unrequited desire is character building. OriginalGriff
I'm sitting here giving you a standing ovation - Len Goodman
|
|
|
|
|
Thats not something i'd thought about, thats for this.
Derek
|
|
|
|
|
Derek Henderson wrote: how it's should be done
Write a parser.
Don't use a tool since it appears you haven't done this before. The project is simple enough that it can be used as a learning experience for the basics.
Derek Henderson wrote: Other people must have spent years writing assemblers and languages and must
know the best approach.
Buy the Dragon book. That isn't the name but you can find it by looking for that (add 'compiler')
|
|
|
|
|
Im problem is like this.
Im getting these exceptions when running my application.
"A transport-level error has occurred when receiving results from the server. (provider: TCP Provider, error: 0 - The specified network name is no longer available.)"
after this exception
"An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)"
In mssql server machine SQl server browser is also enabled and firewall is also disabled.
Please help.
|
|
|
|
|
Don't post your question on multiple locations. Already I have answered here[^]
Always give us some time for the answers.
|
|
|
|
|
Hi.
There is my problem.
I've created a wpf 4.0 app. When I debug and run it from vs 2010, there is no problem.
At the same pc, when I try to run the .exe, the application crashes.
The error is the following
EventType : clr20r3 P1 : application.exe P2 : 1.0.0.0 P3 : 4ee325f2 P4 : mscorlib P5 : 4.0.0.0 P6 : 4e181ae3 P7 : 41ed P8 : 460
P9 : system.windows.markup.xamlparse
Why is this happening? Any ideas?
|
|
|
|
|
Over 43000 results for the google search phrase "wpf clr20r3" [^]
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997
|
|
|
|
|
Hi,
I am using OpenXML to open an existing Word document and modify it. After modifications I want to save it to a database. When I am using a stream in the WordprocessingDocument.MainDocumentPart.Document.Save method, only the main document part is saved and not the whole docx file.
Is there a method available which saves the whole file to a stream or converts it to a byte array?
Thx,
Danny
|
|
|
|
|
if you want to store an existing file in a database, you don't care about the file content, it is just bytes to you.
This implies you don't want to know what application did create the file, and hence you don't involve Word in doing the job. Use Word to create, alter, and save the document to disk; then use file operations and database operations to store it all in a BLOB, as if it were an image or anything else that fits in a byte array.
|
|
|
|
|
dennieku wrote: When I am using a stream in the WordprocessingDocument.MainDocumentPart.Document.Save method, only the main document part is saved and not the whole docx file.
That's because the <a href="http://msdn.microsoft.com/en-us/library/cc840441.aspx">MainDocumentPart</a>[<a href="http://msdn.microsoft.com/en-us/library/cc840441.aspx" target="_blank" title="New Window">^</a>] is merely a part of the document; a typical Word-file also contains attachments like pictures, and links to those embedded blobs.
dennieku wrote: Is there a method available which saves the whole file to a stream or converts it to a byte array?
Yes, the <a href="http://msdn.microsoft.com/en-us/library/documentformat.openxml.packaging.openxmlpackage.close.aspx">Close</a>[<a href="http://msdn.microsoft.com/en-us/library/documentformat.openxml.packaging.openxmlpackage.close.aspx" target="_blank" title="New Window">^</a>] -method, according to MSDN;
Saves and closes the OpenXml package plus all underlying part streams.
Once closed, read all the bytes from the file and delete the original.
Bastard Programmer from Hell
|
|
|
|
|
... moving on from my excursion into the mysteries of Tuple, and bearing down on a thorough review of struct ...
... edit #1 ...
The reason I originally titled this post with a qualification stating I thought use of Reference Types in a struct with self-initialization were illegal was because of this statement in the MS doc, "Using Structs: C# Programming Guide:"
"When a struct contains a reference type as a member, the default constructor of the member must be invoked explicitly, otherwise the member remains unassigned and the struct cannot be used. (This results in compiler error CS0171.)"
As mentioned below, this view was modified, as I discovered that internal members of a struct declared 'static can be self-initilizing.
... end edit #1 ...
I am aware that 'struct' in .NET is primarily intended to hold value Types, but I wanted to explore the use of reference Types inside a struct in various ways. I would be very interested in knowing, in terms of allocation, what happens "under the hood" in .NET if a reference Type is included in a struct.
This "quest" led to this particular (final) experiment, where not only did I use a reference Type, but made it static. That a static Type could be self-initializing inside a struct interested me:
public struct TestStruct
{
public static ListInnerList = new List<TestStruct>();
public int testInt;
public string testString;
} And, creating instances updates the static List 'InnerList' as expected:
for (int i = 0; i < 10; i++)
{
TestStruct.InnerList.Add(new TestStruct {testInt = i, testString = "string #" + i.ToString()});
}
Where it becomes more interesting to this well-trodden blade of grass is: if you want a TestStruct public field modified, using the 'InnerList: got to do something like this:
TestStruct selectedStruct = TestStruct.InnerList[5];
selectedStruct.testString += " modified !";
TestStruct.InnerList[5] = selectedStruct; As MS says: "When a struct is assigned to a new variable, all the data is copied, and any modification to the new copy does not change the data for the original copy."
I assume there's no work-around possible for this, where you could directly modify an existing instance of a struct inside a List of instances of said struct ... without creating a new variable, and then re-assigning.
The closest idea I could come up with to simplify modification was something like this to be included in the definition of TestStruct:
public static void modifyString(int index, string stringToAdd)
{
InnerList[index] = new TestStruct {testInt = index, testString = InnerList[index].testString + stringToAdd};
} Which would be called like this:
TestStruct.modifyString(5, " ... modified !"); I don't regard that as "pretty" code.
My guess is that if you reach the point where you need the kind of facilities created (awkwardly) in this example, you've reached the point where you should use a Class.
Aside: interesting you cannot use the "==" operator on two instances of a struct, but, after all, why should something built to be "mean and lean," and live on the Heap, implement IEqual??? or other fancy Interfaces ?
Appreciate any comments, and if this code reflects "bad practices," I certainly am eager to know, since I hear LOPEARS, League Of Programmers Extrajudicial Authority Respecting Structs, has been authorized to shoot to kill.
thanks, Bill?
"For no man lives in the external truth among salts and acids, but in the warm, phantasmagoric chamber of his brain, with the painted windows and the storied wall." Robert Louis Stevenson
modified 11-Dec-11 23:18pm.
|
|
|
|
|
BillWoodruff wrote: "bad practices"
Absolutely.
I see only two situations that really justify using a struct:
1.
a group of data items that need exact relative positioning in memory, e.g. when one needs to map to a native struct (as in P/Invoking user32.dll and kernel.dll ).
2.
a really small amount of data that doesn't justify the overhead of a class instance. The best examples are System.Drawing.Point/Size/Rectangle, and maybe a Complex number, and the like. All of them are below 32 bytes of data (note: 32 bytes is about the cost of an object).
For everything else, I use classes. Which gives better performance, and less hassle: cheaper sorting, no boxing/unboxing, no equality problems, ...
|
|
|
|
|
+5 Thanks, Luc, I asked to "tell it like it is,"[^] and, you did
best, Bill
"For no man lives in the external truth among salts and acids, but in the warm, phantasmagoric chamber of his brain, with the painted windows and the storied wall." Robert Louis Stevenson
|
|
|
|
|
You're welcome.
|
|
|
|
|
BillWoodruff wrote: interesting you cannot use the "==" operator on two instances of a struct
Yes and no...
A class can be automatically tested for equality by comparing the reference (pointer) to it, obviously a struct has no reference so has no always defined value.
The reference comparison for classes is pretty much useless without some kind of inlining in most cases so if equality needs to be tested then overloading the == and != operators, and bool Equals(object) and int GetHashCode() methods need to be overriden, something like:
public class TestClass
{
private int value;
public TestClass(int value)
{
this.value = value;
}
public int Value
{
get { return value; }
}
public static bool operator ==(TestClass first, TestClass second)
{
bool result = false;
if (object.ReferenceEquals(first, second))
result = true;
else
if (!(object.ReferenceEquals(null, first) || object.ReferenceEquals(null, second)))
result = first.value == second.value;
return result;
}
public static bool operator !=(TestClass first, TestClass second)
{
return !(first == second);
}
public override bool Equals(object obj)
{
bool result = false;
TestClass other = obj as TestClass;
if (!object.ReferenceEquals(null, other))
result = value == other.value;
return result;
}
public override int GetHashCode()
{
return value;
}
}
If you implement the same operators and override the same methods on a struct you can use == etc, in fact the implementation is easier as there is no need for null checking on value types! :
public struct TestStruct
{
private int value;
public TestStruct(int value)
{
this.value = value;
}
public int Value
{
get { return value; }
}
public static bool operator ==(TestStruct first, TestStruct second)
{
return first.value == second.value;
}
public static bool operator !=(TestStruct first, TestStruct second)
{
return !(first == second);
}
public override bool Equals(object obj)
{
bool result = false;
if (obj is TestStruct)
result = value == ((TestStruct)obj).value;
return result;
}
public override int GetHashCode()
{
return value;
}
}
|
|
|
|
|
+5 Thanks, Dave, for this thoughtful answer, and the sample code, which I will study diligently.
best, Bill
"For no man lives in the external truth among salts and acids, but in the warm, phantasmagoric chamber of his brain, with the painted windows and the storied wall." Robert Louis Stevenson
|
|
|
|
|
Good answer.
A quick question on this: I have been missing out on object.ReferenceEquals, so I have typically written my == operator along the lines of
public static operator==(MyType a, MyType b){
return ((object)a == null) ? (object)b == null : a.Equals(b);
}
public override bool Equals(object o){
MyType b = o as MyType;
if(b == null) return false;
else if((object)b == this) return true;
return (field1 == b.field1) &&
}
Is it better to use ReferenceEquals than the cast to object?
|
|
|
|
|
I don't know that it makes any difference to be honest... Does boxing* MyType to object have any overhead worth considering?
Using object.ReferenceEquals appears cleaner IMO as we are obviously comparing the references of each instance (or null ), rather than a cast to object when, apart from the null equality check, we never actually want it as an object.
*Edit: If MyType is a reference type then it's not boxing, but you get what I mean
Edit 2: See this SO thread[^]
modified 14-Dec-11 8:02am.
|
|
|
|
|
Nice link. And I think 'cast' is the word you want; if it's a value type then you don't need to bother with the null check anyway since a null can never be passed.
So basically it makes no difference as long as they've improved the compiler since .Net 2, which didn't inline ReferenceEquals calls. And yeah, any overhead is trivial and of academic interest only, but that's fun to think about sometimes.
|
|
|
|
|
hello guys... First of all, if this is wrong platform for this question, plz guide me to the right one. Second, I want to play with communication protocols like TCP/IP Model. I know basics for Network programming or Socket Programming but I dont know where to start for programming basic internet protocols. Any pointers to such topics will be appreciated. thnx
|
|
|
|
|
Not sure if this would be the wrong place but you at least kept your question wide open.
I think that you will need to be a bit more specific about what you want to accomplish. Are you just interrested in builing a chat app(tons of sources which i seriously don't need to point out). If you are aiming to controll a device of sorts you'll need to think about your own message protocol (which at least negotiates a version!). Either way let us know your aim and we'll give you a lot of hints, tips and/or links.
Cheers, AT
Cogito ergo sum
|
|
|
|
|
I guess im interested in Internet Security, if that is an appropriate term to use. I want to make sure that when I send some data over internet, its as much secure as possible. I guess I will need to play with .... Network Layer or something, I dont know. Let me know if I was able to help you.
|
|
|
|
|