Expression editor / parser / evaluator [closed] - c#

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am trying to work on a C# project and I would like to allow the user to run code on the fly, by typing code into an editor.
So with that in mind, I thought about writing some sort of scripting editor, parser and evaluator, but wanted to check with other people first, in case I am just reinventing the wheel?
So, my idea is/was to...
Write a syntax highlighted editor that I can write code into,
including the use of custom keywords.
Add logic to the editor so that it will be able to format the
content based on the text in the editor.
Have a way of 'actioning' the text that has been entered.
For instance, if I enter the following...
if (Shape.IsACube())
{
// Do some cube stuff
}
else if (Shape.Area(height, length, width) > 40)
{
// Do some large area stuff
}
...then I would like to be able to run that code on the fly as though it was part of the application.
I hope all of that makes sense. Any thoughts?

Which grammar for your syntax you want to use? Your own or C# ?
If you just want to compile some C# code in runtime - you can use something like CodeDOM. Highlighting can be achieved through C# Syntax Highlighter.
If you want your own grammar with your terminals/non-terminals - you should use grammar makers like ANTLR4. This way you will understand how most compilers/interpretators works.
The main idea is that you write Lexer (which responsible for tokenization of your input) and Parser (which is actualy just list of grammar rules, 'productions' in other words). It will give you full AST from which you can evaluate/highlight your text. Many language grammars already written before you in here including C# syntax (even ANTLR4 itself is presented). So you can just pick some and modify it for your needs.

Related

Generate C code using C# [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I've got this task that requires me to generate some basic C code using a software written in C#.
The generated code should be based on some input files I provide to my software, we'll call it btOS for easy of communication.
So when starting btOS I give it as input file1, config.xml. When I hit run it should output a file.c that contains some basic structures and/or methods based on what the input files contain.
Is there any elegant way to do this ? Maybe some already generated templates or methods or stuff like that ? The only way I could think of handling this was creating specific strings in C# and outputting them to a C file.
L.E.: It seems that somehow my question was not clear enough. I assume the fault of including C++ in the title, I have remove it but I don't see how that is relevant because the question was very simple.
Anyway, to make it more clear. All i need to do is read some config files (their content is irrelevant, all they contain are some variables that will be used to generate some function templates, which will mostly impact the name of the function) - and write an output file with the extension .C (as in Main.c) that will contain those templates I generated.
So, again, the question: Are there any "elegant" and maybe somehow "professional" ways to do this other than using custom generated strings within the code that I will write to the file ? Right now the only way I see fit to do this without too much hassle is using some template text files with a naming convention defined by me(e.g. function_variableName{...}) where I just change the [variableName] text with whatever I need to to be there and "Abracadabra" I have a function that I will write to the file.
Now as Soonts suggested please try and be helpful, read multiple times if you don't clearly understand or maybe even don't bother - let somebody who is interested in this topic, tries to help or gain some new knowledge before flagging it.
Double Cheers.

C# checking if a word is in an English dictionary? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am trying to go through a list of words and for each one determine if it is a valid English word (for Scrabble). I'm not sure how to approach this, do I have to go find a text file of all English words and then use file reading and parsing methods to manually build a data structure like a trie or hashmap - or can I find those premade somewhere? What is the simplest way to go about this?
You can use NetSpell library for checking this. It can be installed through Nuget Console easily with the following command
PM> Install-Package NetSpell
Then, loop through the words and check them using the library
NetSpell.SpellChecker.Dictionary.WordDictionary oDict = new NetSpell.SpellChecker.Dictionary.WordDictionary();
oDict.DictionaryFile = "en-US.dic";
oDict.Initialize();
string wordToCheck = "door";
NetSpell.SpellChecker.Spelling oSpell = new NetSpell.SpellChecker.Spelling();
oSpell.Dictionary = oDict;
if(!oSpell.TestWord(wordToCheck))
{
//Word does not exist in dictionary
...
}
Since you're looking specifically for valid Scrabble words, there are a few APIs that validate words for Scrabble. If you use anything that's not for that intended purpose then it's likely going to leave out some words that are valid.
Here's one, here's another, and here's a separate question that lists available APIs.
So that I can add some value beyond just pasting links, I'd recommend wrapping this in your own interface so that you can swap these out in case one or another is unavailable (since they're all free services.)
public interface IScrabbleWordValidator
{
bool IsValidScrabbleWord(string word);
}
Make sure your code only depends on that interface, and then write implementations of it that call whatever APIs you use.

