Reputation: 1014
I am building a app like a compiler with my own script language. The user will enter the code and the output will be another app. So I need tell to user if some line is wrong and why it is.
But I don't know how to start.
I thought this: All lines will start with a keyword, except for those who start with an variable. So different that are wrong. So, I can calculate the next valid entries and check them.
Also, I thought that I can check each line, but it's complex because I can have this
var varName { /* ... */ };
Or
var varName {
/* ... */
};
Or Even
var varName
{
/* ... */
};
So why not remove the break-lines and check? Because I will lose the line number, which in this case is the most important. Maybe I'm going to create a map between the code with and without break-line.
But first I want to hear you, if you already has this experience or you have any idea.
Thanks
Upvotes: 3
Views: 1614
Reputation: 8476
You should check tools like: lex
, bison
and yacc
.
lex
is lexical analyser generator. It generates a code, which could be used for breaking the script to tokens (like numbers, keywords and so on...).
bison
and yacc
are both parser generators. Both can be used for generating code for parsing your language (combining tokens to statements).
Just google tutorials for those tools.
Upvotes: 2
Reputation: 1357
It'll be fairly complicated to write your own language. But totally doable.
To able to recognize if a line is wrong, in the syntactical sense, you'd need to build a parser. The parser checks the context-free grammar for a correct derivation of a structure from its tokens.
First you need to tokenize the file, then reconstruct it into a parse tree (to check syntax).
I took a class in this, CS 241. There's a very nice set of course notes which this is all explained in detail. https://github.com/christhomson/lecture-notes/blob/master/cs241.pdf
Upvotes: 1
Reputation: 11
There are formal languages to describe syntax and semantics of the language and there are tools that will generate parsers out of these descriptions. I suggest reading on flex and bison for starters.
Upvotes: 1