Writing Your First Compiler - Part 6: Extending the Language
dev.to·7h·
Discuss: DEV
Flag this post

We have a parser that handles arithmetic expressions. Numbers, operators, parentheses. It works. But we’re not trying to build a calculator - we’re building a compiler for a real programming language.

Time to add the rest. Functions, variables, conditionals. Everything we need to write actual programs.

Same strategy as before: one piece at a time. We’ll start simple and build up.

Adding Identifiers

The first thing we need is identifiers - names for variables and functions. When you write factorial(n), both factorial and n are identifiers.

Lexing Identifiers

An identifier is a sequence of letters, digits, and underscores. But it can’t start with a digit - x1 is valid, 1x is not. That’s a convention most programming languages follow, and we will too.

So …

Similar Posts

Loading similar posts...