picoe / Eto.Parse

Recursive descent LL(k) parser for .NET with Fluent API, BNF, EBNF and Gold Grammars
MIT License
148 stars 30 forks source link

[Q] Is Grammar/Parser Threadsafe #30

Closed AndyPook closed 7 years ago

AndyPook commented 8 years ago

Just looking for confirmation... It is possible to create a Grammar as a singleton then have many threads concurrently parsing many strings? (This is the purpose of ParseArgs)

cwensley commented 8 years ago

Hi @AndyPook, yes it should be thread safe. Some of the grammars in eto (e.g. EbnfGrammar, GoldGrammar, etc) are not thread safe but could be made to be.

AndyPook commented 8 years ago

Thanks. Probably wouldn't be worth the effort as they would probably only be required to bootstrap the singleton.

cwensley commented 8 years ago

Exactly (;

One thing I forgot though, is that upon first use of a grammar, it goes through an initialization phase to optimize and fix left recursive grammars, etc. This is not thread safe as it updates the grammar structure. You can do this manually by calling Grammar.Initialize() after construction, and all should be good.