We can use it to study parser theory. The generate rule is from <Language Implementation Patterns>
- FirstSet generate.
- FollowSet generate.
- LL(1) Predicate Parsing Table generate.
cd cmd
go run main.go
Output to the terminal default
FirstSet:
FIRST(D) = {g ε f}
FIRST(E) = {g ε}
FIRST(F) = {f ε}
FIRST(S) = {a}
FIRST(B) = {c}
FIRST(C) = {b ε}
FollowSet:
FOLLOW(F) = {h}
FOLLOW(S) = {$}
FOLLOW(B) = {g h f}
FOLLOW(C) = {g h f}
FOLLOW(D) = {h}
FOLLOW(E) = {f h}
PredictTable:
+---+----------------+------------+------------+------------+------------+------------+---+
| # | a | b | c | f | g | h | $ |
+---+----------------+------------+------------+------------+------------+------------+---+
| S | S -> {a B D h} | | | | | | |
+---+----------------+------------+------------+------------+------------+------------+---+
| B | | | B -> {c C} | | | | |
+---+----------------+------------+------------+------------+------------+------------+---+
| C | | C -> {b C} | | C -> {ε} | C -> {ε} | C -> {ε} | |
+---+----------------+------------+------------+------------+------------+------------+---+
| D | | | | D -> {E F} | D -> {E F} | D -> {E F} | |
+---+----------------+------------+------------+------------+------------+------------+---+
| E | | | | E -> {ε} | E -> {g} | E -> {ε} | |
+---+----------------+------------+------------+------------+------------+------------+---+
| F | | | | F -> {f} | | F -> {ε} | |
+---+----------------+------------+------------+------------+------------+------------+---+
cd cmd
go run main.go -grammar your_own_grammar_file
- use
ε
indicateEPSILON
(unicode is'\u03B5'
) - use
$
indicateinput right end marker
. - use UpperCase letter indicate
Nonterminal
- use lowerCase letter indicate
Terminal
BNF
format with|
support alternate.- use
->
distinguishLHS
andRHS
More demo see the cmd/demo.bnf
, or testdata
- FirstFollow
- Compiler Construction
- Parsing Topics Note: some example is confused(some letter is not printed in the web page. so maybe confused when reading it.)
- Online calculate FIRST/FOLLOW/PREDICT.
- BNF and EBNF: What are they and how do they work?
Definition in FirstFollow
FirstSet
: Ifa
is any string of grammar symbols, letFIRST(a)
be the set of terminals that begin the strings derived froma
. Ifa -> e
thene
is also inFIRST(a)
.FollowSet
: DefineFOLLOW(A)
, for nonterminalA
, to be the set of terminals a that can appear immediately to the right ofA
in some sentential form, that is, the set of terminals a such that there exists a derivation of the formS -> aAab
for somea
andb
. Note that there may, at some time during the derivation, have been symbols betweenA
anda
, but if so, they derivede
and disappeared. IfA
can be the rightmost symbol in some sentential form, then$
, representing the input right endmarker, is inFOLLOW(A)
.
Compute Rule in <Language Implementation Patterns>