what is formal grammar in compiler design

Mastering Formal Grammar in Compiler Design: Essentials Explained

By Laura Thomas
AD

Compilers, the bedrock of programming, transform our human-readable code into machine-friendly binary. At the heart of this transformation lies a robust understanding of grammar - formal grammar, to be precise. But what exactly is formal grammar in the context of compiler design, and why is it crucial? Let's delve into this fascinating realm.

Formal grammar, a branch of mathematics, provides a set of rules that define a language. In terms of compiler design, it's an abstract model that describes the structure of programming languages. It governs what programmers can write and what a compiler will accept as valid syntax.

Formal Grammar Definitions

Formal grammars are primarily defined using four components: terminals, non-terminals, production rules, and the start symbol.

AD
Compiler Design Cheat Sheet 📚 Phases of Compilation Explained for Beginners
Compiler Design Cheat Sheet 📚 Phases of Compilation Explained for Beginners
Compiler Design Unit 2 Cheat Sheet 💻 | Lexical Analysis, Tokens, DFA/NFA (AKTU)
Compiler Design Unit 2 Cheat Sheet 💻 | Lexical Analysis, Tokens, DFA/NFA (AKTU)
Client Challenge
Client Challenge

Terminals & Non-terminals

Terminals are the atoms or symbols of the language, like '+' or 'if'. Non-terminals, on the other hand, are placeholders that can be replaced by a sequence of terminals or other non-terminals, such as '' or ''.

For instance, in a simple arithmetic expression grammar, '+' and '0' are terminals, while '' is a non-terminal that could be expanded as ' " +" '.

What is a Compiler Design? Types, Construction Tools, Example
What is a Compiler Design? Types, Construction Tools, Example
a white board with words written in different languages on it, including formal and informal
a white board with words written in different languages on it, including formal and informal
a diagram with words and pictures on it
a diagram with words and pictures on it

Production Rules & Start Symbol

Production rules are the heart of a grammar, dictating how non-terminals can be replaced. They're usually written as 'A → α', meaning that the non-terminal A can be replaced by the string α, which can contain both terminals and non-terminals.

The start symbol, often denoted as 'S', is a special non-terminal from which all possible strings can be derived according to the production rules.

AD

Formal Grammar Types

Formal grammars are typically classified using the Chomsky hierarchy, based on the complexity and restrictiveness of their production rules.

Formal / Informal (3)
Formal / Informal (3)
Grammar Formulas
Grammar Formulas
two different types of formal and informal writing are shown in this graphic above the image
two different types of formal and informal writing are shown in this graphic above the image
a table with words that say did you know?
a table with words that say did you know?
two different types of formal and informal writing are shown in red, green and blue
two different types of formal and informal writing are shown in red, green and blue
English
English

Regular Grammars

Regular grammars are the simplest type, with production rules that can only expand single non-terminals. They're used to define regular expressions and can be recognized by finite automata. An example is 'A → AB | a'.

However, they're often inadequate for programming languages, which require more expressive power.

AD

Context-Free Grammars

Context-free grammars, like the one describing our previous example, allow production rules that can expand multiple non-terminals. They're powerful enough to handle most programming languages and can be recognized by pushdown automata. An example is 'E → E + T | T'.

two words that are in different languages on a yellow background, one says informal and the other is formal
two words that are in different languages on a yellow background, one says informal and the other is formal
Formal vs Informal English Know the Difference
Formal vs Informal English Know the Difference
Polishing Your English: Informal vs. Formal Phrases
Polishing Your English: Informal vs. Formal Phrases
Compiler Design Pipeline
Compiler Design Pipeline
Formal and Informal words in English
Formal and Informal words in English
an info sheet with different types of information
an info sheet with different types of information
the words in different languages are displayed on a white sheet with purple and green lettering
the words in different languages are displayed on a white sheet with purple and green lettering
Formal vs Informal English | Speak & Write Like a Pro
Formal vs Informal English | Speak & Write Like a Pro
Informal - Formal 💯👩‍🏫✅️ #english #education #grammar #englishtips
Informal - Formal 💯👩‍🏫✅️ #english #education #grammar #englishtips
a white paper with writing on it that says informal formal and ask / buy purchase check
a white paper with writing on it that says informal formal and ask / buy purchase check
What is Grammar?
What is Grammar?
Formal vs Informal English 💬 | Choose the Right Words for Every Situation
Formal vs Informal English 💬 | Choose the Right Words for Every Situation
an info sheet with different types of language and their meanings, including thesauruss
an info sheet with different types of language and their meanings, including thesauruss

However, they can still struggle with languages that require context-dependent rules, like shell scripts or some SQL dialects.

Context-Sensitive & Unrestricted Grammars

Context-sensitive and unrestricted grammars offer even more expressiveness, allowing production rules that depend on the surrounding context (left, right, or both). They can parse complex languages, but their parsing algorithms are often inefficient or undecidable.

AD

Most compilers use subsets of context-free grammars to balance parsing power with efficiency.

Formal grammar is thus the blueprint of programming languages. It enables compilers to 'understand' code by translating its syntax into a format that the machine can execute. It's a potent tool in the compiler designer's toolbox, unlocking a level of language expressiveness that empowers programmers worldwide.

AD