项目作者: geekysuavo

项目描述 :
A bison grammar for parsing bison grammars
高级语言: Yacc
项目地址: git://github.com/geekysuavo/ll1.git
创建时间: 2016-04-30T22:26:44Z
项目社区:https://github.com/geekysuavo/ll1

开源协议:MIT License

下载


ll1

The ll1 tool performs a small set of analyses on a
context-free grammar
in order to determine whether it is
LL(1), and thus suitable
for implementation via a
predictive parser
algorithm. Running a grammar through ll1 can help you catch
left recursion and other
conflicts
that could ruin your day if they pop up later during parser implementation.

Input format

ll1 parses a CFG in an ‘un-adorned’
GNU Bison format. The use of
%empty is absolutely mandatory for writing epsilon productions. Character
literals are also accepted as tokens, so the following grammar:

  1. %%
  2. /* left-recursive! */
  3. expr : expr '+' term
  4. | expr '-' term
  5. | term ;
  6. /* left-recursive! */
  7. term : term '*' factor
  8. | term '/' factor
  9. | factor ;
  10. factor : ID | NUM ;
  11. %%

would be perfectly acceptable input, even though it will cause ll1
to pitch a fit about predict set overlaps between productions of the
same nonterminal. ;)

However, ll1 will be tickled pink to accept this input:

  1. %%
  2. /* tail-recursive... :) */
  3. expr : term expr_next ;
  4. term : factor term_next ;
  5. expr_next : '+' expr | '-' expr | %empty ;
  6. term_next : '*' term | '/' term | %empty ;
  7. factor : ID | NUM ;
  8. %%

In addition to LL(1) conflict reports, ll1 will also output information
about:

  • Terminal symbols
  • Nonterminal symbols
  • Nonterminals deriving %empty
  • Recapitulation of the input grammar
  • First, follow and predict sets

It’s neither perfect nor complete, but it helps provide some basic insights.

Caveats

I wrote ll1 in a day, and only passed it through valgrind a handful of
times to check for serious errors. Given the fact that most of the data
structures are patterned after the null-termination patterns of C strings,
there has to be a bug or two in there… somewhere…

Anyways, if you run into one, let me know and I’ll push a patch.

Licensing

The ll1 source code is released under the
MIT license. See the
LICENSE.md file for the complete license terms.

And as always, enjoy!

~ Brad.