Documentation
¶
Overview ¶
Package slr provides an SLR(1)-parser. Clients have to use the tools of package lr to prepare the necessary parse tables. The SLR parser utilizes these tables to create a right derivation for a given input, provided through a scanner interface.
This parser is intended for small to moderate grammars, e.g. for configuration input or small domain-specific languages. It is *not* intended for full-fledged programming languages (there are superb other tools around for these kinds of usages, usually creating LALR(1)-parsers, which are able to recognize a super-set of SLR-languages).
The main focus for this implementation is adaptability and on-the-fly usage. Clients are able to construct the parse tables from a grammar and use the parser directly, without a code-generation or compile step. If you want, you can create a grammar from user input and use a parser for it in a couple of lines of code.
Package slr can only handle SLR(1) grammars. All SLR-grammars are deterministic (but not vice versa). For parsing ambiguous grammars, see package glr.
Usage ¶
Clients construct a grammar, usually by using a grammar builder:
b := lr.NewGrammarBuilder("Signed Variables Grammar") b.LHS("Var").N("Sign").T("a", scanner.Ident).End() // Var --> Sign Id b.LHS("Sign").T("+", '+').End() // Sign --> + b.LHS("Sign").T("-", '-').End() // Sign --> - b.LHS("Sign").Epsilon() // Sign --> g, err := b.Grammar()
This grammar is subjected to grammar analysis and table generation.
ga := lr.NewGrammarAnalysis(g) lrgen := lr.NewTableGenerator(ga) lrgen.CreateTables() if lrgen.HasConflicts { ... } // cannot use an SLR parser
Finally parse some input:
p := slr.NewParser(g, lrgen.GotoTable(), lrgen.ActionTable()) scanner := slr.NewStdScanner(string.NewReader("+a") accepted, err := p.Parse(lrgen.CFSM().S0, scanner)
Clients may instrument the grammar with semantic operations or let the parser create a parse tree. See the examples below.
Warning ¶
This is a very early implementation. Currently you should use it for study purposes only. The API may change significantly without prior notice.
BSD License ¶
Copyright (c) 2017–20, Norbert Pillmayer ¶
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
3. Neither the name of this software nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.