The smallest translator you've ever seen
This morning I wrote a brainf**k c translator. It took me about an hour in total.
The entire content is less than 50 lines of c code. You can see it here.
What is brainf**k?
This is a profound encoding language. Invented by Swiss students in 1993, it is almost considered the minimum requirement for Turing completeness.
It is also one of the most famous Essolan languages in existence.
The syntax is extremely simple: there are only 8 characters, and the rest will be ignored.
> [-]<.> [-]> [-] [-]>> [-]<p>Guess what it does. Guess it.</p> <p> This is a Hello world! programme.</p> <p> Essentially, in brainf**k you get an array of 30000 bytes and a cursor. You can use > and Print the value of the current cell</p> <pre class="brush:php;toolbar:false"> > ; increments cursor position <p>It's almost everything about brainf**k.<br> I've written more functional programs before in <em>assembly</em> .</p><h2 id="Why-Just-why"> Why? Just why?</h2><p> I wrote this compiler completely because I was bored and I found a lot of interpreters so I think the world needs a brainf**k compiler.</p><blockquote><p> Nevertheless, there is no denying that if you want a very good brainf**k compiler, check out this one.</p></blockquote><h3 id="Why-c"> Why c?</h3><p> Many reasons:</p>
- I want more practice
- Very fast
- It's mainly portable
- Very common
- Anyway, I don't need much code.
The last reason: integer overflow. Often, this is a bad thing that people hate. This may be the reason why unit testing is invented (uh). But brainf**k is different. The upper limit of the number in the memory tape is 255, and if it exceeds this, it should be reset to 0. Also, if the value is below 0, it should be reset to 255. c will do this on its own; I don't need to write any code for it.
how?
A higher level overview:
- Read code from file or standard input to memory
- Convert each character to c code
- Printed c code.
It reads brainf**k code from the file into code[].
Then, it sets up a basic C program:
#include <stdio.h> int main() {char t[30000]={0};int p=0; </stdio.h>
You may have noticed that it lacks the closing bracket. That's because more code is added to the char[].
If you want to know, char t[30000] is the memory given to you. I use t as the abbreviation of the tape, but shorten it because these programs are not suitable for human readability.
Next, it loops through the code array, which is an array of single characters. For each character, it converts it to c code:
character | Becomes |
---|---|
> | p |
p-- | |
- | t[p]-- |
t[p] | |
. | putchar(t[p]) |
, | t[p]=getchar() |
[ | while(t[p] != 0) |
] | } |
You can check out the above to see what these symbols do.
These values will be added to the program output, which will contain valid c code.
Finally, add this code to the final output:
return 0;}
The output program ends here. The translator then prints this code to standard output so that the user can operate as he wishes.
The final thought
I have some ideas for improvement while writing this post. I don't think I can do too many things to make it faster, but I can do something to make it safer and better.
You can view the final code here.
You can also make any kind of contribution if you wish.
Thank you for reading!
The above is the detailed content of The smallest translator you've ever seen. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Cryptocurrency data platforms suitable for beginners include CoinMarketCap and non-small trumpet. 1. CoinMarketCap provides global real-time price, market value, and trading volume rankings for novice and basic analysis needs. 2. The non-small quotation provides a Chinese-friendly interface, suitable for Chinese users to quickly screen low-risk potential projects.

Institutional investors should choose compliant platforms such as Coinbase Pro and Genesis Trading, focusing on cold storage ratios and audit transparency; retail investors should choose large platforms such as Binance and Huobi, focusing on user experience and security; users in compliance-sensitive areas can conduct fiat currency trading through Circle Trade and Huobi Global, and mainland Chinese users need to go through compliant over-the-counter channels.

The following factors should be considered when choosing a bulk trading platform: 1. Liquidity: Priority is given to platforms with an average daily trading volume of more than US$5 billion. 2. Compliance: Check whether the platform holds licenses such as FinCEN in the United States, MiCA in the European Union. 3. Security: Cold wallet storage ratio and insurance mechanism are key indicators. 4. Service capability: Whether to provide exclusive account managers and customized transaction tools.

Priority is given to compliant platforms such as OKX and Coinbase, enabling multi-factor verification, and asset self-custody can reduce dependencies: 1. Select an exchange with a regulated license; 2. Turn on the whitelist of 2FA and withdrawals; 3. Use a hardware wallet or a platform that supports self-custody.

Provides a variety of complex trading tools and market analysis. It covers more than 100 countries, has an average daily derivative trading volume of over US$30 billion, supports more than 300 trading pairs and 200 times leverage, has strong technical strength, a huge global user base, provides professional trading platforms, secure storage solutions and rich trading pairs.

The top ten secure digital currency exchanges in 2025 are: 1. Binance, 2. OKX, 3. gate.io, 4. Coinbase, 5. Kraken, 6. Huobi, 7. Bitfinex, 8. KuCoin, 9. Bybit, 10. Bitstamp. These platforms adopt multi-level security measures, including separation of hot and cold wallets, multi-signature technology, and a 24/7 monitoring system to ensure the safety of user funds.

Common stablecoins are: 1. Tether, issued by Tether, pegged to the US dollar, widely used but transparency has been questioned; 2. US dollar, issued by Circle and Coinbase, with high transparency and favored by institutions; 3. DAI, issued by MakerDAO, decentralized, and popular in the DeFi field; 4. Binance Dollar (BUSD), cooperated by Binance and Paxos, and performed excellent in transactions and payments; 5. TrustTo

Cryptocurrency market data can be obtained from the following five channels: 1. Exchange market pages (such as Binance, OKX, gate.io) provide real-time price and in-depth charts; 2. Professional market data platforms (such as CoinMarketCap, CoinGecko, TradingView, AI coins) provide global cryptocurrency data and analysis tools; 3. On-chain data and DeFi market tools (such as DeFiLlama, Dune analysis) focus on DeFi projects and on-chain data; 4. News and comprehensive market websites (such as Coinjie.com, Currency.com) provide real-time currency price and market analysis; 5. Social media and communities (such as Twitter, Telegram) can obtain market sentiment and sudden new
