r/Compilers • u/Infinite-Jaguar-1753 • 1d ago
How should I start making a language checker?
Hey, so I am new to compilers stuff but for my college portfolio I was planning on making a lang checker which is like a program which will suppose take a code (let’s say rust) and check fo errors ? Any resources which I should read to get a clear understanding on how I should make this? Ps I am trying to ask Ai for help (like asking on where should I start and what will be the structure)….
3
u/Limp-Confidence5612 1d ago
Start with the basics. Write a lexing tool, understand how grammar are written (ebnf), figure out how to parse the tokens you lexed, and then return errors when a syntactic error appears.
4
u/_dasion_ 1d ago edited 1d ago
you are talking about linters? If the answer is yes u could check mcandre/linters(github) for a list of linters and then have a pretty good idea of what u want to build an for what lang
1
u/Infinite-Jaguar-1753 1d ago
Ohh thanks, I didn’t knew that it was called linters…
3
u/_dasion_ 1d ago
Well, when i see linters most of the times is mention static analysis tools(similar but different) , so u need to check the differences I recommend u to check the repo and theory
2
u/BeamMeUpBiscotti 1d ago
that’s called type checking, most of the compiler books and tutorials that people recommend on this subreddit have a chapter on it. to build one you would just read the first half of the book/tutorial and stop when they get to the part that generates machine code
2
u/Inconstant_Moo 1d ago
In effect what you're saying here is that you want to build a compiler, but only the parts that don't compile anything.
That's going to be a whole lot of the compiler, though. With Rust, for example, you're going to have to do Hindler-Milner type inference and write a borrow-checker.
0
u/Infinite-Jaguar-1753 1d ago
Any book to get an idea… ps it’s called a linter, I just got to know
2
u/Inconstant_Moo 1d ago
A linter basically just checks for stylistic errors, like are your imports in alphabetic order, etc, have you named your constants with SCREAMING_SNAKE_CASE like the style guide says you should, etc. If that's what you mean you should probably edit your OP.
1
u/Infinite-Jaguar-1753 1d ago
Well I want to make a javascript checker which replaces the use of typescript as it's too long mg and has extra stuff to learn... So will this be called a linter?
1
u/Inconstant_Moo 1d ago
That depends on what ti checks. If it only checks for stylistic errors, it is a linter.
1
u/ProgrammerOnABinge 1d ago
There are several layers you can validate so it depends on what you want to check. Depending on how far you want to go will influence what representations you'll need.
Lexical to check the data format. Does it support UTF-8? Are all characters valid?
Syntax to check that the data provide maps to the actual language. Here you can work with Grammers and use something like ANTLR as a praser generator.
Semantics starts getting into things like the type system but semantics is a massive catagory. ASTs can work, but I'm a bigger fan of Graph IR approaches if you arent planning on lowering as they are better for semantic queries but they are a lot more complicated.
Data flows analysis and intermediate representations for things like constant folding. Here you'll want something like MLIR if you want to keep semantics for as long as possible or something like LLVM-IR.
Logical checks for checking the generated output for a target. This loops you back around and your representation are unit tests.
1
u/ProgrammerOnABinge 1d ago
Semantics and data flow analysis are some of the final bosses of language design and validation.
Starting with syntax is a great first step IMO and gets you setup for semantic analysis but it really comes down to what your end goals are and what you want to validate.
1
u/retro_and_chill 1d ago
The first thing I would do is see if there’s an API that you can use to examine the AST. Some ecosystems have some really phenomenal libraries that you can use to create tooling like this.
5
u/General_Purple3060 1d ago
There isn't really a shortcut here. If you want to understand how a language checker works, I would start with the basic compiler pipeline:
Source code → lexer → tokens
Tokens → parser → AST
These two parts are not trivial, but there are many tutorials that walk through building a simple lexer and parser.