How to Read a Grammar
This manual uses this one tiny grammar as its main example (for special concepts like Ξ΅, it occasionally shows a small auxiliary grammar separately).
Before we dive into the real material, you'll want to be able to read this grammar, right?
Don't worry β once you know exactly four symbols, it reads smoothly.
Let's take it apart together, slowly.
Here's that grammar.
The first time you see it, with so many symbols, it can feel a little unfamiliar.
Expr : Expr '+' Term | Term ; Term : Term '*' Factor | Factor ; Factor : '(' Expr ')' | id ; id := "[a-zA-Z]+" ;
π¨ Color hint β purple is a nonterminal, teal is a terminal. (We'll explain both properly down below. For now, just "ah, there are two kinds in different colors.")
The Four Symbols
| Symbol | Meaning | In plain words |
|---|---|---|
: |
"is made like this" | make the name on the left, using the method on the right |
| |
"or" | splits things up when there are several ways to make it |
; |
"this rule ends here" | like the period at the end of a sentence |
'+' (quotes) |
the character itself | the actual + symbol that gets printed on screen |
If We Translate the First Line into English
Expr : Expr '+' Term | Term ;β "Expr can be made like this: an Expr, then
+, then a Term, or just a single Term."
Read it again slowly.
: means "is made like this," | means "or," ; means "the end."
Not so hard, right?
Wait, There's Another Expr Inside Expr (Recursion)
Did you notice?
We're explaining Expr, and another Expr shows up inside it.
It points to itself.
This is called recursion.
You might feel a little uneasy β "wait, isn't that an infinite loop?" β but don't worry. Not at all.
This just means "you can keep chaining several things together with +."
For example, something like a + a + a.
(a + a is an Expr, and then we attach + a to it again.)
Natural, right?
Only the word "recursion" sounds scary; the actual content is nothing more than "chaining several things together."
The Other Lines Work the Same Way
Term : Term '*' Factor | Factor ;β "Term is Term*Factor, or a single Factor." (chaining together with*, multiplication)Factor : '(' Expr ')' | id ;β "Factor is(Expr)(an expression wrapped in parentheses), or id (a single name)."
Only the Last Line Is a Little Different
id := "[a-zA-Z]+" ;β "id is a token made of alphabet letters."
Here, "[a-zA-Z]+" is a thing called a regular expression.
But β it's really fine if you don't know regular expressions.
For now, understanding it as just "a name made of English alphabet letters" (e.g., a, x, foo) is 100% enough.
(And := is the mark that says "this is a token defined by a character pattern.")
That Thing We Just Read β It Has a Name
If you've made it this far, you've now learned how to read this grammar.
And β this notation for writing a grammar as rules, the one we just worked through, has a name.
It's called EBNF (Extended BackusβNaur Form).
The name sounds grand, but its form is nothing more than what you just saw.
From now on, when someone says "EBNF," you can think "Ah, that way of writing rules!"
You've picked up a piece of jargon π
Just Two Words: Terminal / Nonterminal
Finally, just two more words.
The names in a grammar split into two kinds.
Tell these two apart and you're done.
- nonterminal = a "rule name" that breaks down further β
Expr,Term,Factor(the "way to make it" is written out on the right.) - terminal = an "actual token" that doesn't break down any further β
+,*,(,),id(the pieces that actually show up in the input.)
Why the names are like this β "terminal" means end, terminus. There's nothing left to break apart, so it "ends" right there. A nonterminal is the opposite β there's still more to unfold. Just knowing what the names mean cuts down on confusion a lot.
To put it as an analogy β a nonterminal is a "dish name" (e.g., kimchi stew), and a terminal is the "actual ingredients" (kimchi, tofu, water).
A dish name can be unfolded into other dishes or ingredients, but an ingredient is the end in itself.
One-line summary: anything starting with a capital letter (Expr, Termβ¦) is a nonterminal; a symbol or
idis a terminal.
(In the dish/ingredient analogy from above β nonterminal = dish name, terminal = ingredient.)
Next Chapter
You've learned how to read a grammar!
The toughest hurdle is actually the one you just cleared π
Now we move on to the first concept of LR parsing, FIRST / FOLLOW.
π FIRST / FOLLOW