Is it possible to make a program that reads its own source code? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
The community reviewed whether to reopen this question 3 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
What I mean is, could one possibly make a program that does the equivalent of
public class PrintOwnSourceCode
{
public static void Main ( )
{
System.Console.WriteLine([something]);
// prints "public class PrintOwnSourceCode { public static void Main ( ) { ... } }"
}
}
???
And would that be an example of reflection?
Somewhat.
Decompilers can do something similar to this:
I just decompiled a decompiler so I could use it to decompile itself
.NET Decompilers, like [.NET Reflector] (http://www.red-gate.com/products/dotnet-development/reflector/) and dotPeek are capable of reflecting upon a .NET assembly and generating files that resemble the source code. It will not look exactly like the source code because compiling and decompiling is kind of like translating English to French and then back to English--the results are not always guaranteed to be 1:1 as Google Translate can demonstrate. Information, like whitespace, that are for easy reading but not required by the compiler will be lost in the decompilation process. So, your application could decompile itself or invoke an external decompiler to print itself.
Aside
In compiled languages, the compiled code does not have direct access to the source code. (Companies don't typically ship the source code with the compiled code to customers. They only ship the compiled executable.) When it comes to parsed languages, like JavaScript, it's a whole different story. Because the source must be available to the runtime so that it can be parsed and run, the code can always find it's own source file, open it, and print it out.
This was answered here.
The short answer is that you cannot print it via reflection.
If you want to print out the file, then you will need to load in the source file itself (and have the file available).

How to structure program that calculates math expression [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I plan to write a program or rather function which will be able to analyze a string parameter which in turn will be math expression. Only the 4 basic operations are allowed(addition, subtraction, multiplication and division) and the numbers are all whole numbers from -100 to 100. The result is allowed to be float. I know the registries work in the same way I.e calculate result of two numbers and store it, than calculate result of stored value and the next operant and store. And so forth until there are no operands left. The number of operands will usually be 2 but I will have a need of 3 or even more so yes, more operands is a requirement.
I was wondering how would you structure this in C#? What tools helper functions you would use in this scenario?
Note: I am working on Unity 5.1.4 project and I want to use a math parser in it. Unity is .NET 2.0
Note: This seems most promising: http://mono.1490590.n4.nabble.com/Javascript-eval-function-in-c-td1490783.html
It uses a variant of eval() function.
In .NET there are no some high level helper functions to help you with this. You would have to parse and tokenize the string in your code. There are however third party libraries that do what you need, for instance Expression Compiler, Simple Math Parser, Mathos Parser, and many other. Search for math expression parser.
If you want to make one from scratch you could look the code of existing ones.
Hans Passant mentions a simple solution, maybe just what you need. You get the result of the expression, so if you need just that, and not the actual expression tokens, then .NET got you covered.
This tool finished the job with no adding external references, dlls or what not: http://mono.1490590.n4.nabble.com/Javascript-eval-function-in-c-td1490783.html

Dynamic Programming and alike in c# [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I need to learn a certain programming subject and I don't know where to start, would like your help.
This is what I need do do, I have a user form (UI) and the user enter "Rules" in the form of:
if operator(obj1) then assign(obj1,string)...
I take this rules, and translate them into actual code, and I want to put that code somewhere in a function/my code.
for example:
main {
UI...
/* when we reach here, means the user done writing rules */
/*Function that translate the user rules to actual code */
translate();
for {
/* This is where I want to put the code after translation */
}
}
How do I put the code inside the loop (or anywhere else for that matters) after the program started running?
I ofcourse don't look for an actual answer, more to give you an idea what I need so you can refer me to a certain subject to study about.
I presume, you are in process of creating a custom rule engine, which has the capability of validating your rules on fly. Within my ability, you need to start reading c# scripting, code generation, dynamic loading or reflection etc are some to start with.
To give a kick start, following are some of the step which I can think off;
Grab the rule definition (xml or csv)
Write a small helper which will read rule entries from the definition and convert it into
c# source code. This is similar to c# scripting.
On successful completion of (2), create a dll out of the source code
Now reflect/dynamically load the dll from (3) to where ever you wanted to validate the rule.

Categories