denuocc grammar_tool
This tool aims to help in the development of predictive recursive descent parser.
There are four main subcommand for grammar_tool:
-
printshows and numbers every production in the grammar; ; ; ;It shows the
startnonterminal, which happens to beS. It shows that the terminals in this grammar areaandb. It then shows the 4 productions in this grammar. -
firstshows the first set for every nonterminal in the grammarThis means that the production
Scan legally start with an empty string or the token string"ab". Similarly, the nonterminalAmay begin with the token strings"aa","ab", or just"b". Notably,Acannot be an empty string. -
followshows the follow set for every nonterminal in a grammarThe
S :line with nothing after the colon means that is valid for the input string to end after parsing anS. The same goes for the lineA :. The lineS : a ameans that the token string"aa"may sometimes legally follow after parsing anS. -
testwill verify if a grammar ifLL(k)or explain why it is not) ; ; ) )As the
followcommand will show, anSproduction may be followed by the sequencea a(which occurs in production #2A : S a a). Thus, it is impossible to tell with only 1 token lookahead which of the twoSproductions to choose.
Syntax
grammar_tool accepts a very simple grammar format similar to YACC. The input is
split into two parts: the header and the body, separated by a line of just %%.
The header may contain a %token or %start line. The %token line declares
identifiers that are terminals. The %start line specifies which nonterminal is
the root of the grammar.
The body contains the definitions of every nonterminal. A definition may provide
multiple productions using the | character. Any identifier referenced in a
production, if not declared by a %token header, is assumed to be a
nonterminal. A quoted string in a production is also a terminal.
%token TERMINAL
%start S
%%
S : variant1
| variant2
;
variant1 : TERMINAL TERMINAL;
variant2 : "terminal" ;
License
This project is dual licensed under the terms of the MIT license and the Apache License Version 2.0 at your option. See ./LICENSE-MIT and ./LICENSE-APACHE for details.
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.