A Grammar is a set of rules (productions) used to generate strings in a language.
👉 In compiler design, grammar defines the syntax of programming languages.
A grammar is written as:
G = (V, T, P, S)
Where:
| Symbol | Meaning |
|---|---|
| V | Variables (Non-terminals) |
| T | Terminals |
| P | Production Rules |
| S | Start Symbol |
E → E + T | T
T → id
👉 Grammar defines structure of language 👉 Uses production rules 👉 Start symbol is very important
👉 Example: id, +, *, ( )
👉 Example: E, T
A Derivation is the process of generating a string from the start symbol using production rules.
👉 Always replace the leftmost non-terminal first
👉 Always replace the rightmost non-terminal first
E → E + T | T
T → id
id + idE ⇒ E + T
⇒ T + T
⇒ id + T
⇒ id + id
E ⇒ E + T
⇒ E + id
⇒ T + id
⇒ id + id
👉 LMD → Leftmost first 👉 RMD → Rightmost first 👉 Both produce same string
A Parse Tree is a tree representation of a derivation showing how a string is generated from the grammar.
id + idE → E + T
E → T
T → id
E
/ | \
E + T
| |
T id
|
id
👉 Parse tree shows hierarchical structure 👉 Same parse tree for both LMD & RMD 👉 Used in syntax analysis
Grammar → Derivation → Parse Tree
A grammar is ambiguous if it has more than one parse tree for the same string.
E → E + E | id
String: id + id + id
👉 Can have multiple parse trees ❌
👉 Ambiguity is undesirable in compilers
👉 Frequently asked:
| Aspect | Grammar | Derivation | Parse Tree |
|---|---|---|---|
| Definition | Set of rules | Process of generating string | Tree representation |
| Purpose | Define syntax | Generate strings | Show structure |
| Form | Rules | Steps | Tree |
| Components | V, T, P, S | Production steps | Nodes & edges |
| Types | — | LMD, RMD | — |
| Output | Language | String | Tree |
| Importance | Very High | High | Very High |
Open this section to load past papers