Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Building a syntax checker

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

like image 353
lcssanches Avatar asked May 01 '26 23:05

lcssanches


2 Answers

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.

like image 135
SKi Avatar answered May 04 '26 03:05

SKi


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.

like image 39
Grzegorz Krugły Avatar answered May 04 '26 03:05

Grzegorz Krugły



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!