6.9.1.4. Grammar

A grammar object is initialised by a nonempty sequence of productions, and a start symbol. The start symbol is required and must equal the LHS of at least one production.

An optional argument with keyword 'verbosity' defaults to 0, and may be used to control the amount of debugging information output during the initialisation process.

The sequence of productions is copied, but the productions objects are not.

The constructor builds some tables of auxilliary information about the grammar. After construction the following attributes are defined:

symbols
The set of symbols used in the client grammar, excluding "<EPS>". These are called the client symbols.
nonterms
The set of symbols found on the LHS of some production.
terms
The set of client symbols of the grammar excluding those found on the LHS of a production.
epslhs
The subset of client nonterminals which directly derive either nothing or "<EPS>".
lhsdereps
The subset of client nonterminals which derive nothing. This is a (possibly improper) superset of 'epslhs'.
lhsprods
A dictionary of keyed by client nonterminals, returning the set of productions of the nonterminal.
productions
A list of productions in the same order as the sequence passed to the constructor.
start
The nonterminal which the client denoted as the start symbol of the grammar.
firstmap
A dictionary keyed by grammar symbols, defining the FIRST set of that symbol. The dictionary includes entried for "<EOF>", "<EPS>", and a special entry for -1 (which is used internally as an error state).

The elements of the FIRST sets will be either client terminals or "<EOF>".

The FIRST set of a symbol is defined as follows: for a terminal, the singleton set containing just that terminal, for a nonterminal, the set of terminals which could be the first symbol of a derivation of the nonterminal, and including "<EPS>" if the nonterminal can derive nothing. See the Dragon book [4.4 p188].

followmap
A dictionary keyed by client nonterminals defining the FOLLOW set of that nonterminal.

The FOLLOW set is defined as the set of terminals which can follow the nonterminal in some sentential form of the grammar, and including "<EOF>" if the nonterminal can appear at the right of a sentential form. See the Dragon book [4.4 p189].

Start python section to interscript/parsers/lalr1.py[4 /24 ] Next Prev Last
    28: #line 154 "lalr1_parser.ipk"
    29: class Grammar:
    30:   DummyLA = -1
    31: 
End python section to interscript/parsers/lalr1.py[4]


6.9.1.4.1. Constructor
6.9.1.4.2. Find Nullable NonTerminals
6.9.1.4.3. Find FIRST sets
6.9.1.4.4. Augment grammar
6.9.1.4.5. Find FOLLOW sets
6.9.1.4.6. LALR Closure